switch to ECS architecture
This commit is contained in:
@@ -1,6 +1,15 @@
|
||||
#ifndef ENTITY_ADMIN_H
|
||||
#define ENTITY_ADMIN_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <QPoint>
|
||||
#include <QSize>
|
||||
#include <QVector2D>
|
||||
|
||||
#include "Tick.h"
|
||||
|
||||
#include "entt/entity/registry.hpp"
|
||||
|
||||
class EntityAdmin
|
||||
@@ -10,36 +19,84 @@ public:
|
||||
EntityAdmin(const EntityAdmin&) = delete;
|
||||
EntityAdmin& operator=(const EntityAdmin&) = delete;
|
||||
|
||||
// -- Queries / iteration ------------------------------------------------
|
||||
|
||||
template <typename... Ts, typename Func>
|
||||
void forEach(Func&& f);
|
||||
|
||||
template <typename... Ts, typename Func>
|
||||
void forEach(Func&& f) const;
|
||||
|
||||
template <typename... Ts>
|
||||
bool hasAll(entt::entity entity);
|
||||
|
||||
template <typename T>
|
||||
T& get(entt::entity entity);
|
||||
|
||||
bool isValid(entt::entity entity);
|
||||
template <typename T>
|
||||
const T& get(entt::entity entity) const;
|
||||
|
||||
bool isValid(entt::entity entity) const;
|
||||
void destroy(entt::entity entity);
|
||||
void clear();
|
||||
|
||||
/*
|
||||
factory methods (like spawnShip, spawnScrap, etc shall go here)
|
||||
*/
|
||||
// -- Public component attachment ----------------------------------------
|
||||
// Used by systems (e.g. ShipSystem) to attach optional components after
|
||||
// a factory method has created the base entity.
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void addComponent(entt::entity entity, Args&&... args);
|
||||
|
||||
template <typename T>
|
||||
void removeComponent(entt::entity entity);
|
||||
|
||||
// -- Factory methods ----------------------------------------------------
|
||||
|
||||
entt::entity spawnShip(QVector2D position, float hp, float maxHp,
|
||||
float maxSpeedPerTick, float mainAccelPerTick,
|
||||
float maneuveringAccelPerTick, float angularAccelPerTick,
|
||||
float maxRotationSpeedPerTick, float sensorRange,
|
||||
int level, const std::string& schematicId, bool isEnemy);
|
||||
|
||||
entt::entity spawnStation(QPoint anchor, QSize footprint,
|
||||
const std::vector<QPoint>& bodyCells,
|
||||
float hp, float maxHp, bool isEnemy);
|
||||
|
||||
entt::entity spawnScrap(QVector2D position, int amount, Tick despawnAt);
|
||||
|
||||
entt::entity spawnHqProxy(QVector2D position, float hp, float maxHp);
|
||||
|
||||
private:
|
||||
entt::entity createEntity();
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T& add(entt::entity entity, Args&&... args);
|
||||
void add(entt::entity entity, Args&&... args);
|
||||
|
||||
entt::registry m_registry;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Template implementations
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
template <typename... Ts, typename Func>
|
||||
void EntityAdmin::forEach(Func&& f)
|
||||
{
|
||||
m_registry.view<Ts...>().each(std::forward<Func>(f));
|
||||
// Avoid view.each() — MSVC 2017 ICEs on the extended_storage_iterator it instantiates.
|
||||
for (entt::entity entity : m_registry.view<Ts...>())
|
||||
{
|
||||
f(entity, m_registry.get<Ts>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Ts, typename Func>
|
||||
void EntityAdmin::forEach(Func&& f) const
|
||||
{
|
||||
entt::registry& reg = const_cast<entt::registry&>(m_registry);
|
||||
for (entt::entity entity : reg.view<Ts...>())
|
||||
{
|
||||
f(entity, reg.get<Ts>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
@@ -54,10 +111,28 @@ T& EntityAdmin::get(entt::entity entity)
|
||||
return m_registry.get<T>(entity);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T& EntityAdmin::add(entt::entity entity, Args&&... args)
|
||||
template <typename T>
|
||||
const T& EntityAdmin::get(entt::entity entity) const
|
||||
{
|
||||
return m_registry.emplace<T>(entity, std::forward<Args>(args)...);
|
||||
return m_registry.get<T>(entity);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void EntityAdmin::addComponent(entt::entity entity, Args&&... args)
|
||||
{
|
||||
m_registry.emplace<T>(entity, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void EntityAdmin::removeComponent(entt::entity entity)
|
||||
{
|
||||
m_registry.remove<T>(entity);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void EntityAdmin::add(entt::entity entity, Args&&... args)
|
||||
{
|
||||
m_registry.emplace<T>(entity, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
#endif // ENTITY_ADMIN_H
|
||||
|
||||
Reference in New Issue
Block a user