src/Projectile.cc
author saiam
Sun, 07 Dec 2008 19:02:45 +0000
changeset 249 5647f58e37cd
parent 248 e40ef56dc62c
child 252 25054ce94d07
permissions -rw-r--r--
Removed some unnecessary TODOs
#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);

    target_visible = false;
    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() {
    onDestroy(position, 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, Vector cam) const {
    CL_GraphicContext *gc = g->get_gc();

    if (visible) {
  
        CL_Quad projectile(
                           (int)((position).x+1-cam.x), (int)((position).y+1-cam.y),
                           (int)((position).x-1-cam.x), (int)((position).y+1-cam.y),
                           (int)((position).x+1-cam.x), (int)((position).y-1-cam.y),
                           (int)((position).x-1-cam.x), (int)((position).y-1-cam.y)
                           );
        
        gc->fill_quad(projectile, CL_Color::green);
        
        const uint16_t chlen = 10;
        int x = projectile.center().x -cam.x;
        int y = projectile.center().y -cam.y;
        if (target_visible) {
            if (facingRight) {
                gc->draw_line(x, y,
                              x + std::cos(aim)*chlen,
                              y - std::sin(aim)*chlen,
                              CL_Color::black);
            } else {
                gc->draw_line(x, y,
                              x - std::cos(aim)*chlen,
                              y - std::sin(aim)*chlen,
                              CL_Color::black);
            }
        }
    }
}