From 5701da5df3e5f0d3a40af0abf7e03302275dcca2 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 4 Dec 2012 21:42:54 +0000 Subject: Introduce Entity::hit() callback, prevent projectiles from doing no damage. --- src/game/base/ship.cc | 137 +++++++++++++++++++++++++-------------------- src/game/base/ship.h | 6 +- src/game/base/spacemine.cc | 2 + 3 files changed, 83 insertions(+), 62 deletions(-) (limited to 'src/game') diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index fd7b1b8..46dba6e 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -380,7 +380,6 @@ void Ship::set_spawn(core::Entity *spawn) void Ship::action (btScalar seconds) { - //float maxspeed = 0; float engine_force = 0; float turn_force = ship_turn_force; float roll_force = ship_roll_force; @@ -429,7 +428,9 @@ void Ship::action (btScalar seconds) // apply afterburner body()->applyCentralImpulse(math::to_btVector3(axis().forward() * current_target_afterburner * ship_strafe_force)); + // apply main thruster + // note: negative afterburner puts the ship in reverse and disables the main thruster if (current_target_afterburner >= 0) { body()->applyCentralImpulse(math::to_btVector3(axis().forward() * engine_force)); } @@ -448,10 +449,9 @@ void Ship::action (btScalar seconds) entity_speed = Game::g_impulsespeed->value() * 0.01f; body()->setLinearVelocity(math::to_btVector3(axis().forward() * entity_speed)); } - // FIXME set speed to 0 if below threshold } -void Ship::collision(core::Entity *other) +void Ship::hit(core::Entity *other) { if (state() == core::Entity::Destroyed) { return; @@ -461,37 +461,7 @@ void Ship::collision(core::Entity *other) return; } - // do not fly into planets - if (other->moduletype() == planet_enttype) { - - // ran into a planet - explode(); - - if (owner()) { - std::string message("^B"); - message.append(owner()->name()); - message.append(" ^Bran into "); - message.append(other->name()); - message.append("."); - core::server()->broadcast(message); - } else { - die(); - } - } else if (other->moduletype() == star_enttype) { - - // flew into a star - explode(); - - if (owner()) { - std::string message("^B"); - message.append(owner()->name()); - message.append(" ^Bflew into a star."); - core::server()->broadcast(message); - } else { - die(); - } - - } else if (other->moduletype() == spacemine_enttype) { + if (other->moduletype() == spacemine_enttype) { // hit by a mine SpaceMine * spacemine = static_cast(other); @@ -534,46 +504,91 @@ void Ship::collision(core::Entity *other) } else if (other->type() == core::Entity::Projectile) { // hit by projectile - - // TODO don't hit self core::EntityProjectile *projectile = static_cast(other); - if (projectile->state() != core::Entity::Destroyed) { + + // don't hit self + if ((projectile->state() != core::Entity::Destroyed) && + (projectile->ownerid() != id())) { + ship_armor -= projectile->damage(); - } - - // destroyed - if (ship_armor <= 0) { - explode(); - if (owner()) { - ship_armor = 0; - std::string message("^B"); - message.append(owner()->name()); - - core::Player *assassin = 0; - - if (projectile->ownerid()) { - assassin = core::server()->find_player(projectile->ownerid()); - } + // destroyed + if (ship_armor <= 0) { + explode(); - if (assassin) { - if (assassin == owner()) { - message.append(" ^Bate his own bullet."); + if (owner()) { + ship_armor = 0; + std::string message("^B"); + message.append(owner()->name()); + + core::Player *assassin = 0; + + if (projectile->ownerid()) { + assassin = core::server()->find_player(projectile->ownerid()); + } + + if (assassin) { + if (assassin == owner()) { + message.append(" ^Bate his own bullet."); + } else { + message.append(" ^Bwas blown to bits by "); + message.append(assassin->name()); + } } else { - message.append(" ^Bwas blown to bits by "); - message.append(assassin->name()); + message.append(" ^Bwas blown to bits."); } + core::server()->broadcast(message); } else { - message.append(" ^Bwas blown to bits."); + die(); } - core::server()->broadcast(message); - } else { - die(); } } } } +void Ship::collision(core::Entity *other) +{ + if (state() == core::Entity::Destroyed) { + return; + } + + if (state() == core::Entity::Docked) { + return; + } + + // do not fly into planets + if (other->moduletype() == planet_enttype) { + + // ran into a planet + explode(); + + if (owner()) { + std::string message("^B"); + message.append(owner()->name()); + message.append(" ^Bran into "); + message.append(other->name()); + message.append("."); + core::server()->broadcast(message); + } else { + die(); + } + } else if (other->moduletype() == star_enttype) { + + // flew into a star + explode(); + + if (owner()) { + std::string message("^B"); + message.append(owner()->name()); + message.append(" ^Bflew into a star."); + core::server()->broadcast(message); + } else { + die(); + } + + } +} + void Ship::frame(const unsigned long elapsed) { //const float direction_reaction = 2.0f; // directional control reaction time diff --git a/src/game/base/ship.h b/src/game/base/ship.h index 8b06727..4e95f5d 100644 --- a/src/game/base/ship.h +++ b/src/game/base/ship.h @@ -154,8 +154,12 @@ public: /// reset physics state and ship controls virtual void reset(); - virtual void collision(Entity *other); + /// collision callback + virtual void collision(core::Entity *other); + /// hit-by weapon callback + virtual void hit(core::Entity *other); + /// explode the ship void explode(); diff --git a/src/game/base/spacemine.cc b/src/game/base/spacemine.cc index 865deb0..133f4e0 100644 --- a/src/game/base/spacemine.cc +++ b/src/game/base/spacemine.cc @@ -93,6 +93,8 @@ void SpaceMine::collision(core::Entity *other) entity->body()->applyCentralImpulse(math::to_btVector3(explosion_direction * force )); entity->body()->applyTorqueImpulse(math::to_btVector3(explosion_torque * force * 0.1f)); + + entity->hit(this); } spacemine_detonated_timestamp = core::game()->time(); -- cgit v1.2.3