src/Projectile.cc
author terom
Mon, 08 Dec 2008 01:08:00 +0000
changeset 276 87434abc1ba1
parent 272 97de051edbcf
child 279 e36f5e1a1c8d
permissions -rw-r--r--
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
#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), player(player),
    visible(visible), explosionRadius(weapon->getExplosionRadius()), radius(weapon->getRadius()),
    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);
    
    // XXX: get this as an argument
    collision_elasticity = 0.9;

    // 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) {
    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;

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