From 1f95c377b2abfaa454b1f2298af10956d95ad941 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 13 Feb 2008 00:40:59 +0000 Subject: split client from game module --- src/game/game.cc | 50 ++++++++++++++++++++++++++++++++++++-------------- src/game/game.h | 6 ++++++ src/game/ship.cc | 55 +++++++++++++++++++------------------------------------ src/game/ship.h | 31 +++++-------------------------- 4 files changed, 66 insertions(+), 76 deletions(-) (limited to 'src/game') diff --git a/src/game/game.cc b/src/game/game.cc index 51bb565..25f8926 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -4,20 +4,28 @@ the terms of the GNU General Public License version 2 */ -// project headers +#include + #include "game/game.h" #include "game/sector.h" #include "game/ship.h" #include "game/star.h" -#include "filesystem/filesystem.h" +#include "core/entity.h" #include "sys/sys.h" - -// C++ headers -#include +#include "math/mathlib.h" +#include "filesystem/filesystem.h" namespace game { +Game::Game() +{ +} + +Game::~Game() +{ +} + bool Game::init() { using math::Vector3f; @@ -102,11 +110,6 @@ bool Game::init() star->location = Vector3f(256.0f, 0.0f, 256.0f); star->label = "star: Sabishi Hoshi"; - - ship = new Ship(); - ship->location = Vector3f(0,0,0); - ship->label = "ship: Micron Vector"; - core::Entity *cube = new core::Entity(core::entity::Solid & core::entity::Static); cube->core_shape = core::entity::Cube; cube->core_color = Color(0.0f, 0.8f, 0.0f); @@ -151,7 +154,7 @@ void Game::shutdown() void Game::frame(float seconds) { - ship->update(seconds); + } void Game::event_join(core::Player *player) @@ -160,8 +163,27 @@ void Game::event_join(core::Player *player) return; ship = new Ship(); - ship->location = Vector3f(0,0,0); - ship->label = "ship: Micron Vector"; + ship->location = math::Vector3f(0,0,0); + ship->label = "ship: <"+player->name+"> Micron Vector"; + ship->owner = player; + + player->controled = ship; + + con_print << player->name << " joins" << std::endl; } -}; // namespace game +void Game::event_leave(core::Player *player) +{ + if (!player) + return; + + con_print << player->name << " leaves" << std::endl; + + if (player->controled) { + // player only has one ship for now + core::entity::remove(player->controled->id); + } +} + +} // namespace game + diff --git a/src/game/game.h b/src/game/game.h index abe0c0d..9c993fe 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -26,6 +26,9 @@ namespace game class Game : public core::GameInterface { public: + Game(); + ~Game(); + /// initialize the game bool init(); /// shutdown the game @@ -36,6 +39,9 @@ public: /// a player joins the game void event_join(core::Player *player); + /// a player leaves the game + void event_leave(core::Player *player); + /// sectors in space std::vector sectors; diff --git a/src/game/ship.cc b/src/game/ship.cc index d6333cd..baa97f1 100644 --- a/src/game/ship.cc +++ b/src/game/ship.cc @@ -17,62 +17,45 @@ using math::degrees180f; namespace game { -Ship::Ship() : core::Entity(0) +Ship::Ship() : core::EntityControlable(0) { type = ship_enttype; - speed = 0; - yaw_current = 0; - yaw_target = yaw_current; - - thrust_current = 0; - // ship specs acceleration = 1.5f; - speed_max = 4.0f; - yaw_speed = 4.0f; + max_speed = 4.0f; + turn_speed = 0.5f; } Ship::~Ship() { } -void Ship::set_yaw(float y) +void Ship::frame(float seconds) { - 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; -} + if (target_thrust < 0) target_thrust = 0.0f; + else if(target_thrust > 1) target_thrust = 1.0f; -void Ship::update(float elapsed) -{ - // update yaw - float yaw_offset = degrees180f(yaw_target - yaw_current); - float d = yaw_speed * elapsed * yaw_offset; - yaw_current = degrees360f(yaw_current + d); + // update direction + float direction_offset = degrees180f(target_direction - direction); + float d = turn_speed * seconds * direction_offset; + direction = degrees360f(direction + d); - // update thrust - if (speed < thrust_current * speed_max) { - speed += acceleration * elapsed; - if (speed > thrust_current * speed_max) { - speed = thrust_current * speed_max; + // update speed + if (speed < target_thrust * max_speed) { + speed += acceleration * seconds; + if (speed > target_thrust * max_speed) { + speed = target_thrust * max_speed; } - } else if(speed > thrust_current * speed_max) { - speed -= acceleration * elapsed; + } else if(speed > target_thrust * max_speed) { + speed -= acceleration * seconds; if (speed < 0) speed = 0; } // location TODO avoid sin/cos calculations - location.x += cosf(yaw_current * M_PI / 180) * speed * elapsed; - location.z -= sinf(yaw_current * M_PI / 180) * speed * elapsed; + location.x += cosf(direction * M_PI / 180) * speed * seconds; + location.z -= sinf(direction * M_PI / 180) * speed * seconds; } } // namespace game diff --git a/src/game/ship.h b/src/game/ship.h index e919f38..d108223 100644 --- a/src/game/ship.h +++ b/src/game/ship.h @@ -13,43 +13,22 @@ namespace game { -class Ship : public core::Entity +class Ship : public core::EntityControlable { public: Ship(); ~Ship(); /// update the ship state - void update(float elapsed); - - /// speed vector in units/second - float speed; - - /// return the current yaw angle - inline float yaw() const { return yaw_current; } - /// set the target yaw angle - void set_yaw(float y); - - /// return the current thruster power - inline float thrust() const { return thrust_current; } - /// set thruster power - void set_thrust(float t); + void frame(float seconds); /* -- Ship SPECS --*/ /// acceleration float acceleration; /// maximum speed - float speed_max; - /// yaw turn speed - float yaw_speed; -private: - /// current yaw, angle in XZ plane, 0-360 - float yaw_current; - /// target yaw angle, angle in XYplane, 0-360 - float yaw_target; - - /// current thruster power in % [0-1] - float thrust_current; + float max_speed; + /// turn speed in rotations per second + float turn_speed; }; } // namespace game -- cgit v1.2.3