4 #include <entt/entt.hpp>
6 #include <core/Assertions.h>
7 #include "component/NameComponent.h"
8 #include "component/TagComponents.h"
9 #include "component/TransformComponent.h"
10 #include "component/UUIDComponent.h"
11 #include "component/LayerComponents.h"
23 , handle(scene_->getRegistry().create()) {
24 this->addComponent<TransformComponent>();
25 this->addTagComponent<LayerComponent<0>>();
34 template<
typename T,
typename... Args>
35 T& addComponent(Args&& ...args) {
36 runtime_assert(!this->hasComponent<T>(),
"Entity already has this component!");
37 auto& component = this->scene->getRegistry().emplace<T>(this->handle, std::forward<Args>(args)...);
38 if constexpr (CComponentHasTransform<T>) {
39 component.transform = &this->getComponent<TransformComponent>();
45 void addTagComponent() {
46 runtime_assert(!this->hasComponent<T>(),
"Entity already has this component!");
47 this->scene->getRegistry().emplace<T>(this->handle);
51 [[nodiscard]] T& getComponent() {
52 runtime_assert(this->hasComponent<T>(),
"Entity doesn't have this component!");
53 return this->scene->getRegistry().get<T>(this->handle);
57 [[nodiscard]] T& getComponent()
const {
58 runtime_assert(this->hasComponent<T>(),
"Entity doesn't have this component!");
59 return this->scene->getRegistry().get<T>(this->handle);
63 [[nodiscard]] T* tryGetComponent()
const {
64 return this->scene->getRegistry().try_get<T>(this->handle);
68 [[nodiscard]]
bool hasComponent()
const {
69 return this->scene->getRegistry().all_of<T>(this->handle);
73 void tryRemoveComponent() {
74 this->scene->getRegistry().remove<T>(this->handle);
78 void removeComponent() {
79 runtime_assert(this->hasComponent<T>(),
"Entity doesn't have this component!");
80 this->scene->getRegistry().erase<T>(this->handle);
83 [[nodiscard]]
bool getVisible()
const {
84 return !this->hasComponent<NoRenderTagComponent>();
87 void setVisible(
bool visible) {
89 this->tryRemoveComponent<NoRenderTagComponent>();
90 }
else if (!this->hasComponent<NoRenderTagComponent>()) {
91 this->addTagComponent<NoRenderTagComponent>();
95 [[nodiscard]] std::string getName()
const {
96 if (
auto nameComponent = this->tryGetComponent<NameComponent>()) {
97 return nameComponent->name;
99 return uuids::to_string(this->getComponent<UUIDComponent>().uuid);
110 uuids::uuid getUUID() {
111 return this->getComponent<UUIDComponent>().uuid;
114 uuids::uuid getSceneUUID() {
115 return this->scene->getUUID();
118 [[nodiscard]] entt::entity getRawHandle()
const {
122 explicit operator bool()
const {
123 return this->scene->getRegistry().valid(this->handle);
126 bool operator!()
const {
127 return !
static_cast<bool>(*this);
130 bool operator==(
const Entity& other)
const {
131 return this->handle == other.handle && this->scene == other.scene;
134 bool operator!=(
const Entity& other)
const {
135 return !(*
this == other);
139 Scene* scene =
nullptr;
140 entt::entity handle = entt::null;