This repository was archived by the owner on Jan 18, 2024. It is now read-only.
forked from rpbpolis/spic-prj-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameObject.hpp
307 lines (265 loc) · 10.5 KB
/
GameObject.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#ifndef GAMEOBJECT_H_
#define GAMEOBJECT_H_
#include "Component.hpp"
#include "Transform.hpp"
#include <string>
#include <vector>
#include <memory>
#if __has_include("GameObject_includes.hpp")
#include "GameObject_includes.hpp"
#endif
namespace spic {
/**
* @brief Any object which should be represented on screen.
* @spicapi
*/
class GameObject {
public:
virtual ~GameObject() = default;
/**
* @brief Finds a GameObject by name and returns it.
* @param name The name of the GameObject you want to find.
* @return Pointer to GameObject, or nullptr if not found.
* @spicapi
*/
static std::shared_ptr<GameObject> Find(const std::string& name);
/**
* @brief Returns a vector of active GameObjects tagged tag. Returns empty
* vector if no GameObject was found.
* @param tag The tag to find.
* @return std::vector of GameObject pointers. No ownership.
* @spicapi
*/
static std::vector<std::shared_ptr<GameObject>> FindGameObjectsWithTag(const std::string& tag);
/**
* @brief Returns one active GameObject tagged tag. Returns nullptr if no GameObject was found.
* @param tag The tag to find.
* @return Pointer to GameObject, or nullptr if not found.
* @spicapi
*/
static std::shared_ptr<GameObject> FindWithTag(const std::string& tag);
/**
* @brief Returns the first active loaded object of Type type.
* @spicapi
*/
template<class T>
static std::shared_ptr<T> FindObjectOfType(bool includeInactive = false);
/**
* @brief Gets a list of all loaded objects of Type type.
* @spicapi
*/
template<class T>
static std::vector<std::shared_ptr<T>> FindObjectsOfType(bool includeInactive = false);
/**
* @brief Removes a GameObject from the administration.
* @details TODO What happens if this GameObject is a parent to others? What happens
* to the Components it possesses?
* @param obj The GameObject to be destroyed. Must be a valid pointer to existing Game Object.
* @exception A std::runtime_exception is thrown when the pointer is not valid.
* @spicapi
*/
static void Destroy(std::shared_ptr<GameObject> obj);
/**
* @brief Removes a Component.
* @details Will search for the Component among the GameObjects.
* @param obj The Component to be removed.
* @spicapi
*/
static void Destroy(Component* obj);
/**
* @brief Constructor.
* @details The new GameObject will also be added to a statically
* available collection, the administration. This makes the
* Find()-functions possible.
* @param name The name for the game object.
* @param tag The tag for the game object.
* @param layer The layer for the game object.
* @spicapi
*/
GameObject(const std::string& name, const std::string& tag, int layer);
/**
* @brief Does the object exist?
* @spicapi
*/
operator bool() const;
/**
* @brief Compare two GameObjects.
* @param other The other object to compare this one with.
* @return true if not equal, false otherwise.
* @spicapi
*/
bool operator!=(const GameObject& other) const;
/**
* @brief Compare two GameObjects
* @param other The other object to compare this one with.
* @return true if equal, false otherwise.
* @spicapi
*/
bool operator==(const GameObject& other) const;
/**
* @brief Add a Component of the specified type. Must be a valid
* subclass of Component. The GameObject assumes ownership of
* the Component.
* @details This function places a pointer to the component in
* a suitable container.
* @param component Reference to the component.
* @spicapi
*/
template<class T>
void AddComponent(std::shared_ptr<T> component);
/**
* @brief Removes a component from a game object.
* @param component Reference to the component.
* @sharedapi
*/
void RemoveComponent(std::shared_ptr<Component> component);
/**
* @brief Get the first component of the specified type. Must be
* a valid subclass of Component.
* @return Pointer to Component instance.
* @spicapi
*/
template<class T>
std::shared_ptr<T> GetComponent() const;
/**
* @brief Get the first component of the specified type from
* contained game objects. Must be
* a valid subclass of Component.
* @return Pointer to Component instance.
* @spicapi
*/
template<class T>
std::shared_ptr<T> GetComponentInChildren() const;
/**
* @brief Get the first component of the specified type from
* the parent game object. Must be
* a valid subclass of Component.
* @return Pointer to Component instance.
* @spicapi
*/
template<class T>
std::shared_ptr<T> GetComponentInParent() const;
/**
* @brief Get all components of the specified type. Must be
* a valid subclass of Component.
* @return Vector with pointers to Component instances.
* @spicapi
*/
template<class T>
std::vector<std::shared_ptr<T>> GetComponents() const;
/**
* @brief Get all components of the specified type from
* contained game objects. Must be
* a valid subclass of Component.
* @return Vector with pointers to Component instances.
* @spicapi
*/
template<class T>
std::vector<std::shared_ptr<T>> GetComponentsInChildren() const;
/**
* @brief Get all components op the specified type from
* the parent game object. Must be
* a valid subclass of Component.
* @return Vector with pointers to Component instances.
* @spicapi
*/
template<class T>
std::vector<std::shared_ptr<T>> GetComponentsInParent() const;
/**
* @brief Activates/Deactivates the GameObject, depending on the given true or false value.
* @param active Desired value.
* @spicapi
*/
void Active(bool flag);
/**
* @brief Returns whether this game object is itself active.
* @return true if active, false if not.
* @spicapi
*/
bool Active() const;
/**
* @brief Returns whether this game component is active, taking its parents
* into consideration as well.
* @return true if game object and all of its parents are active,
* false otherwise.
* @spicapi
*/
bool IsActiveInWorld() const;
/**
* @brief Returns the transform of this GameObject
* @return A reference to the transform
* @sharedapi
*/
spic::Transform& Transform();
/**
* @brief Returns a const reference to the transform of this GameObject
* @return A const reference to the transform
* @sharedapi
*/
const spic::Transform& Transform() const;
/**
* The parent of this GameObject.
* @return A weak pointer to the parent.
* @sharedapi
*/
std::weak_ptr<GameObject> Parent() const;
/**
* The parent of this GameObject.
* @param parent A weak pointer to the new parent
* @sharedapi
*/
void Parent(std::weak_ptr<GameObject> parent);
/**
* Returns a list of children in this GameObject.
* @return A list of shared pointers to the children.
* @sharedapi
*/
const std::vector<std::shared_ptr<GameObject>>& Children() const;
/**
* Add a child to the children of this GameObject.
* @param child the child to add.
* @sharedapi
*/
void AddChild(std::shared_ptr<GameObject> child);
/**
* Remove a child from the children of this GameObject.
* @param child the child to remove.
* @sharedapi
*/
void RemoveChild(std::shared_ptr<GameObject> child);
/**
* Retrieve the name of this GameObject.
* @return the name of this GameObject.
* @sharedapi
*/
const std::string& Name() const;
/**
* Retrieve the tag of this GameObject.
* @return the tag of this GameObject.
* @sharedapi
*/
const std::string& Tag() const;
/**
* Retrieve the layer of this GameObject.
* @return the layer of this GameObject.
* @sharedapi
*/
int Layer() const;
// Include "package private" methods
#if __has_include("GameObject_public.hpp")
#include "GameObject_public.hpp"
#endif
private:
std::string name;
std::string tag;
bool active;
int layer;
#if __has_include("GameObject_private.hpp")
#include "GameObject_private.hpp"
#endif
};
}
#if __has_include("GameObject_templates.hpp")
#include "GameObject_templates.hpp"
#endif
#endif // GAMEOBJECT_H_