Initial support for Event Hooks.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
32
src/Battling/EventHooks/EventData.hpp
Normal file
32
src/Battling/EventHooks/EventData.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef CREATURELIB_EVENTDATA_HPP
|
||||
#define CREATURELIB_EVENTDATA_HPP
|
||||
|
||||
#include "../Models/DamageSource.hpp"
|
||||
#include "EventDataKind.hpp"
|
||||
namespace CreatureLib::Battling {
|
||||
// Predeclare some classes.
|
||||
class Creature;
|
||||
|
||||
class EventData {
|
||||
public:
|
||||
virtual EventDataKind GetKind() = 0;
|
||||
};
|
||||
|
||||
class DamageEvent : public EventData {
|
||||
Creature* _creature;
|
||||
DamageSource _damageSource;
|
||||
uint32_t _originalHealth;
|
||||
uint32_t _newHealth;
|
||||
|
||||
public:
|
||||
DamageEvent(Creature* c, DamageSource s, uint32_t oHealth, uint32_t newHealth)
|
||||
: _creature(c), _damageSource(s), _originalHealth(oHealth), _newHealth(newHealth) {}
|
||||
EventDataKind GetKind() override { return EventDataKind ::Damage; }
|
||||
Creature* GetCreature() const { return _creature; }
|
||||
DamageSource GetDamageSource() const { return _damageSource; }
|
||||
uint32_t GetOriginalHealth() const { return _originalHealth; }
|
||||
uint32_t GetNewHealth() const { return _newHealth; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CREATURELIB_EVENTDATA_HPP
|
||||
8
src/Battling/EventHooks/EventDataKind.hpp
Normal file
8
src/Battling/EventHooks/EventDataKind.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef CREATURELIB_EVENTDATAKIND_HPP
|
||||
#define CREATURELIB_EVENTDATAKIND_HPP
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
enum EventDataKind { Damage };
|
||||
}
|
||||
|
||||
#endif // CREATURELIB_EVENTDATAKIND_HPP
|
||||
1
src/Battling/EventHooks/EventHook.cpp
Normal file
1
src/Battling/EventHooks/EventHook.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "EventHook.hpp"
|
||||
25
src/Battling/EventHooks/EventHook.hpp
Normal file
25
src/Battling/EventHooks/EventHook.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef CREATURELIB_EVENTHOOK_HPP
|
||||
#define CREATURELIB_EVENTHOOK_HPP
|
||||
|
||||
#include <vector>
|
||||
#include "EventData.hpp"
|
||||
|
||||
#define EVENT_HOOK_FUNC(name) void (*name)(const EventData*)
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
/// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces
|
||||
/// for it.
|
||||
class EventHook {
|
||||
std::vector<EVENT_HOOK_FUNC()> _listeners;
|
||||
|
||||
public:
|
||||
void RegisterListener(EVENT_HOOK_FUNC(func)) { _listeners.push_back(func); }
|
||||
|
||||
void TriggerEvent(EventData* eventData) {
|
||||
for (auto listener : _listeners) {
|
||||
listener(eventData);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // CREATURELIB_EVENTHOOK_HPP
|
||||
Reference in New Issue
Block a user