1 #ifndef HB_GAME_OBJECT_H
2 #define HB_GAME_OBJECT_H
4 #include <initializer_list>
8 #include <unordered_map>
10 #include "DataRepository.h"
12 #include "MessageManager.h"
13 #include "Transform.h"
20 typedef MessageManager<DataRepository> GameObjectMessageManager;
68 m_game_object(nullptr)
112 void addListenerToGameObject(
const std::string& name, std::function<
void(
DataRepository&)>&& listener)
114 hb_assert(m_game_object !=
nullptr,
"Accessing Component's GameObject before being assigned.");
117 void ignoreToGameObject(
const GameObjectMessageManager::ListenerId<DataRepository>& listener_id)
119 hb_assert(m_game_object !=
nullptr,
"Accessing Component's GameObject before being assigned.");
124 void setGameObject(GameObject* game_object)
125 {m_game_object = game_object;}
128 GameObject* m_game_object;
160 GameObject(
const std::initializer_list<Component*>& components);
176 const std::string&
getName()
const;
180 void setName(
const std::string& name);
216 void addComponents(
const std::vector<Component*>& components);
222 template <
typename ComponentType>
225 for (
Component* component : m_components)
227 if (dynamic_cast<ComponentType*>(component))
228 return dynamic_cast<ComponentType*
>(component);
237 template <
typename ComponentType>
240 std::vector<ComponentType*> r;
241 for (
Component* component : m_components)
243 if (dynamic_cast<ComponentType*>(component))
244 r.push_back(dynamic_cast<ComponentType*>(component));
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;
278 bool m_active, m_marked_to_destroy;
281 std::vector<Component*> m_components;
282 GameObjectMessageManager m_message_manager;
int getId() const
Get the GameObject's id.
void setActive(bool active)
Set if Component is active.
Definition: GameObject.h:102
~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