src/proto2/GameState.cc
author saiam
Wed, 03 Dec 2008 16:47:17 +0000
changeset 183 e31ff2152774
parent 182 84675387ca74
permissions -rw-r--r--
Now we use different lists for players and projectiles
26
5685602aeb9c it works \o/
terom
parents:
diff changeset
     1
5685602aeb9c it works \o/
terom
parents:
diff changeset
     2
#include "GameState.hh"
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 66
diff changeset
     3
#include "Engine.hh"
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
     4
#include "Config.hh"
26
5685602aeb9c it works \o/
terom
parents:
diff changeset
     5
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
     6
/**
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
     7
 * shoots the selected weapon.
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
     8
 * TODO: selection and weapon information
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
     9
 */
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 170
diff changeset
    10
void Player::shoot (void) {
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    11
    // here should be somehow considered which projectile it is
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    12
    if(!canShoot())
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    13
        return;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    14
    reloadTimer += 0;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    15
    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    16
            Vector(-std::cos(aim), -std::sin(aim));
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    17
    float shotspeed = 100*PHYSICS_TICK_MS;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    18
    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    19
    Vector shotVelocity = this->velocity + shotRelativeVelocity;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    20
    this->state.addProjectile(new Shot(this->state, this->position, shotVelocity, true));
180
bfe1077edab3 Ammuksia f:lla
ekku
parents: 170
diff changeset
    21
}
bfe1077edab3 Ammuksia f:lla
ekku
parents: 170
diff changeset
    22
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    23
void Player::handleMove (PlayerInput_Move input) {
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    24
    float fx = 0; // Force in x-direction
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    25
    float da = 0; // Crosshair angle
26
5685602aeb9c it works \o/
terom
parents:
diff changeset
    26
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 94
diff changeset
    27
    // handle left/right
160
ba0b6f421a3c not anything big
nireco
parents: 153
diff changeset
    28
    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED))
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    29
        fx -= PLAYER_MOVE_FORCE;
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 26
diff changeset
    30
160
ba0b6f421a3c not anything big
nireco
parents: 153
diff changeset
    31
    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED))
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    32
        fx += PLAYER_MOVE_FORCE;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 66
diff changeset
    33
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    34
    if (input & INPUT_MOVE_UP)
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    35
        da += CROSSHAIR_ANGLE_SPEED;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    36
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    37
    if (input & INPUT_MOVE_DOWN)
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    38
        da -= CROSSHAIR_ANGLE_SPEED;
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    39
170
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    40
    if (input & INPUT_MOVE_JUMP) {
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    41
        if ((input & INPUT_MOVE_LEFT))
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    42
            jump(-1);
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    43
        else if ((input & INPUT_MOVE_RIGHT))
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    44
            jump(1);
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    45
        else
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    46
            jump(0);
fe74105c07ea jumppi toimii oikeisiin suuntiin
ekku
parents: 160
diff changeset
    47
    }
114
71f7e9d3d052 V?h?n siistin tota walkkia, viel? on kyll? tekemist?.
saiam
parents: 108
diff changeset
    48
117
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    49
    if (input & INPUT_MOVE_DIG) {
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    50
        // Should create Shot which destroys ground, but also should be destroyed then,
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    51
        // but it doesn't.
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    52
        // But this now just segfaults
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    53
//        world.addObject(new Shot(state, position, true));
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    54
153
73402d5b778e Lots of small fixe. Moved drawing PhysicsObjects away from Graphics.cc.
saiam
parents: 122
diff changeset
    55
        world.removeGround(position, 15);
117
2877eef6c1d5 digging
nireco
parents: 116
diff changeset
    56
    }
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    57
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    58
    if (input & INPUT_SHOOT) {
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    59
        this->shoot();
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    60
    }
183
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    61
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    62
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    63
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    64
    // Player facing
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    65
    if (fx < 0) setFacing(false);
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    66
    else if (fx > 0) setFacing(true);
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    67
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    68
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    69
    this->changeAim(da); // Move crosshair
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    70
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    71
    // Apply force
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    72
    applyForce(Vector(fx, 0));
e31ff2152774 Now we use different lists for players and projectiles
saiam
parents: 182
diff changeset
    73
26
5685602aeb9c it works \o/
terom
parents:
diff changeset
    74
}
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 66
diff changeset
    75
08bebac3d0c2 Maalla liikkumista
ekku
parents: 66
diff changeset
    76
void Player::debugInfo (void) {
108
1b93045a5b0a T?ht?in lis?tty, tosin se piirret??n tosi rumasti.
saiam
parents: 96
diff changeset
    77
    Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 66
diff changeset
    78
}
116
0d36aade845e some stuff, don't remember what
nireco
parents: 114
diff changeset
    79
0d36aade845e some stuff, don't remember what
nireco
parents: 114
diff changeset
    80
void Shot::onCollision() {
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    81
    world.removeGround(position, 20);
116
0d36aade845e some stuff, don't remember what
nireco
parents: 114
diff changeset
    82
}
182
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    83
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    84
void Shot::draw(CL_GraphicContext *gc) {
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    85
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    86
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    87
    CL_Quad player(
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    88
                   (position).x+1, (position).y+1,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    89
                   (position).x-1, (position).y+1,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    90
                   (position).x+1, (position).y-1,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    91
                   (position).x-1, (position).y-1
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    92
                   );
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    93
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    94
    gc->fill_quad(player, CL_Color::green);
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    95
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    96
    const uint16_t chlen = 10;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    97
    uint16_t x = player.center().x;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    98
    uint16_t y = player.center().y;
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
    99
    if(target_visible) {
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   100
        if (facingRight) {
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   101
            gc->draw_line(x, y,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   102
                          x + std::cos(aim)*chlen,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   103
                          y - std::sin(aim)*chlen,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   104
                          CL_Color::black);
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   105
        } else {
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   106
            gc->draw_line(x, y,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   107
                          x - std::cos(aim)*chlen,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   108
                          y - std::sin(aim)*chlen,
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   109
                          CL_Color::black);
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   110
        }
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   111
    }
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   112
}
84675387ca74 PhyWo draw, Shot speed, Shot.show_target, Shot is now drawn with const shape...
nireco
parents: 180
diff changeset
   113