CreatureLib/src/Battling/Flow/TurnHandler.cpp

125 lines
3.6 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2019-11-02 12:57:43 +00:00
#include "../Models/Creature.hpp"
#include "../Models/Battle.hpp"
2019-11-03 12:47:50 +00:00
#include "../Models/ExecutingAttack.hpp"
2019-10-31 12:26:56 +00:00
#include "../../Core/Exceptions/NotImplementedException.hpp"
void CreatureLib::Battling::TurnHandler::RunTurn(CreatureLib::Battling::ChoiceQueue &queue) {
2019-11-02 12:57:43 +00:00
//HOOK: On Before Turn hook for all choices
2019-10-31 12:13:36 +00:00
while (queue.HasNext()){
ExecuteChoice(queue.Dequeue());
}
}
2019-10-31 12:13:36 +00:00
void CreatureLib::Battling::TurnHandler::ExecuteChoice(const CreatureLib::Battling::BaseTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
if (choice == nullptr)
{
return;
}
auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) {
return;
}
auto user = choice->GetUser();
// If the user is fainted, we don't want to execute its choice.
if (user->IsFainted()){
return;
}
auto battle = user->GetBattle();
// If the user is not in the field, we don't want to execute its choice.
if (!battle->CreatureInField(user)){
return;
}
// If the choice is not valid, we don't want to execute it.
if (!battle->CanUse(choice)){
return;
}
switch (choiceKind){
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Pass: return;
case TurnChoiceKind::Attack:
return ExecuteAttackChoice(static_cast<const AttackTurnChoice*>(choice));
case TurnChoiceKind::Item:
case TurnChoiceKind::Switch:
case TurnChoiceKind::RunAway:
2019-10-31 12:26:56 +00:00
throw NotImplementedException();
2019-10-31 12:13:36 +00:00
}
2019-11-02 12:57:43 +00:00
delete choice;
}
2019-10-31 12:13:36 +00:00
void CreatureLib::Battling::TurnHandler::ExecuteAttackChoice(const CreatureLib::Battling::AttackTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
//HOOK: Change attack
//HOOK: Prevent attack
2019-11-03 12:47:50 +00:00
auto attack = ExecutingAttack();
2019-10-31 12:13:36 +00:00
2019-11-03 12:47:50 +00:00
//HOOK: override targets
if (!choice->GetAttack()->TryUse(1)){
return;
}
//HOOK: check if attack fails
//HOOK: Check if attack stops after decreasing PP
//HOOK: On Before Attack
for (auto kv: attack.GetTargets()){
HandleAttackForTarget(attack, kv.first, kv.second);
}
2019-10-31 12:13:36 +00:00
}
2019-11-03 12:47:50 +00:00
void CreatureLib::Battling::TurnHandler::HandleAttackForTarget(CreatureLib::Battling::ExecutingAttack &attack,
CreatureLib::Battling::Creature *target,
CreatureLib::Battling::ExecutingAttack::TargetData &targetData) {
//HOOK: Check if attack fails on target
//HOOK: Check if target is invulnerable
if (!targetData.IsHit()){
//HOOK: On attack miss.
return;
}
auto numHits = targetData.GetNumberOfHits();
if (numHits == 0)
return;
auto user = attack.GetUser();
auto attackData = attack.GetAttack()->GetAttack();
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){
if (user->IsFainted()){
break;
}
if (target->IsFainted()){
// STOP, STOP! HE'S ALREADY DEAD ;_;
break;
}
auto hit = targetData.GetHit(hitIndex);
//TODO: calculate data for hit
if (attackData->GetCategory() == Library::AttackCategory::Status){
//HOOK: Status attack
}
else{
auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()){
damage = target->GetCurrentHealth();
hit.OverrideDamage(damage);
}
if (damage > 0){
target->Damage(damage, DamageSource::AttackDamage);
//HOOK: Prevent secondary effects
//HOOK: On Move Hit
}
}
}
if (!user->IsFainted()){
//HOOK: On After Hits
}
}