Calculates kills and deaths. You get frags by killing yourself \o/
#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),
weapon(weapon)
{
// set birth tick
birth_tick = world.tick_timer.get_ticks();
// 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.removeGround(position, weapon->getExplosionRadius());
destroy();
}
Health Projectile::getDamage () {
return weapon->getDamage();
}
void Projectile::addKillToOwner () {
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.tick_timer.get_ticks() > birth_tick + weapon->getExpire())
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) weapon->getRadius();
CL_Quad projectile(
pos.x, pos.y - weapon->getRadius(),
pos.x + weapon->getRadius(), pos.y,
pos.x, pos.y + weapon->getRadius(),
pos.x - weapon->getRadius(), pos.y
);
gc->fill_quad(projectile, CL_Color::green);
}
}