Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Entity.h
1#pragma once
2#include <Engine/Core/Tick.h>
3#include <vector>
4#include <memory>
5#include <algorithm>
6#include <cstdint>
7#include <type_traits>
8#include <plog/Log.h>
9#include <entt/entt.hpp>
10#include "Engine/Component/LogicComponent.h"
11
12namespace Engine::Entities{
13 class EntityContainer;
14
18 class Entity : public Core::ITickable, protected std::enable_shared_from_this<Entity> {
19 public:
24
28 ~Entity() override;
29
34 void enableTick(bool enable);
35
40 void setTickPriority(int32_t priority);
41
45 [[nodiscard]] int32_t getTickPriority() const { return tickPriority_; }
46
51 void tick(double deltaTime) override;
52
56 void destroy();
57
62
66 [[nodiscard]] EntityContainer * getOwningContainer() const;
67
71 [[nodiscard]] bool isPendingDestroy() const;
72
76 [[nodiscard]] bool isPlaying() const;
77
86 template<typename T, typename... Args>
87 requires std::is_base_of_v<Components::Logic, T> && std::is_constructible_v<T, Args...>
88 T* addComponent(Args&&... args) {
89
90 if constexpr (T::IsUnique) {
91 T* existing = getComponent<T>();
92 if (existing) {
93 PLOGW << "Attempted to add unique component " << typeid(T).name() << " which already exists on entity.";
94 return existing;
95 }
96 }
97
98 auto component = std::make_unique<T>(std::forward<Args>(args)...);
99 component->setOwningEntity(this);
100 T* rawComponent = component.get();
101 components_.push_back(std::move(component));
102
103 if ( isPlaying() && !rawComponent->hasBegunPlay() ) {
104 rawComponent->beginPlay();
105 }
106
107 return rawComponent;
108 }
109
114 template<typename T>
115 requires std::is_base_of_v<Components::Logic, T>
116 T* getComponent() const {
117 for (const auto& component : components_) {
118 if (auto typed = dynamic_cast<T*>(component.get())) {
119 return typed;
120 }
121 }
122 return nullptr;
123 }
124
129 template<typename T>
130 requires std::is_base_of_v<Components::Logic, T>
131 std::vector<T*> getComponents() const {
132 std::vector<T*> result;
133 for (const auto& component : components_) {
134 if (auto typed = dynamic_cast<T*>(component.get())) {
135 result.push_back(typed);
136 }
137 }
138 return result;
139 }
140
146 auto it = std::ranges::find_if(components_, [component](const auto& ptr) {
147 return ptr.get() == component;
148 });
149
150 if (it != components_.end()) {
151 (*it)->endPlay(); // Ensure cleanup
152 components_.erase(it);
153 }
154 }
155
160 template<typename T>
161 requires std::is_base_of_v<Components::Logic, T>
163 auto it = std::ranges::find_if(components_, [](const auto& ptr) {
164 return dynamic_cast<T*>(ptr.get()) != nullptr;
165 });
166
167 if (it != components_.end()) {
168 (*it)->endPlay();
169 components_.erase(it);
170 return true;
171 }
172 return false;
173 }
174
179 template<typename T>
180 requires std::is_base_of_v<Components::Logic, T>
181 bool hasComponent() const {
182 return getComponent<T>() != nullptr;
183 }
184
189 [[nodiscard]] const std::vector<std::unique_ptr<Components::Logic>>& getAllComponents() const {
190 return components_;
191 }
192
197 [[nodiscard]] bool canTick() const;
198
203 [[nodiscard]] bool canEverTick() const;
204
209 [[nodiscard]] entt::entity getEcsEntity() const { return data_; }
210
211 protected:
215 std::vector<std::unique_ptr<Components::Logic>> components_;
216
217 entt::entity data_ = entt::null;
218
219 bool allowTicking_ = false;
220 int32_t tickPriority_ = 0;
221
225 virtual void onEngineBeginPlay();
226
230 virtual void onEngineEndPlay();
231
235 virtual void onEngineDestroyed();
236 private:
237 friend class EntityContainer;
238
247
250
251 [[nodiscard]] bool isDestroyingOrDestroyed() const;
252
255 };
256
257}
Base class for all logic components that can be attached to an actor. Provides access to the scene,...
This is the interface which is used to call a tick function on an object. Everything which should be ...
Definition Tick.h:24
Owns entity lifetime and deferred destruction for a scene or subsystem.
entt::entity getEcsEntity() const
Gets the ECS entity handle for this Entity.
Definition Entity.h:209
void enableTick(bool enable)
Enables or disables ticking for this entity.
T * addComponent(Args &&... args)
Adds a component to the entity. Checks for uniqueness if the component defines 'static constexpr bool...
Definition Entity.h:88
bool allowTicking_
Whether ticking is currently enabled.
Definition Entity.h:219
bool isPlaying() const
Returns whether this entity has begun play and has not ended play.
bool removeFirstComponent()
Removes the first component of type T found.
Definition Entity.h:162
LifecycleState lifecycleState_
Definition Entity.h:254
int32_t getTickPriority() const
Returns the tick ordering priority for this entity.
Definition Entity.h:45
EntityContainer * owningContainer_
Definition Entity.h:253
Entity()
Creates an entity in the constructed lifecycle state.
bool canTick() const
Tells you if the entity can execute its tick function.
std::vector< std::unique_ptr< Components::Logic > > components_
List of all logical components.
Definition Entity.h:215
bool hasComponent() const
Checks if a component of type T exists.
Definition Entity.h:181
const std::vector< std::unique_ptr< Components::Logic > > & getAllComponents() const
Gets all components on this entity.
Definition Entity.h:189
virtual void onEngineEndPlay()
Hook called when the engine ends play for this entity.
bool isDestroyingOrDestroyed() const
virtual void onEngineBeginPlay()
Hook called when the engine begins play for this entity.
virtual void onEngineDestroyed()
Hook called when the entity is being destroyed by the engine lifecycle.
void tick(double deltaTime) override
Executes every frame if tick is enabled.
friend class EntityContainer
Definition Entity.h:237
void destroyImmediate()
Destroys this entity immediately through its owning container.
~Entity() override
Destroys the entity and owned logic components.
entt::entity data_
ECS entity handle associated with this entity.
Definition Entity.h:217
void removeComponent(Components::Logic *component)
Removes a specific component instance.
Definition Entity.h:145
bool canEverTick() const
Tells if this entity can ever tick.
EntityContainer * getOwningContainer() const
Returns the container that owns this entity.
int32_t tickPriority_
Priority used to order entity ticks.
Definition Entity.h:220
std::vector< T * > getComponents() const
Returns all components of type T.
Definition Entity.h:131
void destroy()
Tells the scene to destroy this Entity.
T * getComponent() const
Returns the first component of type T found.
Definition Entity.h:116
bool isPendingDestroy() const
Returns whether this entity has been queued for destruction.
void setTickPriority(int32_t priority)
Sets the tick ordering priority for this entity.