From abccc23851f21a1b18e3a26279db1bd1d1b84fc0 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 16 Jan 2021 21:16:47 +0100 Subject: [PATCH] Trigger event when stat boost changes. Signed-off-by: Deukhoofd --- CInterface/Battling/EventData.cpp | 7 +++++ CInterface/Core.hpp | 1 - src/Battling/EventHooks/EventDataClasses.hpp | 1 + src/Battling/EventHooks/EventDataKind.hpp | 2 +- .../EventHooks/Events/ChangeStatEvent.hpp | 26 +++++++++++++++++++ src/Battling/Models/Creature.cpp | 5 ++++ 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/Battling/EventHooks/Events/ChangeStatEvent.hpp diff --git a/CInterface/Battling/EventData.cpp b/CInterface/Battling/EventData.cpp index 2ccab4e..442c2ee 100644 --- a/CInterface/Battling/EventData.cpp +++ b/CInterface/Battling/EventData.cpp @@ -50,3 +50,10 @@ DESTRUCTOR(AttackUseEvent); export const char* CreatureLib_DisplayTextEvent_GetText(const DisplayTextEvent* p) { return p->GetText().c_str(); } export void CreatureLib_DisplayTextEvent_Destruct(const DisplayTextEvent* p) { delete p; } + +BORROWED_GET_FUNC(ChangeStatBoostEvent, GetCreature, const Creature*); +SIMPLE_GET_FUNC(ChangeStatBoostEvent, GetStatistic, CreatureLib::Library::Statistic); +SIMPLE_GET_FUNC(ChangeStatBoostEvent, GetOldValue, int8_t); +SIMPLE_GET_FUNC(ChangeStatBoostEvent, GetNewValue, int8_t); + +DESTRUCTOR(ChangeStatBoostEvent); diff --git a/CInterface/Core.hpp b/CInterface/Core.hpp index 3a406ca..db2be55 100644 --- a/CInterface/Core.hpp +++ b/CInterface/Core.hpp @@ -9,7 +9,6 @@ #define export extern "C" [[maybe_unused]] #define CreatureLibException 2; -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) class ExceptionHandler { static std::string _creatureLibLastException; diff --git a/src/Battling/EventHooks/EventDataClasses.hpp b/src/Battling/EventHooks/EventDataClasses.hpp index 5f095e8..b465ecc 100644 --- a/src/Battling/EventHooks/EventDataClasses.hpp +++ b/src/Battling/EventHooks/EventDataClasses.hpp @@ -3,6 +3,7 @@ #include "Events/AttackUseEvent.hpp" #include "Events/ChangeSpeciesEvent.hpp" +#include "Events/ChangeStatEvent.hpp" #include "Events/ChangeVariantEvent.hpp" #include "Events/DamageEvent.hpp" #include "Events/DisplayTextEvent.hpp" diff --git a/src/Battling/EventHooks/EventDataKind.hpp b/src/Battling/EventHooks/EventDataKind.hpp index f9f01f1..74e2c83 100644 --- a/src/Battling/EventHooks/EventDataKind.hpp +++ b/src/Battling/EventHooks/EventDataKind.hpp @@ -3,7 +3,7 @@ namespace CreatureLib::Battling { ENUM(EventDataKind, uint8_t, Damage, Heal, Faint, Switch, TurnStart, TurnEnd, ExperienceGain, Miss, DisplayText, - ChangeSpecies, ChangeVariant, AttackUse) + ChangeSpecies, ChangeVariant, AttackUse, ChangeStatBoost) } #endif // CREATURELIB_EVENTDATAKIND_HPP diff --git a/src/Battling/EventHooks/Events/ChangeStatEvent.hpp b/src/Battling/EventHooks/Events/ChangeStatEvent.hpp new file mode 100644 index 0000000..bae4a78 --- /dev/null +++ b/src/Battling/EventHooks/Events/ChangeStatEvent.hpp @@ -0,0 +1,26 @@ +#ifndef CHANGESTATEVENT_HPP +#define CHANGESTATEVENT_HPP + +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class ChangeStatBoostEvent final : public EventData { + const ArbUt::BorrowedPtr _creature; + const CreatureLib::Library::Statistic _statistic; + const int8_t _oldValue; + const int8_t _newValue; + + public: + ChangeStatBoostEvent(const ArbUt::BorrowedPtr& creature, CreatureLib::Library::Statistic statistic, + int8_t oldValue, int8_t newValue) noexcept + : _creature(creature), _statistic(statistic), _oldValue(oldValue), _newValue(newValue) {} + + EventDataKind GetKind() const noexcept override { return EventDataKind ::ChangeStatBoost; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + CreatureLib::Library::Statistic GetStatistic() const noexcept { return _statistic; } + int8_t GetOldValue() const noexcept { return _oldValue; } + int8_t GetNewValue() const noexcept { return _newValue; } + }; +} + +#endif // CHANGESTATEVENT_HPP diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 9735f93..a8560a0 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -109,10 +109,15 @@ namespace CreatureLib::Battling { bool Creature::ChangeStatBoost(Library::Statistic stat, int8_t diffAmount) { bool changed = false; + auto oldValue = this->_statBoost.GetStat(stat); if (diffAmount > 0) changed = this->_statBoost.IncreaseStatBy(stat, diffAmount); else changed = this->_statBoost.DecreaseStatBy(stat, -diffAmount); + if (this->GetBattle().HasValue()) { + auto newValue = this->_statBoost.GetStat(stat); + this->GetBattle().GetValue()->TriggerEventListener(this, stat, oldValue, newValue); + } this->RecalculateBoostedStat(stat); return changed; }