From 982562fa19bb87a3dab352e562f386f61c171b7b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 17 Feb 2008 18:59:52 +0000 Subject: major rewrite of Cvar, Func and Entity --- src/core/entity.cc | 164 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 92 insertions(+), 72 deletions(-) (limited to 'src/core/entity.cc') 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 +#include + #include "sys/sys.h" #include "core/entity.h" -#include - 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 Entity::registry; -Entity::~Entity() -{} +void Entity::add(Entity *ent) +{ + std::map::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::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::iterator it = registry.find(id); + if (it != registry.end()) { + delete((*it).second); + registry.erase(it); + } } -EntityDynamic::~EntityDynamic() +void Entity::list() { + std::map::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 registry; +EntityDynamic::~EntityDynamic() +{ +} -void add(Entity *ent) +void EntityDynamic::frame(float seconds) { - std::vector::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::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::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::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::iterator it; - for (it=registry.begin(); it != registry.end(); it++) { - if ((*it)->core_type() == entity::Controlable) { - static_cast(*it)->frame(seconds); - } - } + target_thrust = thrust; } +void EntityControlable::set_direction(float direction) +{ + target_direction = direction; } } -- cgit v1.2.3