From 5a976e40310a870787e13df344a0f01d129a2aa8 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 9 Mar 2020 17:43:36 +0100 Subject: [PATCH] Added display text event hook. --- CInterface/Battling/EventData.cpp | 2 ++ src/Battling/EventHooks/EventData.hpp | 9 +++++++++ src/Battling/EventHooks/EventDataKind.hpp | 2 +- src/Battling/EventHooks/EventHook.hpp | 2 +- src/Battling/Models/Battle.cpp | 1 + src/Battling/Models/Battle.hpp | 3 ++- 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CInterface/Battling/EventData.cpp b/CInterface/Battling/EventData.cpp index 5c8fc00..7074dd2 100644 --- a/CInterface/Battling/EventData.cpp +++ b/CInterface/Battling/EventData.cpp @@ -19,3 +19,5 @@ SIMPLE_GET_FUNC(HealEvent, GetOriginalHealth, uint32_t); SIMPLE_GET_FUNC(HealEvent, GetNewHealth, uint32_t); SIMPLE_GET_FUNC(FaintEvent, GetCreature, Creature*); + +export const char* CreatureLib_DisplayTextEvent_GetText(const DisplayTextEvent* p) { return p->GetText().c_str(); } diff --git a/src/Battling/EventHooks/EventData.hpp b/src/Battling/EventHooks/EventData.hpp index f2dc95e..1c7d4cd 100644 --- a/src/Battling/EventHooks/EventData.hpp +++ b/src/Battling/EventHooks/EventData.hpp @@ -52,6 +52,15 @@ namespace CreatureLib::Battling { Creature* GetCreature() const { return _creature; } }; + class DisplayTextEvent : public EventData { + const std::string _text; + + public: + DisplayTextEvent(const std::string& text) : _text(text) {} + EventDataKind GetKind() const override { return EventDataKind ::DisplayText; } + const std::string& GetText() const { return _text; } + }; + } #endif // CREATURELIB_EVENTDATA_HPP diff --git a/src/Battling/EventHooks/EventDataKind.hpp b/src/Battling/EventHooks/EventDataKind.hpp index 86a9a74..e45b1bb 100644 --- a/src/Battling/EventHooks/EventDataKind.hpp +++ b/src/Battling/EventHooks/EventDataKind.hpp @@ -3,7 +3,7 @@ #include namespace CreatureLib::Battling { - ENUM(EventDataKind, uint8_t, Damage, Heal, Faint) + ENUM(EventDataKind, uint8_t, Damage, Heal, Faint, DisplayText) } diff --git a/src/Battling/EventHooks/EventHook.hpp b/src/Battling/EventHooks/EventHook.hpp index 93717bc..6e0d940 100644 --- a/src/Battling/EventHooks/EventHook.hpp +++ b/src/Battling/EventHooks/EventHook.hpp @@ -15,7 +15,7 @@ namespace CreatureLib::Battling { public: void RegisterListener(EVENT_HOOK_FUNC(func)) { _listeners.push_back(func); } - void TriggerEvent(EventData* eventData) { + void TriggerEvent(EventData* eventData) const { for (auto listener : _listeners) { listener(eventData); } diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 931c81a..d13afe8 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -130,3 +130,4 @@ void Battle::AddVolatileScript(const ConstString& key) { } void Battle::AddVolatileScript(Script* script) { return _volatile.Add(script); } void Battle::RemoveVolatileScript(Script* script) { _volatile.Remove(script->GetName()); } +void Battle::DisplayText(const std::string& text) const { TriggerEventListener(new DisplayTextEvent(text)); } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 27b476a..296179b 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -89,8 +89,9 @@ namespace CreatureLib::Battling { bool HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); } bool HasVolatileScript(uint32_t keyHash) const { return _volatile.Has(keyHash); } + void DisplayText(const std::string& text) const; void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook.RegisterListener(listener); } - void TriggerEventListener(EventData* data) { this->_eventHook.TriggerEvent(data); } + void TriggerEventListener(EventData* data) const { this->_eventHook.TriggerEvent(data); } }; }