From 5536a6b83d2cf172eb7353548d095c81be87cbfa Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 14 Aug 2020 14:00:41 +0200 Subject: [PATCH] Implements Attack Use event. Signed-off-by: Deukhoofd --- src/Battling/EventHooks/EventDataClasses.hpp | 1 + src/Battling/EventHooks/EventDataKind.hpp | 2 +- .../EventHooks/Events/AttackUseEvent.hpp | 16 +++++++++++++++ src/Battling/Flow/TurnHandler.cpp | 20 +++++++++++-------- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 src/Battling/EventHooks/Events/AttackUseEvent.hpp diff --git a/src/Battling/EventHooks/EventDataClasses.hpp b/src/Battling/EventHooks/EventDataClasses.hpp index 7d433e1..5f095e8 100644 --- a/src/Battling/EventHooks/EventDataClasses.hpp +++ b/src/Battling/EventHooks/EventDataClasses.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_EVENTDATACLASSES_HPP #define CREATURELIB_EVENTDATACLASSES_HPP +#include "Events/AttackUseEvent.hpp" #include "Events/ChangeSpeciesEvent.hpp" #include "Events/ChangeVariantEvent.hpp" #include "Events/DamageEvent.hpp" diff --git a/src/Battling/EventHooks/EventDataKind.hpp b/src/Battling/EventHooks/EventDataKind.hpp index 56803d2..1455b7f 100644 --- a/src/Battling/EventHooks/EventDataKind.hpp +++ b/src/Battling/EventHooks/EventDataKind.hpp @@ -4,7 +4,7 @@ namespace CreatureLib::Battling { ENUM(EventDataKind, uint8_t, Damage, Heal, Faint, Switch, TurnStart, TurnEnd, ExperienceGain, Miss, DisplayText, - ChangeSpecies, ChangeVariant) + ChangeSpecies, ChangeVariant, AttackUse) } #endif // CREATURELIB_EVENTDATAKIND_HPP diff --git a/src/Battling/EventHooks/Events/AttackUseEvent.hpp b/src/Battling/EventHooks/Events/AttackUseEvent.hpp new file mode 100644 index 0000000..ae5a8fe --- /dev/null +++ b/src/Battling/EventHooks/Events/AttackUseEvent.hpp @@ -0,0 +1,16 @@ +#ifndef CREATURELIB_ATTACKUSEEVENT_HPP +#define CREATURELIB_ATTACKUSEEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class AttackUseEvent : public EventData { + ArbUt::BorrowedPtr _attack; + + public: + AttackUseEvent(const ArbUt::BorrowedPtr& attack) noexcept : _attack(attack) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::AttackUse; } + const ArbUt::BorrowedPtr& GetAttack() const noexcept { return _attack; } + }; +} + +#endif // CREATURELIB_ATTACKUSEEVENT_HPP diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 57e2058..cb5803f 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -82,9 +82,9 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr choic TargetResolver::ResolveTargets(choice->GetTarget(), targetType, choice->GetUser()->GetBattle()); , "Exception during target determination"); - auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); + auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); bool prevented = false; - HOOK_LOCAL(PreventAttack, attack, &attack, &prevented); + HOOK(PreventAttack, attack, attack, &prevented); if (prevented) { return; } @@ -95,25 +95,29 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr choic return; } + attack->GetUser()->GetBattle()->TriggerEventListener(attack); + // HOOK: check if attack fails bool fail = false; - HOOK_LOCAL(FailAttack, attack, &attack, &fail); + HOOK(FailAttack, attack, attack, &fail); if (fail) { // TODO: Fail handling. return; } bool stopBeforeAttack = false; - HOOK_LOCAL(StopBeforeAttack, attack, &attack, &stopBeforeAttack); + HOOK(StopBeforeAttack, attack, attack, &stopBeforeAttack); if (stopBeforeAttack) { return; } - HOOK_LOCAL(OnBeforeAttack, attack, &attack); + HOOK(OnBeforeAttack, attack, attack); - for (uint8_t i = 0; i < attack.GetTargetCount(); i++) { - auto target = attack.GetTargets()[i]; - try_creature(HandleAttackForTarget(&attack, target), "Exception occurred during handling attack for target"); + for (uint8_t i = 0; i < attack->GetTargetCount(); i++) { + auto target = attack->GetTargets()[i]; + try_creature(HandleAttackForTarget(attack, target), "Exception occurred during handling attack for target"); } + + delete attack; } void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr& target) {