cangine
Loading...
Searching...
No Matches
game_object.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <typeinfo>
4#include <cassert>
5
6#include "../components/component.hpp"
7#include "../../platform/mouse.hpp"
8
9namespace cangine {
10 class ObjectManager;
20 class GameObject {
21 private:
22 std::vector<IComponent*> components;
23 bool pendingDestroy = false;
24 bool active = true;
25 friend class ObjectManager;
26 public:
27 GameObject();
28 virtual ~GameObject();
33 virtual void onUpdate(double deltaTime);
38 virtual void onMouseEvent(const MouseEvent* e);
40 virtual void onDisable() {};
42 virtual void onEnable() {};
43
45 void destroy() {
46 this->pendingDestroy = true;
47 }
48
57 template <typename T>
58 void appendComponent(T* component) {
59 if (component) {
60 if (getComponent<T>() != nullptr && component->isUnique) {
61 assert(false && "Unique component already exist in gameobject");
62 return;
63 }
64
65 component->gameObject = this;
66 components.push_back(component);
67
68 component->firstFrame();
69 }
70 }
71
76 template <typename T>
78 for (auto it = components.begin(); it != components.end(); ++it) {
79 if (dynamic_cast<T*>(*it)) {
80 delete *it;
81 components.erase(it);
82 return;
83 }
84 }
85 }
86
92 template <typename T>
94 for (auto* component : components) {
95 T* target = dynamic_cast<T*>(component);
96 if (target != nullptr) {
97 return target;
98 }
99 }
100 return nullptr;
101 }
102
108 template <typename T>
109 std::vector<T*> getComponents() {
110 std::vector<T*> result;
111 for (auto* component : components) {
112 T* target = dynamic_cast<T*>(component);
113 if (target != nullptr) {
114 result.push_back(target);
115 }
116 }
117 return result;
118 }
119
127 template <typename T, typename... Args>
129 T* component = new T(std::forward<Args>(args)...);
130 this->appendComponent<T>(component);
131 return component;
132 }
133
138 void setActive(bool a);
143 bool isActive();
144 };
145}
Base class for everything that lives in the scene.
Definition game_object.hpp:20
virtual void onEnable()
Called when the object becomes active.
Definition game_object.hpp:42
virtual void onUpdate(double deltaTime)
Per-frame update hook.
Definition game_object.cpp:40
bool active
Whether the object is active (updated/drawn).
Definition game_object.hpp:24
bool isActive()
Reports whether the object is active.
Definition game_object.cpp:30
virtual ~GameObject()
Definition game_object.cpp:34
std::vector< IComponent * > components
Components attached to this object.
Definition game_object.hpp:22
T * getComponent()
Returns the first attached component of a type.
Definition game_object.hpp:93
void setActive(bool a)
Activates or deactivates the object.
Definition game_object.cpp:24
bool pendingDestroy
Whether the object is queued for destruction.
Definition game_object.hpp:23
void appendComponent(T *component)
Attaches an existing component to this object.
Definition game_object.hpp:58
void destroyComponent()
Destroys and removes the first component of a type.
Definition game_object.hpp:77
GameObject()
Definition game_object.cpp:8
std::vector< T * > getComponents()
Returns all attached components of a type.
Definition game_object.hpp:109
virtual void onMouseEvent(const MouseEvent *e)
Mouse event hook.
Definition game_object.cpp:18
virtual void onDisable()
Called when the object becomes inactive.
Definition game_object.hpp:40
T * newComponent(Args &&... args)
Constructs a component in place and attaches it.
Definition game_object.hpp:128
void destroy()
Marks the object for destruction by the ObjectManager.
Definition game_object.hpp:45
Singleton registry that owns and updates all game objects.
Definition object_manager.hpp:17
Definition audiomanager.cpp:4
A mouse event carrying its type, button, position and scroll offset.
Definition mouse.hpp:37