diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/ship.cc | 73 | ||||
-rw-r--r-- | src/game/ship.h | 31 |
2 files changed, 45 insertions, 59 deletions
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 <iostream> +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 |