diff options
Diffstat (limited to 'src/core/entity.cc')
-rw-r--r-- | src/core/entity.cc | 166 |
1 files changed, 165 insertions, 1 deletions
diff --git a/src/core/entity.cc b/src/core/entity.cc index 229fb52..7e9c788 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -7,6 +7,11 @@ #include <vector> #include <iomanip> +namespace core +{ + class Entity; +} + #include "sys/sys.h" #include "core/entity.h" @@ -31,6 +36,16 @@ void Entity::add(Entity *ent) registry[id] = ent; } +void Entity::add(Entity *ent, unsigned int id) +{ + if (find(id)) { + con_warn << "Duplicate entity " << id << "!\n"; + return; + } + ent->entity_id = id; + registry[id] = ent; +} + Entity *Entity::find(unsigned int id) { std::map<unsigned int, Entity *>::iterator it = registry.find(id); @@ -77,13 +92,78 @@ Entity::Entity(unsigned int flags) : entity_direction = 0; entity_shape = Diamond; + entity_created = true; + entity_destroyed = false; + entity_dirty = false; + add(this); } +Entity::Entity(std::istream & is) +{ + // type is already determined + unsigned int s; + std::string n; + + is >> entity_id; + is >> entity_moduletypeid; + is >> entity_flags; + is >> entity_location; + is >> entity_color; + is >> s; // shape + is >> entity_radius; + is >> entity_direction; + + char c; + while ( (is.get(c)) && (c != '"')); + while ( (is.get(c)) && (c != '"')) + n += c; + + entity_shape = (Shape) s ; + entity_name = n; + + entity_created = true; + entity_destroyed = false; + entity_dirty = false; + + add(this, entity_id); +} + Entity::~Entity() { } +void Entity::serialize(std::ostream & os) const +{ + os << type() << " " + << entity_id << " " + << entity_moduletypeid << " " + << entity_flags << " " + << entity_location << " " + << entity_color << " " + << entity_shape << " " + << entity_radius << " " + << entity_direction << " " + << "\"" << entity_name << "\""; +} + +void Entity::serialize_client_update(std::ostream & os) const +{ +} + +void Entity::recieve_client_update(std::istream &is) +{ +} + +void Entity::serialize_server_update(std::ostream & os) const +{ +} + +void Entity::recieve_server_update(std::istream &is) +{ +} + + void Entity::frame(float seconds) { } @@ -96,15 +176,51 @@ EntityDynamic::EntityDynamic(unsigned int flags) : entity_speed = 0.0f; } +EntityDynamic::EntityDynamic(std::istream & is) : + Entity(is) +{ + is >> entity_speed; +} + EntityDynamic::~EntityDynamic() { } void EntityDynamic::frame(float seconds) { + if (entity_speed == 0) + return; + // 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; + entity_dirty = true; +} + +void EntityDynamic::serialize(std::ostream & os) const +{ + Entity::serialize(os); + os << " " << entity_speed; +} + +void EntityDynamic::serialize_client_update(std::ostream & os) const +{ +} + +void EntityDynamic::recieve_client_update(std::istream &is) +{ +} + +void EntityDynamic::serialize_server_update(std::ostream & os) const +{ + os << entity_location << " " << entity_direction << " " << entity_speed; +} + +void EntityDynamic::recieve_server_update(std::istream &is) +{ + is >> entity_location; + is >> entity_direction; + is >> entity_speed; } /*----- EntityControlable ------------------------------------------ */ @@ -119,26 +235,74 @@ EntityControlable::EntityControlable(Player *player, unsigned int flags) : target_thrust = 0.0f; } +EntityControlable::EntityControlable(std::istream & is) : + EntityDynamic(is) +{ + unsigned int o; + + is >> entity_thrust; + is >> o; + + // FIXME resolve owner + entity_owner = 0; +} + + EntityControlable::~EntityControlable() { } +void EntityControlable::serialize(std::ostream & os) const +{ + EntityDynamic::serialize(os); + os << " " << entity_thrust; + os << " " << entity_owner->id(); +} + +void EntityControlable::serialize_client_update(std::ostream & os) const +{ + EntityDynamic::serialize_client_update(os); + os << " " << target_direction; + os << " " << target_thrust; +} + +void EntityControlable::recieve_client_update(std::istream &is) +{ + EntityDynamic::recieve_client_update(is); + is >> target_direction; + is >> target_thrust; +} + +void EntityControlable::serialize_server_update(std::ostream & os) const +{ + EntityDynamic::serialize_server_update(os); + os << " " << entity_thrust; +} + +void EntityControlable::recieve_server_update(std::istream &is) +{ + EntityDynamic::recieve_server_update(is); + is >> entity_thrust; +} + void EntityControlable::frame(float seconds) { entity_direction = target_direction; entity_thrust = target_thrust; - + entity_dirty = true; EntityDynamic::frame(seconds); } void EntityControlable::set_thrust(float thrust) { target_thrust = thrust; + entity_dirty = true; } void EntityControlable::set_direction(float direction) { target_direction = direction; + entity_dirty = true; } } |