Initial support for Event Hooks.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
6ba708ad12
commit
410487c86b
|
@ -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
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef CREATURELIB_EVENTDATAKIND_HPP
|
||||||
|
#define CREATURELIB_EVENTDATAKIND_HPP
|
||||||
|
|
||||||
|
namespace CreatureLib::Battling {
|
||||||
|
enum EventDataKind { Damage };
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CREATURELIB_EVENTDATAKIND_HPP
|
|
@ -0,0 +1 @@
|
||||||
|
#include "EventHook.hpp"
|
|
@ -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
|
|
@ -2,6 +2,7 @@
|
||||||
#define CREATURELIB_BATTLE_HPP
|
#define CREATURELIB_BATTLE_HPP
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "../EventHooks/EventHook.hpp"
|
||||||
#include "../Flow/ChoiceQueue.hpp"
|
#include "../Flow/ChoiceQueue.hpp"
|
||||||
#include "../Library/BattleLibrary.hpp"
|
#include "../Library/BattleLibrary.hpp"
|
||||||
#include "../TurnChoices/BaseTurnChoice.hpp"
|
#include "../TurnChoices/BaseTurnChoice.hpp"
|
||||||
|
@ -22,6 +23,7 @@ namespace CreatureLib::Battling {
|
||||||
ChoiceQueue* _currentTurnQueue = nullptr;
|
ChoiceQueue* _currentTurnQueue = nullptr;
|
||||||
bool _hasEnded = false;
|
bool _hasEnded = false;
|
||||||
BattleResult _battleResult = BattleResult::Empty();
|
BattleResult _battleResult = BattleResult::Empty();
|
||||||
|
EventHook* _eventHook = new EventHook();
|
||||||
|
|
||||||
ScriptSet _volatile;
|
ScriptSet _volatile;
|
||||||
|
|
||||||
|
@ -71,6 +73,9 @@ namespace CreatureLib::Battling {
|
||||||
inline const BattleResult& GetResult() const { return _battleResult; }
|
inline const BattleResult& GetResult() const { return _battleResult; }
|
||||||
|
|
||||||
const std::vector<BattleSide*>& GetSides() const { return _sides; }
|
const std::vector<BattleSide*>& GetSides() const { return _sides; }
|
||||||
|
|
||||||
|
void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook->RegisterListener(listener); }
|
||||||
|
void TriggerEventListener(EventData* data) { this->_eventHook->TriggerEvent(data); }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,7 +107,9 @@ void Battling::Creature::Damage(uint32_t damage, Battling::DamageSource source)
|
||||||
damage = __CurrentHealth;
|
damage = __CurrentHealth;
|
||||||
}
|
}
|
||||||
// HOOK: On Damage
|
// HOOK: On Damage
|
||||||
__CurrentHealth -= damage;
|
auto newHealth = __CurrentHealth - damage;
|
||||||
|
this->GetBattle()->TriggerEventListener(new DamageEvent(this, source, __CurrentHealth, newHealth));
|
||||||
|
__CurrentHealth = newHealth;
|
||||||
|
|
||||||
if (IsFainted() && damage > 0) {
|
if (IsFainted() && damage > 0) {
|
||||||
OnFaint();
|
OnFaint();
|
||||||
|
|
Loading…
Reference in New Issue