src/Projectile.cc
author nireco
Mon, 08 Dec 2008 16:30:13 +0000
changeset 291 5d7221003b26
parent 282 e0e4dfc3e528
child 292 d837bd7a0da3
permissions -rw-r--r--
some stuff
#include "Projectile.hh"
#include "Graphics.hh"
#include "Timer.hh"

   
Projectile::Projectile (Player *player, Vector position, Vector velocity, Weapon *weapon, bool visible) :
    PhysicsObject(player->state.world, PROJECTILE_MASS, position, velocity, PROJECTILE, weapon->getBounce()),
    player(player), 
    visible(visible), 
    radius(weapon->getRadius()),
    explosionRadius(weapon->getExplosionRadius()), 
    expire(weapon->getExpire())
{
    // set birth tick
    birth_tick = world.tick_timer.get_ticks();

    // XXX: projectiles should be particles?
    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);
    
    // add to state
    player->state.addProjectile(this);
}

Projectile::~Projectile (void) {
    player->state.projectiles.remove(this);
}
 
void Projectile::onDestroy (Vector position, bool removeGround) {
    if (removeGround)
        world.removeGround(position, explosionRadius);

    destroy();
}

void Projectile::onCollision (Vector collisionPoint, PhysicsObject *other) {
    (void) other;

    if(other->getType() == PLAYER) {
        Player* pl = dynamic_cast<Player*>(other);
        pl->takeDamage(12345);
    }

    if (collision_elasticity == 0)
        onDestroy(collisionPoint, true);
}

   
void Projectile::tick (TimeMS dt) {
    // expire projectiles
    if (world.tick_timer.get_ticks() > birth_tick + expire)
        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;
        PixelDimension radius = (unsigned int) this->radius;

        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);
    }
}