// // Created by Daniu // #ifndef EU07_ECWORLD_H #define EU07_ECWORLD_H #pragma once #include #include #include class ECWorld { public: ECWorld() = default; ~ECWorld() = default; entt::entity CreateEntity(); void DestroyEntity(entt::entity entity); void Clear(); bool IsAlive(entt::entity entity) const; entt::registry& Registry(); const entt::registry& Registry() const; template T& AddComponent(entt::entity entity, Args&&... args); template bool HasComponent(entt::entity entity) const; template T* GetComponent(entt::entity entity); template const T* GetComponent(entt::entity entity) const; template void RemoveComponent(entt::entity entity); entt::entity FindEntityByName(const std::string& name) const; template auto View() { return m_registry.view(); } template void Each(Func&& func) { auto view = m_registry.view(); for (auto entity : view) { auto components = std::forward_as_tuple(view.get(entity)...); std::apply([&](Components&... comps) { func(entity, comps...); }, components); } } std::vector GetEntities() const { std::vector entities; auto *storage = m_registry.storage(); for (auto entity : *storage) { entities.push_back(entity); } return entities; } std::size_t GetEntityCount() const { return m_registry.storage()->size(); } private: entt::registry m_registry; }; template T& ECWorld::AddComponent(entt::entity entity, Args&&... args) { static_assert(std::is_constructible_v, "Component must be constructible with provided arguments"); if (m_registry.all_of(entity)) { return m_registry.replace(entity, std::forward(args)...); } return m_registry.emplace(entity, std::forward(args)...); } template bool ECWorld::HasComponent(entt::entity entity) const { return m_registry.all_of(entity); } template T* ECWorld::GetComponent(entt::entity entity) { return m_registry.try_get(entity); } template const T* ECWorld::GetComponent(entt::entity entity) const { return m_registry.try_get(entity); } template void ECWorld::RemoveComponent(entt::entity entity) { if (m_registry.all_of(entity)) m_registry.remove(entity); } extern ECWorld& GetComponentSystem(); #define ECS GetComponentSystem() #endif //EU07_ECWORLD_H