diff --git a/CInterface/Battling/EventData.cpp b/CInterface/Battling/EventData.cpp index 99f16a0..161c465 100644 --- a/CInterface/Battling/EventData.cpp +++ b/CInterface/Battling/EventData.cpp @@ -1,4 +1,4 @@ -#include "../../src/Battling/EventHooks/EventData.hpp" +#include "../../src/Battling/EventHooks/EventDataClasses.hpp" #include "../Core.hpp" using namespace CreatureLib::Battling; diff --git a/src/Battling/EventHooks/EventData.hpp b/src/Battling/EventHooks/EventData.hpp deleted file mode 100644 index 4f2b529..0000000 --- a/src/Battling/EventHooks/EventData.hpp +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef CREATURELIB_EVENTDATA_HPP -#define CREATURELIB_EVENTDATA_HPP - -#include -#include "../Models/CreatureIndex.hpp" -#include "../Models/DamageSource.hpp" -#include "EventDataKind.hpp" - -namespace CreatureLib::Battling { - // Predeclare some classes. - class Creature; - - class EventData { - public: - virtual ~EventData() = default; - virtual EventDataKind GetKind() const noexcept = 0; - }; - - class DamageEvent : public EventData { - ArbUt::BorrowedPtr _creature; - DamageSource _damageSource; - uint32_t _originalHealth; - uint32_t _newHealth; - - public: - DamageEvent(Creature* c, DamageSource s, uint32_t oHealth, uint32_t newHealth) noexcept - : _creature(c), _damageSource(s), _originalHealth(oHealth), _newHealth(newHealth) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::Damage; } - const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } - DamageSource GetDamageSource() const noexcept { return _damageSource; } - uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } - uint32_t GetNewHealth() const noexcept { return _newHealth; } - }; - - class HealEvent : public EventData { - ArbUt::BorrowedPtr _creature; - uint32_t _originalHealth; - uint32_t _newHealth; - - public: - HealEvent(ArbUt::BorrowedPtr c, uint32_t oHealth, uint32_t newHealth) noexcept - : _creature(c), _originalHealth(oHealth), _newHealth(newHealth) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::Heal; } - const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } - uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } - uint32_t GetNewHealth() const noexcept { return _newHealth; } - }; - - class FaintEvent : public EventData { - ArbUt::BorrowedPtr _creature; - - public: - FaintEvent(ArbUt::BorrowedPtr c) noexcept : _creature(c) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; } - const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } - }; - - class SwitchEvent : public EventData { - CreatureIndex _index; - ArbUt::BorrowedPtr _newCreature; - - public: - SwitchEvent(const CreatureIndex& index, const ArbUt::BorrowedPtr& newCreature) - : _index(index), _newCreature(newCreature) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::Switch; } - const CreatureIndex& GetIndex() const noexcept { return _index; } - const ArbUt::BorrowedPtr& GetNewCreature() const noexcept { return _newCreature; } - }; - - class TurnStartEvent : public EventData { - public: - TurnStartEvent() {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::TurnStart; } - }; - - class TurnEndEvent : public EventData { - public: - TurnEndEvent() {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::TurnEnd; } - }; - - class ExperienceGainEvent : public EventData { - ArbUt::BorrowedPtr _creature; - uint32_t _previousExperience; - uint32_t _newExperience; - - public: - ExperienceGainEvent(const ArbUt::BorrowedPtr& creature, uint32_t previousExp, uint32_t newExp) - : _creature(creature), _previousExperience(previousExp), _newExperience(newExp) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::ExperienceGain; } - const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } - uint32_t GetPreviousExperience() const noexcept { return _previousExperience; } - uint32_t GetNewExperience() const noexcept { return _newExperience; } - }; - - class DisplayTextEvent : public EventData { - const ArbUt::StringView _text; - - public: - DisplayTextEvent(const ArbUt::StringView& text) noexcept : _text(text) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::DisplayText; } - const ArbUt::StringView& GetText() const noexcept { return _text; } - }; - - class MissEvent : public EventData { - ArbUt::BorrowedPtr _creature; - - public: - MissEvent(const ArbUt::BorrowedPtr& creature) noexcept : _creature(creature) {} - EventDataKind GetKind() const noexcept override { return EventDataKind ::Miss; } - const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } - }; - -} - -#endif // CREATURELIB_EVENTDATA_HPP diff --git a/src/Battling/EventHooks/EventDataClasses.hpp b/src/Battling/EventHooks/EventDataClasses.hpp new file mode 100644 index 0000000..7d433e1 --- /dev/null +++ b/src/Battling/EventHooks/EventDataClasses.hpp @@ -0,0 +1,16 @@ +#ifndef CREATURELIB_EVENTDATACLASSES_HPP +#define CREATURELIB_EVENTDATACLASSES_HPP + +#include "Events/ChangeSpeciesEvent.hpp" +#include "Events/ChangeVariantEvent.hpp" +#include "Events/DamageEvent.hpp" +#include "Events/DisplayTextEvent.hpp" +#include "Events/EventData.hpp" +#include "Events/ExperienceGainEvent.hpp" +#include "Events/FaintEvent.hpp" +#include "Events/HealEvent.hpp" +#include "Events/MissEvent.hpp" +#include "Events/SwitchEvent.hpp" +#include "Events/TurnEvents.hpp" + +#endif // CREATURELIB_EVENTDATACLASSES_HPP diff --git a/src/Battling/EventHooks/EventDataKind.hpp b/src/Battling/EventHooks/EventDataKind.hpp index d0e662e..56803d2 100644 --- a/src/Battling/EventHooks/EventDataKind.hpp +++ b/src/Battling/EventHooks/EventDataKind.hpp @@ -3,7 +3,8 @@ #include namespace CreatureLib::Battling { - ENUM(EventDataKind, uint8_t, Damage, Heal, Faint, Switch, TurnStart, TurnEnd, ExperienceGain, Miss, DisplayText) + ENUM(EventDataKind, uint8_t, Damage, Heal, Faint, Switch, TurnStart, TurnEnd, ExperienceGain, Miss, DisplayText, + ChangeSpecies, ChangeVariant) } #endif // CREATURELIB_EVENTDATAKIND_HPP diff --git a/src/Battling/EventHooks/EventHook.hpp b/src/Battling/EventHooks/EventHook.hpp index 71d171e..5edfb6d 100644 --- a/src/Battling/EventHooks/EventHook.hpp +++ b/src/Battling/EventHooks/EventHook.hpp @@ -5,7 +5,7 @@ #include #include #include "../../Library/Exceptions/CreatureException.hpp" -#include "EventData.hpp" +#include "Events/EventData.hpp" namespace CreatureLib::Battling { /// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces diff --git a/src/Battling/EventHooks/Events/ChangeSpeciesEvent.hpp b/src/Battling/EventHooks/Events/ChangeSpeciesEvent.hpp new file mode 100644 index 0000000..e004279 --- /dev/null +++ b/src/Battling/EventHooks/Events/ChangeSpeciesEvent.hpp @@ -0,0 +1,22 @@ +#ifndef CREATURELIB_CHANGESPECIESEVENT_HPP +#define CREATURELIB_CHANGESPECIESEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class ChangeSpeciesEvent : public EventData { + const ArbUt::BorrowedPtr _creature; + const ArbUt::BorrowedPtr _newSpecies; + + public: + ChangeSpeciesEvent(const ArbUt::BorrowedPtr& creature, + const ArbUt::BorrowedPtr& species) noexcept + : _creature(creature), _newSpecies(species) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::ChangeSpecies; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + const ArbUt::BorrowedPtr& GetNewSpecies() const noexcept { + return _newSpecies; + } + }; +} + +#endif // CREATURELIB_CHANGESPECIESEVENT_HPP diff --git a/src/Battling/EventHooks/Events/ChangeVariantEvent.hpp b/src/Battling/EventHooks/Events/ChangeVariantEvent.hpp new file mode 100644 index 0000000..dfbac21 --- /dev/null +++ b/src/Battling/EventHooks/Events/ChangeVariantEvent.hpp @@ -0,0 +1,22 @@ +#ifndef CREATURELIB_CHANGEVARIANTEVENT_HPP +#define CREATURELIB_CHANGEVARIANTEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class ChangeVariantEvent : public EventData { + const ArbUt::BorrowedPtr _creature; + const ArbUt::BorrowedPtr _newVariant; + + public: + ChangeVariantEvent(const ArbUt::BorrowedPtr& creature, + const ArbUt::BorrowedPtr& variant) noexcept + : _creature(creature), _newVariant(variant) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::ChangeVariant; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + const ArbUt::BorrowedPtr& GetNewVariant() const noexcept { + return _newVariant; + } + }; +} + +#endif // CREATURELIB_CHANGEVARIANTEVENT_HPP diff --git a/src/Battling/EventHooks/Events/DamageEvent.hpp b/src/Battling/EventHooks/Events/DamageEvent.hpp new file mode 100644 index 0000000..b583f38 --- /dev/null +++ b/src/Battling/EventHooks/Events/DamageEvent.hpp @@ -0,0 +1,23 @@ +#ifndef CREATURELIB_DAMAGEEVENT_HPP +#define CREATURELIB_DAMAGEEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class DamageEvent : public EventData { + ArbUt::BorrowedPtr _creature; + DamageSource _damageSource; + uint32_t _originalHealth; + uint32_t _newHealth; + + public: + DamageEvent(Creature* c, DamageSource s, uint32_t oHealth, uint32_t newHealth) noexcept + : _creature(c), _damageSource(s), _originalHealth(oHealth), _newHealth(newHealth) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::Damage; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + DamageSource GetDamageSource() const noexcept { return _damageSource; } + uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } + uint32_t GetNewHealth() const noexcept { return _newHealth; } + }; +} + +#endif // CREATURELIB_DAMAGEEVENT_HPP diff --git a/src/Battling/EventHooks/Events/DisplayTextEvent.hpp b/src/Battling/EventHooks/Events/DisplayTextEvent.hpp new file mode 100644 index 0000000..9beb9ae --- /dev/null +++ b/src/Battling/EventHooks/Events/DisplayTextEvent.hpp @@ -0,0 +1,16 @@ +#ifndef CREATURELIB_DISPLAYTEXTEVENT_HPP +#define CREATURELIB_DISPLAYTEXTEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class DisplayTextEvent : public EventData { + const ArbUt::StringView _text; + + public: + DisplayTextEvent(const ArbUt::StringView& text) noexcept : _text(text) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::DisplayText; } + const ArbUt::StringView& GetText() const noexcept { return _text; } + }; +} + +#endif // CREATURELIB_DISPLAYTEXTEVENT_HPP diff --git a/src/Battling/EventHooks/Events/EventData.hpp b/src/Battling/EventHooks/Events/EventData.hpp new file mode 100644 index 0000000..3a77c31 --- /dev/null +++ b/src/Battling/EventHooks/Events/EventData.hpp @@ -0,0 +1,21 @@ +#ifndef CREATURELIB_EVENTDATA_HPP +#define CREATURELIB_EVENTDATA_HPP + +#include +#include "../../Models/CreatureIndex.hpp" +#include "../../Models/DamageSource.hpp" +#include "../EventDataKind.hpp" + +namespace CreatureLib::Battling { + // Predeclare some classes. + class Creature; + + class EventData { + public: + virtual ~EventData() = default; + virtual EventDataKind GetKind() const noexcept = 0; + }; + +} + +#endif // CREATURELIB_EVENTDATA_HPP diff --git a/src/Battling/EventHooks/Events/ExperienceGainEvent.hpp b/src/Battling/EventHooks/Events/ExperienceGainEvent.hpp new file mode 100644 index 0000000..cf9daf9 --- /dev/null +++ b/src/Battling/EventHooks/Events/ExperienceGainEvent.hpp @@ -0,0 +1,21 @@ +#ifndef CREATURELIB_EXPERIENCEGAINEVENT_HPP +#define CREATURELIB_EXPERIENCEGAINEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class ExperienceGainEvent : public EventData { + ArbUt::BorrowedPtr _creature; + uint32_t _previousExperience; + uint32_t _newExperience; + + public: + ExperienceGainEvent(const ArbUt::BorrowedPtr& creature, uint32_t previousExp, uint32_t newExp) + : _creature(creature), _previousExperience(previousExp), _newExperience(newExp) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::ExperienceGain; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + uint32_t GetPreviousExperience() const noexcept { return _previousExperience; } + uint32_t GetNewExperience() const noexcept { return _newExperience; } + }; +} + +#endif // CREATURELIB_EXPERIENCEGAINEVENT_HPP diff --git a/src/Battling/EventHooks/Events/FaintEvent.hpp b/src/Battling/EventHooks/Events/FaintEvent.hpp new file mode 100644 index 0000000..02152f2 --- /dev/null +++ b/src/Battling/EventHooks/Events/FaintEvent.hpp @@ -0,0 +1,16 @@ +#ifndef CREATURELIB_FAINTEVENT_HPP +#define CREATURELIB_FAINTEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class FaintEvent : public EventData { + ArbUt::BorrowedPtr _creature; + + public: + FaintEvent(ArbUt::BorrowedPtr c) noexcept : _creature(c) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + }; +} + +#endif // CREATURELIB_FAINTEVENT_HPP diff --git a/src/Battling/EventHooks/Events/HealEvent.hpp b/src/Battling/EventHooks/Events/HealEvent.hpp new file mode 100644 index 0000000..e83e270 --- /dev/null +++ b/src/Battling/EventHooks/Events/HealEvent.hpp @@ -0,0 +1,21 @@ +#ifndef CREATURELIB_HEALEVENT_HPP +#define CREATURELIB_HEALEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class HealEvent : public EventData { + ArbUt::BorrowedPtr _creature; + uint32_t _originalHealth; + uint32_t _newHealth; + + public: + HealEvent(ArbUt::BorrowedPtr c, uint32_t oHealth, uint32_t newHealth) noexcept + : _creature(c), _originalHealth(oHealth), _newHealth(newHealth) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::Heal; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } + uint32_t GetNewHealth() const noexcept { return _newHealth; } + }; +} + +#endif // CREATURELIB_HEALEVENT_HPP diff --git a/src/Battling/EventHooks/Events/MissEvent.hpp b/src/Battling/EventHooks/Events/MissEvent.hpp new file mode 100644 index 0000000..4f0330e --- /dev/null +++ b/src/Battling/EventHooks/Events/MissEvent.hpp @@ -0,0 +1,15 @@ +#ifndef CREATURELIB_MISSEVENT_HPP +#define CREATURELIB_MISSEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class MissEvent : public EventData { + ArbUt::BorrowedPtr _creature; + + public: + explicit MissEvent(const ArbUt::BorrowedPtr& creature) noexcept : _creature(creature) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::Miss; } + const ArbUt::BorrowedPtr& GetCreature() const noexcept { return _creature; } + }; +} +#endif // CREATURELIB_MISSEVENT_HPP diff --git a/src/Battling/EventHooks/Events/SwitchEvent.hpp b/src/Battling/EventHooks/Events/SwitchEvent.hpp new file mode 100644 index 0000000..f40914c --- /dev/null +++ b/src/Battling/EventHooks/Events/SwitchEvent.hpp @@ -0,0 +1,19 @@ +#ifndef CREATURELIB_SWITCHEVENT_HPP +#define CREATURELIB_SWITCHEVENT_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class SwitchEvent : public EventData { + CreatureIndex _index; + ArbUt::BorrowedPtr _newCreature; + + public: + SwitchEvent(const CreatureIndex& index, const ArbUt::BorrowedPtr& newCreature) + : _index(index), _newCreature(newCreature) {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::Switch; } + const CreatureIndex& GetIndex() const noexcept { return _index; } + const ArbUt::BorrowedPtr& GetNewCreature() const noexcept { return _newCreature; } + }; +} + +#endif // CREATURELIB_SWITCHEVENT_HPP diff --git a/src/Battling/EventHooks/Events/TurnEvents.hpp b/src/Battling/EventHooks/Events/TurnEvents.hpp new file mode 100644 index 0000000..cc87d88 --- /dev/null +++ b/src/Battling/EventHooks/Events/TurnEvents.hpp @@ -0,0 +1,20 @@ +#ifndef CREATURELIB_TURNEVENTS_HPP +#define CREATURELIB_TURNEVENTS_HPP +#include "EventData.hpp" + +namespace CreatureLib::Battling { + class TurnStartEvent : public EventData { + public: + TurnStartEvent() {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::TurnStart; } + }; + + class TurnEndEvent : public EventData { + public: + TurnEndEvent() {} + EventDataKind GetKind() const noexcept override { return EventDataKind ::TurnEnd; } + }; + +} + +#endif // CREATURELIB_TURNEVENTS_HPP diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 1c4d576..57e2058 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -2,6 +2,7 @@ #include #include #include "../../Library/Exceptions/NotImplementedException.hpp" +#include "../EventHooks/EventDataClasses.hpp" #include "../ScriptHandling/ScriptMacros.hpp" #include "ResolveTarget.hpp" diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index dfa8c9b..ee2e7c3 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -1,6 +1,7 @@ #include "Battle.hpp" #include #include +#include "../EventHooks/EventDataClasses.hpp" #include "../Flow/TurnHandler.hpp" #include "../Flow/TurnOrdering.hpp" diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 5f8e7e8..311525c 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -1,6 +1,6 @@ #include "BattleSide.hpp" #include -#include "../../Library/Exceptions/CreatureException.hpp" +#include "../EventHooks/EventDataClasses.hpp" #include "Battle.hpp" using namespace CreatureLib::Battling; diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index b404020..f50c3a2 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -1,6 +1,7 @@ #include "Creature.hpp" #include #include +#include "../EventHooks/EventDataClasses.hpp" #include "../Models/Battle.hpp" #include "../ScriptHandling/ScriptMacros.hpp" @@ -36,7 +37,6 @@ namespace CreatureLib::Battling { AssertNotNull(species); AssertNotNull(variant); _species = species; - ChangeVariant(variant); // If the creature is genderless, but it's new species is not, we want to set its gender if (_gender != CreatureLib::Library::Gender::Genderless && _species->GetGenderRate() != -1) { @@ -53,6 +53,10 @@ namespace CreatureLib::Battling { else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) { _gender = CreatureLib::Library::Gender::Genderless; } + if (_battle != nullptr) { + _battle->TriggerEventListener(this, _species); + } + ChangeVariant(variant); } void Creature::ChangeVariant(const ArbUt::BorrowedPtr& variant) { @@ -79,6 +83,10 @@ namespace CreatureLib::Battling { } // TODO: consider variant specific attacks? + + if (_battle != nullptr) { + _battle->TriggerEventListener(this, _variant); + } } void Creature::ChangeLevelBy(int8_t amount) { diff --git a/tests/BattleTests/EventHookTests.cpp b/tests/BattleTests/EventHookTests.cpp index 1568cfe..008dcb4 100644 --- a/tests/BattleTests/EventHookTests.cpp +++ b/tests/BattleTests/EventHookTests.cpp @@ -1,5 +1,6 @@ #ifdef TESTS_BUILD #include "../../extern/catch.hpp" +#include "../../src/Battling/EventHooks/EventDataClasses.hpp" #include "../../src/Battling/EventHooks/EventHook.hpp" #include "../../src/Battling/Models/CreateCreature.hpp" #include "../TestLibrary/TestLibrary.hpp"