src/Projectile.cc
author terom
Sun, 07 Dec 2008 21:10:04 +0000
changeset 263 8c999cf4c182
parent 257 549783d71e51
child 265 d97bf6790c22
permissions -rw-r--r--
weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
#include "Projectile.hh"
#include "Graphics.hh"
#include "Timer.hh"

Projectile::Projectile (GameState &state, Vector position, Vector velocity, bool visible, float explosionRadius, float radius, TickCount age) :
    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), explosionRadius(explosionRadius), radius(radius), age(age) {
    birth_tick = world.tick_timer.get_ticks();
    // Don't think these are needed anymore
    std::vector<Vector> shape(4);
    shape[0] = Vector(0,        -radius );
    shape[1] = Vector(+radius,  0       );
    shape[2] = Vector(0,        +radius );
    shape[3] = Vector(-radius,  0       );
    setShape(shape);

    collision_elasticity = 0.9; // = shotType.elasticity
    state.addProjectile(this);
}
    
Projectile::~Projectile (void) {
    state.projectiles.remove(this);
}
 
void Projectile::onDestroy (Vector position, bool removeGround) {
    if (removeGround)
        world.removeGround(position, explosionRadius);

    destroy();
}

void Projectile::onCollision (Vector collisionPoint) {
    onDestroy(collisionPoint, true);
}

   
void Projectile::tick (TimeMS dt) {
    // expire projectiles
    if (world.tick_timer.get_ticks() > birth_tick + age)
        onDestroy(position, true);

    // super
    PhysicsObject::tick(dt);
}

void Projectile::draw(Graphics *g, PixelCoordinate camera) const {
    CL_GraphicContext *gc = g->get_gc();

    if (visible) {
        PixelCoordinate pos = getCoordinate() - camera;

        CL_Quad projectile(
                pos.x,          pos.y - radius,
                pos.x + radius, pos.y,
                pos.x,          pos.y + radius,
                pos.x - radius, pos.y
        );
        
        gc->fill_quad(projectile, CL_Color::green);
    }
}