// // Created by Hirek on 3/14/2026. // #ifndef EU07_ECS_H #define EU07_ECS_H #include "components/BasicComponents.h" #include "entt/entity/registry.hpp" #include #include #include #include class ECS final { private: entt::registry world_; // scene registry std::uint64_t nextId_ = 1; // 0 is invalid/none public: // // World lifecycle // /// /// Clears the world, removing all entities and components /// void ClearWorld(); // // Objects // /// /// Creates new object with basic components (Transform, Identification) and returns its entity handle. /// /// Entity handle of created object entt::entity CreateObject(); /// /// Destroys object with it's all components /// /// Entity to be removed void DestroyObject(entt::entity Entity); /// /// Checks if object with given entity handle exists in the registry /// /// Entity handle to check /// true if object exists, false otherwise bool ValidObject(entt::entity entity) const; // // Identification lookups // /// /// Finds object with given UID. Returns null entity if not found. /// /// UID of object entt::entity FindById(std::uint64_t id); /// /// Finds objects with given name. Returns empty vector if not found. /// /// Name of object /// May return more than 1 as many objects can have the same name std::vector FindByName(std::string_view name); // // Components // /// /// Adds component of type T to entity, forwarding provided arguments to component constructor. If component already exists, it will be replaced. /// /// Component type /// Entity to which component will be added /// Arguments forwarded to component constructor /// Reference to added component template T &AddComponent(entt::entity entity, Args &&...args) { return world_.emplace(entity, std::forward(args)...); } /// /// Returns entity's component /// /// type /// Entity to which component will be added /// Reference to added component template T &GetComponent(entt::entity entity) { return world_.get(entity); } /// /// Returns entity's component /// /// type /// Entity to which component will be added /// Reference to added component template const T &GetComponent(entt::entity entity) const { return world_.get(entity); } /// /// Tries to get component of type T from entity. Returns nullptr if component does not exist. /// /// Component type /// Entity from which component will be retrieved /// Pointer to component if exists, nullptr otherwise template T *TryGetComponent(entt::entity entity) { return world_.try_get(entity); } /// /// Tries to get component of type T from entity. Returns nullptr if component does not exist. /// /// Component type /// Entity from which component will be retrieved /// Pointer to component if exists, nullptr otherwise template const T *TryGetComponent(entt::entity entity) const { return world_.try_get(entity); } /// /// Checks if entity has component of type T. /// /// Component type /// Entity to check /// true if entity has component, false otherwise template bool HasComponent(entt::entity entity) const { return world_.all_of(entity); } /// /// Removes component of type T from entity. Does nothing if component does not exist. /// Component type /// Entity from which component will be removed template void RemoveComponent(entt::entity entity) { world_.remove(entity); } }; extern ECS& GetComponentSystem(); #define CS GetComponentSystem() #endif // EU07_ECS_H