add event system and use it to propagate to print traces
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
SET(HDRS)
|
||||
SET(SRCS)
|
||||
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(config)
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(ecs)
|
||||
add_subdirectory(eventsystem)
|
||||
add_subdirectory(utility)
|
||||
add_subdirectory(sim)
|
||||
add_subdirectory(ecs)
|
||||
|
||||
SET(HDRS
|
||||
${HDRS}
|
||||
|
||||
23
src/lib/eventsystem/CMakeLists.txt
Normal file
23
src/lib/eventsystem/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
add_subdirectory(event)
|
||||
|
||||
SET(HDRS
|
||||
${HDRS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Event.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventHandler.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventHandlerBase.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventManager.h
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
SET(SRCS
|
||||
${SRCS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventHandlerBase.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventManager.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
SET(LIB_INCLUDE_PATH
|
||||
${LIB_INCLUDE_PATH}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PARENT_SCOPE
|
||||
)
|
||||
10
src/lib/eventsystem/Event.h
Normal file
10
src/lib/eventsystem/Event.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef EVENT_H
|
||||
#define EVENT_H
|
||||
|
||||
class Event
|
||||
{
|
||||
public:
|
||||
virtual ~Event() = default;
|
||||
};
|
||||
|
||||
#endif // EVENT_H
|
||||
80
src/lib/eventsystem/EventHandler.h
Normal file
80
src/lib/eventsystem/EventHandler.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef EVENT_HANDLER_H
|
||||
#define EVENT_HANDLER_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "EventHandlerBase.h"
|
||||
#include "EventManager.h"
|
||||
|
||||
template <typename T>
|
||||
class EventHandler: public EventHandlerBase
|
||||
{
|
||||
public:
|
||||
void registerForEvent()
|
||||
{
|
||||
EventManager::getInstance()->registerEventHandler(this);
|
||||
}
|
||||
|
||||
void unregisterForEvent()
|
||||
{
|
||||
EventManager::getInstance()->unregisterEventHandler(this);
|
||||
}
|
||||
|
||||
void handleBaseEvent(std::shared_ptr<const Event> event) override
|
||||
{
|
||||
std::shared_ptr<const T> specificEvent = std::dynamic_pointer_cast<const T>(event);
|
||||
if (specificEvent)
|
||||
{
|
||||
handleEvent(specificEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void handleEvent(std::shared_ptr<const T> event) = 0;
|
||||
};
|
||||
|
||||
|
||||
template <typename... Ts> class CombinedEventHandlerHelper
|
||||
{
|
||||
protected:
|
||||
void registerForEventsHelper() {}
|
||||
void unregisterForEventsHelper() {}
|
||||
};
|
||||
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
class CombinedEventHandlerHelper<T, Ts...>
|
||||
: public EventHandler<T>
|
||||
, public CombinedEventHandlerHelper<Ts...>
|
||||
{
|
||||
protected:
|
||||
void registerForEventsHelper()
|
||||
{
|
||||
EventHandler<T>::registerForEvent();
|
||||
CombinedEventHandlerHelper<Ts...>::registerForEventsHelper();
|
||||
}
|
||||
|
||||
void unregisterForEventsHelper()
|
||||
{
|
||||
EventHandler<T>::unregisterForEvent();
|
||||
CombinedEventHandlerHelper<Ts...>::unregisterForEventsHelper();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename... Ts>
|
||||
class CombinedEventHandler: public CombinedEventHandlerHelper<Ts...>
|
||||
{
|
||||
protected:
|
||||
void registerForEvents()
|
||||
{
|
||||
CombinedEventHandlerHelper<Ts...>::registerForEventsHelper();
|
||||
}
|
||||
|
||||
void unregisterForEvents()
|
||||
{
|
||||
CombinedEventHandlerHelper<Ts...>::unregisterForEventsHelper();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // EVENT_HANDLER_H
|
||||
7
src/lib/eventsystem/EventHandlerBase.cpp
Normal file
7
src/lib/eventsystem/EventHandlerBase.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "EventHandlerBase.h"
|
||||
|
||||
unsigned int EventHandlerBase::s_nextId = 0;
|
||||
|
||||
EventHandlerBase::EventHandlerBase(): m_id(s_nextId++)
|
||||
{
|
||||
}
|
||||
25
src/lib/eventsystem/EventHandlerBase.h
Normal file
25
src/lib/eventsystem/EventHandlerBase.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef EVENT_HANDLER_BASE_H
|
||||
#define EVENT_HANDLER_BASE_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Event;
|
||||
class EventManager;
|
||||
|
||||
class EventHandlerBase
|
||||
{
|
||||
private:
|
||||
static unsigned int s_nextId;
|
||||
|
||||
public:
|
||||
EventHandlerBase();
|
||||
virtual ~EventHandlerBase() = default;
|
||||
virtual void handleBaseEvent(std::shared_ptr<const Event> event) = 0;
|
||||
|
||||
private:
|
||||
const unsigned int m_id;
|
||||
|
||||
friend EventManager;
|
||||
};
|
||||
|
||||
#endif // EVENT_HANDLER_BASE_H
|
||||
123
src/lib/eventsystem/EventManager.cpp
Normal file
123
src/lib/eventsystem/EventManager.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "EventManager.h"
|
||||
|
||||
#include <set>
|
||||
|
||||
#include "EventHandlerBase.h"
|
||||
|
||||
std::shared_ptr<EventManager> EventManager::getInstance()
|
||||
{
|
||||
if (!s_instance)
|
||||
{
|
||||
s_instance = std::shared_ptr<EventManager>(new EventManager());
|
||||
}
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void EventManager::destroyInstance()
|
||||
{
|
||||
if (s_instance)
|
||||
{
|
||||
s_instance.reset();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<EventManager> EventManager::s_instance = std::shared_ptr<EventManager>();
|
||||
|
||||
void EventManager::registerEventHandler(EventHandlerBase *eventHandler)
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
m_eventHandlers[eventHandler->m_id] = eventHandler;
|
||||
}
|
||||
|
||||
void EventManager::unregisterEventHandler(EventHandlerBase *eventHandler)
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
m_eventHandlers.erase(eventHandler->m_id);
|
||||
}
|
||||
|
||||
void EventManager::sendEventImmediately(std::shared_ptr<Event> event)
|
||||
{
|
||||
if (event)
|
||||
{
|
||||
std::set<unsigned int> eventHandlerIds;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
for (auto it : m_eventHandlers)
|
||||
{
|
||||
eventHandlerIds.insert(it.first);
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int id : eventHandlerIds)
|
||||
{
|
||||
// this is necessary to allow HandleBaseEvent() to remove event handlers without causing a crash
|
||||
EventHandlerBase *eventHandler = nullptr;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
auto it = m_eventHandlers.find(id);
|
||||
if (it != m_eventHandlers.end())
|
||||
{
|
||||
eventHandler = it->second;
|
||||
}
|
||||
}
|
||||
if (eventHandler)
|
||||
{
|
||||
eventHandler->handleBaseEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventManager::addEvent(std::shared_ptr<Event> event)
|
||||
{
|
||||
if (event)
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventsMutex);
|
||||
m_events.push_back(event);
|
||||
}
|
||||
}
|
||||
|
||||
void EventManager::processEvents()
|
||||
{
|
||||
std::vector<std::shared_ptr<Event>> events;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventsMutex);
|
||||
events = m_events;
|
||||
m_events.clear();
|
||||
}
|
||||
|
||||
std::set<unsigned int> eventHandlerIds;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
for (auto it : m_eventHandlers)
|
||||
{
|
||||
eventHandlerIds.insert(it.first);
|
||||
}
|
||||
}
|
||||
|
||||
for (std::shared_ptr<Event> event : events)
|
||||
{
|
||||
for (unsigned int id : eventHandlerIds)
|
||||
{
|
||||
// this is necessary to allow HandleBaseEvent() to remove event handlers without causing a crash
|
||||
EventHandlerBase *eventHandler = nullptr;
|
||||
{
|
||||
std::scoped_lock<std::mutex> lock(m_eventHandlersMutex);
|
||||
auto it = m_eventHandlers.find(id);
|
||||
if (it != m_eventHandlers.end())
|
||||
{
|
||||
eventHandler = it->second;
|
||||
}
|
||||
}
|
||||
if (eventHandler)
|
||||
{
|
||||
eventHandler->handleBaseEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool EventManager::hasEvents() const
|
||||
{
|
||||
return !m_events.empty();
|
||||
}
|
||||
43
src/lib/eventsystem/EventManager.h
Normal file
43
src/lib/eventsystem/EventManager.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef EVENT_MANAGER_H
|
||||
#define EVENT_MANAGER_H
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
class EventHandlerBase;
|
||||
|
||||
class EventManager
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<EventManager> getInstance();
|
||||
static void destroyInstance();
|
||||
|
||||
private:
|
||||
static std::shared_ptr<EventManager> s_instance;
|
||||
|
||||
public:
|
||||
EventManager(EventManager const &) = delete;
|
||||
void operator=(EventManager const &) = delete;
|
||||
|
||||
void registerEventHandler(EventHandlerBase *eventHandler);
|
||||
void unregisterEventHandler(EventHandlerBase *eventHandler);
|
||||
void sendEventImmediately(std::shared_ptr<Event> event);
|
||||
void addEvent(std::shared_ptr<Event> event);
|
||||
void processEvents();
|
||||
|
||||
bool hasEvents() const;
|
||||
|
||||
private:
|
||||
EventManager() = default;
|
||||
|
||||
std::map<unsigned int, EventHandlerBase *> m_eventHandlers;
|
||||
std::vector<std::shared_ptr<Event>> m_events;
|
||||
std::mutex m_eventHandlersMutex;
|
||||
std::mutex m_eventsMutex;
|
||||
};
|
||||
|
||||
#endif // EVENT_MANAGER_H
|
||||
17
src/lib/eventsystem/event/CMakeLists.txt
Normal file
17
src/lib/eventsystem/event/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
SET(HDRS
|
||||
${HDRS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TracePrintRequestedEvent.h
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
SET(SRCS
|
||||
${SRCS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TracePrintRequestedEvent.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
set(LIB_INCLUDE_PATH
|
||||
${LIB_INCLUDE_PATH}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PARENT_SCOPE
|
||||
)
|
||||
1
src/lib/eventsystem/event/TracePrintRequestedEvent.cpp
Normal file
1
src/lib/eventsystem/event/TracePrintRequestedEvent.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "TracePrintRequestedEvent.h"
|
||||
10
src/lib/eventsystem/event/TracePrintRequestedEvent.h
Normal file
10
src/lib/eventsystem/event/TracePrintRequestedEvent.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef TRACE_PRINT_REQUESTED_EVENT_H
|
||||
#define TRACE_PRINT_REQUESTED_EVENT_H
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
class TracePrintRequestedEvent : public Event
|
||||
{
|
||||
};
|
||||
|
||||
#endif // TRACE_PRINT_REQUESTED_EVENT_H
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "CombatSystem.h"
|
||||
#include "DynamicBodySystem.h"
|
||||
#include "FactionComponent.h"
|
||||
#include "EventManager.h"
|
||||
#include "HealthComponent.h"
|
||||
#include "ModuleOwnerComponent.h"
|
||||
#include "MovementIntentSystem.h"
|
||||
@@ -71,9 +72,13 @@ Simulation::Simulation(GameConfig config, unsigned int seed)
|
||||
}
|
||||
|
||||
placeInitialStructures();
|
||||
registerForEvents();
|
||||
}
|
||||
|
||||
Simulation::~Simulation() = default;
|
||||
Simulation::~Simulation()
|
||||
{
|
||||
unregisterForEvents();
|
||||
}
|
||||
|
||||
const GameConfig& Simulation::config() const
|
||||
{
|
||||
@@ -88,6 +93,7 @@ void Simulation::reset(GameConfig newConfig, unsigned int seed)
|
||||
|
||||
void Simulation::reset(unsigned int seed)
|
||||
{
|
||||
EventManager::destroyInstance();
|
||||
m_rng.seed(seed);
|
||||
m_currentTick = 0;
|
||||
m_nextDepartureTick = secondsToTicks(m_config.world.departureIntervalSeconds);
|
||||
@@ -147,6 +153,8 @@ void Simulation::reset(unsigned int seed)
|
||||
|
||||
void Simulation::tick()
|
||||
{
|
||||
EventManager::getInstance()->processEvents();
|
||||
|
||||
// Step 1: wave scheduler
|
||||
m_waveSystem->tickWaveScheduler(m_currentTick, *m_shipSystem,
|
||||
m_config.world.heightTiles);
|
||||
@@ -199,8 +207,6 @@ void Simulation::tick()
|
||||
m_scrapSystem->tickDespawn(m_currentTick);
|
||||
|
||||
++m_currentTick;
|
||||
|
||||
PRINT_TRACES();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -642,6 +648,11 @@ const EntityAdmin& Simulation::admin() const
|
||||
return m_admin;
|
||||
}
|
||||
|
||||
void Simulation::handleEvent(std::shared_ptr<const TracePrintRequestedEvent> event)
|
||||
{
|
||||
PRINT_TRACES();
|
||||
}
|
||||
|
||||
BuildingId Simulation::allocateBuildingId()
|
||||
{
|
||||
return m_nextBuildingId++;
|
||||
|
||||
@@ -10,15 +10,16 @@
|
||||
|
||||
#include "BeltSystem.h"
|
||||
#include "EntityAdmin.h"
|
||||
|
||||
#include "entt/entity/entity.hpp"
|
||||
#include "SchematicDropEvent.h"
|
||||
#include "BuildingType.h"
|
||||
#include "BuildingId.h"
|
||||
#include "EventHandler.h"
|
||||
#include "FireEvent.h"
|
||||
#include "GameConfig.h"
|
||||
#include "Rotation.h"
|
||||
#include "Tick.h"
|
||||
#include "TracePrintRequestedEvent.h"
|
||||
|
||||
class AiSystem;
|
||||
class BuildingSystem;
|
||||
@@ -29,7 +30,7 @@ class ShipSystem;
|
||||
class ScrapSystem;
|
||||
class WaveSystem;
|
||||
|
||||
class Simulation
|
||||
class Simulation: public CombinedEventHandler<TracePrintRequestedEvent>
|
||||
{
|
||||
public:
|
||||
explicit Simulation(GameConfig config, unsigned int seed = 0);
|
||||
@@ -83,6 +84,8 @@ public:
|
||||
const EntityAdmin& admin() const;
|
||||
|
||||
private:
|
||||
void handleEvent(std::shared_ptr<const TracePrintRequestedEvent> event) override;
|
||||
|
||||
BuildingId allocateBuildingId(); // Strictly increasing; never returns kInvalidBuildingId.
|
||||
|
||||
// Populate HQ, player defence stations, and the first enemy station set.
|
||||
|
||||
Reference in New Issue
Block a user