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 //
60 // UPDATE (before) March 22, 2016:
61 //
62 // The following is now working with GraphicList and EntityManager by using two similar, but DIFFERENT, AddItem()
63 // functions: AddItem(ItemInterface* managedItem) will get deleted automatically, add and forget, while the alternative
64 // AddItem(ItemInterface& ownedItem) will not be deleted or cleaned up, and is expected to remain in scope/valid until
65 // after it gets manually removed.
66 //
68 //class ItemInterface
69 //{
70 //public:
71 // virtual ~ItemInterface(void) = 0;
72 //};
73 //
75 //class Manager
76 //{
77 //public:
78 // void AddItem(ItemInterface* item);
79 // void RemoveItem(ItemInterface* item);
80 //
81 // //Contain and do things with Items...
82 //};
83 //
85 //class Scene
86 //{
87 //public:
88 // void OnEnter()
89 // {
90 // //The following line is desired, but currently can't be done because Scene needs to own (cleanup) the item.
91 // //But I want to support dynamically allocated items for bullets and other effects/entities etc... that then
92 // //clean themselves up automatically when theManager.RemoveItem() is called on it, without needing Scene() to
93 // //do so.
94 // theManager.AddItem(new AwesomeItem());
95 //
96 // //The following is okay and works flawlessly without dynamic memory!
97 // theManager.AddItem(&mCoin);
98 // }
99 //
100 // void OnExit()
101 // {
102 //
103 // theManager.RemoveItem(&mCoin);
104 // theManager.ClearItems();
105 // }
106 //
107 //private:
108 // CoinItem mCoin;
109 //};
110 
111 
112 
113 
114 
115 
116 
124  {
125  public:
129  typedef std::list<Entity*> EntityList;
130 
134  EntityManager(void);
135 
140  virtual ~EntityManager(void) = 0;
141 
148 // Entity& AddEntity(const EntityType& entityType);
149 
155  void AddEntity(Entity* entity);
156 
163  void AddEntity(Entity& entity);
164 
169  void RemoveEntity(Entity* entity);
170 
174  void RemoveEntities(const EntityType& byType = Entity::kInvalidType);
175 
179  void EntityTypeChanged(Entity& entity, const EntityTypeContainer& oldTypes);
180 
184  EntityList GetAllEntities(void);
185 
189  EntityList GetEntitiesByType(const EntityType& byType);
190 
197  EntityList GetEntitiesAt(const tbMath::Vector2& point, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
198 
202  EntityList GetEntitiesWithin(const tbMath::Vector2& center, const float radius, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
203 
207  EntityList GetEntitiesWithin(const tbMath::Vector2& center, const float width, const float height, const EntityType& byType = Entity::kInvalidType, bool onlyCollidableEntities = false);
208 
213  void Simulate(void);
214 
215  protected:
219  virtual void OnUpdate(const float deltaTime) override;
220 
221  private:
222  void ReallyAddEntity(Entity* entity, bool managed);
223  void SafeToRemoveEntities(void);
224 
225  typedef std::set<Entity*> EntitySet;
226  typedef std::map<EntityType, EntityList> EntityByTypeMap;
227 
228  EntityList mEntities;
229  EntityList mManagedEntities;
230  EntitySet mEntitiesToRemove;
231  EntityByTypeMap mEntitiesByType;
232  };
233 
234  }; /* namespace Game */
235 }; /* namespace TurtleBrains */
236 
237 namespace tbGame = TurtleBrains::Game;
238 
239 #endif /* _TurtleBrains_EntityManager_h_ */
Definition: tb_vector.h:47
Definition: tb_entity.h:46
void AddEntity(Entity *entity)
Definition: tb_graphic_list.h:27
Entity * CreateEntityByType(const EntityType &entityType)
std::list< EntityType > EntityTypeContainer
Definition: tb_entity.h:35
std::list< Entity * > EntityList
Definition: tb_entity_manager.h:129
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:123
void RemoveEntities(const EntityType &byType=Entity::kInvalidType)
tbCore::tbString EntityType
Definition: tb_entity.h:30
static const EntityType kInvalidType
Definition: tb_entity.h:53
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)
void EntityTypeChanged(Entity &entity, const EntityTypeContainer &oldTypes)
EntityList GetEntitiesAt(const tbMath::Vector2 &point, const EntityType &byType=Entity::kInvalidType, bool onlyCollidableEntities=false)
void RemoveEntity(Entity *entity)
This is the heart of TurtleBrains for game developers to create GameScenes and Entities to interact w...