From 08120d54338eb8ad90188ec5eb158bf0253e18d6 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 9 Jul 2021 15:33:30 +0200 Subject: [PATCH] Adds status handling functions Signed-off-by: Deukhoofd --- CInterface/Battling/Creature.cpp | 6 ++++- src/Battling/EventHooks/EventDataClasses.hpp | 1 + src/Battling/EventHooks/EventDataKind.hpp | 2 +- .../EventHooks/Events/StatusChangeEvent.hpp | 22 +++++++++++++++++++ src/Battling/Models/Creature.cpp | 20 +++++++++++++++++ src/Battling/Models/Creature.hpp | 8 +++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/Battling/EventHooks/Events/StatusChangeEvent.hpp diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index d5ec1e0..cb41239 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -135,4 +135,8 @@ export uint8_t CreatureLib_Creature_AddAttack(Creature* p, LearnedAttack* attack export uint8_t CreatureLib_Creature_ReplaceAttack(Creature* p, size_t index, LearnedAttack* attack) { Try(p->ReplaceAttack(index, attack);) } -export uint8_t CreatureLib_Creature_SwapAttack(Creature* p, size_t a, size_t b) { Try(p->SwapAttacks(a, b);) } \ No newline at end of file +export uint8_t CreatureLib_Creature_SwapAttack(Creature* p, size_t a, size_t b) { Try(p->SwapAttacks(a, b);) } + +export u8 CreatureLib_Creature_SetStatus(Creature* p, const char* name) { Try(p->SetStatus(ArbUt::StringView(name))); }; +export u8 CreatureLib_Creature_ClearStatus(Creature* p) { Try(p->ClearStatus()); }; +export const char* PCreatureLib_Creature_GetStatusName(Creature* p) { return p->GetStatusName().c_str(); } diff --git a/src/Battling/EventHooks/EventDataClasses.hpp b/src/Battling/EventHooks/EventDataClasses.hpp index 8d9eeba..4880b67 100644 --- a/src/Battling/EventHooks/EventDataClasses.hpp +++ b/src/Battling/EventHooks/EventDataClasses.hpp @@ -13,6 +13,7 @@ #include "Events/FaintEvent.hpp" #include "Events/HealEvent.hpp" #include "Events/MissEvent.hpp" +#include "Events/StatusChangeEvent.hpp" #include "Events/SwapEvent.hpp" #include "Events/SwitchEvent.hpp" #include "Events/TurnEvents.hpp" diff --git a/src/Battling/EventHooks/EventDataKind.hpp b/src/Battling/EventHooks/EventDataKind.hpp index eb08e14..3390ba8 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, ChangeStatBoost, Fail, Swap) + ChangeSpecies, ChangeVariant, AttackUse, ChangeStatBoost, Fail, Swap, StatusChange) } #endif // CREATURELIB_EVENTDATAKIND_HPP diff --git a/src/Battling/EventHooks/Events/StatusChangeEvent.hpp b/src/Battling/EventHooks/Events/StatusChangeEvent.hpp new file mode 100644 index 0000000..f80d05d --- /dev/null +++ b/src/Battling/EventHooks/Events/StatusChangeEvent.hpp @@ -0,0 +1,22 @@ +#ifndef CREATURELIB_STATUSCHANGEEVENT_HPP +#define CREATURELIB_STATUSCHANGEEVENT_HPP + +namespace CreatureLib::Battling { + class StatusChangeEvent final : public CreatureLib::Battling::EventData { + ArbUt::BorrowedPtr _creature; + ArbUt::StringView _statusName; + + public: + explicit StatusChangeEvent(const ArbUt::BorrowedPtr& creature, const ArbUt::StringView& name) + : _creature(creature), _statusName(name) {} + + [[nodiscard]] CreatureLib::Battling::EventDataKind GetKind() const noexcept override { + return static_cast(EventDataKind::StatusChange); + } + + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + const ArbUt::StringView& GetStatusName() const noexcept { return _statusName; } + }; +} + +#endif // CREATURELIB_STATUSCHANGEEVENT_HPP diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 3de7220..aa8fc94 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -371,4 +371,24 @@ namespace CreatureLib::Battling { return c; } +} + +void CreatureLib::Battling::Creature::SetStatus(const ArbUt::StringView& name) { + if (_status != nullptr) { + _status->OnRemove(); + } + _status = std::unique_ptr(_library->LoadScript(ScriptCategory::Status, name)); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, name); + } +} +void CreatureLib::Battling::Creature::ClearStatus() { + if (_status == nullptr) { + return; + } + _status->OnRemove(); + _status = nullptr; + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, ""_cnc); + } } \ No newline at end of file diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 820add5..26ef4a2 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -179,6 +179,14 @@ namespace CreatureLib::Battling { void ReplaceAttack(size_t index, LearnedAttack* attack); void SwapAttacks(size_t a, size_t b) { _attacks.Swap(a, b); } + void SetStatus(const ArbUt::StringView& name); + void ClearStatus(); + const ArbUt::StringView& GetStatusName() noexcept { + if (_status == nullptr) + return ArbUt::StringView::EmptyString(); + return _status->GetName(); + } + // region Stat APIs bool ChangeStatBoost(Library::Statistic stat, int8_t diffAmount);