Add Signal to ECS
- Add Signal class to ECS used to send signals/events into systems
This commit is contained in:
@@ -184,10 +184,12 @@
|
||||
<ClCompile Include="src\copium\ecs\ECSManager.cpp" />
|
||||
<ClCompile Include="src\copium\ecs\Entity.cpp" />
|
||||
<ClCompile Include="src\copium\ecs\EntitySet.cpp" />
|
||||
<ClCompile Include="src\copium\ecs\Signal.cpp" />
|
||||
<ClCompile Include="src\copium\ecs\SystemOrderer.cpp" />
|
||||
<ClCompile Include="src\copium\ecs\SystemPool.cpp" />
|
||||
<ClCompile Include="src\copium\event\Event.cpp" />
|
||||
<ClCompile Include="src\copium\event\EventDispatcher.cpp" />
|
||||
<ClCompile Include="src\copium\event\EventSignal.cpp" />
|
||||
<ClCompile Include="src\copium\event\KeyPressEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\KeyReleaseEvent.cpp" />
|
||||
<ClCompile Include="src\copium\event\MouseMoveEvent.cpp" />
|
||||
@@ -252,6 +254,7 @@
|
||||
<ClInclude Include="src\copium\ecs\ECSManager.h" />
|
||||
<ClInclude Include="src\copium\ecs\Entity.h" />
|
||||
<ClInclude Include="src\copium\ecs\EntitySet.h" />
|
||||
<ClInclude Include="src\copium\ecs\Signal.h" />
|
||||
<ClInclude Include="src\copium\ecs\System.h" />
|
||||
<ClInclude Include="src\copium\ecs\SystemBase.h" />
|
||||
<ClInclude Include="src\copium\ecs\SystemOrderer.h" />
|
||||
@@ -260,6 +263,7 @@
|
||||
<ClInclude Include="src\copium\event\EventDispatcher.h" />
|
||||
<ClInclude Include="src\copium\event\EventHandler.h" />
|
||||
<ClInclude Include="src\copium\event\EventResult.h" />
|
||||
<ClInclude Include="src\copium\event\EventSignal.h" />
|
||||
<ClInclude Include="src\copium\event\EventType.h" />
|
||||
<ClInclude Include="src\copium\event\KeyPressEvent.h" />
|
||||
<ClInclude Include="src\copium\event\KeyReleaseEvent.h" />
|
||||
|
||||
@@ -210,6 +210,12 @@
|
||||
<ClCompile Include="src\copium\ecs\SystemOrderer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\event\EventSignal.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\copium\ecs\Signal.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
|
||||
@@ -452,5 +458,11 @@
|
||||
<ClInclude Include="src\copium\example\RenderSystem.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\ecs\Signal.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\copium\event\EventSignal.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -23,8 +23,9 @@ namespace Copium
|
||||
renderer = std::make_unique<Renderer>();
|
||||
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(descriptorPool, 1);
|
||||
ecs = std::make_unique<ECSManager>();
|
||||
ecs->AddSystem<FrameCountSystem>();
|
||||
ecs->AddSystem<RenderSystem>(renderer.get(), descriptorSetRenderer.get(), &commandBuffer); // better way to store the RenderSystem data?
|
||||
ecs->AddSystem<FrameCountSystem>().Before<FrameCountSystem>();
|
||||
ecs->AddSystem<MouseFollowSystem>();
|
||||
|
||||
// TODO: Load from scene file
|
||||
for (int y = 0; y < 10; y++)
|
||||
@@ -66,8 +67,7 @@ namespace Copium
|
||||
|
||||
EventResult Scene::OnEvent(const Event& event)
|
||||
{
|
||||
// ecs->UpdateEventSystems(event);
|
||||
MouseFollowSystem{ecs.get(), event}.Run(); // TODO: Remove when I figure out how to handle events in systems
|
||||
ecs->UpdateSystems(EventSignal{event});
|
||||
return EventResult::Continue;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "copium/ecs/Signal.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
int Signal::GetAllocatedId()
|
||||
{
|
||||
allocatedIds++;
|
||||
return allocatedIds;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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) {};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,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);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "copium/event/EventSignal.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
EventSignal::EventSignal(const Event& event)
|
||||
: event{event}
|
||||
{}
|
||||
|
||||
const Event& EventSignal::GetEvent() const
|
||||
{
|
||||
return event;
|
||||
}
|
||||
|
||||
CP_SIGNAL_DEFINITION(EventSignal);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/event/Event.h"
|
||||
#include "copium/ecs/Signal.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
|
||||
class EventSignal : public Signal
|
||||
{
|
||||
private:
|
||||
const Event& event;
|
||||
|
||||
public:
|
||||
EventSignal(const Event& event);
|
||||
|
||||
const Event& GetEvent() const;
|
||||
|
||||
CP_SIGNAL_DECLERATION(EventSignal);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "copium/core/Vulkan.h"
|
||||
#include "copium/ecs/System.h"
|
||||
#include "copium/event/Event.h"
|
||||
#include "copium/event/EventSignal.h"
|
||||
#include "copium/event/MouseMoveEvent.h"
|
||||
#include "copium/example/Components.h"
|
||||
|
||||
@@ -11,23 +12,19 @@ namespace Copium
|
||||
class MouseFollowSystem : public System<MouseFollowC, TransformC>
|
||||
{
|
||||
public:
|
||||
const Event& event;
|
||||
public:
|
||||
MouseFollowSystem(ECSManager* manager, const Event& event)
|
||||
: event{event}
|
||||
void RunEntity(const Signal& signal, Entity entity, MouseFollowC& mouseFollow, TransformC& transform)
|
||||
{
|
||||
System::manager = manager;
|
||||
}
|
||||
|
||||
void RunEntity(Entity entity, MouseFollowC& mouseFollow, TransformC& transform)
|
||||
{
|
||||
if (event.GetType() == EventType::MouseMove)
|
||||
if (signal.GetId() == EventSignal::GetIdStatic())
|
||||
{
|
||||
const MouseMoveEvent& mouseMoveEvent = static_cast<const MouseMoveEvent&>(event);
|
||||
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
|
||||
transform.position = {(mouseMoveEvent.GetPos().x / Vulkan::GetSwapChain().GetExtent().width - 0.5) * 2.0 * aspect,
|
||||
-(mouseMoveEvent.GetPos().y / Vulkan::GetSwapChain().GetExtent().height - 0.5) * 2.0};
|
||||
transform.position -= transform.size * glm::vec2{0.5f};
|
||||
const EventSignal& eventSignal = static_cast<const EventSignal&>(signal);
|
||||
if (eventSignal.GetEvent().GetType() == EventType::MouseMove)
|
||||
{
|
||||
const MouseMoveEvent& mouseMoveEvent = static_cast<const MouseMoveEvent&>(eventSignal.GetEvent());
|
||||
float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height;
|
||||
transform.position = {(mouseMoveEvent.GetPos().x / Vulkan::GetSwapChain().GetExtent().width - 0.5) * 2.0 * aspect,
|
||||
-(mouseMoveEvent.GetPos().y / Vulkan::GetSwapChain().GetExtent().height - 0.5) * 2.0};
|
||||
transform.position -= transform.size * glm::vec2{0.5f};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Copium
|
||||
class RenderSystem : public System<TransformC>
|
||||
{
|
||||
private:
|
||||
// Find better way to store these?
|
||||
Renderer* renderer;
|
||||
DescriptorSet* descriptorSet;
|
||||
CommandBuffer* commandBuffer;
|
||||
@@ -51,10 +52,6 @@ namespace Copium
|
||||
uniformBuffer.Set("view", glm::mat4(1));
|
||||
uniformBuffer.Update();
|
||||
|
||||
// Not sure how to put this in the ECSManager system handler
|
||||
// Potentially have the system take in the descriptorSetRenderer as constructor parameter
|
||||
// But not sure how commandBuffer can be added, since it can change each frame
|
||||
// Maybe introducing Resource concept to ECSManager?
|
||||
renderer->SetDescriptorSet(*descriptorSet);
|
||||
renderer->Begin(*commandBuffer);
|
||||
System::Run();
|
||||
|
||||
Reference in New Issue
Block a user