Adds status handling functions
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-07-09 15:33:30 +02:00
parent 9303ec53e0
commit 08120d5433
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 57 additions and 2 deletions

View File

@ -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);) }
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(); }

View File

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

View File

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

View File

@ -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> _creature;
ArbUt::StringView _statusName;
public:
explicit StatusChangeEvent(const ArbUt::BorrowedPtr<Creature>& creature, const ArbUt::StringView& name)
: _creature(creature), _statusName(name) {}
[[nodiscard]] CreatureLib::Battling::EventDataKind GetKind() const noexcept override {
return static_cast<CreatureLib::Battling::EventDataKind>(EventDataKind::StatusChange);
}
const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
const ArbUt::StringView& GetStatusName() const noexcept { return _statusName; }
};
}
#endif // CREATURELIB_STATUSCHANGEEVENT_HPP

View File

@ -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<CreatureLib::Battling::BattleScript>(_library->LoadScript(ScriptCategory::Status, name));
if (_battleData.Battle.HasValue()) {
_battleData.Battle.GetValue()->TriggerEventListener<StatusChangeEvent>(this, name);
}
}
void CreatureLib::Battling::Creature::ClearStatus() {
if (_status == nullptr) {
return;
}
_status->OnRemove();
_status = nullptr;
if (_battleData.Battle.HasValue()) {
_battleData.Battle.GetValue()->TriggerEventListener<StatusChangeEvent>(this, ""_cnc);
}
}

View File

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