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
35#include "Utility/Path/Path.h"
36
37struct Script;
38class Action;
39
40class Entity : public GameObject
41{
42public:
49#define HAS(type) GetComponent<type>(Component::c##type)
50
51 Entity();
52 Entity(const Entity& other);
53 ~Entity() override;
54
55 auto clone() const -> std::unique_ptr<GameObject> override;
56
57 auto showMenu() -> void override;
58 auto update(float dt) -> void override;
59 auto onUpdate(float dt) -> void override;
60
61 auto endWindow() -> void override
62 {
63 }; // for debug ImGUI
64 auto render() -> void override;
65 auto onRender() -> void override;
66 auto onEnterEngine() -> void override;
67 auto onSceneStart(const std::string& sceneName) -> void override;
68 auto onSceneExit(const std::string& sceneName) -> void override;
69 auto for_each(std::function<void(GameObject&)> func) -> void override;
70 auto for_each(std::function<void(const GameObject&)> func) const -> void override;
71 auto load(const Stream& stream) -> void override; // we may need to return something, not sure
72 auto save(Stream& stream) const -> void override; // we may need to return something, not sure
73
74
75 auto getParent() const -> Entity* override;
76 auto destroy() -> void override;
77
78
83 auto setShouldUpdate(bool _shouldUpdate) -> void override;
84
89 auto setShouldRender(bool _shouldRender) -> void override;
90
95 auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void override;
96
102 auto pop() -> std::unique_ptr<Node>;
103
113 template <typename type>
114 auto GetComponent(Component::ComponentTypeEnum typeId) -> type*
115 {
116 return static_cast<type*>(this->Get(typeId));
117 }
118
119 template <typename comp>
120 auto GetComponent() -> comp*
121 {
122 auto typeId = comp::getEnum();
123 return static_cast<comp*>(this->Get(typeId));
124 }
125
131 [[nodiscard]] auto addChild(std::unique_ptr<GameObject> newChild) -> bool override;
132
133 auto addChild(std::unique_ptr<Entity> newChild) -> bool;
134
142 auto addComponent(std::unique_ptr<Component>&& component) -> void;
143
150 auto addComponent(std::unique_ptr<GameObject> component) -> bool;
151
157 template <std::derived_from<Component> T>
158 auto addComponent(const T& component) -> void
159 {
160 addComponent(component.clone());
161 }
162
163
169 template <std::derived_from<Component> T>
170 auto addComponent(std::unique_ptr<T> component) -> void
171 {
172 addComponent(std::unique_ptr<Component>(component.release()));
173 }
174
180 auto popComponent(const Key& key) -> std::unique_ptr<Component>;
181
185 auto deregisterComponents() const -> void
186 {
187 auto messaging = Engine::getSystem<sys::Messaging>();
188 // call remove all with every component
189 components.for_each([&](const Component& component)
190 {
191 messaging->removeAll(&component);
192 });
193 }
194
195 friend auto to_json(json& j, const Entity& obj) -> void;
196 friend auto from_json(const json& j, Entity& obj) -> void;
197
198
202 auto showComponentsMenu() -> void;
203
204
205// We have action_functions inside of entity for easier access (you don't need to get the ActionList component
206// every time)
207#pragma region action_functions
208
209 template<typename ActionType>
216 auto operator<<(const ActionType& action) -> Entity&
217 {
218 auto actionList = GetComponent<ActionList>(Component::cActionList);
219 assert(actionList && "Tried adding an action to an entity without an actionList");
220 actionList->pushBack(action);
221
222 return *this;
223 }
224
225
230 template<typename ActionType>
231 auto addAction(const ActionType& action) -> void
232 {
233 ActionList* actionList;
234 if (!((actionList = GetComponent<ActionList>(Component::cActionList))))
235 {
236 addComponent(th::Factory::make<ActionList>("Default/ActionList"));
237 actionList = GetComponent<ActionList>(Component::cActionList);
238 }
239 assert(actionList && "failed adding an Action to an Entity");
240 actionList->pushBack(action);
241 }
242
247 template<typename ActionType>
248 auto addActionToFront(const ActionType& action) -> void
249 {
250 ActionList* actionList;
251 if (!((actionList = GetComponent<ActionList>(Component::cActionList))))
252 {
253 addComponent(th::Factory::make<ActionList>("Default/ActionList"));
254 actionList = GetComponent<ActionList>(Component::cActionList);
255 }
256 assert(actionList && "failed adding an Action to an Entity");
257 actionList->pushFront(action);
258 }
259
265 template<typename ActionType>
266 auto insertAction(const ActionType& action, int position) -> void
267 {
268 ActionList* actionList;
269 if (!((actionList = GetComponent<ActionList>(Component::cActionList))))
270 {
271 addComponent(th::Factory::make<ActionList>("Default/ActionList"));
272 actionList = GetComponent<ActionList>(Component::cActionList);
273 }
274 assert(actionList && "failed adding an Action to an Entity");
275 actionList->pushFront(action);
276 }
277
282 auto clearActions() -> void
283 {
284 if (auto actionList = GetComponent<ActionList>(Component::cActionList))
285 {
286 actionList->clear();
287 }
288 }
289
290
298 auto getAction(const std::string& actionName) -> Action*
299 {
300 if (auto actionList = GetComponent<ActionList>(Component::cActionList))
301 {
302 return actionList->getAction(actionName);
303 }
304 return nullptr;
305 }
306
313 auto removeAction(const std::string& actionName) -> void
314 {
315 if (auto actionList = GetComponent<ActionList>(Component::cActionList))
316 {
317 actionList->removeAction(actionName);
318 }
319 }
320
321#pragma endregion
322
329 auto operator[](const std::string& scriptName) -> Script&;
330
331
337 auto contains(const std::string& scriptName) -> bool;
338
344 auto getScript(const std::string& scriptName ) -> Script&;
345
346
348 static auto bindLuaTypes(sol::state& lua) -> void;
349
350protected:
355 auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>> override;
356
357
358 //------------------------------------------------------------------------------
359 // Private Variables:
360 //------------------------------------------------------------------------------
361private:
363
364 //------------------------------------------------------------------------------
365 // Private Functions:
366 //------------------------------------------------------------------------------
367
374 auto Get(Component::ComponentTypeEnum typeId) -> Component*;
375
376
380 auto addComponentButton() -> void;
381
386
390 auto showNameInputMenu() -> void;
391
397
399 auto addChildLua(std::unique_ptr<GameObject>& newChild) -> bool;
400};
401
402// This two have be non-member functions
403
411template<typename ActionType>
412auto operator<<(gobj::Path<Entity>& entity, const ActionType& action) -> gobj::Path<Entity>&
413{
414 auto actionList = entity->GetComponent<ActionList>(Component::cActionList);
415 assert(actionList && "Tried adding an action to an entity without an actionList");
416 actionList->pushBack(action);
417
418 return entity;
419}
420
421
429template<typename ActionType>
430auto operator<<(Entity* entity, const ActionType& action) -> Entity&
431{
432 auto actionList = entity->GetComponent<ActionList>(Component::cActionList);
433 assert(actionList && "Tried adding an action to an entity without an actionList");
434 actionList->pushBack(action);
435
436 return *entity;
437}
438
439namespace gobj
440{
441
447auto moveEntity(Entity& entityToMove, Entity& newParent) -> void;
448
449
450}
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:205
auto pushFront(const std::unique_ptr< Action > &newAction) -> void
Definition ActionList.cpp:211
Definition Action.h:131
Definition Component.h:32
static auto getSystem() -> system *
A function to get a system.
Definition Engine.h:50
Definition Entity.h:41
auto clearActions() -> void
Removes all actions that an entity has inside its ActionList.
Definition Entity.h:282
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:591
auto removeAction(const std::string &actionName) -> void
Removes the first action of a specific type.
Definition Entity.h:313
auto update(float dt) -> void override
called once every frame.
Definition Entity.cpp:563
auto contains(const std::string &scriptName) -> bool
checks if a script is on an entity
Definition Entity.cpp:400
auto setShouldUpdate(bool _shouldUpdate) -> void override
set whether the Entity, its components, and its children Entities should update every frame
Definition Entity.cpp:245
auto showComponentsMenu() -> void
call showMenu() on all children
Definition Entity.cpp:379
auto addComponent(std::unique_ptr< Component > &&component) -> void
takes a component pointer, and adds it to the Entity.
Definition Entity.cpp:99
static auto bindLuaTypes(sol::state &lua) -> void
register Entity as a lua usertype
Definition Entity.cpp:418
auto getParent() const -> Entity *override
Definition Entity.cpp:207
auto destroy() -> void override
Marks an GameObject to be destroyed.
Definition Entity.cpp:237
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:231
auto addComponentButton() -> void
A helper function for drawMenu() to make a button that lets you add components or entities.
Definition Entity.cpp:191
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:582
auto getScript(const std::string &scriptName) -> Script &
get a script that's on an entity
Definition Entity.cpp:409
auto operator[](const std::string &scriptName) -> Script &
get a script that's on an entity gets a script on an entity
Definition Entity.cpp:395
ChildrenHandeler< Component > components
Definition Entity.h:362
auto operator<<(const ActionType &action) -> Entity &
Adds a new action to this entity's ActionList component.
Definition Entity.h:216
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Entity.cpp:611
auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void override
set whether the Entity, its components, and its children Entities should receive messages
Definition Entity.cpp:287
friend auto from_json(const json &j, Entity &obj) -> void
Definition Entity.cpp:628
auto render() -> void override
called every frame after update has been called for every object.
Definition Entity.cpp:575
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:596
auto for_each(std::function< void(GameObject &)> func) -> void override
applies a function to every child.
Definition Entity.cpp:601
auto sendAddedComponentMessage(AddedComponentMessage &message) -> void
helper function for addComponent
Definition Entity.cpp:179
auto deregisterComponents() const -> void
deregister every component from messaging system
Definition Entity.h:185
friend auto to_json(json &j, const Entity &obj) -> void
Definition Entity.cpp:620
auto setShouldRender(bool _shouldRender) -> void override
set whether the Entity, its components, and its children Entities should render every frame
Definition Entity.cpp:266
auto addChildLua(std::unique_ptr< GameObject > &newChild) -> bool
wrapper for addChild to allow it to be bound and called in lua
Definition Entity.cpp:473
~Entity() override
Definition Entity.cpp:58
auto showMenu() -> void override
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition Entity.cpp:478
Entity()
Definition Entity.cpp:40
auto addActionToFront(const ActionType &action) -> void
Adds a new action to the front of an entity's ActionList component, while making sure the component h...
Definition Entity.h:248
auto onUpdate(float dt) -> void override
called once every frame.
Definition Entity.cpp:570
auto insertAction(const ActionType &action, int position) -> void
Adds a new action to the front of an entity's ActionList component, while making sure the component h...
Definition Entity.h:266
auto getAction(const std::string &actionName) -> Action *
Returns the first action of a specific type.
Definition Entity.h:298
auto addComponent(const T &component) -> void
takes a component directly and adds it to the Entity.
Definition Entity.h:158
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Entity.cpp:308
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Entity.h:61
auto addComponent(std::unique_ptr< T > component) -> void
allows adding components to objects with unique_ptrs to concrete components
Definition Entity.h:170
auto GetComponent(Component::ComponentTypeEnum typeId) -> type *
the implementation of the GetComponent Template
Definition Entity.h:114
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:66
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Entity.cpp:616
auto pop() -> std::unique_ptr< Node >
Removes the Entity from the Engine tree, and gives ownership to the caller.
Definition Entity.cpp:314
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition Entity.cpp:586
auto GetComponent() -> comp *
Definition Entity.h:120
auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > > override
Definition Entity.cpp:147
auto showUpdateRenderMessagesFlagButtons() -> void
A helper function for showMenu() to make buttons for toggling shouldRender, shouldUpdate,...
Definition Entity.cpp:328
auto popComponent(const Key &key) -> std::unique_ptr< Component >
Removes a component from the Entity, and gives ownership to the caller.
Definition Entity.cpp:321
auto showNameInputMenu() -> void
A helper function for showMenu() to show a input field for the entity name.
Definition Entity.cpp:457
auto Get(Component::ComponentTypeEnum typeId) -> Component *
Definition Entity.cpp:168
the base class for the engine, most things inherit from this.
Definition GameObject.h:83
friend class ChildrenHandeler
Definition GameObject.h:95
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
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:45
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:440
auto moveEntity(Entity &entityToMove, Entity &newParent) -> void
Makes the provided entity a child of another entity.
Definition Entity.cpp:635
Definition AddedComponentMessage.h:14
Definition Script.h:32