Cleaned up Events, added events on species and variant changes.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-14 13:52:22 +02:00
parent c484c376c3
commit 64b08d4ff0
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
21 changed files with 249 additions and 121 deletions

View File

@ -1,4 +1,4 @@
#include "../../src/Battling/EventHooks/EventData.hpp" #include "../../src/Battling/EventHooks/EventDataClasses.hpp"
#include "../Core.hpp" #include "../Core.hpp"
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;

View File

@ -1,116 +0,0 @@
#ifndef CREATURELIB_EVENTDATA_HPP
#define CREATURELIB_EVENTDATA_HPP
#include <Arbutils/Memory/BorrowedPtr.hpp>
#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> _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<Creature>& 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> _creature;
uint32_t _originalHealth;
uint32_t _newHealth;
public:
HealEvent(ArbUt::BorrowedPtr<Creature> 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<Creature>& 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> _creature;
public:
FaintEvent(ArbUt::BorrowedPtr<Creature> c) noexcept : _creature(c) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
};
class SwitchEvent : public EventData {
CreatureIndex _index;
ArbUt::BorrowedPtr<Creature> _newCreature;
public:
SwitchEvent(const CreatureIndex& index, const ArbUt::BorrowedPtr<Creature>& newCreature)
: _index(index), _newCreature(newCreature) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Switch; }
const CreatureIndex& GetIndex() const noexcept { return _index; }
const ArbUt::BorrowedPtr<Creature>& 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> _creature;
uint32_t _previousExperience;
uint32_t _newExperience;
public:
ExperienceGainEvent(const ArbUt::BorrowedPtr<Creature>& creature, uint32_t previousExp, uint32_t newExp)
: _creature(creature), _previousExperience(previousExp), _newExperience(newExp) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::ExperienceGain; }
const ArbUt::BorrowedPtr<Creature>& 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> _creature;
public:
MissEvent(const ArbUt::BorrowedPtr<Creature>& creature) noexcept : _creature(creature) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Miss; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
};
}
#endif // CREATURELIB_EVENTDATA_HPP

View File

@ -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

View File

@ -3,7 +3,8 @@
#include <Arbutils/Enum.hpp> #include <Arbutils/Enum.hpp>
namespace CreatureLib::Battling { 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 #endif // CREATURELIB_EVENTDATAKIND_HPP

View File

@ -5,7 +5,7 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include "../../Library/Exceptions/CreatureException.hpp" #include "../../Library/Exceptions/CreatureException.hpp"
#include "EventData.hpp" #include "Events/EventData.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
/// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces /// The Event Hook class allows users to write consumers for the battle events, for example to write User Interfaces

View File

@ -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> _creature;
const ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies> _newSpecies;
public:
ChangeSpeciesEvent(const ArbUt::BorrowedPtr<Creature>& creature,
const ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies>& species) noexcept
: _creature(creature), _newSpecies(species) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::ChangeSpecies; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
const ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies>& GetNewSpecies() const noexcept {
return _newSpecies;
}
};
}
#endif // CREATURELIB_CHANGESPECIESEVENT_HPP

View File

@ -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> _creature;
const ArbUt::BorrowedPtr<const CreatureLib::Library::SpeciesVariant> _newVariant;
public:
ChangeVariantEvent(const ArbUt::BorrowedPtr<Creature>& creature,
const ArbUt::BorrowedPtr<const CreatureLib::Library::SpeciesVariant>& variant) noexcept
: _creature(creature), _newVariant(variant) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::ChangeVariant; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
const ArbUt::BorrowedPtr<const CreatureLib::Library::SpeciesVariant>& GetNewVariant() const noexcept {
return _newVariant;
}
};
}
#endif // CREATURELIB_CHANGEVARIANTEVENT_HPP

View File

@ -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> _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<Creature>& 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

View File

@ -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

View File

@ -0,0 +1,21 @@
#ifndef CREATURELIB_EVENTDATA_HPP
#define CREATURELIB_EVENTDATA_HPP
#include <Arbutils/Memory/BorrowedPtr.hpp>
#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

View File

@ -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> _creature;
uint32_t _previousExperience;
uint32_t _newExperience;
public:
ExperienceGainEvent(const ArbUt::BorrowedPtr<Creature>& creature, uint32_t previousExp, uint32_t newExp)
: _creature(creature), _previousExperience(previousExp), _newExperience(newExp) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::ExperienceGain; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
uint32_t GetPreviousExperience() const noexcept { return _previousExperience; }
uint32_t GetNewExperience() const noexcept { return _newExperience; }
};
}
#endif // CREATURELIB_EXPERIENCEGAINEVENT_HPP

