mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-05 02:49:57 +00:00
Adding an event system
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by Zoe Roux on 6/1/21.
|
||||
//
|
||||
|
||||
#include "EventSystem.hpp"
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
EventSystem::EventSystem()
|
||||
: WAL::System({})
|
||||
{}
|
||||
|
||||
void EventSystem::dispatchEvent(const std::function<void(WAL::Entity &)> &event)
|
||||
{
|
||||
this->_events.emplace_back(event);
|
||||
}
|
||||
|
||||
void EventSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds)
|
||||
{
|
||||
for (auto &event : this->_events)
|
||||
event(entity);
|
||||
}
|
||||
|
||||
void EventSystem::onSelfUpdate()
|
||||
{
|
||||
this->_events.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by Zoe Roux on 6/1/21.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <System/System.hpp>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
class EventSystem : public WAL::System
|
||||
{
|
||||
private:
|
||||
//! @brief The list of events that occurred in the last update.
|
||||
std::vector<std::function<void (WAL::Entity &)>> _events;
|
||||
public:
|
||||
//! @brief Inform the system that a new event has occurred and it should run the given method on every entities.
|
||||
void dispatchEvent(const std::function<void (WAL::Entity &)>& event);
|
||||
|
||||
//! @inherit
|
||||
void onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) override;
|
||||
//! @inherit
|
||||
void onSelfUpdate() override;
|
||||
|
||||
//! @brief A default constructor
|
||||
EventSystem();
|
||||
//! @brief An event system is copy constructable.
|
||||
EventSystem(const EventSystem &) = default;
|
||||
//! @brief A default destructor
|
||||
~EventSystem() override = default;
|
||||
//! @brief An event system is not assignable.
|
||||
EventSystem &operator=(const EventSystem &) = delete;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user