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 "Framework/Factory.h"
31#include <memory>
32
34#include "Utility/Path/Path.h"
35
36class Action;
37
38class Entity : public GameObject
39{
40public:
47#define HAS(type) GetComponent<type>(Component::c##type)
48
49 Entity();
50 Entity(const Entity& other);
51 ~Entity() override;
52
53 auto clone() const -> std::unique_ptr<GameObject> override;
54
55 auto showMenu() -> void override;
56 auto update(float dt) -> void override;
57 auto onUpdate(float dt) -> void override;
58
59 auto endWindow() -> void override
60 {
61 }; // for debug ImGUI
62 auto render() -> void override;
63 auto onRender() -> void override;
64 auto onEnterEngine() -> void override;
65 auto onSceneStart(const std::string& sceneName) -> void override;
66 auto onSceneExit(const std::string& sceneName) -> void override;
67 auto for_each(std::function<void(GameObject&)> func) -> void override;
68 auto for_each(std::function<void(const GameObject&)> func) const -> void override;
69 auto load(const Stream& stream) -> void override; // we may need to return something, not sure
70 auto save(Stream& stream) const -> void override; // we may need to return something, not sure
71
72
73 auto getParent() const -> Entity* override;
74 auto destroy() -> void override;
75
76
81 auto setShouldUpdate(bool _shouldUpdate) -> void override;
82
87 auto setShouldRender(bool _shouldRender) -> void override;
88
93 auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void override;
94
100 auto pop() -> std::unique_ptr<Node>;
101
111 template <typename type>
112 auto GetComponent(Component::ComponentTypeEnum typeId) -> type*
113 {
114 return static_cast<type*>(this->Get(typeId));
115 }
116
122 [[nodiscard]] auto addChild(std::unique_ptr<GameObject> newChild) -> bool override;
123
124 auto addChild(std::unique_ptr<Entity> newChild) -> bool;
125
133 auto addComponent(std::unique_ptr<Component>&& component) -> void;
134
141 auto addComponent(std::unique_ptr<GameObject> component) -> bool;
142
148 template <std::derived_from<Component> T>
149 auto addComponent(const T& component) -> void
150 {
151 addComponent(component.clone());
152 }
153
154
160 template <std::derived_from<Component> T>
161 auto addComponent(std::unique_ptr<T> component) -> void
162 {
163 addComponent(std::unique_ptr<Component>(component.release()));
164 }
165
169 auto deregisterComponents() const -> void
170 {
171 auto messaging = Engine::getSystem<sys::Messaging>();
172 // call remove all with every component
173 components.for_each([&](const Component& component)
174 {
175 messaging->removeAll(&component);
176 });
177 }
178
179 friend auto to_json(json& j, const Entity& obj) -> void;
180 friend auto from_json(const json& j, Entity& obj) -> void;
181
182
186 auto showComponentsMenu() -> void;
187
188
189 template<typename ActionType>
196 auto operator<<(const ActionType& action) -> Entity&
197 {
199 assert(actionList && "Tried adding an action to an entity without an actionList");
200 actionList->pushBack(action);
201
202 return *this;
203 }
204
205
210 template<typename ActionType>
211 auto addAction(const ActionType& action) -> void
212 {
213 ActionList* actionList;
215 {
216 addComponent(th::Factory::make<ActionList>("Default/ActionList"));
218 }
219 assert(actionList && "failed adding an Action to an Entity");
220 actionList->pushBack(action);
221 }
222
223protected:
228 auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>> override;
229
230
231 //------------------------------------------------------------------------------
232 // Private Variables:
233 //------------------------------------------------------------------------------
234private:
236
237 //------------------------------------------------------------------------------
238 // Private Functions:
239 //------------------------------------------------------------------------------
240
247 auto Get(Component::ComponentTypeEnum typeId) -> Component*;
248
249
253 auto addComponentButton() -> void;
254
259
263 auto showNameInputMenu() -> void;
264};
265
266// This two have be non-member functions
267
275template<typename ActionType>
276auto operator<<(gobj::Path<Entity>& entity, const ActionType& action) -> gobj::Path<Entity>&
277{
278 auto actionList = entity->GetComponent<ActionList>(Component::cActionList);
279 assert(actionList && "Tried adding an action to an entity without an actionList");
280 actionList->pushBack(action);
281
282 return entity;
283}
284
285
293template<typename ActionType>
294auto operator<<(Entity* entity, const ActionType& action) -> Entity*
295{
296 auto actionList = entity->GetComponent<ActionList>(Component::cActionList);
297 assert(actionList && "Tried adding an action to an entity without an actionList");
298 actionList->pushBack(action);
299
300 return entity;
301}
302
303namespace test
304{
305auto entityTest() -> void;
306}
307
308namespace gobj
309{
310
316auto moveEntity(Entity& entityToMove, Entity& newParent) -> void;
317
318
319}
Component that stores all actions currently affecting the parent entity.
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
An object that represents where a GameObject or a range of GameObjects exists in the Engine.
Component that stores all actions currently affecting the parent entity.
Definition ActionList.h:30
auto pushBack(const std::unique_ptr< Action > &newAction) -> void
Definition ActionList.cpp:159
Definition Action.h:131
Definition Component.h:28
@ cActionList
Definition Component.h:64
static auto getSystem() -> system *
A function to get a system.
Definition Engine.h:46
Definition Entity.h:39
auto onSceneStart(const std::string &sceneName) -> void override
Hook that is called before the first frame a scene is run, but after the entire scene is loaded into ...
Definition Entity.cpp:462
auto update(float dt) -> void override
called once every frame.
Definition Entity.cpp:434
auto setShouldUpdate(bool _shouldUpdate) -> void override
set whether the Entity, its components, and its children Entities should update every frame
Definition Entity.cpp:237
auto showComponentsMenu() -> void
call showMenu() on all children
Definition Entity.cpp:366
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 destroy() -> void override
Marks an GameObject to be destroyed.
Definition Entity.cpp:229
auto addAction(const ActionType &action) -> void
Adds a new action to an entity's ActionList component, while making sure the component has an ActionL...
Definition Entity.h:211
auto addComponentButton() -> void
A helper function for drawMenu() to make a button that lets you add components or entities.
Definition Entity.cpp:164
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:453
ChildrenHandeler< Component > components
Definition Entity.h:235
auto operator<<(const ActionType &action) -> Entity &
Adds a new action to this entity's ActionList component.
Definition Entity.h:196
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Entity.cpp:482
auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void override
set whether the Entity, its components, and its children Entities should receive messages
Definition Entity.cpp:279
friend auto from_json(const json &j, Entity &obj) -> void
Definition Entity.cpp:526
auto render() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:446
auto onSceneExit(const std::string &sceneName) -> void override
Hook that is called right before a scene is unloaded To receive the hook, simply override the functio...
Definition Entity.cpp:467
auto for_each(std::function< void(GameObject &)> func) -> void override
applies a function to every child.
Definition Entity.cpp:472
auto deregisterComponents() const -> void
deregister every component from messaging system
Definition Entity.h:169
friend auto to_json(json &j, const Entity &obj) -> void
Definition Entity.cpp:518
auto setShouldRender(bool _shouldRender) -> void override
set whether the Entity, its components, and its children Entities should render every frame
Definition Entity.cpp:258
~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:395
Entity()
Definition Entity.cpp:34
auto onUpdate(float dt) -> void override
called once every frame.
Definition Entity.cpp:441
auto addComponent(const T &component) -> void
takes a component directly and adds it to the Entity.
Definition Entity.h:149
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Entity.cpp:307
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Entity.h:59
auto addComponent(std::unique_ptr< T > component) -> void
allows adding components to objects with unique_ptrs to concrete components
Definition Entity.h:161
auto GetComponent(Component::ComponentTypeEnum typeId) -> type *
the implementation of the GetComponent Template
Definition Entity.h:112
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:487
auto pop() -> std::unique_ptr< Node >
Removes the Entity from the Engine tree, and gives ownership to the caller.
Definition Entity.cpp:300
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition Entity.cpp:457
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:315
auto showNameInputMenu() -> void
A helper function for showMenu() to show a input field for the entity name.
Definition Entity.cpp:380
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:507
Manages the heirarchical structure between Systems and Entities.
Definition Node.h:41
static auto make(const std::string &name) -> std::unique_ptr< T >
create an object and heap allocate it.
Definition Factory.h:44
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:309
auto moveEntity(Entity &entityToMove, Entity &newParent) -> void
Makes the provided entity a child of another entity.
Definition Entity.cpp:533
Definition Entity.h:304
auto entityTest() -> void
Definition Entity.cpp:492