View File

@ -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> _creature;
public:
FaintEvent(ArbUt::BorrowedPtr<Creature> c) noexcept : _creature(c) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
};
}
#endif // CREATURELIB_FAINTEVENT_HPP

View File

@ -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> _creature;
uint32_t _originalHealth;
uint32_t _newHealth;
public:
HealEvent(ArbUt::BorrowedPtr<Creature> 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<Creature>& GetCreature() const noexcept { return _creature; }
uint32_t GetOriginalHealth() const noexcept { return _originalHealth; }
uint32_t GetNewHealth() const noexcept { return _newHealth; }
};
}
#endif // CREATURELIB_HEALEVENT_HPP

View File

@ -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> _creature;
public:
explicit MissEvent(const ArbUt::BorrowedPtr<Creature>& creature) noexcept : _creature(creature) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Miss; }
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
};
}
#endif // CREATURELIB_MISSEVENT_HPP

View File

@ -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<Creature> _newCreature;
public:
SwitchEvent(const CreatureIndex& index, const ArbUt::BorrowedPtr<Creature>& newCreature)
: _index(index), _newCreature(newCreature) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Switch; }
const CreatureIndex& GetIndex() const noexcept { return _index; }
const ArbUt::BorrowedPtr<Creature>& GetNewCreature() const noexcept { return _newCreature; }
};
}
#endif // CREATURELIB_SWITCHEVENT_HPP

View File

@ -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

View File

@ -2,6 +2,7 @@
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <unordered_set> #include <unordered_set>
#include "../../Library/Exceptions/NotImplementedException.hpp" #include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../EventHooks/EventDataClasses.hpp"
#include "../ScriptHandling/ScriptMacros.hpp" #include "../ScriptHandling/ScriptMacros.hpp"
#include "ResolveTarget.hpp" #include "ResolveTarget.hpp"

View File

@ -1,6 +1,7 @@
#include "Battle.hpp" #include "Battle.hpp"
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <memory> #include <memory>
#include "../EventHooks/EventDataClasses.hpp"
#include "../Flow/TurnHandler.hpp" #include "../Flow/TurnHandler.hpp"
#include "../Flow/TurnOrdering.hpp" #include "../Flow/TurnOrdering.hpp"

View File

@ -1,6 +1,6 @@
#include "BattleSide.hpp" #include "BattleSide.hpp"
#include <algorithm> #include <algorithm>
#include "../../Library/Exceptions/CreatureException.hpp" #include "../EventHooks/EventDataClasses.hpp"
#include "Battle.hpp" #include "Battle.hpp"
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;

View File

@ -1,6 +1,7 @@
#include "Creature.hpp" #include "Creature.hpp"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include "../EventHooks/EventDataClasses.hpp"
#include "../Models/Battle.hpp" #include "../Models/Battle.hpp"
#include "../ScriptHandling/ScriptMacros.hpp" #include "../ScriptHandling/ScriptMacros.hpp"
@ -36,7 +37,6 @@ namespace CreatureLib::Battling {
AssertNotNull(species); AssertNotNull(species);
AssertNotNull(variant); AssertNotNull(variant);
_species = species; _species = species;
ChangeVariant(variant);
// If the creature is genderless, but it's new species is not, we want to set its gender // 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) { 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) { else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) {
_gender = CreatureLib::Library::Gender::Genderless; _gender = CreatureLib::Library::Gender::Genderless;
} }
if (_battle != nullptr) {
_battle->TriggerEventListener<ChangeSpeciesEvent>(this, _species);
}
ChangeVariant(variant);
} }
void Creature::ChangeVariant(const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) { void Creature::ChangeVariant(const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant) {
@ -79,6 +83,10 @@ namespace CreatureLib::Battling {
} }
// TODO: consider variant specific attacks? // TODO: consider variant specific attacks?
if (_battle != nullptr) {
_battle->TriggerEventListener<ChangeVariantEvent>(this, _variant);
}
} }
void Creature::ChangeLevelBy(int8_t amount) { void Creature::ChangeLevelBy(int8_t amount) {

View File

@ -1,5 +1,6 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../extern/catch.hpp" #include "../../extern/catch.hpp"
#include "../../src/Battling/EventHooks/EventDataClasses.hpp"
#include "../../src/Battling/EventHooks/EventHook.hpp" #include "../../src/Battling/EventHooks/EventHook.hpp"
#include "../../src/Battling/Models/CreateCreature.hpp" #include "../../src/Battling/Models/CreateCreature.hpp"
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"