Hummingbird Framework
 All Classes Functions Variables Modules Pages
GameObject.h
1 #ifndef HB_GAME_OBJECT_H
2 #define HB_GAME_OBJECT_H
3 #include <algorithm>
4 #include <initializer_list>
5 #include <map>
6 #include <string>
7 #include <typeindex>
8 #include <unordered_map>
9 #include <vector>
10 #include "DataRepository.h"
11 #include "Log.h"
12 #include "MessageManager.h"
13 #include "Transform.h"
14 
15 namespace hb
16 {
20  typedef MessageManager<DataRepository> GameObjectMessageManager;
50  class GameObject : public Transform
51  {
52  public:
57  class Component
58  {
59  public:
60  friend class GameObject;
67  m_active(true),
68  m_game_object(nullptr)
69  {}
73  virtual ~Component(){}
79  virtual void init(){}
83  virtual void preUpdate(){}
87  virtual void update(){}
91  virtual void postUpdate(){}
97  GameObject* getGameObject() const {return m_game_object;}
102  void setActive(bool active)
103  {m_active = active;}
108  bool isActive() const
109  {return m_active;}
110 
111  protected:
112  void addListenerToGameObject(const std::string& name, std::function<void(DataRepository&)>&& listener)
113  {
114  hb_assert(m_game_object != nullptr, "Accessing Component's GameObject before being assigned.");
115  getGameObject()->m_message_manager.listen(name, std::move(listener));
116  }
117  void ignoreToGameObject(const GameObjectMessageManager::ListenerId<DataRepository>& listener_id)
118  {
119  hb_assert(m_game_object != nullptr, "Accessing Component's GameObject before being assigned.");
120  getGameObject()->m_message_manager.ignore(listener_id);
121  }
122 
123  private:
124  void setGameObject(GameObject* game_object)
125  {m_game_object = game_object;}
126 
127  bool m_active;
128  GameObject* m_game_object;
129  };
130 
136  static GameObject* getGameObjectById(int id);
142  static const std::vector<GameObject*>& getGameObjectsByName(const std::string& name);
146  static void destroyAll();
150  static void updateAll();
151 
155  GameObject();
160  GameObject(const std::initializer_list<Component*>& components);
166  ~GameObject();
171  int getId() const;
176  const std::string& getName() const;
180  void setName(const std::string& name);
185  void setActive(bool active);
190  bool isActive() const;
194  void preUpdate();
198  void update();
202  void postUpdate();
208  void destroy();
212  void addComponent(Component* component);
216  void addComponents(const std::vector<Component*>& components);
222  template <typename ComponentType>
223  ComponentType* getComponent() const
224  {
225  for (Component* component : m_components)
226  {
227  if (dynamic_cast<ComponentType*>(component))
228  return dynamic_cast<ComponentType*>(component);
229  }
230  return nullptr;
231  }
237  template <typename ComponentType>
238  std::vector<ComponentType*> getComponents() const
239  {
240  std::vector<ComponentType*> r;
241  for (Component* component : m_components)
242  {
243  if (dynamic_cast<ComponentType*>(component))
244  r.push_back(dynamic_cast<ComponentType*>(component));
245  }
246  return r;
247  }
251  void sendMessage(const std::string& name, DataRepository& data);
265  static void setNextGameObjectId(int id);
269  GameObject(int id);
271 
272  private:
273  static int s_game_object_identifier;
274  static std::unordered_map<int, GameObject*> s_game_objects_by_id;
275  static std::unordered_map<std::string, std::vector<GameObject*>> s_game_objects_by_name;
276  static DataRepository s_data_repository;
277 
278  bool m_active, m_marked_to_destroy;
279  int m_identifier;
280  std::string m_name;
281  std::vector<Component*> m_components;
282  GameObjectMessageManager m_message_manager;
283  };
284 }
285 #endif
int getId() const
Get the GameObject's id.
void setActive(bool active)
Set if Component is active.
Definition: GameObject.h:102
Object with a position, rotation and scale in a 3D space.
Definition: Transform.h:12
~GameObject()
Class destructor.
void update()
Runs all its Component's update().
std::vector< ComponentType * > getComponents() const
Get all Components of type ComponentType.
Definition: GameObject.h:238
void setActive(bool active)
Set if GameObject is active.
static const std::vector< GameObject * > & getGameObjectsByName(const std::string &name)
Get vector of GameObjects with the given name.
void postUpdate()
Runs all its Component's postUpdate().
void ignore(const ListenerId< Type > &id)
Disable the listener identified by id.
Definition: MessageManager.h:160
virtual void preUpdate()
Function called in the pre-update step.
Definition: GameObject.h:83
virtual void init()
Function called once after the Component has been added to a GameObject.
Definition: GameObject.h:79
static GameObject * getGameObjectById(int id)
Get GameObject instance given its id.
Definition: DataRepository.h:12
void setName(const std::string &name)
Set the GameObject's name.
void destroy()
Marks the GameObject to be destroyed.
Definition: Box2DPlugin.h:6
Base class for implementing custom Components.
Definition: GameObject.h:57
Component()
Default constructor.
Definition: GameObject.h:66
ComponentType * getComponent() const
Get first Component of type ComponentType.
Definition: GameObject.h:223
GameObject (also known as Actor).
Definition: GameObject.h:50
virtual void postUpdate()
Function called in the post-update step.
Definition: GameObject.h:91
virtual void update()
Function called in the update step.
Definition: GameObject.h:87
bool isActive() const
Get if GameObject is active.
bool isActive() const
Get if Component is active.
Definition: GameObject.h:108
GameObject()
Default constructor.
static void setNextGameObjectId(int id)
Set next GameObject id.
ListenerId< typename detail::traits< Listener >::type > listen(Listener &&listener)
Connects a listener to all messages of a type (no name filtering).
Definition: MessageManager.h:109
void addComponents(const std::vector< Component * > &components)
Add a group of Components to the GameObject.
static void updateAll()
Run the Update for all existing GameObjects.
void preUpdate()
Runs all its Component's preUpdate().
const std::string & getName() const
Get the GameObject's name.
static void destroyAll()
Destroy all existing GameObjects.
void sendMessage(const std::string &name, DataRepository &data)
Send message to this GameObject's Components.
GameObject * getGameObject() const
Get the GameObject instance composed by this Component.
Definition: GameObject.h:97
void addComponent(Component *component)
Add a Component to the GameObject.
virtual ~Component()
Default destructor.
Definition: GameObject.h:73