From 09789179cd77ce2f626fffbc94a90ec8784b76df Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 28 Oct 2007 09:19:35 +0000 Subject: game::Ship interface changes --- src/client/camera.cc | 21 +++++++------- src/client/input.cc | 8 +++--- src/client/shipdrawer.cc | 8 +++--- src/game/ship.cc | 73 ++++++++++++++++++++---------------------------- src/game/ship.h | 31 ++++++++++---------- 5 files changed, 64 insertions(+), 77 deletions(-) diff --git a/src/client/camera.cc b/src/client/camera.cc index bc8ee3e..03eff83 100644 --- a/src/client/camera.cc +++ b/src/client/camera.cc @@ -10,7 +10,8 @@ #include "game/game.h" #include "common/functions.h" -using namespace common; +using common::degrees360f; +using common::degrees180f; namespace client { @@ -38,16 +39,16 @@ Camera::~Camera() void Camera::draw(float elapsed) { if (mode == Track) { - yaw_target = game::ship.yaw; + yaw_target = game::ship.yaw(); } // adjust yaw - float d = degreesf(yaw_current - yaw_target); - yaw_current = degreesf( yaw_current - d * elapsed) ; + float d = degrees180f(yaw_current - yaw_target); + yaw_current = degrees360f( yaw_current - d * elapsed) ; // adjust pitch target - d = degreesf(pitch_current - pitch_target); - pitch_current = degreesf( pitch_current - d *elapsed); + d = degrees180f(pitch_current - pitch_target); + pitch_current = degrees360f( pitch_current - d *elapsed); switch (mode) { case Free: @@ -68,14 +69,14 @@ void Camera::draw(float elapsed) void Camera::rotate_right() { if (mode == Free ) { - yaw_target = degreesf( yaw_target + offset_inc); + yaw_target = degrees360f( yaw_target + offset_inc); } } void Camera::rotate_left() { if (mode == Free ) { - yaw_target = degreesf( yaw_target - offset_inc); + yaw_target = degrees360f( yaw_target - offset_inc); } } @@ -100,7 +101,7 @@ void Camera::nextmode() { case Free: // switch camera to Track mode mode = Track; - yaw_target = game::ship.yaw; + yaw_target = game::ship.yaw(); yaw_current = yaw_target; pitch_target = pitch_track; pitch_current = pitch_target; @@ -108,7 +109,7 @@ void Camera::nextmode() { case Track: // switch camera to Free mode mode = Free; - yaw_target = game::ship.yaw; + yaw_target = game::ship.yaw(); yaw_current = yaw_target; pitch_target = pitch_track; pitch_current = pitch_target; diff --git a/src/client/input.cc b/src/client/input.cc index 5cafb7d..270ccb7 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -56,16 +56,16 @@ void Input::handle_keypressed(SDL_keysym* keysym) camera.rotate_down(); break; case SDLK_KP_PLUS: - game::ship.thrust_increase(); + game::ship.set_thrust(game::ship.thrust() + 0.08f); break; case SDLK_KP_MINUS: - game::ship.thrust_decrease(); + game::ship.set_thrust(game::ship.thrust() - 0.1f); break; case SDLK_KP4: - game::ship.turn_left(); + game::ship.set_yaw(game::ship.yaw() + 10); break; case SDLK_KP6: - game::ship.turn_right(); + game::ship.set_yaw(game::ship.yaw() - 10); break; default: break; diff --git a/src/client/shipdrawer.cc b/src/client/shipdrawer.cc index de8dee0..09f95b1 100644 --- a/src/client/shipdrawer.cc +++ b/src/client/shipdrawer.cc @@ -38,7 +38,7 @@ void ShipDrawer::draw(float elapsed) { gl::push(); - rotate(ship->yaw, 0.0f, 1.0f, 0.0f ); + rotate(ship->yaw(), 0.0f, 1.0f, 0.0f ); Vector3f tl(0.25, 0.125, 0.125); Vector3f br(-0.25, -0.125, -0.125); @@ -67,14 +67,14 @@ void ShipDrawer::draw(float elapsed) cockpit.bottomcolor = engine1.bottomcolor; cockpit.draw(); - if(ship->thrust > 0 ) { + if(ship->thrust() > 0 ) { color(1.0f,0 ,0 ); begin(Lines); vertex(-0.5f, 0, 0.185); - vertex(-0.5f-0.25f*ship->thrust, 0, 0.185); + vertex(-0.5f-0.25f*ship->thrust(), 0, 0.185); vertex(-0.5f, 0, -0.185f); - vertex(-0.5f-0.25f*ship->thrust, 0, -0.185f); + vertex(-0.5f-0.25f*ship->thrust(), 0, -0.185f); end(); } diff --git a/src/game/ship.cc b/src/game/ship.cc index f0c40c4..049283f 100644 --- a/src/game/ship.cc +++ b/src/game/ship.cc @@ -11,21 +11,22 @@ // C++ headers #include +using common::degrees360f; +using common::degrees180f; + namespace game { Ship::Ship() { speed = 0; - yaw = 0; - yaw_offset = 0; + yaw_current = 0; + yaw_target = yaw_current; - thrust = 0; + thrust_current = 0; // ship specs acceleration = 1.5f; - max_speed = 4.0f; - - max_yaw_offset = 45.0f; + speed_max = 4.0f; yaw_speed = 4.0f; } @@ -33,56 +34,42 @@ Ship::~Ship() { } +void Ship::set_yaw(float y) +{ + yaw_target = degrees360f(y); +} + +void Ship::set_thrust(float t) +{ + if (t < 0) + thrust_current = 0; + else if (t > 1) + thrust_current = 1; + else + thrust_current = t; +} + void Ship::update(float elapsed) { // update yaw + float yaw_offset = degrees180f(yaw_target - yaw_current); float d = yaw_speed * elapsed * yaw_offset; - yaw_offset -= d; - yaw +=d; + yaw_current = degrees360f(yaw_current + d); // update thrust - if (speed < thrust * max_speed) { + if (speed < thrust_current * speed_max) { speed += acceleration * elapsed; - if (speed > thrust * max_speed) { - speed = thrust * max_speed; + if (speed > thrust_current * speed_max) { + speed = thrust_current * speed_max; } - } else if(speed > thrust * max_speed) { + } else if(speed > thrust_current * speed_max) { speed -= acceleration * elapsed; if (speed < 0) speed = 0; } // location TODO avoid sin/cos calculations - location.x += cosf(yaw * M_PI / 180) * speed * elapsed; - location.z -= sinf(yaw * M_PI / 180) * speed * elapsed; -} - - -void Ship::thrust_increase() -{ - thrust += 0.05; - if (thrust > 1) thrust = 1; -} - -void Ship::thrust_decrease() -{ - thrust -= 0.08; - if (thrust < 0) thrust = 0; -} - -void Ship::turn_left() -{ - //yaw = degreesf(yaw + 2); - yaw_offset += 2; - if (yaw_offset > max_yaw_offset) - yaw_offset = max_yaw_offset; -} - -void Ship::turn_right() -{ - //yaw = degreesf(yaw - 2); - yaw_offset -= 2; - if (yaw_offset < -max_yaw_offset) - yaw_offset = - max_yaw_offset; + location.x += cosf(yaw_current * M_PI / 180) * speed * elapsed; + location.z -= sinf(yaw_current * M_PI / 180) * speed * elapsed; } } // namespace game diff --git a/src/game/ship.h b/src/game/ship.h index 31266a4..ac38547 100644 --- a/src/game/ship.h +++ b/src/game/ship.h @@ -26,32 +26,31 @@ public: /// speed vector in units/second float speed; - /// turn left, increase yaw_offset - void turn_left(); - /// turn right, decrease yaw_offset - void turn_right(); - /// yaw, angle in the x/z plane - float yaw; + /// return the current yaw angle + inline float yaw() const { return yaw_current; } + /// set the target yaw angle + void set_yaw(float y); - /// increase thrust - void thrust_increase(); - /// decrease thrust - void thrust_decrease(); - /// forward thruster in % [0-1] - float thrust; + /// return the current thruster power + inline float thrust() const { return thrust_current; } + /// set thruster power + void set_thrust(float t); /* -- Ship SPECS --*/ /// acceleration float acceleration; /// maximum speed - float max_speed; - /// maximum yaw_offset - float max_yaw_offset; + float speed_max; /// yaw turn speed float yaw_speed; private: - float yaw_offset; + /// current yaw, angle in the x/z plane + float yaw_current; + /// target yaw angle + float yaw_target; + /// current thruster power in % [0-1] + float thrust_current; }; } // namespace game -- cgit v1.2.3