CreatureLib/src/Battling/TurnChoices/AttackTurnChoice.hpp

80 lines
3.3 KiB
C++
Raw Normal View History

#ifndef CREATURELIB_ATTACKTURNCHOICE_HPP
#define CREATURELIB_ATTACKTURNCHOICE_HPP
#include "../Models/Battle.hpp"
2019-12-05 08:53:48 +00:00
#include "../Models/CreatureIndex.hpp"
#include "../Models/LearnedAttack.hpp"
#include "../ScriptHandling/ScriptCategory.hpp"
2020-06-20 17:44:41 +00:00
#include "../ScriptHandling/ScriptMacros.hpp"
#include "BaseTurnChoice.hpp"
namespace CreatureLib::Battling {
class AttackTurnChoice : public BaseTurnChoice {
ArbUt::BorrowedPtr<LearnedAttack> _attack;
2019-12-05 08:53:48 +00:00
CreatureIndex _target;
std::unique_ptr<BattleScript> _attackScript = nullptr;
2020-06-20 17:44:41 +00:00
int8_t _priority = 0;
void ResolveScript(ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData> attack) {
if (_attackScript != nullptr)
return;
auto user = GetUser();
auto battle = user->GetBattle();
if (battle.HasValue()) {
if (attack->HasSecondaryEffect()) {
auto library = battle.GetValue()->GetLibrary();
auto& effect = attack->GetSecondaryEffect();
_attackScript = std::unique_ptr<BattleScript>(
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) {
EnsureNotNull(user)
ResolveScript(attack->GetAttack());
_priority = _attack->GetAttack()->GetPriority();
}
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
BattleScript* script)
2020-06-20 17:44:41 +00:00
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {
_priority = _attack->GetAttack()->GetPriority();
}
2020-06-20 17:44:41 +00:00
~AttackTurnChoice() = default;
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() const noexcept { return _attack; }
void ChangeAttackScript(ArbUt::BorrowedPtr<const CreatureLib::Library::AttackData> attack) {
_attackScript = nullptr;
ResolveScript(attack);
ResetActiveScripts();
}
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Attack; }
2019-10-31 11:31:31 +00:00
inline int8_t GetPriority() const noexcept { return _priority; }
inline void SetPriority(int8_t priority) noexcept { _priority = priority; }
2019-11-02 12:57:43 +00:00
const CreatureIndex& GetTarget() const noexcept { return _target; }
const std::unique_ptr<BattleScript>& GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); }
2019-11-24 10:06:51 +00:00
2020-05-26 16:31:06 +00:00
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
GetOwnScripts(scripts);
GetUser()->GetActiveScripts(scripts);
}
void GetOwnScripts(ArbUt::List<ScriptWrapper>& scripts) override {
scripts.Append(ScriptWrapper::FromScript(&_attackScript));
}
};
}
#endif // CREATURELIB_ATTACKTURNCHOICE_HPP