/* core/entity.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2. */ #include #include #include "sys/sys.h" #include "core/entity.h" namespace core { using math::Color; using math::Vector3f; /* ---- Static functions for the Entity registry ------------------- */ std::map Entity::registry; 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; } Entity *Entity::find(unsigned int id) { std::map::iterator it = registry.find(id); if (it == registry.end()) return 0; else return (*it).second; } void Entity::remove(unsigned int id) { std::map::iterator it = registry.find(id); if (it != registry.end()) { delete((*it).second); registry.erase(it); } } 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; } /*----- Entity ----------------------------------------------------- */ Entity::Entity(unsigned int flags) : entity_location(0.0f, 0.0f, 0.0f), entity_color(1.0f, 1.0f, 1.0f, 1.0f) { entity_id = 0; entity_flags = flags; entity_moduletypeid = 0; entity_radius = 1.0f; entity_direction = 0; entity_shape = Diamond; add(this); } Entity::~Entity() { } void Entity::frame(float seconds) { } /* ---- EntityDynamic ---------------------------------------------- */ EntityDynamic::EntityDynamic(unsigned int flags) : Entity(flags) { entity_speed = 0.0f; } EntityDynamic::~EntityDynamic() { } void EntityDynamic::frame(float seconds) { // 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; } /*----- EntityControlable ------------------------------------------ */ EntityControlable::EntityControlable(Player *player, unsigned int flags) : EntityDynamic(flags) { entity_owner = 0; entity_thrust = 0; target_direction = 0.0f; target_thrust = 0.0f; } EntityControlable::~EntityControlable() { } void EntityControlable::frame(float seconds) { entity_direction = target_direction; entity_thrust = target_thrust; EntityDynamic::frame(seconds); } void EntityControlable::set_thrust(float thrust) { target_thrust = thrust; } void EntityControlable::set_direction(float direction) { target_direction = direction; } }