Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Entity.h
Go to the documentation of this file.
1#pragma once
2#include <Engine/Core/Tick.h>
3#include <vector>
4#include <memory>
5#include <algorithm>
6#include <plog/Log.h>
7#include <entt/entt.hpp>
8#include "uuid/uuid.h"
10
11namespace EngineCore{
18 class Entity : public ITickable, protected std::enable_shared_from_this<Entity> {
19 public:
20 Entity();
21 virtual ~Entity();
22
27 void enableTick(bool enable);
28
33 void tick(double deltaTime) override;
34
43 template<typename T, typename... Args>
44 T* addComponent(Args&&... args) {
45 static_assert(std::is_base_of_v<LogicComponent, T>, "T must derive from LogicComponent");
46
47 if constexpr (T::IsUnique) {
48 T* existing = getComponent<T>();
49 if (existing) {
50 PLOGW << "Attempted to add unique component " << typeid(T).name() << " which already exists on entity.";
51 return existing;
52 }
53 }
54
55 auto component = std::make_shared<T>(std::forward<Args>(args)...);
56 component->setOwningEntity(this);
57 components.push_back(component);
58
59 // If the entity is already initialized (e.g. gameplay started), we might want to call beginPlay immediately?
60 // For now, we assume standard flow where components are added before play or beginPlay handles late init if needed.
61 // But typical pattern is to call beginPlay() if the actor has already begun play.
62 // We will leave that responsibility to the caller or Actor subclass for now to keep Entity simple.
63 // Note: Actor implementation will need to ensure new components get initialized.
64
65 return component.get();
66 }
67
72 template<typename T>
73 T* getComponent() const {
74 static_assert(std::is_base_of_v<LogicComponent, T>, "T must derive from LogicComponent");
75 for (const auto& component : components) {
76 if (auto typed = dynamic_cast<T*>(component.get())) {
77 return typed;
78 }
79 }
80 return nullptr;
81 }
82
87 template<typename T>
88 std::vector<T*> getComponents() const {
89 static_assert(std::is_base_of_v<LogicComponent, T>, "T must derive from LogicComponent");
90 std::vector<T*> result;
91 for (const auto& component : components) {
92 if (auto typed = dynamic_cast<T*>(component.get())) {
93 result.push_back(typed);
94 }
95 }
96 return result;
97 }
98
104 auto it = std::ranges::find_if(components, [component](const auto& ptr) {
105 return ptr.get() == component;
106 });
107
108 if (it != components.end()) {
109 (*it)->endPlay(); // Ensure cleanup
110 components.erase(it);
111 }
112 }
113
118 template<typename T>
120 static_assert(std::is_base_of_v<LogicComponent, T>, "T must derive from LogicComponent");
121 auto it = std::ranges::find_if(components, [](const auto& ptr) {
122 return dynamic_cast<T*>(ptr.get()) != nullptr;
123 });
124
125 if (it != components.end()) {
126 (*it)->endPlay();
127 components.erase(it);
128 return true;
129 }
130 return false;
131 }
132
137 template<typename T>
138 bool hasComponent() const {
139 return getComponent<T>() != nullptr;
140 }
141
146 [[nodiscard]] bool canTick() const;
147
152 [[nodiscard]] bool canEverTick() const;
153 protected:
161 uuids::uuid getUUID() const { return uuid; }
162
170 std::string getUuidString() const;
171
175 std::vector<std::shared_ptr<LogicComponent>> components;
176
177 entt::entity data = entt::null;
178 protected:
179 bool allowTicking = false;
180 private:
181 uuids::uuid uuid;
182 };
183
184}
entt::entity data
Definition Entity.h:177
bool canTick() const
Tells you if the entity can execute its tick function.
Definition Entity.cpp:35
virtual ~Entity()
Definition Entity.cpp:13
T * getComponent() const
Returns the first component of type T found.
Definition Entity.h:73
std::string getUuidString() const
Getter for the UUID of this entity as a string.
Definition Entity.cpp:49
void removeComponent(LogicComponent *component)
Removes a specific component instance.
Definition Entity.h:103
std::vector< T * > getComponents() const
Returns all components of type T.
Definition Entity.h:88
void tick(double deltaTime) override
Executes every frame if tick is enabled.
Definition Entity.cpp:31
uuids::uuid uuid
Definition Entity.h:181
void enableTick(bool enable)
Enables or disables ticking for this entity.
Definition Entity.cpp:18
std::vector< std::shared_ptr< LogicComponent > > components
List of all logical components.
Definition Entity.h:175
T * addComponent(Args &&... args)
Adds a component to the entity. Checks for uniqueness if the component defines 'static constexpr bool...
Definition Entity.h:44
bool canEverTick() const
Tells if this entity can ever tick.
Definition Entity.cpp:44
uuids::uuid getUUID() const
Getter for the UUID of this entity.
Definition Entity.h:161
bool removeFirstComponent()
Removes the first component of type T found.
Definition Entity.h:119
bool hasComponent() const
Checks if a component of type T exists.
Definition Entity.h:138
This is the interface which is used to call a tick function on an object. Everything which should be ...
Definition Tick.h:10
Base class for all logic components that can be attached to an actor. Provides access to the scene,...
Log category system implementation.