TurtleBrains  0.2.1
High quality, portable, C++ API for native application and game development.
tb_entity_manager.h
1 
9 #ifndef _TurtleBrains_EntityManager_h_
10 #define _TurtleBrains_EntityManager_h_
11 
12 #include "tb_entity.h" //For EntityType definition.
13 #include "../core/tb_noncopyable.h"
14 
15 #include <map>
16 #include <set>
17 #include <list>
18 
19 namespace TurtleBrains
20 {
21  namespace Game
22  {
23 
24  class Entity;
25 
31  {
32  public:
37 
41  virtual ~EntityFactoryInterface(void);
42 
46  Entity* CreateEntityByType(const EntityType& entityType);
47 
51  bool DestroyEntity(Entity* entityToDestroy);
52  };
53 
59 //
61 //class ItemInterface
62 //{
63 //public:
64 // virtual ~ItemInterface(void) = 0;
65 //};
66 //
68 //class Manager
69 //{
70 //public:
71 // void AddItem(ItemInterface* item);
72 // void RemoveItem(ItemInterface* item);
73 //
74 // //Contain and do things with Items...
75 //};
76 //
78 //class Scene
79 //{
80 //public:
81 // void OnEnter()
82 // {
83 // //The following line is desired, but currently can't be done because Scene needs to own (cleanup) the item.
84 // //But I want to support dynamically allocated items for bullets and other effects/entities etc... that then
85 // //clean themselves up automatically when theManager.RemoveItem() is called on it, without needing Scene() to
86 // //do so.
87 // theManager.AddItem(new AwesomeItem());
88 //
89 // //The following is okay and works flawlessly without dynamic memory!
90 // theManager.AddItem(&mCoin);
91 // }
92 //
93 // void OnExit()
94 // {
95 //
96 // theManager.RemoveItem(&mCoin);
97 // theManager.ClearItems();
98 // }
99 //
100 //private:
101 // CoinItem mCoin;
102 //};
103 
104 
105 
106 
107 
108 
109 
115  {
116  public:
120  typedef std::list<EntityInterface*> EntityList;
121 
125  EntityManager(void);
126 
131  virtual ~EntityManager(void) = 0;
132 
139 // Entity& AddEntity(const EntityType& entityType);
140 
146  void AddEntity(EntityInterface* entity);
147 
154  void AddEntity(EntityInterface& entity);
155 
160  void RemoveEntity(EntityInterface* entity);
161 
165  void RemoveEntities(const EntityType& byType = Entity::kInvalidType);
166 
170  EntityList GetAllEntities(void);
171 
175  EntityList GetEntitiesByType(const EntityType& byType);
176 
183  EntityList GetEntitiesAt(const tbMath::Vector2& point, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
184 
188  EntityList GetEntitiesWithin(const tbMath::Vector2& center, const float radius, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
189 
193  EntityList GetEntitiesWithin(const tbMath::Vector2& center, const float width, const float height, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
194 
199  void Simulate(void);
200 
201  protected:
205  virtual void OnUpdate(const float deltaTime) override;
206 
207  private:
208  void ReallyAddEntity(EntityInterface* entity, bool managed);
209  void SafeToRemoveEntities(void);
210 
211  typedef std::set<EntityInterface*> EntitySet;
212  typedef std::map<EntityType, EntityList> EntityByTypeMap;
213 
214  EntityList mEntities;
215  EntityList mManagedEntities;
216  EntitySet mEntitiesToRemove;
217  EntityByTypeMap mEntitiesByType;
218  };
219 
220  }; /* namespace Game */
221 }; /* namespace TurtleBrains */
222 
223 namespace tbGame = TurtleBrains::Game;
224 
225 #endif /* _TurtleBrains_EntityManager_h_ */
Definition: tb_vector.h:48
Definition: tb_entity.h:39
Definition: tb_graphic_list.h:25
void AddEntity(EntityInterface *entity)
Entity * CreateEntityByType(const EntityType &entityType)
std::list< EntityInterface * > EntityList
Definition: tb_entity_manager.h:120
void RemoveEntity(EntityInterface *entity)
Definition: tb_noncopyable.h:22
Contains all functions, classes and helpers related to game/application development written by Tim "B...
Definition: tb_application_dialog.h:21
virtual void OnUpdate(const float deltaTime) override
Definition: tb_entity_manager.h:114
void RemoveEntities(const EntityType &byType=Entity::kInvalidType)
tbCore::tbString EntityType
Definition: tb_entity.h:28
static const EntityType kInvalidType
Definition: tb_entity.h:46
Definition: tb_entity_manager.h:30
EntityList GetEntitiesWithin(const tbMath::Vector2 &center, const float radius, const EntityType &byType=Entity::kInvalidType, bool onlyCollidableEntities=false)
EntityList GetEntitiesByType(const EntityType &byType)
bool DestroyEntity(Entity *entityToDestroy)
EntityList GetEntitiesAt(const tbMath::Vector2 &point, const EntityType &byType=Entity::kInvalidType, bool onlyCollidableEntities=false)
This is the heart of TurtleBrains for game developers to create GameScenes and Entities to interact w...