Initial work on item use handling
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-02-13 13:40:33 +01:00
parent 173c6c9926
commit c40f063683
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 60 additions and 6 deletions

View File

@ -14,7 +14,7 @@ export uint8_t CreatureLib_ItemUseScript_IsUseValidForCreature(ItemUseScript* p,
export uint8_t CreatureLib_ItemUseScript_IsHoldable(ItemUseScript* p, uint8_t& out) { Try(out = p->IsHoldable()) } export uint8_t CreatureLib_ItemUseScript_IsHoldable(ItemUseScript* p, uint8_t& out) { Try(out = p->IsHoldable()) }
export uint8_t CreatureLib_ItemUseScript_OnUse(ItemUseScript* p) { Try(p->OnUse()) } export uint8_t CreatureLib_ItemUseScript_OnUse(ItemUseScript* p, Battle* battle) { Try(p->OnUse(battle)) }
export uint8_t CreatureLib_ItemUseScript_OnCreatureUse(ItemUseScript* p, Creature* creature) { export uint8_t CreatureLib_ItemUseScript_OnCreatureUse(ItemUseScript* p, Creature* creature, bool isBattle) {
Try(p->OnCreatureUse(creature)) Try(p->OnCreatureUse(creature, isBattle))
} }

View File

@ -79,7 +79,7 @@ void TurnHandler::ExecuteChoice(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>()); case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>());
case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>()); case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>());
case TurnChoiceKind::Item: NOT_IMPLEMENTED; case TurnChoiceKind::Item: return ExecuteItemChoice(choice.ForceAs<ItemTurnChoice>());
} }
} }
@ -361,3 +361,29 @@ void TurnHandler::ExecuteFleeChoice(const ArbUt::BorrowedPtr<FleeTurnChoice>& ch
battle.GetValue()->ValidateBattleState(); battle.GetValue()->ValidateBattleState();
} }
} }
void TurnHandler::ExecuteItemChoice(const ArbUt::BorrowedPtr<ItemTurnChoice>& choice) {
auto user = choice->GetUser();
auto battle = user->GetBattle();
if (!battle.HasValue()) {
return;
}
auto* script = battle.GetValue()->GetLibrary()->GetScriptResolver()->LoadItemScript(choice->GetItem());
auto targetIndex = choice->GetTarget();
ArbUt::OptionalBorrowedPtr<Creature> target = nullptr;
if (targetIndex.has_value()) {
target = battle.GetValue()->GetCreature(targetIndex.value());
}
auto isCreatureUseItem = script->IsCreatureUseItem();
if (isCreatureUseItem && !target.HasValue()) {
target = choice->GetUser();
}
if (isCreatureUseItem) {
if (!script->IsUseValidForCreature(target.GetValue())) {
return;
}
script->OnCreatureUse(target.GetValue(), true);
} else {
script->OnUse(battle.GetValue());
}
}

View File

@ -4,6 +4,7 @@
#include "../Models/ExecutingAttack.hpp" #include "../Models/ExecutingAttack.hpp"
#include "../TurnChoices/AttackTurnChoice.hpp" #include "../TurnChoices/AttackTurnChoice.hpp"
#include "../TurnChoices/FleeTurnChoice.hpp" #include "../TurnChoices/FleeTurnChoice.hpp"
#include "../TurnChoices/ItemTurnChoice.hpp"
#include "../TurnChoices/SwitchTurnChoice.hpp" #include "../TurnChoices/SwitchTurnChoice.hpp"
#include "ChoiceQueue.hpp" #include "ChoiceQueue.hpp"
@ -18,6 +19,7 @@ namespace CreatureLib::Battling {
static void ExecuteSwitchChoice(const ArbUt::BorrowedPtr<SwitchTurnChoice>& choice); static void ExecuteSwitchChoice(const ArbUt::BorrowedPtr<SwitchTurnChoice>& choice);
static void ExecuteFleeChoice(const ArbUt::BorrowedPtr<FleeTurnChoice>& choice); static void ExecuteFleeChoice(const ArbUt::BorrowedPtr<FleeTurnChoice>& choice);
static void ExecuteItemChoice(const ArbUt::BorrowedPtr<ItemTurnChoice>& choice);
public: public:
static void RunTurn(const ArbUt::BorrowedPtr<ChoiceQueue>& queue, const ArbUt::BorrowedPtr<Battle>& battle); static void RunTurn(const ArbUt::BorrowedPtr<ChoiceQueue>& queue, const ArbUt::BorrowedPtr<Battle>& battle);

View File

@ -5,6 +5,7 @@
#include "../../Library/EffectParameter.hpp" #include "../../Library/EffectParameter.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class Creature; class Creature;
class Battle;
class ItemUseScript { class ItemUseScript {
public: public:
@ -22,8 +23,8 @@ namespace CreatureLib::Battling {
/// @brief Can the item be held? /// @brief Can the item be held?
[[nodiscard]] virtual bool IsHoldable() const { return false; } [[nodiscard]] virtual bool IsHoldable() const { return false; }
virtual void OnUse() const {} virtual void OnUse([[maybe_unused]] Battle* battle) const {}
virtual void OnCreatureUse([[maybe_unused]] Creature* creature) const {} virtual void OnCreatureUse([[maybe_unused]] Creature* creature, [[maybe_unused]] bool isBattle) const {}
}; };
} }

View File

@ -0,0 +1,25 @@
#ifndef CREATURELIB_ITEMTURNCHOICE_HPP
#define CREATURELIB_ITEMTURNCHOICE_HPP
#include "../../Library/Items/Item.hpp"
#include "../Models/CreatureIndex.hpp"
#include "BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class ItemTurnChoice : public BaseTurnChoice {
ArbUt::BorrowedPtr<const Library::Item> _item;
std::optional<CreatureIndex> _target;
public:
ItemTurnChoice(ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<Library::Item>& item,
const std::optional<CreatureIndex>& target)
: BaseTurnChoice(user), _item(item), _target(target) {}
~ItemTurnChoice() override = default;
[[nodiscard]] TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind::Item; }
[[nodiscard]] const ArbUt::BorrowedPtr<const Library::Item>& GetItem() const noexcept { return _item; }
[[nodiscard]] const std::optional<CreatureIndex>& GetTarget() const noexcept { return _target; }
};
}
#endif // CREATURELIB_ITEMTURNCHOICE_HPP