CreatureLib/src/Battling/TurnChoices/AttackTurnChoice.hpp

88 lines
3.2 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<Script> _attackScript = nullptr;
2020-06-20 17:44:41 +00:00
int8_t _priority = 0;
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) {
2020-06-20 17:44:41 +00:00
#ifndef TESTS_BUILD
AssertNotNull(user)
2020-03-22 12:42:26 +00:00
AssertNotNull(attack)
2020-06-20 17:44:41 +00:00
#endif
ResolveScript();
_priority = _attack->GetAttack()->GetPriority();
}
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
2020-06-20 17:44:41 +00:00
Script* script)
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {
#ifndef TESTS_BUILD
AssertNotNull(user)
AssertNotNull(attack)
#endif
_priority = _attack->GetAttack()->GetPriority();
}
2020-06-20 17:44:41 +00:00
~AttackTurnChoice() = default;
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() const noexcept { return _attack; }
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<Script>& GetAttackScript() const noexcept { return _attackScript; }
2020-06-20 17:44:41 +00:00
size_t ScriptCount() const override {
if (_user == nullptr) {
return 1;
}
return 1 + GetUser()->ScriptCount();
}
2019-11-24 10:06:51 +00:00
protected:
2020-05-26 16:31:06 +00:00
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override {
2020-04-23 21:23:58 +00:00
scripts.Append(ScriptWrapper::FromScript(&_attackScript));
2020-06-20 17:44:41 +00:00
if (_user != nullptr) {
GetUser()->GetActiveScripts(scripts);
}
}
};
}
#endif // CREATURELIB_ATTACKTURNCHOICE_HPP