src/Projectile.cc
author terom
Sun, 07 Dec 2008 20:25:57 +0000
changeset 257 549783d71e51
parent 255 99431fdb0dc8
child 263 8c999cf4c182
permissions -rw-r--r--
add a handful of consts to PhysicsObject and modify draw to use getCoordinate, and replace old skin.png with new skin.png
#include "Projectile.hh"
#include "Graphics.hh"
#include "Timer.hh"

Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age) :
    PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), 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(-1, -1);
    shape[1] = Vector(-1, 1);
    shape[2] = Vector(1, 1);
    shape[3] = Vector(1, -1);
    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, radius);

    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 + 1, pos.y + 1,
                pos.x - 1, pos.y + 1,
                pos.x + 1, pos.y - 1,
                pos.x - 1, pos.y - 1
        );
        
        gc->fill_quad(projectile, CL_Color::green);
    }
}