ekku@197: #include "Projectile.hh" terom@233: #include "Graphics.hh" saiam@208: #include "Timer.hh" ekku@197: saiam@208: Projectile::Projectile(GameState &state, Vector position, Vector velocity, bool visible, float radius, TickCount age) : ekku@222: PhysicsObject(state.world, PLAYER_MASS, position, velocity), state(state), visible(visible), radius(radius), age(age) { saiam@208: birth_tick = world.tick_timer.get_ticks(); nireco@211: // Don't think these are needed anymore ekku@197: std::vector shape(4); ekku@197: shape[0] = Vector(-1, -1); ekku@197: shape[1] = Vector(-1, 1); ekku@197: shape[2] = Vector(1, 1); ekku@197: shape[3] = Vector(1, -1); ekku@197: setShape(shape); nireco@211: ekku@197: target_visible = false; ekku@197: collision_elasticity = 0.9; // = shotType.elasticity ekku@222: state.addProjectile(this); ekku@197: } ekku@222: ekku@222: Projectile::~Projectile (void) { ekku@222: state.projectiles.remove(this); ekku@222: } ekku@197: terom@224: void Projectile::onDestroy (Vector position, bool removeGround) { terom@224: if (removeGround) terom@224: world.removeGround(position, radius); terom@224: terom@224: destroy(); terom@224: } terom@224: terom@201: void Projectile::onCollision() { terom@224: onDestroy(position, true); terom@224: } terom@224: terom@224: terom@224: void Projectile::tick (TimeMS dt) { terom@224: // expire projectiles terom@224: if (world.tick_timer.get_ticks() > birth_tick + age) terom@226: onDestroy(position, true); terom@224: terom@224: // super terom@224: PhysicsObject::tick(dt); saiam@199: } saiam@199: nireco@248: void Projectile::draw(Graphics *g, Vector cam) const { terom@233: CL_GraphicContext *gc = g->get_gc(); terom@233: saiam@208: if (visible) { saiam@204: saiam@208: CL_Quad projectile( nireco@248: (int)((position).x+1-cam.x), (int)((position).y+1-cam.y), nireco@248: (int)((position).x-1-cam.x), (int)((position).y+1-cam.y), nireco@248: (int)((position).x+1-cam.x), (int)((position).y-1-cam.y), nireco@248: (int)((position).x-1-cam.x), (int)((position).y-1-cam.y) saiam@208: ); saiam@208: saiam@208: gc->fill_quad(projectile, CL_Color::green); saiam@208: saiam@208: const uint16_t chlen = 10; nireco@248: int x = projectile.center().x -cam.x; nireco@248: int y = projectile.center().y -cam.y; terom@224: if (target_visible) { saiam@208: if (facingRight) { saiam@208: gc->draw_line(x, y, saiam@208: x + std::cos(aim)*chlen, saiam@208: y - std::sin(aim)*chlen, saiam@208: CL_Color::black); saiam@208: } else { saiam@208: gc->draw_line(x, y, saiam@208: x - std::cos(aim)*chlen, saiam@208: y - std::sin(aim)*chlen, saiam@208: CL_Color::black); saiam@208: } saiam@199: } saiam@199: } saiam@199: } terom@224: