Add Signal to ECS

- Add Signal class to ECS used to send signals/events into systems
This commit is contained in:
Thraix
2023-05-22 12:52:15 +02:00
parent 0cd35928a7
commit cd4abe6007
15 changed files with 144 additions and 23 deletions
@@ -22,6 +22,13 @@ namespace Copium
systemPool->Update();
}
void ECSManager::UpdateSystems(const Signal& signal)
{
// TODO: Maybe we want a different pool for Signal based Systems for performance reasons?
// Maybe even a pool for each type of Signal?
systemPool->Update(signal);
}
size_t ECSManager::GetEntityCount() const
{
return entities.size();
+2
View File
@@ -3,6 +3,7 @@
#include "copium/ecs/ComponentPool.h"
#include "copium/ecs/Config.h"
#include "copium/ecs/Signal.h"
#include "copium/ecs/SystemPool.h"
#include "copium/util/Common.h"
@@ -33,6 +34,7 @@ namespace Copium
}
void UpdateSystems();
void UpdateSystems(const Signal& signal);
EntityId CreateEntity();
void DestroyEntity(EntityId entity);
+10
View File
@@ -0,0 +1,10 @@
#include "copium/ecs/Signal.h"
namespace Copium
{
int Signal::GetAllocatedId()
{
allocatedIds++;
return allocatedIds;
}
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#define CP_SIGNAL_DECLERATION(SignalClass) \
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(); \
}
namespace Copium
{
class Signal
{
private:
static inline int allocatedIds = 0;
public:
Signal() = default;
virtual ~Signal() = default;
virtual int GetId() const = 0;
protected:
static int GetAllocatedId();
};
}
+7 -1
View File
@@ -15,6 +15,12 @@ namespace Copium
manager->Each<Components...>([&](EntityId entityId, Components&... components) { RunEntity(Entity{manager, entityId}, components...); });
}
virtual void RunEntity(Entity entity, Components&... components) = 0;
void Run(const Signal& signal) override
{
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) {};
};
}
+3
View File
@@ -1,5 +1,7 @@
#pragma once
#include "copium/ecs/Signal.h"
namespace Copium
{
class ECSManager;
@@ -12,5 +14,6 @@ namespace Copium
public:
virtual void Run() = 0;
virtual void Run(const Signal& signal) = 0;
};
}
@@ -32,6 +32,14 @@ namespace Copium
}
}
void SystemPool::Update(const Signal& signal)
{
for (auto& system : systemOrder)
{
system->Run(signal);
}
}
void SystemPool::MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId)
{
auto it1 = systems.find(systemId);
+2
View File
@@ -2,6 +2,7 @@
#include "copium/ecs/SystemBase.h"
#include "copium/ecs/SystemOrderer.h"
#include "copium/ecs/Signal.h"
#include "copium/util/Common.h"
#include <vector>
@@ -25,6 +26,7 @@ namespace Copium
~SystemPool();
SystemOrderer AddSystem(const std::type_index& systemId, SystemBase* system);
void Update();
void Update(const Signal& signal);
void MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId);
void MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId);