CreatureLib/src/Battling/TurnChoices/AttackTurnChoice.hpp

75 lines
2.8 KiB
C++

#ifndef CREATURELIB_ATTACKTURNCHOICE_HPP
#define CREATURELIB_ATTACKTURNCHOICE_HPP
#include "../Models/Battle.hpp"
#include "../Models/CreatureIndex.hpp"
#include "../Models/LearnedAttack.hpp"
#include "../ScriptHandling/ScriptCategory.hpp"
#include "BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class AttackTurnChoice : public BaseTurnChoice {
ArbUt::BorrowedPtr<LearnedAttack> _attack;
CreatureIndex _target;
std::unique_ptr<Script> _attackScript = nullptr;
bool _scriptIsTaken = false;
void ResolveScript() {
if (_attackScript != nullptr)
return;
auto user = GetUser();
if (user == nullptr)
return;
auto battle = user->GetBattle();
if (battle != nullptr) {
if (_attack->GetAttack()->HasSecondaryEffect()) {
auto library = battle->GetLibrary();
auto& effect = _attack->GetAttack()->GetSecondaryEffect();
_attackScript =
std::unique_ptr<Script>(library->LoadScript(ScriptCategory::Attack, effect->GetEffectName()));
if (_attackScript != nullptr) {
_attackScript->OnInitialize(effect->GetParameters());
}
}
}
}
public:
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target)
: BaseTurnChoice(user), _attack(attack), _target(target) {
AssertNotNull(attack)
ResolveScript();
}
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
Script* script) noexcept
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {}
~AttackTurnChoice() {
if (!_scriptIsTaken) {
}
}
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() const noexcept { return _attack; }
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Attack; }
int8_t GetPriority() const {
// HOOK: Change priority
return _attack->GetAttack()->GetPriority();
}
const CreatureIndex& GetTarget() const noexcept { return _target; }
const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); }
protected:
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromScript(&_attackScript));
GetUser()->GetActiveScripts(scripts);
}
};
}
#endif // CREATURELIB_ATTACKTURNCHOICE_HPP