rework Network send() code to use NetworkTarget/Node/Group::send classes, add a NetworkMessage class for sending NetworkObject messages, and fix a bug whereby the server's client TCP sockets weren't nonblocking.... I wonder how this has worked before?\!
#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 () {
return weapon->getDamage();
}
void Projectile::addKillToOwner () {
player->addKill();
}
const Player* Projectile::getOwner () {
return player;
}
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