00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 class AbstractComponent;
00019 class Game;
00020
00021 #include <iostream>
00022 #include <hash_map>
00023 #include <string>
00024
00025 #include "game/AbstractComponent.h"
00026 #include "game/ComponentFactory.h"
00027
00028 #include "util/AbstractStreamedObject.h"
00029 #include "util/Attribute.h"
00030 #include "util/Macros.h"
00031 #include "util/Matrix.h"
00032 #include "util/StringHasher.h"
00033 #include "util/Vector2.h"
00034
00035 #define INVALIDID 0xFFFFFFFF
00036
00037 class Game;
00038
00050 class GameEntity : public AbstractStreamedObject
00051 {
00052 private:
00053 typedef stdext::hash_map<ComponentId, AbstractComponent*, StringHasher> ComponentMap;
00054 typedef std::list<AbstractComponent*> ComponentList;
00055 typedef ComponentMap::const_iterator ComponentMapIterator;
00056 typedef ComponentList::const_iterator ComponentListIterator;
00057
00058 Vector2 m_position;
00059 float m_fRotation;
00060
00061 bool m_bActive;
00062
00063 UNIQUEID m_id;
00064
00065 Game* m_pGame;
00066
00067 ComponentMap m_componentMap;
00068 ComponentList m_updateList;
00069 public:
00073 GameEntity();
00074
00079 ~GameEntity();
00080
00084 const Vector2& GetPosition() const { return m_position; }
00085
00091 void SetPosition(const Vector2 & position) { m_position = position; }
00092
00096 float GetRotation() const { return m_fRotation; }
00097
00103 void SetRotation(const float & fRotation) { m_fRotation = fRotation; }
00104
00109 Matrix GetXForm() const;
00110
00115 const UNIQUEID & GetId() const { return m_id; }
00116
00122 void SetId(const UNIQUEID & id) { m_id = id; }
00123
00129 bool GetActive() const { return m_bActive; }
00130
00136 void SetActive(bool bActive) { m_bActive = bActive; }
00137
00142 Game* GetGame() const { return m_pGame; }
00143
00150 void SetGame(Game* pGame) { m_pGame = pGame; }
00151
00163 template<class T>
00164 T* GetComponent(const ComponentId& familyId) const;
00165
00174 template<class T>
00175 T* CreateComponentCast(const ComponentId& id);
00176
00186 AbstractComponent* CreateComponent(const ComponentId& id);
00187
00195 void RemoveComponent(const ComponentId& id);
00196
00202 void Update(const int nDelta);
00203
00214 bool FromAttribute(const Attribute * pAttributes);
00215
00223 virtual void ToStream(std::ostream& stream) const;
00224
00232 virtual void FromStream(std::istream& stream);
00233 };
00234
00235 template<class T>
00236 inline T* GameEntity::GetComponent(const ComponentId& familyId) const
00237 {
00238 ComponentMapIterator i = m_componentMap.find(familyId);
00239
00240 if(i != m_componentMap.end())
00241 return reinterpret_cast<T*>(i->second);
00242
00243 return 0;
00244 }
00245
00246 template<class T>
00247 inline T* GameEntity::CreateComponentCast(const ComponentId& id)
00248 {
00249 return reinterpret_cast<T*>(CreateComponent(id));
00250 }
00251
00252 inline Matrix GameEntity::GetXForm() const
00253 {
00254 Matrix xform;
00255
00256 xform.Rotation(MATH_2PI - m_fRotation);
00257 xform.Translation(m_position._x, m_position._y);
00258
00259 return xform;
00260 }