Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-02-17 18:59:52 +0000
committerStijn Buys <ingar@osirion.org>2008-02-17 18:59:52 +0000
commit982562fa19bb87a3dab352e562f386f61c171b7b (patch)
treeaeade8d5b7d3c68f5c222af1d8ecc6a734e1b43f /src/core/entity.cc
parentd198b7b8d9ff713d891f35ab173d1f428f610e7d (diff)
major rewrite of Cvar, Func and Entity
Diffstat (limited to 'src/core/entity.cc')
-rw-r--r--src/core/entity.cc164
1 files changed, 92 insertions, 72 deletions
diff --git a/src/core/entity.cc b/src/core/entity.cc
index 6115e71..229fb52 100644
--- a/src/core/entity.cc
+++ b/src/core/entity.cc
@@ -4,121 +4,141 @@
the terms of the GNU General Public License version 2.
*/
+#include <vector>
+#include <iomanip>
+
#include "sys/sys.h"
#include "core/entity.h"
-#include <iomanip>
-
namespace core
{
-// --- Entity -----------------------------------------------------
+using math::Color;
+using math::Vector3f;
-Entity::Entity(unsigned int entity_flags)
-{
- flags = entity_flags;
-
- core_shape = entity::Diamond;
- core_color = math::Color(1.0f, 1.0f, 1.0f);
- core_radius = 1.0f;
+/* ---- Static functions for the Entity registry ------------------- */
- core::entity::add(this);
- type = 0;
-
- direction = 0;
-}
+std::map<unsigned int, Entity *> Entity::registry;
-Entity::~Entity()
-{}
+void Entity::add(Entity *ent)
+{
+ std::map<unsigned int, Entity*>::iterator it;
+ unsigned int id = 1;
+ for (it = registry.begin(); it != registry.end() && id == (*it).second->id(); it++) {
+ id++;
+ }
+ ent->entity_id = id;
+ registry[id] = ent;
+}
-// --- EntityDynamic ------------------------------------------
+Entity *Entity::find(unsigned int id)
+{
+ std::map<unsigned int, Entity *>::iterator it = registry.find(id);
+ if (it == registry.end())
+ return 0;
+ else
+ return (*it).second;
+}
-EntityDynamic::EntityDynamic(unsigned int entity_flags) :
- Entity(entity_flags)
+void Entity::remove(unsigned int id)
{
- speed = 0.0f;
+ std::map<unsigned int, Entity *>::iterator it = registry.find(id);
+ if (it != registry.end()) {
+ delete((*it).second);
+ registry.erase(it);
+ }
}
-EntityDynamic::~EntityDynamic()
+void Entity::list()
{
+ std::map<unsigned int, Entity *>::iterator it;
+ for (it = registry.begin(); it != registry.end(); it++) {
+ std::string typeindicator;
+ Entity *entity = (*it).second;
+ con_print << " id " << std::setw(4) << entity->id()
+ << " type " << std::setw(4) << entity->type()
+ << ":" << std::setw(4) << entity->moduletype()
+ << " " << entity->name() << std::endl;
+ }
+ con_print << registry.size() << " registered entities" << std::endl;
}
-// --- EntityControlable ------------------------------------------
+/*----- Entity ----------------------------------------------------- */
-EntityControlable::EntityControlable(unsigned int entity_flags) :
- EntityDynamic(entity_flags)
+Entity::Entity(unsigned int flags) :
+ entity_location(0.0f, 0.0f, 0.0f),
+ entity_color(1.0f, 1.0f, 1.0f, 1.0f)
{
- owner = 0;
- target_direction = 0.0f;
- target_thrust = 0.0f;
+ entity_id = 0;
+ entity_flags = flags;
+ entity_moduletypeid = 0;
+
+ entity_radius = 1.0f;
+ entity_direction = 0;
+ entity_shape = Diamond;
+
+ add(this);
}
-EntityControlable::~EntityControlable()
+Entity::~Entity()
+{
+}
+
+void Entity::frame(float seconds)
{
}
-// --- namespace entity -------------------------------------------
+/* ---- EntityDynamic ---------------------------------------------- */
-namespace entity
+EntityDynamic::EntityDynamic(unsigned int flags) :
+ Entity(flags)
{
+ entity_speed = 0.0f;
+}
-std::vector<Entity*> registry;
+EntityDynamic::~EntityDynamic()
+{
+}
-void add(Entity *ent)
+void EntityDynamic::frame(float seconds)
{
- std::vector<Entity *>::iterator it;
- unsigned int entity_id = 1;
- for (it=registry.begin(); it != registry.end() && entity_id == (*it)->id; it++) {
- entity_id++;
- }
- ent->id = entity_id;
- registry.push_back(ent);
+ // location avoid sin/cos calculations
+ entity_location.x += cosf(entity_direction * M_PI / 180) * entity_speed * seconds;
+ entity_location.z -= sinf(entity_direction * M_PI / 180) * entity_speed * seconds;
}
-void remove(unsigned int entity_id)
+/*----- EntityControlable ------------------------------------------ */
+
+EntityControlable::EntityControlable(Player *player, unsigned int flags) :
+ EntityDynamic(flags)
{
- std::vector<Entity *>::iterator it;
- for (it=registry.begin(); it != registry.end(); it++) {
- if (entity_id == (*it)->id) {
- delete((*it));
- registry.erase(it);
- return;
- }
- }
+ entity_owner = 0;
+ entity_thrust = 0;
+
+ target_direction = 0.0f;
+ target_thrust = 0.0f;
}
-void clear()
+EntityControlable::~EntityControlable()
{
- std::vector<Entity *>::iterator it;
- for (it=registry.begin(); it != registry.end(); it++) {
- delete(*it);
- (*it) = 0;
- }
- registry.clear();
}
-void list()
+void EntityControlable::frame(float seconds)
{
- std::vector<Entity *>::iterator it;
- for (it=registry.begin(); it != registry.end(); it++) {
- con_print << " id " << std::setw(4) << (*it)->id
- << " type " << std::setw(4) << (*it)->core_type()
- << ":" << std::setw(4) << (*it)->type
- << " " << (*it)->label << std::endl;
- }
- con_print << registry.size() << " registered entities" << std::endl;
+ entity_direction = target_direction;
+ entity_thrust = target_thrust;
+
+ EntityDynamic::frame(seconds);
}
-void frame(float seconds)
+void EntityControlable::set_thrust(float thrust)
{
- std::vector<Entity *>::iterator it;
- for (it=registry.begin(); it != registry.end(); it++) {
- if ((*it)->core_type() == entity::Controlable) {
- static_cast<EntityControlable *>(*it)->frame(seconds);
- }
- }
+ target_thrust = thrust;
}
+void EntityControlable::set_direction(float direction)
+{
+ target_direction = direction;
}
}