/* game/ship.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ // project headers #include "game/game.h" #include "game/ship.h" #include "math/mathlib.h" // C++ headers #include using math::degrees360f; using math::degrees180f; namespace game { Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable(owner, ship_enttype) { entity_modelname = "ships/" + shipmodel->modelname(); entity_name = shipmodel->name() + ": <" + owner->name() + ">"; ship_shipmodel = shipmodel; entity_moduletypeid = ship_enttype; current_target_direction = 0.0f; current_target_pitch = 0.0f;; current_target_roll = 0.0f;; } Ship::~Ship() { if (entity_owner) entity_owner->remove_asset(this); } void Ship::frame(float seconds) { const float direction_change_speed = 2; // update thrust math::clamp(target_thrust, 0.0f, 1.0f); entity_thrust = target_thrust; // update pitch math::clamp(target_pitch, -1.0f, 1.0f); if (current_target_pitch < target_pitch) { current_target_pitch += direction_change_speed * seconds; if (current_target_pitch > target_pitch) current_target_pitch = target_pitch; } else if (current_target_pitch > target_pitch) { current_target_pitch -= direction_change_speed * seconds; if (current_target_pitch < target_pitch) current_target_pitch = target_pitch; } if (fabs(seconds*current_target_pitch) > 0.0f) { math::clamp(current_target_pitch, -1.0f, 1.0f); float pitch_offset = seconds * current_target_pitch; entity_axis.change_pitch(360.0f * ship_shipmodel->turnspeed() * pitch_offset); } // update direction math::clamp(target_direction, -1.0f, 1.0f); if (current_target_direction < target_direction) { current_target_direction += direction_change_speed * seconds; if (current_target_direction > target_direction) { current_target_direction = target_direction; } } else if (current_target_direction > target_direction) { current_target_direction -= direction_change_speed * seconds; if (current_target_direction < target_direction) { current_target_direction = target_direction; } } if (fabs(seconds*current_target_direction) > 0.0f ) { math::clamp(current_target_direction, -1.0f, 1.0f); float direction_offset = seconds * current_target_direction; entity_axis.change_direction(360.0f * ship_shipmodel->turnspeed() * direction_offset); } // update roll math::clamp(target_roll, -1.0f, 1.0f); if (current_target_roll < target_roll) { current_target_roll += direction_change_speed * seconds; if (current_target_roll > target_roll) current_target_roll = target_roll; } else if (current_target_roll > target_roll) { current_target_roll -= direction_change_speed * seconds; if (current_target_roll < target_roll) current_target_roll = target_roll; } if (fabs(current_target_roll) > 0.0f) { math::clamp(current_target_roll, -1.0f, 1.0f); float roll_offset = seconds * current_target_roll; entity_axis.change_roll(360.0f * ship_shipmodel->turnspeed() * roll_offset); } // update speed if (entity_speed < entity_thrust * ship_shipmodel->maxspeed()) { entity_speed += ship_shipmodel->acceleration() * seconds; if (entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { entity_speed = entity_thrust * ship_shipmodel->maxspeed(); } } else if(entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { entity_speed -= ship_shipmodel->acceleration() * seconds; if (entity_speed < 0.0f) entity_speed = 0.0f; } if (entity_speed != 0.0f) { entity_location += entity_axis.forward() * entity_speed * seconds; entity_dirty = true; } else if ((current_target_pitch != 0.0f) || (current_target_direction != 0.0f) || (current_target_roll != 0.0f)) { entity_dirty = true; } } } // namespace game