/* core/entity.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_CORE_ENTITY_H__ #define __INCLUDED_CORE_ENTITY_H__ #include #include #include #include "model/model.h" #include "math/axis.h" #include "math/mathlib.h" namespace core { class Entity; class EntityControlable; } #include "core/clientstate.h" #include "core/player.h" namespace core { /// The base world entity. All gameworld entities must derive from this class. class Entity { public: /// Entity flags /** * entities with the Static flag set will not get client-side interpolation */ enum Flags {Static=1, Solid=2, Bright=4}; /// Entity type constants enum Type {Default=0, Dynamic=1, Controlable=2, Globe=3}; /// Entity shape constants enum Shape {Diamond=0, Sphere=1, Cube=2, Axis=3}; /// create a new entity and add it to the registry Entity(unsigned int flags = 0); /// create an entity froms stream data Entity(std::istream & is); /// destroy an entity virtual ~Entity(); /*----- inspectors ------------------------------------------------ */ /// entity id inline unsigned int id() const { return entity_id; } /// module type id inline unsigned int moduletype() const { return entity_moduletypeid; } /// core type id virtual inline unsigned int type() const { return Default; } /// entity flags inline unsigned int flags() const { return entity_flags; } /// entity name (can not contain double qoutes ") inline std::string const & name() { return entity_name; } /// entity model name inline std::string const & modelname() { return entity_modelname; } /// entity client render state inline ClientState * state() { return entity_clientstate; } /// pointer to the model, is used client-side inline model::Model * model() { return entity_model; } /// dirty flag inline bool dirty() const { return entity_dirty; } /// entity location inline math::Vector3f & location() { return entity_location; } /// local coordinate system of the entity inline math::Axis & axis() { return entity_axis; } /// base color of the entity inline math::Color const & color() const { return entity_color; } /// base shape of the entity inline Shape shape() const { return entity_shape; } /// base radius of the entity inline float radius() const { return entity_radius; } /// serialize the entity to a stream virtual void serialize(std::ostream & os) const; /// serialize a client-to-server update on a stream virtual void serialize_client_update(std::ostream & os) const; /// serialize a server-to-client update on a stream virtual void serialize_server_update(std::ostream & os) const; /*----- mutators -------------------------------------------------- */ /// receive a client-to-server update from a stream virtual void recieve_client_update(std::istream &is); /// receive a server-to-client update from a stream virtual void recieve_server_update(std::istream &is); /// mark the entity as destroyed inline void die() { entity_destroyed = true; } /// runs one game frame for the entity /** * The default implementation does nothing */ virtual void frame(float seconds); /*----- static ---------------------------------------------------- */ /// the entity registry static std::map registry; /// find an entity in the registry static Entity *find(unsigned int id); /// remove one entity from the registry and deletes it static void remove(unsigned int entity_id); /// list the entity registry static void list(); /* entity_ variables can be set by the module */ math::Vector3f entity_location; math::Axis entity_axis; float entity_radius; std::string entity_name; std::string entity_modelname; model::Model *entity_model; Shape entity_shape; math::Color entity_color; unsigned int entity_moduletypeid; unsigned int entity_flags; bool entity_dirty; bool entity_created; bool entity_destroyed; /// timestamp when entity data was received from the server float entity_servertimestamp; ClientState *entity_clientstate; private: /// add an entity to the registry static void add(Entity *ent); /// add an entity with id to the registry void add(Entity *ent, unsigned int id); /// the id is set by add() unsigned int entity_id; }; /// an entity that can move around in the game world class EntityDynamic : public Entity { public: /// create a dynamic entity EntityDynamic(unsigned int flags = 0); /// create a dynamic entity from stream data EntityDynamic(std::istream & is); virtual ~EntityDynamic(); /*----- inspectors ------------------------------------------------ */ /// core type id virtual inline unsigned int type() const { return Entity::Dynamic; } /// current speed of the entity in game units per second inline float speed() const { return entity_speed; } /// serialize the entity to a stream virtual void serialize(std::ostream & os) const; /// serialize a client-to-server update on a stream virtual void serialize_client_update(std::ostream & os) const ; /// serialize a server-to-client update on a stream virtual void serialize_server_update(std::ostream & os) const; /*----- mutators -------------------------------------------------- */ /// receive a client-to-server update from a stream virtual void recieve_client_update(std::istream &is); /// receive a server-to-client update from a stream virtual void recieve_server_update(std::istream &is); /// runs one game frame for the entity /** * The default implementation will update the position() of the entity, * determined by its speed() and axis() */ virtual void frame(float seconds); /// speed of the entity float entity_speed; }; /// an entity that can be controlled by a player class EntityControlable : public EntityDynamic { public: /// create a controlable entity EntityControlable(Player *player, unsigned int flags = 0); /// create a controlable entity from stream data EntityControlable(std::istream & is); virtual ~EntityControlable(); /*----- inspectors ------------------------------------------------ */ /// core type id virtual inline unsigned int type() const { return Entity::Controlable; } /// owner of this entity inline Player *owner() const { return entity_owner; } /// thrust inline float thrust() const { return entity_thrust; } /// serialize the entity to a stream virtual void serialize(std::ostream & os) const; /// serialize a client-to-server update on a stream virtual void serialize_client_update(std::ostream & os) const; /// serialize a server-to-client update on a stream virtual void serialize_server_update(std::ostream & os) const; /*----- mutators -------------------------------------------------- */ /// receive a client-to-server update from a stream virtual void recieve_client_update(std::istream &is); /// receive a server-to-client update from a stream virtual void recieve_server_update(std::istream &is); /// set the target thrust void set_thrust(float thrust); /// set the target direction void set_direction(float direction); /// set the target pitch void set_pitch(float pitch); /// set target roll void set_roll(float roll); /// runs one game frame for the entity /** * The default implementation will set direction() and thrust() to the desired targets * and calls its parent frame() funcion. */ virtual void frame(float seconds); /* entity_ variables can be set by the module */ /// owner of the entity Player *entity_owner; /// current thrust float entity_thrust; /* target_ variables can be set by the client */ /// target thrust as set by the client float target_thrust; /// target direction as set by the client /** target_direction must be in the [-1, 1] range */ float target_direction; /// target pitch as set by the client /** target_pitch must be in the [-1, 1] range */ float target_pitch; /// target roll as set by the client /** target_roll must be in the [-1, 1] range */ float target_roll; }; /// a Globe entity class EntityGlobe : public Entity { public: EntityGlobe(unsigned int flags = 0); EntityGlobe(std::istream & is); ~EntityGlobe(); virtual void serialize(std::ostream & os) const; /*----- inspectors ------------------------------------------------ */ /// core type id virtual inline unsigned int type() const { return Entity::Globe; } std::string entity_texture; /// client side, texture id unsigned int render_texture; }; } #endif // __INCLUDED_CORE_ENTITY_H__