Initial support for Event Hooks.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-12-15 12:24:08 +01:00
parent 6ba708ad12
commit 410487c86b
6 changed files with 74 additions and 1 deletions

View 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

View File

@@ -0,0 +1,8 @@
#ifndef CREATURELIB_EVENTDATAKIND_HPP
#define CREATURELIB_EVENTDATAKIND_HPP
namespace CreatureLib::Battling {
enum EventDataKind { Damage };
}
#endif // CREATURELIB_EVENTDATAKIND_HPP

View File

@@ -0,0 +1 @@
#include "EventHook.hpp"

View 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