src/Projectile.cc
author ekku
Sat, 06 Dec 2008 19:38:01 +0000
changeset 225 22ecb9cb9245
parent 224 e6faefba2ec1
child 226 381487d07d17
permissions -rw-r--r--
Rope can be drawn.
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     1
#include "Projectile.hh"
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     2
#include "Timer.hh"
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     3
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     4
Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age) :
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
     5
    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), radius(radius), age(age) {
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
     6
    birth_tick = world.tick_timer.get_ticks();
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
     7
    // Don't think these are needed anymore
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     8
    std::vector<Vector> shape(4);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
     9
    shape[0] = Vector(-1, -1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    10
    shape[1] = Vector(-1, 1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    11
    shape[2] = Vector(1, 1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    12
    shape[3] = Vector(1, -1);
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    13
    setShape(shape);
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
    14
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    15
    target_visible = false;
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    16
    collision_elasticity = 0.9; // = shotType.elasticity
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    17
    state.addProjectile(this);
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    18
}
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    19
    
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    20
Projectile::~Projectile (void) {
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    21
    state.projectiles.remove(this);
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 211
diff changeset
    22
}
197
d9ac888de778 Hajotetaan lis??
ekku
parents:
diff changeset
    23
 
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    24
void Projectile::onDestroy (Vector position, bool removeGround) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    25
    if (removeGround)
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    26
        world.removeGround(position, radius);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    27
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    28
    destroy();
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    29
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    30
201
135616467a0a rename Shot -> Projectile
terom
parents: 200
diff changeset
    31
void Projectile::onCollision() {
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    32
    onDestroy(position, true);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    33
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    34
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    35
   
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    36
void Projectile::tick (TimeMS dt) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    37
    // expire projectiles
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    38
    if (world.tick_timer.get_ticks() > birth_tick + age)
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    39
        onDestroy(position, false);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    40
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    41
    // super
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    42
    PhysicsObject::tick(dt);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    43
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    44
211
d5d52fb191e4 some minor fixes, shots now explode in collisionpoint
nireco
parents: 208
diff changeset
    45
void Projectile::draw(CL_GraphicContext *gc) const {
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    46
    if (visible) {
204
4c386e9c950f Removed one memory leak.
saiam
parents: 201
diff changeset
    47
  
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    48
        CL_Quad projectile(
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    49
                           (int)((position).x+1), (int)((position).y+1),
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    50
                           (int)((position).x-1), (int)((position).y+1),
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    51
                           (int)((position).x+1), (int)((position).y-1),
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    52
                           (int)((position).x-1), (int)((position).y-1)
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    53
                           );
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    54
        
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    55
        gc->fill_quad(projectile, CL_Color::green);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    56
        
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    57
        const uint16_t chlen = 10;
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    58
        uint16_t x = projectile.center().x;
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    59
        uint16_t y = projectile.center().y;
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    60
        if (target_visible) {
208
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    61
            if (facingRight) {
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    62
                gc->draw_line(x, y,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    63
                              x + std::cos(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    64
                              y - std::sin(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    65
                              CL_Color::black);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    66
            } else {
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    67
                gc->draw_line(x, y,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    68
                              x - std::cos(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    69
                              y - std::sin(aim)*chlen,
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    70
                              CL_Color::black);
7709571e1131 Digging now uses projectiles.
saiam
parents: 204
diff changeset
    71
            }
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    72
        }
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    73
    }
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 197
diff changeset
    74
}
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 222
diff changeset
    75