diff options
author | Stijn Buys <ingar@osirion.org> | 2008-10-18 17:58:45 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-10-18 17:58:45 +0000 |
commit | 35613f0860a2d8cb643ca8de006de08503e48e53 (patch) | |
tree | 8a5436de643e818e68a82df2e5cb2df2145f5062 /src/game/example | |
parent | db287e4a5133125bb6f25ba21ea97c47b19ac67f (diff) |
example module
Diffstat (limited to 'src/game/example')
-rw-r--r-- | src/game/example/Makefile.am | 7 | ||||
-rw-r--r-- | src/game/example/example.cc | 125 | ||||
-rw-r--r-- | src/game/example/example.h | 65 | ||||
-rw-r--r-- | src/game/example/spectator.cc | 65 | ||||
-rw-r--r-- | src/game/example/spectator.h | 32 |
5 files changed, 294 insertions, 0 deletions
diff --git a/src/game/example/Makefile.am b/src/game/example/Makefile.am new file mode 100644 index 0000000..81fb507 --- /dev/null +++ b/src/game/example/Makefile.am @@ -0,0 +1,7 @@ +INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/src/game +METASOURCES = AUTO +libexample_la_LDFLAGS = -avoid-version +noinst_LTLIBRARIES = libexample.la +noinst_HEADERS = example.h +libexample_la_SOURCES = example.cc spectator.cc +_SOURCES = spectator.h diff --git a/src/game/example/example.cc b/src/game/example/example.cc new file mode 100644 index 0000000..d8e18ea --- /dev/null +++ b/src/game/example/example.cc @@ -0,0 +1,125 @@ +/* + intro/intro.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "core/gameserver.h" +#include "example/example.h" +#include "example/spectator.h" + +namespace example { + +Example::Example() : core::Module("example", "The Osirion Project Example", true) +{ +} + +Example::~Example() +{ +} + +void Example::init() +{ + /* + Initialize engine game variables + */ + Spectator::g_spectatorspeed = core::Cvar::get("g_spectatorspeed", "4", core::Cvar::Game | core::Cvar::Archive); + Spectator::g_spectatorspeed->set_info("[float] speed of the spectator"); + + Spectator::g_spectatorrotation = core::Cvar::get("g_spectatorrotation", "45", core::Cvar::Game | core::Cvar::Archive); + Spectator::g_spectatorrotation->set_info("[float] rotation speed of the spectator in degrees per second"); + + /* + The Osirion world consists of Zones. Zones are different + areas in the game. The example has only one zone. + */ + zone = new core::Zone("example"); // create a Zone object + zone->set_name("Example Zone"); // set the zone name + core::Zone::add(zone); // add the zone to the world + + /* + Every object in the Osirion world is derived from core::Entity. + We create a few basic entities + */ + + core::Entity *cube = new core::Entity(); // a new entity + cube->set_label("cube"); + cube->set_name("The Red Cube"); + cube->entity_shape = core::Entity::Cube; // set the shape to cube + cube->entity_location.assign(16, -8, 0); // set location + cube->entity_color.assign(1, 0, 0); // set RGB color red + cube->entity_radius = 0.25f; // set radius, in game units + cube->set_zone(zone); // add the entity to the zone + + core::Entity *sphere = new core::Entity(); // a new entity + sphere->set_label("sphere"); + sphere->set_name("The Green Sphere"); + sphere->entity_shape = core::Entity::Sphere; // set the shape to sphere + sphere->entity_location.assign(16, 0, 0); // set location + sphere->entity_color.assign(0, 1, 0); // set RGB color green + sphere->entity_radius = 0.25f; // set radius, in game units + sphere->set_zone(zone); // add the entity to the zone + + core::Entity *diamond = new core::Entity(); // a new entity + diamond->set_label("diamond"); + diamond->set_name("The Blue Diamond"); + diamond->entity_shape = core::Entity::Diamond; // set the shape to cube + diamond->entity_location.assign(16, 8, 0); // set location + diamond->entity_color.assign(0, 0, 1); // set RGB color blue + diamond->entity_radius = 0.25f; // set radius, in game units + diamond->set_zone(zone); // add the entity to the zone + + core::Entity *axis = new core::Entity(); // a new entity + axis->set_label("origin"); + axis->set_name("The Origin"); + axis->entity_shape = core::Entity::Axis; // set the shape to axis + axis->entity_location.assign(0, 0, 0); // set location + axis->entity_color.assign(1); // set greyscale color white + axis->entity_color_second.assign(0.5f, 0.0f, 0.5f); // set RGB secondary color + axis->entity_radius = 0.25f; // set radius, in game units + axis->set_zone(zone); // add the entity to the zone + +} + + +void Example::player_connect(core::Player *player) +{ + // add a spectator object for the new player + Spectator *spectator; + spectator = new Spectator(player); + spectator->set_zone(zone); + spectator->set_dirty(); + + // set the player's control to the spectator object + player->set_zone(zone); + player->set_control(spectator); + player->set_dirty(); + + // send a message to the player + core::server()->send(player, "Welcome to " + name()); + + // broadcast a message to all players + core::server()->broadcast("^B" + player->name() + " entered."); +} + +void Example::player_disconnect(core::Player *player) +{ + // broadcast a message to all players + core::server()->broadcast("^B" + player->name() + " has left."); +} + +void Example::frame(float elapsed) +{ + +} + +void Example::shutdown() +{ + /* + The world is automaticly deleted on shutdown, + but local variables have to be cleaned up + */ + zone = 0; +} + +} diff --git a/src/game/example/example.h b/src/game/example/example.h new file mode 100644 index 0000000..ba8aa72 --- /dev/null +++ b/src/game/example/example.h @@ -0,0 +1,65 @@ +/* + intro/example.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_EXAMPLE_H__ +#define __INCLUDED_EXAMPLE_H__ + +// import core functionality +#include "core/core.h" + +// console functions +#include "sys/sys.h" + +/// example game module +/** This is the osirion Project example module. It describes + * how to create custom modules and how to use the engine functionality. + */ +namespace example +{ + +/// example game module +/** Every game module derives from core::Module and has to implement a + * number of virtual functions + */ +class Example : public core::Module +{ +public: + /// constructor, called on module load + /** Modules are loaded at the start of the program, + * the constructor is only called once. + */ + Example(); + + /// desctructor, called on module unload + ~Example(); + + /// called once every server frame + /** @param elapsed time elapsed since the precious server frame, in seconds + */ + void frame(float elapsed); + + /// called when a player connects + void player_connect(core::Player *player); + + /// called when a player disconnects + void player_disconnect(core::Player *player); + +protected: + /// called when the game starts + void init(); + + /// called when the game is shut down + void shutdown(); + + +private: + core::Zone *zone; +}; + +} + +#endif // __INCLUDED_EXAMPLE_H__ + diff --git a/src/game/example/spectator.cc b/src/game/example/spectator.cc new file mode 100644 index 0000000..7b3a165 --- /dev/null +++ b/src/game/example/spectator.cc @@ -0,0 +1,65 @@ +/* + base/spectator.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 +*/ + +#include "example/spectator.h" + +namespace example { + +core::Cvar *Spectator::g_spectatorspeed = 0; +core::Cvar *Spectator::g_spectatorrotation = 0; + +Spectator::Spectator(core::Player *owner) : core::EntityControlable(owner) +{ + // default properties + entity_shape = core::Entity::Diamond; + entity_radius = 0.25f; + + // the spectator gets player color + entity_color.assign(owner->color()); + entity_color_second.assign(owner->color_second()); + + // set dirty flag + set_dirty(); +} + +Spectator::~Spectator() +{ +} + +void Spectator::frame(float elapsed) +{ + // only update if necessary + if (!entity_speed && ! target_thrust && !target_direction && !target_pitch && !target_roll) + return; + + // assign thrust value from input + entity_thrust = target_thrust; + + // rotate according to input + float rotation = g_spectatorrotation->value() * elapsed; + entity_axis.change_direction(target_direction * rotation); + entity_axis.change_pitch(target_pitch * rotation); + entity_axis.change_roll(target_roll * rotation); + + // assign speed from thruster + float maxspeed = g_spectatorspeed->value(); + entity_speed = entity_thrust * maxspeed; + + // assign new location + if (entity_speed) + entity_location += entity_axis.forward() * entity_speed * elapsed; + + if (target_afterburner) + entity_location += entity_axis.forward() * maxspeed * target_afterburner * elapsed; + + if (target_strafe) + entity_location += entity_axis.left() * maxspeed * target_strafe * elapsed; + + // set dirty flag + set_dirty(); +} + +} diff --git a/src/game/example/spectator.h b/src/game/example/spectator.h new file mode 100644 index 0000000..435c45c --- /dev/null +++ b/src/game/example/spectator.h @@ -0,0 +1,32 @@ +/* + base/spectator.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_EXAMPLE_SPECTATOR_H__ +#define __INCLUDED_EXAMPLE_SPECTATOR_H__ + +#include "core/entity.h" +#include "core/cvar.h" + +namespace example { + +/// A spectator entity +class Spectator : public core::EntityControlable +{ +public: + Spectator(core::Player *owner); + ~Spectator(); + + /// update the ship state + virtual void frame(float elapsed); + + static core::Cvar *g_spectatorspeed; + static core::Cvar *g_spectatorrotation; +}; + +} + +#endif // __INCLUDED_EXAMPLE_SPECTATOR_H__ + |