CreatureLib/src/Battling/TurnChoices/AttackTurnChoice.hpp

88 lines
3.2 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 "../ScriptHandling/ScriptMacros.hpp"
#include "BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class AttackTurnChoice : public BaseTurnChoice {
ArbUt::BorrowedPtr<LearnedAttack> _attack;
CreatureIndex _target;
std::unique_ptr<Script> _attackScript = nullptr;
int8_t _priority = 0;
void ResolveScript() {
if (_attackScript != nullptr)
return;
auto user = GetUser();
if (user == nullptr)
return;
auto battle = user->GetBattle();
if (battle.HasValue()) {
if (_attack->GetAttack()->HasSecondaryEffect()) {
auto library = battle.GetValue()->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) {
#ifndef TESTS_BUILD
AssertNotNull(user)
AssertNotNull(attack)
#endif
ResolveScript();
_priority = _attack->GetAttack()->GetPriority();
}
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
Script* script)
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {
#ifndef TESTS_BUILD
AssertNotNull(user)
AssertNotNull(attack)
#endif
_priority = _attack->GetAttack()->GetPriority();
}
~AttackTurnChoice() = default;
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() const noexcept { return _attack; }
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Attack; }
inline int8_t GetPriority() const noexcept { return _priority; }
inline void SetPriority(int8_t priority) noexcept { _priority = priority; }
const CreatureIndex& GetTarget() const noexcept { return _target; }
const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override {
if (_user == nullptr) {
return 1;
}
return 1 + GetUser()->ScriptCount();
}
protected:
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromScript(&_attackScript));
if (_user != nullptr) {
GetUser()->GetActiveScripts(scripts);
}
}
};
}
#endif // CREATURELIB_ATTACKTURNCHOICE_HPP