Brunot
Loading...
Searching...
No Matches
GameObject.h
Go to the documentation of this file.
1
9// ____ __ __ __
10// /\__ _\/\ \ /\ \/\ \
11// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
12// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
13// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
14// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
15// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
16
17#pragma once
18
19
20#include <string>
21#include <memory>
22
23#include "System/Json.h"
24
25
26namespace sol
27{
28class state;
29}
30
31class Engine;
32class Node;
33template <class>
35
36namespace sys
37{
38class SceneManager;
39}
40
41class Entity;
45namespace gobj
46{
47struct PathNode;
48
49enum class Type
50{
51 null_obj_type = 0, // reserved for uninitialized or invalid objects
52 no_parent_type = 100, // reserved for Engine
53 Engine = 200,
54 Root = 300, // object held by root node. see Root.h
55 Node = 400,
57 System = 600,
58 Entity = 700,
59 Component = 800,
60 Action = 900,
61 Script = 1000,
62};
63
64}
65
66namespace gobj
67{
68using UUID = unsigned int; // replace with boost later
69}
70
71using RenderLayer = unsigned int; // replace with a real enum
72using Stream = sys::Json; // replace with whatever stream we are using
73
74
75static unsigned int ObjectCount = 0;
76
83{
84public:
86 class Key;
87
89 friend gobj::PathNode;
90 friend Entity;
91 friend sys::SceneManager;
92 friend Node;
93
94 template <typename T>
95 friend class ChildrenHandeler;
96
97 friend Engine;
98
99#pragma region big_6_functions
100
111 virtual ~GameObject(); //possibly declare noexcept
112
117 GameObject(const GameObject& other);
123 auto operator=(const GameObject& other) -> GameObject&;
128 GameObject(GameObject&& other) noexcept;
134 auto operator=(GameObject&& other) noexcept -> GameObject&;
135
136#pragma endregion
137
138#pragma region abstract functions
139
145 virtual auto clone() const -> std::unique_ptr<GameObject> = 0;
146
147
155 virtual auto update(float dt) -> void;
156
157private:
165 virtual auto onUpdate(float dt) -> void = 0;
166
167public:
173 virtual auto endWindow() -> void = 0; // for debug ImGUI
174
175
184 virtual auto render() -> void;
185
186private:
193 virtual auto onRender() -> void = 0;
194
195public:
200 virtual auto load(const Stream& stream) -> void = 0;
201
206 virtual auto save(Stream& stream) const -> void = 0;
207
211 virtual auto destroy() -> void;
212
220 virtual auto showMenu() -> void
221 {
222 }
223
229 virtual auto onEnterEngine() -> void
230 {
231 }
232
238 virtual auto onSceneStart(const std::string& sceneName) -> void
239 {
240 }
241
247 virtual auto onSceneExit(const std::string& sceneName) -> void
248 {
249 }
250
256 virtual auto for_each(std::function<void(GameObject&)> func) -> void;
257
263 virtual auto for_each(std::function<void(const GameObject&)> func) const -> void;
264
265#pragma endregion
266
267
268#pragma region parent_functions
278 virtual auto getParent() const -> GameObject*;
279
287 [[nodiscard("Parenting may fail")]] virtual auto parentTo(GameObject* newParent) -> bool;
288
298 [[nodiscard]] auto getInternalParent() const -> GameObject*;
299
300
301#pragma endregion
302
303#pragma region child_functions
304
314 [[nodiscard("adding child may fail")]]
315 virtual auto addChild(std::unique_ptr<GameObject> newChild) -> bool;
316
317public:
319 auto addChildLua(std::unique_ptr<GameObject>& newChild) -> bool;
320public:
321
322#pragma endregion
323
329 [[nodiscard]] virtual auto getKey() const -> Key;
330
331 [[nodiscard]] auto destroyed() const -> bool;
332 [[nodiscard]] auto getUUID() const -> gobj::UUID;
333 auto coolerGetUUID() const -> unsigned int;
334 [[nodiscard]] auto isUUID(gobj::UUID compareUUID) const -> bool;
339 [[nodiscard]] auto getName() const -> const std::string&;
344 [[nodiscard]] auto getNickname() const -> const std::string&;
350 [[nodiscard]] auto getFormattedName() const -> const std::string&;
351
358 [[nodiscard]] auto nameEquals(const std::string& compareString) const -> bool;
359
366 [[nodiscard]] auto isType(gobj::Type otherType) const -> bool;
371 [[nodiscard]] auto getObjectType() const -> gobj::Type;
372
378 auto setNickname(const std::string& newNickname) -> void;
379
383 [[nodiscard]] auto updates() const -> bool;
384
388 [[nodiscard]] auto renders() const -> bool;
389
393 [[nodiscard]] auto receivesMessages() const -> bool;
394
398 [[nodiscard]] auto isActive() const -> bool;
399
404 virtual auto setShouldUpdate(bool _shouldUpdate) -> void;
405
410 virtual auto setShouldRender(bool _shouldRender) -> void;
411
416 virtual auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void;
417
422 auto setActive(bool shouldBeActive) -> void;
423
429 auto getGameObjectJson() const -> json;
430
436 auto setGameObjectJson(const json& stream) -> void;
437
442 class Key
443 {
444 public:
445 Key(gobj::UUID uuid, gobj::Type type, unsigned short subtype);
446 auto getUUID() const -> gobj::UUID;
447 auto getType() const -> gobj::Type;
448 auto getSubtype() const -> unsigned short;
458 friend auto operator<(const Key& lhs, const Key& rhs) -> bool;
459
460 private:
465
470
475 unsigned short subtype;
476 };
477
478
479protected:
490 auto setInternalParent(GameObject* parent) -> bool;
491
492
493
498 virtual auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>>;
499
504 auto checkAddToSceneHook() -> void;
505
506private:
510 const gobj::UUID uuid;
524 gobj::Type type;
534
538 bool shouldUpdate = true;
539
543 bool shouldRender = true;
544
549
554 bool inEngine = false;
555
562 std::string typeName;
567 std::string nickname;
568
572 mutable std::string formattedName;
573
574
578 auto calculateFormattedName() const -> void;
579};
580
581namespace gobj
582{
590auto operator<(const GameObject& lhs, const GameObject& rhs) -> bool;
591}
sys::Json Stream
Definition AudioObject.h:20
auto operator<(const GameObject::Key &lhs, const GameObject::Key &rhs) -> bool
Definition GameObject.cpp:455
unsigned int RenderLayer
Definition GameObject.h:71
static unsigned int ObjectCount
Definition GameObject.h:75
nlohmann::json json
Definition Json.cpp:19
Definition Action.h:131
A container for child objects.
Definition ChildrenHandeler.h:42
Definition Component.h:32
Definition Engine.h:25
Definition Entity.h:41
a Key class to be used as a key when holding GameObjects (or unique pointers to game objects) in <key...
Definition GameObject.h:443
auto getType() const -> gobj::Type
Definition GameObject.cpp:340
gobj::UUID uuid
the UUID of the GameObject the Key was generated from
Definition GameObject.h:464
auto getSubtype() const -> unsigned short
Definition GameObject.cpp:345
gobj::Type type
the type of the GameObject the Key was generated from
Definition GameObject.h:469
Key(gobj::UUID uuid, gobj::Type type, unsigned short subtype)
Definition GameObject.cpp:328
unsigned short subtype
Definition GameObject.h:475
the base class for the engine, most things inherit from this.
Definition GameObject.h:83
std::string formattedName
the combined name of the GameObject.
Definition GameObject.h:572
virtual auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition GameObject.cpp:313
virtual auto onSceneStart(const std::string &sceneName) -> void
Hook that is called before the first frame a scene is run, but after the entire scene is loaded into ...
Definition GameObject.h:238
auto getUUID() const -> gobj::UUID
Definition GameObject.cpp:214
virtual auto onUpdate(float dt) -> void=0
called once every frame.
auto receivesMessages() const -> bool
Definition GameObject.cpp:291
auto getObjectType() const -> gobj::Type
gets the internal type of the GameObject, as an enum
Definition GameObject.cpp:276
auto coolerGetUUID() const -> unsigned int
Definition GameObject.cpp:219
virtual auto onRender() -> void=0
called every frame after update has been called for every object.
virtual auto onSceneExit(const std::string &sceneName) -> void
Hook that is called right before a scene is unloaded To receive the hook, simply override the functio...
Definition GameObject.h:247
virtual auto getParent() const -> GameObject *
Gets the parent of a GameObject.
Definition GameObject.cpp:174
gobj::Type parentType
Definition GameObject.h:520
auto isType(gobj::Type otherType) const -> bool
check whether the GameObject is the same as a given type.
Definition GameObject.cpp:271
auto setGameObjectJson(const json &stream) -> void
used for deserializing GameObjects
Definition GameObject.cpp:371
virtual auto update(float dt) -> void
called once every frame.
Definition GameObject.cpp:126
bool isDestroyed
whether the GameObject has been destroyed, and if it should be deleted at the end of the frame.
Definition GameObject.h:514
virtual auto onEnterEngine() -> void
hook that is called when a GameObject enters the Engine tree.
Definition GameObject.h:229
auto getInternalParent() const -> GameObject *
returns the actual owning parent of the GameObject
Definition GameObject.cpp:350
friend Entity
Definition GameObject.h:90
auto getGameObjectJson() const -> json
Used for serializing GameObjects.
Definition GameObject.cpp:355
auto updates() const -> bool
Definition GameObject.cpp:281
virtual auto load(const Stream &stream) -> void=0
Implementations will load the state of a GameObject from a th::Json Object.
std::string typeName
the typeName of a GameObject.
Definition GameObject.h:562
GameObject * internalParent
The GameObject that owns and manages this GameObject.
Definition GameObject.h:529
auto getNickname() const -> const std::string &
Gets the nickname (custom, writable name) of a GameObject.
Definition GameObject.cpp:241
auto getFormattedName() const -> const std::string &
combines a GameObjects Type, Nickname, and UUID into one string, for use with logging.
Definition GameObject.cpp:251
friend class ChildrenHandeler
Definition GameObject.h:95
bool shouldRender
whether the GameObject and it's children render
Definition GameObject.h:543
auto getName() const -> const std::string &
Gets the type of the object as a string.
Definition GameObject.cpp:236
friend Node
Definition GameObject.h:92
auto setActive(bool shouldBeActive) -> void
set the GameObject should update, render, and receive messages
Definition GameObject.cpp:321
const gobj::UUID uuid
a unique ID for each GameObject
Definition GameObject.h:510
auto setNickname(const std::string &newNickname) -> void
sets the GameObjects nickname.
Definition GameObject.cpp:260
auto checkAddToSceneHook() -> void
Determine if the GameObject was recently added to the engine, and if it was, call onEngineEnter.
Definition GameObject.cpp:396
virtual auto showMenu() -> void
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition GameObject.h:220
auto calculateFormattedName() const -> void
helper function for updating the formatted name, called when we set a nickname, or lazily when we req...
Definition GameObject.cpp:246
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24
gobj::Type type
What type this GameObject is, for type checking, and safe casts.
Definition GameObject.h:524
auto isActive() const -> bool
Definition GameObject.cpp:296
auto isUUID(gobj::UUID compareUUID) const -> bool
Definition GameObject.cpp:225
virtual auto destroy() -> void
Marks an GameObject to be destroyed.
Definition GameObject.cpp:158
friend Engine
Definition GameObject.h:97
virtual auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > >
gets a vector of a GameObjects public (i.e.
Definition GameObject.cpp:391
virtual auto clone() const -> std::unique_ptr< GameObject >=0
makes a copy a GameObject even if it's held polymorphically
virtual auto parentTo(GameObject *newParent) -> bool
Sets the GameObject as a child of another GameObject.
Definition GameObject.cpp:185
virtual auto render() -> void
called every frame after update has been called for every object.
Definition GameObject.cpp:140
auto nameEquals(const std::string &compareString) const -> bool
check whether a GameObjects typeName is equivilent to a given string.
Definition GameObject.cpp:266
RenderLayer renderLayer
Currently unused, the z layer an GameObject will be drawn on.
Definition GameObject.h:533
auto operator=(const GameObject &other) -> GameObject &
copy assignment operator
Definition GameObject.cpp:57
virtual auto setShouldUpdate(bool _shouldUpdate) -> void
set whether the GameObject should update every frame, and whether all of it's children should update ...
Definition GameObject.cpp:301
bool shouldReceiveMessages
whether the GameObject and it's children receive messages
Definition GameObject.h:548
std::string nickname
the nickname of a GameObject is used to identify in the editor.
Definition GameObject.h:567
auto setInternalParent(GameObject *parent) -> bool
sets the internal parent of a GameObject directly, avoiding abstractions provided by ParentTo().
Definition GameObject.cpp:412
virtual ~GameObject()
destructor for GameObject.
Definition GameObject.cpp:35
virtual auto for_each(std::function< void(GameObject &)> func) -> void
applies a function to every child.
Definition GameObject.cpp:164
auto renders() const -> bool
Definition GameObject.cpp:286
auto addChildLua(std::unique_ptr< GameObject > &newChild) -> bool
for lua
Definition GameObject.cpp:202
bool inEngine
whether the GameObject is current in the Engine, underneath SceneManager Used for making sure the Gam...
Definition GameObject.h:554
virtual auto endWindow() -> void=0
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
virtual auto save(Stream &stream) const -> void=0
Implementations will load the state of a GameObject to a th::Json object.
auto destroyed() const -> bool
Definition GameObject.cpp:208
virtual auto setShouldRender(bool _shouldRender) -> void
set whether the GameObject should render every frame, and whether all of it's children should render ...
Definition GameObject.cpp:307
virtual auto addChild(std::unique_ptr< GameObject > newChild) -> bool
Adds a child to a GameObject.
Definition GameObject.cpp:191
virtual auto getKey() const -> Key
Generates a unique Key that corresponds to the GameObject.
Definition GameObject.cpp:230
bool shouldUpdate
whether the GameObject and it's children updates
Definition GameObject.h:538
Manages the heirarchical structure between Systems and Entities.
Definition Node.h:41
Definition Root.h:22
Definition System.h:17
Definition Json.h:32
Definition SceneManager.h:58
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:440
Type
Definition GameObject.h:50
@ no_parent_type
Definition GameObject.h:52
@ null_obj_type
Definition GameObject.h:51
@ ChildrenHandler
Definition GameObject.h:56
unsigned int UUID
Definition GameObject.h:68
Definition GameObject.h:27
the type of elements in a basic_json container
Definition GameObject.h:37
Definition Script.h:32
internal type for computing Paths
Definition PathNode.h:37