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 <cstdint>
7#include <plog/Log.h>
8#include <entt/entt.hpp>
9#include "uuid/uuid.h"
11
12namespace Engine::Entities{
16 class Entity : public Core::ITickable, protected std::enable_shared_from_this<Entity> {
17 public:
19 ~Entity() override;
20
25 void enableTick(bool enable);
26
27 void setTickPriority(int32_t priority);
28 [[nodiscard]] int32_t getTickPriority() const { return tickPriority_; }
29
34 void tick(double deltaTime) override;
35
44 template<typename T, typename... Args> requires std::is_base_of_v<Components::Logic, T>
45 T* addComponent(Args&&... args) {
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_unique<T>(std::forward<Args>(args)...);
56 component->setOwningEntity(this);
57 T* rawComponent = component.get();
58 components_.push_back(std::move(component));
59
60 // If the entity is already initialized (e.g. gameplay started), we might want to call beginPlay immediately?
61 // For now, we assume standard flow where components are added before play or beginPlay handles late init if needed.
62 // But typical pattern is to call beginPlay() if the actor has already begun play.
63 // We will leave that responsibility to the caller or Actor subclass for now to keep Entity simple.
64 // Note: Actor implementation will need to ensure new components get initialized.
65
66 return rawComponent;
67 }
68
73 template<typename T>
74 requires std::is_base_of_v<Components::Logic, T>
75 T* getComponent() const {
76 for (const auto& component : components_) {
77 if (auto typed = dynamic_cast<T*>(component.get())) {
78 return typed;
79 }
80 }
81 return nullptr;
82 }
83
88 template<typename T>
89 requires std::is_base_of_v<Components::Logic, T>
90 std::vector<T*> getComponents() const {
91 std::vector<T*> result;
92 for (const auto& component : components_) {
93 if (auto typed = dynamic_cast<T*>(component.get())) {
94 result.push_back(typed);
95 }
96 }
97 return result;
98 }
99
105 auto it = std::ranges::find_if(components_, [component](const auto& ptr) {
106 return ptr.get() == component;
107 });
108
109 if (it != components_.end()) {
110 (*it)->endPlay(); // Ensure cleanup
111 components_.erase(it);
112 }
113 }
114
119 template<typename T>
120 requires std::is_base_of_v<Components::Logic, T>
122 auto it = std::ranges::find_if(components_, [](const auto& ptr) {
123 return dynamic_cast<T*>(ptr.get()) != nullptr;
124 });
125
126 if (it != components_.end()) {
127 (*it)->endPlay();
128 components_.erase(it);
129 return true;
130 }
131 return false;
132 }
133
138 template<typename T>
139 requires std::is_base_of_v<Components::Logic, T>
140 bool hasComponent() const {
141 return getComponent<T>() != nullptr;
142 }
143
148 [[nodiscard]] const std::vector<std::unique_ptr<Components::Logic>>& getAllComponents() const {
149 return components_;
150 }
151
156 [[nodiscard]] bool canTick() const;
157
162 [[nodiscard]] bool canEverTick() const;
163
168 [[nodiscard]] entt::entity getEcsEntity() const { return data_; }
169
170 protected:
175 uuids::uuid getUUID() const { return uuid_; }
176
181 std::string getUuidString() const;
182
186 std::vector<std::unique_ptr<Components::Logic>> components_;
187
188 entt::entity data_ = entt::null;
189
190 bool allowTicking_ = false;
191 int32_t tickPriority_ = 0;
192 private:
193 uuids::uuid uuid_;
194 };
195
196}
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
entt::entity getEcsEntity() const
Gets the ECS entity handle for this Entity.
Definition Entity.h:168
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:45
bool removeFirstComponent()
Removes the first component of type T found.
Definition Entity.h:121
int32_t getTickPriority() const
Definition Entity.h:28
std::string getUuidString() const
Getter for the UUID of this entity as a string.
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:186
bool hasComponent() const
Checks if a component of type T exists.
Definition Entity.h:140
const std::vector< std::unique_ptr< Components::Logic > > & getAllComponents() const
Gets all components on this entity.
Definition Entity.h:148
void tick(double deltaTime) override
Executes every frame if tick is enabled.
entt::entity data_
Definition Entity.h:188
void removeComponent(Components::Logic *component)
Removes a specific component instance.
Definition Entity.h:104
bool canEverTick() const
Tells if this entity can ever tick.
uuids::uuid getUUID() const
Getter for the UUID of this entity.
Definition Entity.h:175
std::vector< T * > getComponents() const
Returns all components of type T.
Definition Entity.h:90
T * getComponent() const
Returns the first component of type T found.
Definition Entity.h:75
void setTickPriority(int32_t priority)