src/Projectile.cc
author Tero Marttila <terom@fixme.fi>
Tue, 27 Jan 2009 00:25:58 +0200
changeset 439 9823e6cd1086
parent 434 a8ba81432ddd
permissions -rw-r--r--
some README text
#include "Projectile.hh"
#include "Timer.hh"

   
Projectile::Projectile (Player *player, Vector position, Vector velocity, Weapon *weapon, bool visible) :
    PhysicsObject(player->state.world, weapon->getMass(), position, velocity, PROJECTILE, weapon->getBounce()),
    player(player), 
    visible(visible), 
    weapon(weapon)
{
    // set birth tick
    birth_tick = world.getTicks();

    // XXX: projectiles should be particles?
    std::vector<Vector> shape(4);

    shape[0] = Vector(0                   ,  -weapon->getRadius()  );
    shape[1] = Vector(weapon->getRadius() ,  0                    );
    shape[2] = Vector(0                   ,  weapon->getRadius()  );
    shape[3] = Vector(-weapon->getRadius(),  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.terrain.removeGround(position, weapon->getExplosionRadius());

    destroy();
}

Health Projectile::getDamage (void) {
    return weapon->getDamage();
}

void Projectile::addKillToOwner (void) {
    player->addKill();
} 

void Projectile::onHitPlayer (Player *player) {
    player->takeDamage(this);
}

void Projectile::onCollision (Vector collisionPoint, PhysicsObject *other) {
    // did we hit a player?
    if (other != NULL && other->getType() == PLAYER) {
        Player* player = dynamic_cast<Player*>(other);

        // XXX: check that player really is !NULL
        onHitPlayer(player);
    }

    if (collision_elasticity == 0)
        onDestroy(collisionPoint, true);
}
   
void Projectile::tick (TimeMS dt) {
    // expire projectiles
    if (world.getTicks() > birth_tick + weapon->getExpire())
        onDestroy(getPosition(), true);

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

#if GRAPHICS_ENABLED
void Projectile::draw(graphics::Display &display, PixelCoordinate camera) const {
    CL_GraphicContext *gc = display.get_gc();

    if (visible) {
        PixelCoordinate pos = getCoordinate() - camera;
        // XXX: scale
        PixelDimension radius = (unsigned int) weapon->getRadius();

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