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"
25
26class Engine;
27class Node;
28template <class>
30
31namespace sys
32{
33class SceneManager;
34}
35
36class Entity;
40namespace gobj
41{
42struct PathNode;
43
44enum class Type
45{
46 null_obj_type = 0, // reserved for uninitialized or invalid objects
47 no_parent_type = 100, // reserved for Engine
48 Engine = 200,
49 Root = 300, // object held by root node. see Root.h
50 Node = 400,
52 System = 600,
53 Entity = 700,
54 Component = 800,
55 Action = 900,
56};
57
58}
59
60namespace gobj
61{
62using UUID = unsigned int; // replace with boost later
63}
64
65using RenderLayer = unsigned int; // replace with a real enum
66using Stream = sys::Json; // replace with whatever stream we are using
67
68
69static unsigned int ObjectCount = 0;
70
77{
78public:
80 class Key;
81
83 friend gobj::PathNode;
84 friend Entity;
85 friend sys::SceneManager;
86 friend Node;
88 friend Engine;
89
90#pragma region big_6_functions
91
102 virtual ~GameObject(); //possibly declare noexcept
103
108 GameObject(const GameObject& other);
114 auto operator=(const GameObject& other) -> GameObject&;
119 GameObject(GameObject&& other) noexcept;
125 auto operator=(GameObject&& other) noexcept -> GameObject&;
126
127#pragma endregion
128
129#pragma region abstract functions
130
136 virtual auto clone() const -> std::unique_ptr<GameObject> = 0;
137
138
146 virtual auto update(float dt) -> void;
147
148private:
156 virtual auto onUpdate(float dt) -> void = 0;
157
158public:
164 virtual auto endWindow() -> void = 0; // for debug ImGUI
165
166
175 virtual auto render() -> void;
176
177private:
184 virtual auto onRender() -> void = 0;
185
186public:
191 virtual auto load(Stream& stream) -> void = 0;
192
197 virtual auto save(Stream& stream) const -> void = 0;
198
202 virtual auto destroy() -> void;
203
211 virtual auto showMenu() -> void
212 {
213 }
214
219 virtual auto onEnterEngine() -> void
220 {
221 }
222
223
229 virtual auto for_each(std::function<void(GameObject&)> func) -> void;
230
236 virtual auto for_each(std::function<void(const GameObject&)> func) const -> void;
237
238#pragma endregion
239
240
241#pragma region parent_functions
251 virtual auto getParent() const -> GameObject*;
252
260 [[nodiscard("Parenting may fail")]] virtual auto parentTo(GameObject* newParent) -> bool;
261
271 [[nodiscard]] auto getInternalParent() const -> GameObject*;
272
273
274#pragma endregion
275
276#pragma region child_functions
277
287 [[nodiscard("adding child may fail")]]
288 virtual auto addChild(std::unique_ptr<GameObject> newChild) -> bool;
289
290
291#pragma endregion
292
298 [[nodiscard]] virtual auto getKey() const -> Key;
299
300 [[nodiscard]] auto destroyed() const -> bool;
301 [[nodiscard]] auto getUUID() const -> gobj::UUID;
302 [[nodiscard]] auto isUUID(gobj::UUID compareUUID) const -> bool;
307 [[nodiscard]] auto getName() const -> const std::string&;
312 [[nodiscard]] auto getNickname() const -> const std::string&;
313 void calculateFormattedName() const;
319 [[nodiscard]] auto getFormattedName() const -> const std::string&;
320
327 [[nodiscard]] auto nameEquals(const std::string& compareString) const -> bool;
328
335 [[nodiscard]] auto isType(gobj::Type otherType) const -> bool;
340 [[nodiscard]] auto getObjectType() const -> gobj::Type;
341
347 auto setNickname(const std::string& newNickname) -> void;
348
352 [[nodiscard]] auto updates() const -> bool;
353
357 [[nodiscard]] auto renders() const -> bool;
358
362 [[nodiscard]] auto receivesMessages() const -> bool;
363
367 [[nodiscard]] auto isActive() const -> bool;
368
373 virtual auto setShouldUpdate(const bool _shouldUpdate) -> void;
374
379 virtual auto setShouldRender(const bool _shouldRender) -> void;
380
385 virtual auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void;
386
391 auto setActive(const bool shouldBeActive) -> void;
392
397 class Key
398 {
399 public:
400 Key(gobj::UUID uuid, gobj::Type type, unsigned short subtype);
401 auto getUUID() const -> gobj::UUID;
402 auto getType() const -> gobj::Type;
403 auto getSubtype() const -> unsigned short;
413 friend auto operator<(const Key& lhs, const Key& rhs) -> bool;
414
415 private:
420
425
430 unsigned short subtype;
431 };
432
433protected:
444 auto setInternalParent(GameObject* parent) -> bool;
445
451 auto getGameObjectJson() const -> json;
452
458 auto setGameObjectJson(const json& stream) -> void;
459
464 virtual auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>>;
465
470 auto checkAddToSceneHook() -> void;
471
472private:
476 const gobj::UUID uuid;
490 gobj::Type type;
500
504 bool shouldUpdate = true;
505
509 bool shouldRender = true;
510
515
519 bool inEngine = false;
520
527 std::string typeName;
532 std::string nickname;
533
537 mutable std::string formattedName;
538
539};
540
541namespace gobj
542{
550auto operator<(const GameObject& lhs, const GameObject& rhs) -> bool;
551}
sys::Json Stream
Definition AudioObject.h:20
auto operator<(const GameObject::Key &lhs, const GameObject::Key &rhs) -> bool
Definition GameObject.cpp:437
unsigned int RenderLayer
Definition GameObject.h:65
static unsigned int ObjectCount
Definition GameObject.h:69
nlohmann::json json
Definition Json.cpp:19
Definition Action.h:35
A container for child objects.
Definition ChildrenHandeler.h:42
Definition Component.h:28
Definition Engine.h:25
Definition Entity.h:33
a Key class to be used as a key when holding GameObjects (or unique pointers to game objects) in <key...
Definition GameObject.h:398
auto getType() const -> gobj::Type
Definition GameObject.cpp:329
gobj::UUID uuid
the UUID of the GameObject the Key was generated from
Definition GameObject.h:419
auto getSubtype() const -> unsigned short
Definition GameObject.cpp:334
gobj::Type type
the type of the GameObject the Key was generated from
Definition GameObject.h:424
Key(gobj::UUID uuid, gobj::Type type, unsigned short subtype)
Definition GameObject.cpp:317
unsigned short subtype
Definition GameObject.h:430
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
std::string formattedName
the combined name of the GameObject.
Definition GameObject.h:537
auto getUUID() const -> gobj::UUID
Definition GameObject.cpp:208
virtual auto onUpdate(float dt) -> void=0
called once every frame.
auto receivesMessages() const -> bool
Definition GameObject.cpp:280
auto getObjectType() const -> gobj::Type
gets the internal type of the GameObject, as an enum
Definition GameObject.cpp:265
virtual auto onRender() -> void=0
called every frame after update has been called for every object.
virtual auto getParent() const -> GameObject *
Gets the parent of a GameObject.
Definition GameObject.cpp:173
gobj::Type parentType
Definition GameObject.h:486
auto isType(gobj::Type otherType) const -> bool
check whether the GameObject is the same as a given type.
Definition GameObject.cpp:260
auto setGameObjectJson(const json &stream) -> void
used for deserializing GameObjects
Definition GameObject.cpp:360
virtual auto update(float dt) -> void
called once every frame.
Definition GameObject.cpp:125
bool isDestroyed
whether the GameObject has been destroyed, and if it should be deleted at the end of the frame.
Definition GameObject.h:480
virtual auto onEnterEngine() -> void
hook that is called when a GameObject enters the Engine tree.
Definition GameObject.h:219
auto getInternalParent() const -> GameObject *
returns the actual owning parent of the GameObject
Definition GameObject.cpp:339
friend Entity
Definition GameObject.h:84
auto getGameObjectJson() const -> json
Used for serializing GameObjects.
Definition GameObject.cpp:344
auto updates() const -> bool
Definition GameObject.cpp:270
std::string typeName
the typeName of a GameObject.
Definition GameObject.h:527
GameObject * internalParent
The GameObject that owns and manages this GameObject.
Definition GameObject.h:495
auto getNickname() const -> const std::string &
Gets the nickname (custom, writable name) of a GameObject.
Definition GameObject.cpp:230
virtual auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition GameObject.cpp:302
friend ChildrenHandeler
Definition GameObject.h:87
auto getFormattedName() const -> const std::string &
combines a GameObjects Type, Nickname, and UUID into one string, for use with logging.
Definition GameObject.cpp:240
bool shouldRender
whether the GameObject and it's children render
Definition GameObject.h:509
auto getName() const -> const std::string &
Gets the type of the object as a string.
Definition GameObject.cpp:225
friend Node
Definition GameObject.h:86
const gobj::UUID uuid
a unique ID for each GameObject
Definition GameObject.h:476
auto setNickname(const std::string &newNickname) -> void
sets the GameObjects nickname.
Definition GameObject.cpp:249
auto checkAddToSceneHook() -> void
Determine if the GameObject was recently added to the engine, and if it was, call onEngineEnter.
Definition GameObject.cpp:382
virtual auto showMenu() -> void
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition GameObject.h:211
virtual auto setShouldUpdate(const bool _shouldUpdate) -> void
set whether the GameObject should update every frame, and whether all of it's children should update ...
Definition GameObject.cpp:290
auto setActive(const bool shouldBeActive) -> void
set the GameObject should update, render, and receive messages
Definition GameObject.cpp:310
void calculateFormattedName() const
Definition GameObject.cpp:235
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:23
gobj::Type type
What type this GameObject is, for type checking, and safe casts.
Definition GameObject.h:490
auto isActive() const -> bool
Definition GameObject.cpp:285
auto isUUID(gobj::UUID compareUUID) const -> bool
Definition GameObject.cpp:214
virtual auto destroy() -> void
Marks an GameObject to be destroyed.
Definition GameObject.cpp:157
friend Engine
Definition GameObject.h:88
virtual auto setShouldRender(const bool _shouldRender) -> void
set whether the GameObject should render every frame, and whether all of it's children should render ...
Definition GameObject.cpp:296
virtual auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > >
gets a vector of a GameObjects public (i.e.
Definition GameObject.cpp:377
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:184
virtual auto render() -> void
called every frame after update has been called for every object.
Definition GameObject.cpp:139
auto nameEquals(const std::string &compareString) const -> bool
check whether a GameObjects typeName is equivilent to a given string.
Definition GameObject.cpp:255
RenderLayer renderLayer
Currently unused, the z layer an GameObject will be drawn on.
Definition GameObject.h:499
auto operator=(const GameObject &other) -> GameObject &
copy assignment operator
Definition GameObject.cpp:56
bool shouldReceiveMessages
whether the GameObject and it's children receive messages
Definition GameObject.h:514
std::string nickname
the nickname of a GameObject is used to identify in the editor.
Definition GameObject.h:532
auto setInternalParent(GameObject *parent) -> bool
sets the internal parent of a GameObject directly, avoiding abstractions provided by ParentTo().
Definition GameObject.cpp:397
virtual ~GameObject()
destructor for GameObject.
Definition GameObject.cpp:34
virtual auto for_each(std::function< void(GameObject &)> func) -> void
applies a function to every child.
Definition GameObject.cpp:163
auto renders() const -> bool
Definition GameObject.cpp:275
bool inEngine
whether the GameObject is current in the Engine, underneath SceneManager
Definition GameObject.h:519
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:202
virtual auto addChild(std::unique_ptr< GameObject > newChild) -> bool
Adds a child to a GameObject.
Definition GameObject.cpp:190
virtual auto getKey() const -> Key
Generates a unique Key that corresponds to the GameObject.
Definition GameObject.cpp:219
bool shouldUpdate
whether the GameObject and it's children updates
Definition GameObject.h:504
virtual auto load(Stream &stream) -> void=0
Implementations will load the state of a GameObject from a th::Json Object.
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:57
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:229
Type
Definition GameObject.h:45
@ no_parent_type
Definition GameObject.h:47
@ null_obj_type
Definition GameObject.h:46
@ ChildrenHandler
Definition GameObject.h:51
unsigned int UUID
Definition GameObject.h:62
the type of elements in a basic_json container
Definition GameObject.h:32
internal type for computing Paths
Definition PathNode.h:37