Format all files based on clang-format file
This commit is contained in:
@@ -12,10 +12,17 @@ namespace Copium
|
||||
public:
|
||||
using component_type = Component;
|
||||
friend class ECSManager;
|
||||
|
||||
protected:
|
||||
ECSManager* manager;
|
||||
|
||||
public:
|
||||
virtual void Added(EntityId entityId, Component& component) {}
|
||||
virtual void Removed(EntityId entityId, Component& component) {}
|
||||
virtual void Added(EntityId entityId, Component& component)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void Removed(EntityId entityId, Component& component)
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,30 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "copium/ecs/ComponentListener.h"
|
||||
#include "copium/ecs/ComponentPoolBase.h"
|
||||
#include "copium/ecs/Config.h"
|
||||
#include "copium/ecs/EntitySet.h"
|
||||
#include "copium/ecs/ComponentPoolBase.h"
|
||||
#include "copium/ecs/ComponentListener.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
template <typename Component>
|
||||
class ComponentPool : public ComponentPoolBase
|
||||
{
|
||||
using Iterator = typename std::vector<Component>::iterator;
|
||||
|
||||
private:
|
||||
std::vector<Component> components;
|
||||
ComponentListener<Component>* listener = nullptr;
|
||||
|
||||
public:
|
||||
ComponentPool()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
~ComponentPool() override
|
||||
{
|
||||
if(listener)
|
||||
if (listener)
|
||||
delete listener;
|
||||
}
|
||||
|
||||
@@ -37,7 +39,7 @@ namespace Copium
|
||||
{
|
||||
components.push_back(component);
|
||||
entities.Emplace(entity);
|
||||
if(listener)
|
||||
if (listener)
|
||||
listener->Added(entity, components.back());
|
||||
return components.back();
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "copium/ecs/Config.h"
|
||||
#include "copium/ecs/EntitySet.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class ComponentPoolBase
|
||||
{
|
||||
protected:
|
||||
EntitySet entities;
|
||||
|
||||
public:
|
||||
virtual ~ComponentPoolBase() = default;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace Copium
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace Copium
|
||||
{
|
||||
ECSManager::ECSManager()
|
||||
: systemPool{std::make_unique<SystemPool>(this)}
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
ECSManager::~ECSManager()
|
||||
{
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <typeindex>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "copium/ecs/ComponentPool.h"
|
||||
#include "copium/ecs/Config.h"
|
||||
@@ -7,11 +11,6 @@
|
||||
#include "copium/ecs/SystemPool.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <typeindex>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class ECSManager final
|
||||
@@ -22,6 +21,7 @@ namespace Copium
|
||||
|
||||
std::unique_ptr<SystemPool> systemPool;
|
||||
int currentEntityId = 1;
|
||||
|
||||
public:
|
||||
ECSManager();
|
||||
~ECSManager();
|
||||
@@ -85,7 +85,10 @@ namespace Copium
|
||||
|
||||
if (pool)
|
||||
{
|
||||
CP_ASSERT(!HasComponent<Component>(entity), "Component already exists in entity (entity=%u, Component=%s)", entity, typeid(Component).name());
|
||||
CP_ASSERT(!HasComponent<Component>(entity),
|
||||
"Component already exists in entity (entity=%u, Component=%s)",
|
||||
entity,
|
||||
typeid(Component).name());
|
||||
return pool->Emplace(entity, component);
|
||||
}
|
||||
else
|
||||
@@ -100,7 +103,10 @@ namespace Copium
|
||||
void RemoveComponent(EntityId entity)
|
||||
{
|
||||
auto pool = GetComponentPoolAssure<Component>();
|
||||
CP_ASSERT(pool->Erase(entity), "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
|
||||
CP_ASSERT(pool->Erase(entity),
|
||||
"Entity did not contain component (entity=%u, Component=%s)",
|
||||
entity,
|
||||
typeid(Component).name());
|
||||
}
|
||||
|
||||
template <typename... Components>
|
||||
@@ -114,7 +120,8 @@ namespace Copium
|
||||
{
|
||||
auto pool = GetComponentPoolAssure<Component>();
|
||||
Component* component = pool->FindComponent(entity);
|
||||
CP_ASSERT(component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
|
||||
CP_ASSERT(
|
||||
component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name());
|
||||
return *component;
|
||||
}
|
||||
|
||||
@@ -212,7 +219,8 @@ namespace Copium
|
||||
template <typename Component, typename... Components>
|
||||
EntityId Find()
|
||||
{
|
||||
return Find<Component, Components...>([] (EntityId, const Component& component, const Components&... components) { return true; });
|
||||
return Find<Component, Components...>([](EntityId, const Component& component, const Components&... components)
|
||||
{ return true; });
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -233,7 +241,9 @@ namespace Copium
|
||||
ComponentPool<Component>* GetComponentPoolAssure()
|
||||
{
|
||||
auto it = componentPool.find(GetComponentId<Component>());
|
||||
CP_ASSERT(it != componentPool.end(), "Component has not been added to an entity (Component=%s)", typeid(Component).name());
|
||||
CP_ASSERT(it != componentPool.end(),
|
||||
"Component has not been added to an entity (Component=%s)",
|
||||
typeid(Component).name());
|
||||
return static_cast<ComponentPool<Component>*>(it->second);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,16 +3,22 @@
|
||||
namespace Copium
|
||||
{
|
||||
Entity::Entity()
|
||||
: manager{nullptr}, id{INVALID_ENTITY}
|
||||
{}
|
||||
: manager{nullptr},
|
||||
id{INVALID_ENTITY}
|
||||
{
|
||||
}
|
||||
|
||||
Entity::Entity(ECSManager* manager)
|
||||
: manager{manager}, id{INVALID_ENTITY}
|
||||
{}
|
||||
: manager{manager},
|
||||
id{INVALID_ENTITY}
|
||||
{
|
||||
}
|
||||
|
||||
Entity::Entity(ECSManager* manager, EntityId id)
|
||||
: manager{manager}, id{id}
|
||||
{}
|
||||
: manager{manager},
|
||||
id{id}
|
||||
{
|
||||
}
|
||||
|
||||
Entity::operator EntityId() const
|
||||
{
|
||||
|
||||
@@ -55,9 +55,23 @@ namespace Copium
|
||||
return entitiesList.size();
|
||||
}
|
||||
|
||||
std::vector<EntityId>& EntitySet::GetList() { return entitiesList; }
|
||||
const std::vector<EntityId>& EntitySet::GetList() const { return entitiesList; }
|
||||
std::vector<EntityId>& EntitySet::GetList()
|
||||
{
|
||||
return entitiesList;
|
||||
}
|
||||
|
||||
std::vector<EntityId>::iterator EntitySet::begin() { return entitiesList.begin(); }
|
||||
std::vector<EntityId>::iterator EntitySet::end() { return entitiesList.end(); }
|
||||
const std::vector<EntityId>& EntitySet::GetList() const
|
||||
{
|
||||
return entitiesList;
|
||||
}
|
||||
|
||||
std::vector<EntityId>::iterator EntitySet::begin()
|
||||
{
|
||||
return entitiesList.begin();
|
||||
}
|
||||
|
||||
std::vector<EntityId>::iterator EntitySet::end()
|
||||
{
|
||||
return entitiesList.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/ecs/Config.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "copium/ecs/Config.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class EntitySet
|
||||
{
|
||||
private:
|
||||
std::vector<EntityId> entitiesList;
|
||||
std::unordered_map<EntityId, size_t> entitiesMap; // Maps the entity id to a component index
|
||||
std::unordered_map<EntityId, size_t> entitiesMap; // Maps the entity id to a component index
|
||||
public:
|
||||
bool Emplace(EntityId entity);
|
||||
bool Erase(EntityId entity);
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#define CP_SIGNAL_DECLERATION_DEFINITION() \
|
||||
static int GetIdStatic() \
|
||||
{ \
|
||||
static int id = GetAllocatedId(); \
|
||||
return id; \
|
||||
} \
|
||||
\
|
||||
int GetId() const \
|
||||
{ \
|
||||
return GetIdStatic(); \
|
||||
static int GetIdStatic() \
|
||||
{ \
|
||||
static int id = GetAllocatedId(); \
|
||||
return id; \
|
||||
} \
|
||||
\
|
||||
int GetId() const \
|
||||
{ \
|
||||
return GetIdStatic(); \
|
||||
}
|
||||
|
||||
#define CP_SIGNAL_DECLERATION(SignalClass) \
|
||||
static int GetIdStatic(); \
|
||||
static int GetIdStatic(); \
|
||||
int GetId() const override
|
||||
|
||||
#define CP_SIGNAL_DEFINITION(SignalClass) \
|
||||
int SignalClass::GetIdStatic() \
|
||||
{ \
|
||||
static int id = GetAllocatedId(); \
|
||||
return id; \
|
||||
} \
|
||||
\
|
||||
int SignalClass::GetId() const \
|
||||
{ \
|
||||
return GetIdStatic(); \
|
||||
int SignalClass::GetIdStatic() \
|
||||
{ \
|
||||
static int id = GetAllocatedId(); \
|
||||
return id; \
|
||||
} \
|
||||
\
|
||||
int SignalClass::GetId() const \
|
||||
{ \
|
||||
return GetIdStatic(); \
|
||||
}
|
||||
|
||||
namespace Copium
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/ecs/ECSManager.h"
|
||||
#include "copium/ecs/SystemBase.h"
|
||||
#include "copium/ecs/Entity.h"
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "copium/ecs/ECSManager.h"
|
||||
#include "copium/ecs/Entity.h"
|
||||
#include "copium/ecs/SystemBase.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
template <typename... Components>
|
||||
@@ -14,20 +14,22 @@ namespace Copium
|
||||
public:
|
||||
void Run() override
|
||||
{
|
||||
manager->Each<Components...>([&](EntityId entityId, Components&... components) { RunEntity(Entity{manager, entityId}, components...); });
|
||||
manager->Each<Components...>([&](EntityId entityId, Components&... components)
|
||||
{ RunEntity(Entity{manager, entityId}, components...); });
|
||||
}
|
||||
|
||||
void Run(const Signal& signal) override
|
||||
{
|
||||
manager->Each<Components...>([&](EntityId entityId, Components&... components) { RunEntity(signal, Entity{manager, entityId}, components...); });
|
||||
manager->Each<Components...>([&](EntityId entityId, Components&... components)
|
||||
{ RunEntity(signal, Entity{manager, entityId}, components...); });
|
||||
}
|
||||
|
||||
virtual void RunEntity(Entity entity, Components&... components) {};
|
||||
virtual void RunEntity(const Signal& signal, Entity entity, Components&... components) {};
|
||||
|
||||
|
||||
// TODO: Not sure if this is the way entities should be validated
|
||||
std::set<EntityId> loggedEntities;
|
||||
|
||||
bool ValidateEntity(Entity entity)
|
||||
{
|
||||
if (entity && entity.HasComponents<Components...>())
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace Copium
|
||||
class SystemBase
|
||||
{
|
||||
friend class SystemPool;
|
||||
|
||||
protected:
|
||||
ECSManager* manager;
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace Copium
|
||||
SystemOrderer::SystemOrderer(std::type_index systemId, SystemPool* systemPool)
|
||||
: systemId{systemId},
|
||||
systemPool{systemPool}
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
void SystemOrderer::Before(const std::type_index& otherSystemId)
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Copium
|
||||
private:
|
||||
std::type_index systemId;
|
||||
SystemPool* systemPool = nullptr;
|
||||
|
||||
public:
|
||||
SystemOrderer(std::type_index systemId, SystemPool* systemPool);
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace Copium
|
||||
{
|
||||
SystemPool::SystemPool(ECSManager* manager)
|
||||
: manager{manager}
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
SystemPool::~SystemPool()
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/ecs/SystemBase.h"
|
||||
#include "copium/ecs/SystemOrderer.h"
|
||||
#include "copium/ecs/Signal.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <typeindex>
|
||||
#include <vector>
|
||||
|
||||
#include "copium/ecs/Signal.h"
|
||||
#include "copium/ecs/SystemBase.h"
|
||||
#include "copium/ecs/SystemOrderer.h"
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
@@ -16,6 +16,7 @@ namespace Copium
|
||||
class SystemPool final
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(SystemPool);
|
||||
|
||||
private:
|
||||
ECSManager* manager;
|
||||
std::map<std::type_index, SystemBase*> systems;
|
||||
|
||||
Reference in New Issue
Block a user