Tweaks for EventHook.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
c3de280ebb
commit
fa5184ad77
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "../../Library/Exceptions/CreatureException.hpp"
|
||||||
#include "EventData.hpp"
|
#include "EventData.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace CreatureLib::Battling {
|
namespace CreatureLib::Battling {
|
||||||
/// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces
|
/// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces
|
||||||
/// for it.
|
/// for it.
|
||||||
|
@ -17,13 +17,20 @@ namespace CreatureLib::Battling {
|
||||||
std::vector<EventHookFunc> _listeners;
|
std::vector<EventHookFunc> _listeners;
|
||||||
size_t _position;
|
size_t _position;
|
||||||
size_t _capacity;
|
size_t _capacity;
|
||||||
uint8_t* _memory;
|
uint8_t* _memory = nullptr;
|
||||||
|
|
||||||
// Consider tweaking this size to be in line with the normal amount of events we use.
|
// Consider tweaking this size to be in line with the normal amount of events we use.
|
||||||
static const size_t defaultSize = 1024;
|
static const size_t defaultSize = 1024;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EventHook() : _position(0), _capacity(defaultSize), _memory(static_cast<uint8_t*>(malloc(defaultSize))) {}
|
EventHook() : _position(0), _capacity(defaultSize), _memory(static_cast<uint8_t*>(malloc(defaultSize))) {
|
||||||
|
if (_memory == nullptr) {
|
||||||
|
THROW_CREATURE("Out of memory.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EventHook(const EventHook&) = delete;
|
||||||
|
EventHook& operator=(const EventHook&) = delete;
|
||||||
|
|
||||||
~EventHook() { free(_memory); }
|
~EventHook() { free(_memory); }
|
||||||
|
|
||||||
size_t GetPosition() const noexcept { return _position; }
|
size_t GetPosition() const noexcept { return _position; }
|
||||||
|
@ -32,9 +39,12 @@ namespace CreatureLib::Battling {
|
||||||
template <class T, class... parameters> void Trigger(parameters... args) {
|
template <class T, class... parameters> void Trigger(parameters... args) {
|
||||||
if (_listeners.size() == 0)
|
if (_listeners.size() == 0)
|
||||||
return;
|
return;
|
||||||
if (_position + sizeof(T) > _capacity) {
|
if (_position + sizeof(T) >= _capacity) {
|
||||||
_capacity += defaultSize;
|
_capacity += defaultSize;
|
||||||
_memory = static_cast<uint8_t*>(realloc(_memory, _capacity));
|
_memory = static_cast<uint8_t*>(realloc(_memory, _capacity));
|
||||||
|
if (_memory == nullptr) {
|
||||||
|
THROW_CREATURE("Out of memory.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
uint8_t* ptr = _memory + _position;
|
uint8_t* ptr = _memory + _position;
|
||||||
T* event = new (ptr) T(args...);
|
T* event = new (ptr) T(args...);
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace CreatureLib::Battling {
|
||||||
std::unique_ptr<ChoiceQueue> _currentTurnQueue = nullptr;
|
std::unique_ptr<ChoiceQueue> _currentTurnQueue = nullptr;
|
||||||
bool _hasEnded = false;
|
bool _hasEnded = false;
|
||||||
BattleResult _battleResult = BattleResult::Empty();
|
BattleResult _battleResult = BattleResult::Empty();
|
||||||
EventHook _eventHook = EventHook();
|
EventHook _eventHook;
|
||||||
uint32_t _currentTurn = 0;
|
uint32_t _currentTurn = 0;
|
||||||
|
|
||||||
ScriptSet _volatile;
|
ScriptSet _volatile;
|
||||||
|
|
|
@ -27,4 +27,17 @@ TEST_CASE("Build and use event hook a lot", "[Battling]") {
|
||||||
REQUIRE(events.size() == 10000);
|
REQUIRE(events.size() == 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Build and use event hook with different types", "[Battling]") {
|
||||||
|
auto eventHook = EventHook();
|
||||||
|
std::vector<const EventData*> events;
|
||||||
|
eventHook.RegisterListener([&](const EventData* evt) mutable -> void { events.push_back(evt); });
|
||||||
|
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
|
||||||
|
eventHook.Trigger<FaintEvent>(nullptr);
|
||||||
|
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
|
||||||
|
eventHook.Trigger<FaintEvent>(nullptr);
|
||||||
|
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
|
||||||
|
eventHook.Trigger<FaintEvent>(nullptr);
|
||||||
|
eventHook.Trigger<DamageEvent>(nullptr, DamageSource::AttackDamage, 0, 0);
|
||||||
|
eventHook.Trigger<FaintEvent>(nullptr);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue