Brunot
Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1
12// ____ __ __ __
13// /\__ _\/\ \ /\ \/\ \
14// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
15// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
16// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
17// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
18// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
19
20
21#pragma once
22
23
24#include <vector>
25
26#include "Component.h"
27#include "ChildrenHandeler.h"
28#include "Framework/Engine.h"
29#include "System/Messaging.h"
30#include <memory>
31
32class Entity : public GameObject
33{
34public:
41#define HAS(type) GetComponent<type>(Component::c##type)
42
43 Entity();
44 Entity(const Entity& other);
45 ~Entity() override;
46
47 auto clone() const -> std::unique_ptr<GameObject> override;
48
49 auto showMenu() -> void override;
50 auto update(float dt) -> void override;
51 auto onUpdate(float dt) -> void override;
52
53 auto endWindow() -> void override
54 {
55 }; // for debug ImGUI
56 auto render() -> void override;
57 auto onRender() -> void override;
58 auto onEnterEngine() -> void override;
59 auto for_each(std::function<void(GameObject&)> func) -> void override;
60 auto for_each(std::function<void(const GameObject&)> func) const -> void override;
61 auto load(Stream& stream) -> void override; // we may need to return something, not sure
62 auto save(Stream& stream) const -> void override; // we may need to return something, not sure
63
64
65 auto getParent() const -> Entity* override;
66 auto destroy() -> void override;
67
68
73 auto setShouldUpdate(const bool _shouldUpdate) -> void override;
74
79 auto setShouldRender(const bool _shouldRender) -> void override;
80
85 auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void override;
86
92 auto pop() -> std::unique_ptr<Entity>;
93
103 template <typename type>
104 auto GetComponent(Component::ComponentTypeEnum typeId) -> type*
105 {
106 return static_cast<type*>(this->Get(typeId));
107 }
108
114 [[nodiscard]] auto addChild(std::unique_ptr<GameObject> newChild) -> bool override;
115
116 auto addChild(std::unique_ptr<Entity> newChild) -> bool;
117
125 auto addComponent(std::unique_ptr<Component>&& component) -> void;
126
133 auto addComponent(std::unique_ptr<GameObject> component) -> bool;
134
140 template <std::derived_from<Component> T>
141 auto addComponent(const T& component) -> void
142 {
143 addComponent(component.clone());
144 }
145
146
152 template <std::derived_from<Component> T>
153 auto addComponent(std::unique_ptr<T> component) -> void
154 {
155 addComponent(std::unique_ptr<Component>(component.release()));
156 }
157
161 auto deregisterComponents() const -> void
162 {
163 auto messaging = Engine::getSystem<sys::Messaging>();
164 // call remove all with every component
165 components.for_each([&](const Component& component)
166 {
167 messaging->removeAll(&component);
168 });
169 }
170
171 friend auto to_json(json& j, const Entity& obj) -> void;
172 friend auto from_json(const json& j, Entity& obj) -> void;
173
174
178 auto showComponentsMenu() -> void;
179
180protected:
185 auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>> override;
186
187
188 //------------------------------------------------------------------------------
189 // Private Variables:
190 //------------------------------------------------------------------------------
191private:
193
194 //------------------------------------------------------------------------------
195 // Private Functions:
196 //------------------------------------------------------------------------------
197
204 auto Get(Component::ComponentTypeEnum typeId) -> Component*;
205
206
210 auto addComponentButton() -> void;
211
216
220 auto showNameInputMenu() -> void;
221};
222
223namespace test
224{
225auto entityTest() -> void;
226}
227
228namespace gobj
229{
230
236auto moveEntity(Entity& entityToMove, Entity& newParent) -> void;
237
238
239}
sys::Json Stream
Definition AudioObject.h:20
The class containing Json implemenation for building components.
The base class for components, holding all of their shared All components should inherit from this.
The class that holds the top node, and manages the main game loop, as well as startup and shutdown.
nlohmann::json json
Definition Json.cpp:19
Definition Component.h:28
static auto getSystem() -> system *
A function to get a system.
Definition Engine.h:43
Definition Entity.h:33
auto setShouldRender(const bool _shouldRender) -> void override
set whether the Entity, its components, and its children Entities should render every frame
Definition Entity.cpp:255
auto update(float dt) -> void override
called once every frame.
Definition Entity.cpp:431
auto load(Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Entity.cpp:469
auto showComponentsMenu() -> void
call showMenu() on all children
Definition Entity.cpp:363
auto addComponent(std::unique_ptr< Component > &&component) -> void
takes a component pointer, and adds it to the Entity.
Definition Entity.cpp:93
auto getParent() const -> Entity *override
Definition Entity.cpp:199
auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void override
set whether the Entity, its components, and its children Entities should receive messages
Definition Entity.cpp:276
auto destroy() -> void override
Marks an GameObject to be destroyed.
Definition Entity.cpp:229
auto pop() -> std::unique_ptr< Entity >
Removes the Entity from the Engine tree, and gives ownership to the caller.
Definition Entity.cpp:297
auto addComponentButton() -> void
A helper function for drawMenu() to make a button that lets you add components or entities.
Definition Entity.cpp:164
auto showNameInputMenu() -> void
A helper function for showMenu() to show a input field for the entity name.
Definition Entity.cpp:377
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:450
ChildrenHandeler< Component > components
Definition Entity.h:192
friend auto from_json(const json &j, Entity &obj) -> void
Definition Entity.cpp:513
auto render() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:443
auto for_each(std::function< void(GameObject &)> func) -> void override
applies a function to every child.
Definition Entity.cpp:459
auto setShouldUpdate(const bool _shouldUpdate) -> void override
set whether the Entity, its components, and its children Entities should update every frame
Definition Entity.cpp:234
auto deregisterComponents() const -> void
deregister every component from messaging system
Definition Entity.h:161
friend auto to_json(json &j, const Entity &obj) -> void
Definition Entity.cpp:505
~Entity() override
Definition Entity.cpp:52
auto showMenu() -> void override
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition Entity.cpp:392
Entity()
Definition Entity.cpp:34
auto onUpdate(float dt) -> void override
called once every frame.
Definition Entity.cpp:438
auto addComponent(const T &component) -> void
takes a component directly and adds it to the Entity.
Definition Entity.h:141
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Entity.cpp:304
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Entity.h:53
auto addComponent(std::unique_ptr< T > component) -> void
allows adding components to objects with unique_ptrs to concrete components
Definition Entity.h:153
auto GetComponent(Component::ComponentTypeEnum typeId) -> type *
the implementation of the GetComponent Template
Definition Entity.h:104
auto addChild(std::unique_ptr< GameObject > newChild) -> bool override
adds either a component to the entity, or another game object as a child
Definition Entity.cpp:60
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Entity.cpp:474
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition Entity.cpp:454
auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > > override
Definition Entity.cpp:130
auto showUpdateRenderMessagesFlagButtons() -> void
A helper function for showMenu() to make buttons for toggling shouldRender, shouldUpdate,...
Definition Entity.cpp:312
auto Get(Component::ComponentTypeEnum typeId) -> Component *
Definition Entity.cpp:150
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
friend ChildrenHandeler
Definition GameObject.h:87
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
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:229
auto moveEntity(Entity &entityToMove, Entity &newParent) -> void
Makes the provided entity a child of another entity.
Definition Entity.cpp:520
Definition Entity.h:224
auto entityTest() -> void
Definition Entity.cpp:479