add EntityAdmin wrapper

This commit is contained in:
2026-05-22 06:34:57 +02:00
parent dc344df457
commit cc2cca2442
3 changed files with 188 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ SET(HDRS
${CMAKE_CURRENT_SOURCE_DIR}/EntityId.h
${CMAKE_CURRENT_SOURCE_DIR}/Rotation.h
${CMAKE_CURRENT_SOURCE_DIR}/BuildingType.h
${CMAKE_CURRENT_SOURCE_DIR}/EntityAdmin.h
${CMAKE_CURRENT_SOURCE_DIR}/Blueprint.h
${CMAKE_CURRENT_SOURCE_DIR}/ItemType.h
${CMAKE_CURRENT_SOURCE_DIR}/Item.h
@@ -17,6 +18,7 @@ SET(HDRS
SET(SRCS
${SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/BuildingType.cpp
${CMAKE_CURRENT_SOURCE_DIR}/EntityAdmin.cpp
PARENT_SCOPE
)

View File

@@ -0,0 +1,33 @@
#include "EntityAdmin.h"
std::shared_ptr<EntityAdmin> EntityAdmin::getInstance()
{
if (!s_instance)
{
s_instance = std::shared_ptr<EntityAdmin>(new EntityAdmin());
}
return s_instance;
}
std::shared_ptr<EntityAdmin> EntityAdmin::s_instance;
bool EntityAdmin::isValid(const entt::entity &entity)
{
return m_registry->valid(entity);
}
void EntityAdmin::clear()
{
m_registry->clear();
}
void EntityAdmin::destroy(entt::entity &entity)
{
m_registry->destroy(entity);
}
EntityAdmin::EntityAdmin()
: m_registry(std::make_shared<entt::registry>())
{
}

153
src/lib/core/EntityAdmin.h Normal file
View File

@@ -0,0 +1,153 @@
#ifndef ENTITY_ADMIN_H
#define ENTITY_ADMIN_H
#include "entt/entity/registry.hpp"
#include <QVector2D>
class EntityAdmin
{
public:
static std::shared_ptr<EntityAdmin> getInstance();
private:
static std::shared_ptr<EntityAdmin> s_instance;
public:
template <typename T1>
T1 &add(const entt::entity &entity);
void clear();
template <typename T1>
void forEach(std::function<void(entt::entity &pe, T1 &p1)> f);
template <typename T1, typename T2>
void forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2)> f);
template <typename T1, typename T2, typename T3>
void forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3)> f);
template <typename T1, typename T2, typename T3, typename T4>
void forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3, T4 &p4)> f);
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3, T4 &p4, T5 &p5)> f);
template <typename T1>
bool hasAll(const entt::entity &entity);
template <typename T1, typename T2>
bool hasAll(const entt::entity &entity);
template <typename T1, typename T2, typename T3>
bool hasAll(const entt::entity &entity);
template <typename T1, typename T2, typename T3, typename T4>
bool hasAll(const entt::entity &entity);
template <typename T>
T &get(const entt::entity &entity);
bool isValid(const entt::entity &entity);
/*
factory methods (like spawnShip, spawnScrap, etc shall go here)
*/
void destroy(entt::entity &entity);
private:
EntityAdmin();
EntityAdmin(const EntityAdmin&) = delete;
EntityAdmin& operator=(const EntityAdmin&) = delete;
std::shared_ptr<entt::registry> m_registry;
};
template <typename T1>
T1 &EntityAdmin::add(const entt::entity &entity)
{
return m_registry->emplace<T1>(entity);
}
template <typename T1>
void EntityAdmin::forEach(std::function<void(entt::entity &pe, T1 &p1)> f)
{
auto view = m_registry->view<T1>();
for (auto ae : view)
{
f(ae, view.get<T1>(ae));
}
}
template <typename T1, typename T2>
void EntityAdmin::forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2)> f)
{
auto view = m_registry->view<T1, T2>();
for (auto[ae, a1, a2] : view.each())
{
f(ae, a1, a2);
}
}
template <typename T1, typename T2, typename T3>
void EntityAdmin::forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3)> f)
{
auto view = m_registry->view<T1, T2, T3>();
for (auto[ae, a1, a2, a3] : view.each())
{
f(ae, a1, a2, a3);
}
}
template <typename T1, typename T2, typename T3, typename T4>
void EntityAdmin::forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3, T4 &p4)> f)
{
auto view = m_registry->view<T1, T2, T3, T4>();
for (auto[ae, a1, a2, a3, a4] : view.each())
{
f(ae, a1, a2, a3, a4);
}
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void EntityAdmin::forEach(std::function<void(entt::entity &pe, T1 &p1, T2 &p2, T3 &p3, T4 &p4, T5 &p5)> f)
{
auto view = m_registry->view<T1, T2, T3, T4, T5>();
for (auto[ae, a1, a2, a3, a4, a5] : view.each())
{
f(ae, a1, a2, a3, a4, a5);
}
}
template <typename T1>
bool EntityAdmin::hasAll(const entt::entity &entity)
{
if (m_registry->all_of<T1>(entity))
return true;
return false;
}
template <typename T1, typename T2>
bool EntityAdmin::hasAll(const entt::entity &entity)
{
if (m_registry->all_of<T1, T2>(entity))
return true;
return false;
}
template <typename T1, typename T2, typename T3>
bool EntityAdmin::hasAll(const entt::entity &entity)
{
if (m_registry->all_of<T1, T2, T3>(entity))
return true;
return false;
}
template <typename T1, typename T2, typename T3, typename T4>
bool EntityAdmin::hasAll(const entt::entity &entity)
{
if (m_registry->all_of<T1, T2, T3, T4>(entity))
return true;
return false;
}
template <typename T>
T &EntityAdmin::get(const entt::entity &entity)
{
return m_registry->get<T>(entity);
}
#endif // ENTITY_ADMIN_H