persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
BattleSystem Class Reference

#include <BattleSystem.hpp>

Inheritance diagram for BattleSystem:
Collaboration diagram for BattleSystem:

Public Member Functions

void init () override
void update (ae::q20_12_t) override
 Core update loop that processes the battle state machine and turn resolution.
void shutdown () override
void setMusicComponent (MusicComponent *music)
void on_receive (const Event::ExecuteBattle &msg)
 ETL message handler that initializes and starts a new battle.
void on_receive_unknown (const etl::imessage &msg)
 Fallback handler for unhandled ETL messages.

Private Member Functions

 BattleSystem ()
void applyResult (const TurnResult &r, BattleParticipant *target=nullptr)
 Resolves the mathematical and state changes of a combat action against a target.
void advanceTurn ()
 Progresses the battle state machine to the next logical step.
void setNextPhase (BattlePhase nextPhase)
 Safely transitions the state machine to a new BattlePhase.
void calculateTurnOrder ()
 Calculates the initial turn queue at the start of an encounter.
void handleDeadParticipants ()
 Sweeps the field for dead participants and evaluates encounter end conditions.
etl::vector< BattleParticipant *, 13 > getAliveEnemies ()
 Filters the enemy roster for living combatants.
bool allEnemiesKnockedDown ()
 Evaluates if an All-Out Attack condition is met.
bool isSingleTarget (SkillType type)
 Determines if a skill targets a single entity.

Static Private Member Functions

static bool getParticipantByHigherAgility (BattleParticipant *a, BattleParticipant *b)
 Sorting predicate used to order combatants by agility.

Private Attributes

u32 turnsTaken = 0
BattlePhase phase
Event::BattleResult battleResult
BattleParticipant * currentParticipantTurn = nullptr
u32 currentParticipantIndex = 0
int selectedBattleOption = -1
Skill * selectedSkill = nullptr
bool pendingPersonaSwitch = false
bool switchedPersonaThisTurn = false
PersonaBase * personaBeforeSwitch = nullptr
std::string pendingAlert
BattlePhase alertReturnPhase = BattlePhase::Done
bool allOutAttackWasPossibleThisKnockDown = false
etl::vector< BattleParticipant *, 13 > battleParticipants
etl::vector< Enemy *, 8 > enemies
etl::vector< PartyMember *, 4 > partyMembers
Player * player = nullptr
BattleStartCondition battleStartCondition = BattleStartCondition::Even
AttackAction attack
Guard guard
PersonaAction persona
SwitchPersona switchPersona
std::array< ActionBase *, 4 > actions = {&attack, &guard, &persona, &switchPersona}
MusicComponent * musicCmpt = nullptr
BattleMenu * battleMenuCmpt = nullptr

Static Private Attributes

static constexpr u32 ACTION_ATTACK = 0
static constexpr u32 ACTION_GUARD = 1
static constexpr u32 ACTION_PERSONA = 2
static constexpr u32 ACTION_SWITCH = 3

Friends

class Singleton< BattleSystem >

Detailed Description

Definition at line 40 of file BattleSystem.hpp.

Constructor & Destructor Documentation

◆ BattleSystem()

BattleSystem::BattleSystem ( )
inlineprivate

Definition at line 102 of file BattleSystem.hpp.

Member Function Documentation

◆ advanceTurn()

void BattleSystem::advanceTurn ( )
private

Progresses the battle state machine to the next logical step.

Evaluates the board state after an action resolves. Responsibilities include:

  • Checking for win/loss conditions via handleDeadParticipants().
  • Intercepting the turn flow to prompt an All-Out Attack if all enemies are knocked down.
  • Granting an immediate follow-up turn if the current participant earned a "1 More".
  • Cycling to the next living participant's turn if the current actor is finished, clearing their knockdown and Persona switch states in the process.

Definition at line 448 of file BattleSystem.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ allEnemiesKnockedDown()

bool BattleSystem::allEnemiesKnockedDown ( )
private

Evaluates if an All-Out Attack condition is met.

Returns
true if every living enemy is currently in the knocked-down state, false otherwise.

Definition at line 632 of file BattleSystem.cpp.

Here is the caller graph for this function:

◆ applyResult()

void BattleSystem::applyResult ( const TurnResult & r,
BattleParticipant * target = nullptr )
private

Resolves the mathematical and state changes of a combat action against a target.

Parses a generated TurnResult struct to apply HP modifications, clamp healing to the target's maximum HP, and buffer damage/healing numbers into pendingAlert for UI feedback. Also checks for and triggers the "1 More" state if the hit warrants it.

Parameters
rThe mathematical result of the action (damage delta, hit flag, 1-More flag).
targetThe participant receiving the action (can be null for non-targeted actions).

Definition at line 407 of file BattleSystem.cpp.

Here is the caller graph for this function:

◆ calculateTurnOrder()

void BattleSystem::calculateTurnOrder ( )
private

Calculates the initial turn queue at the start of an encounter.

Applies a random agility boost (1.2x to 1.4x) to all participants to prevent static turn orders, sorts the party and enemies individually, and then merges them based on the BattleStartCondition (forcing Party Advantage or Enemy Advantage).

Note
TODO: This function currently assumes it is only called once per encounter. Calling it mid-battle could result in a participant taking a double turn.

Definition at line 531 of file BattleSystem.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAliveEnemies()

etl::vector< BattleParticipant *, 13 > BattleSystem::getAliveEnemies ( )
private

Filters the enemy roster for living combatants.

Returns
A vector containing pointers to all enemies currently above 0 HP.

Definition at line 619 of file BattleSystem.cpp.

Here is the caller graph for this function:

◆ getParticipantByHigherAgility()

bool BattleSystem::getParticipantByHigherAgility ( BattleParticipant * a,
BattleParticipant * b )
inlinestaticprivate

Sorting predicate used to order combatants by agility.

Parameters
aThe first participant to compare.
bThe second participant to compare.
Returns
true if participant 'a' should act before participant 'b'.

Definition at line 236 of file BattleSystem.hpp.

Here is the caller graph for this function:

◆ handleDeadParticipants()

void BattleSystem::handleDeadParticipants ( )
private

Sweeps the field for dead participants and evaluates encounter end conditions.

Iterates through all combatants to check if HP has reached 0. Triggers the individual onDead() callbacks to populate the battleResult. Immediately halts and shuts down the battle if the main player dies, or if the entire enemy side is wiped out.

Don't display any battle results

Display battle results

Definition at line 579 of file BattleSystem.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

void BattleSystem::init ( )
override

Definition at line 67 of file BattleSystem.cpp.

◆ isSingleTarget()

bool BattleSystem::isSingleTarget ( SkillType type)
private

Determines if a skill targets a single entity.

Parameters
typeThe enumerated type classification of the skill.
Returns
true if the skill is a single-target action (e.g., Attack, single Heal/Buff), false if it is multi-target.

Definition at line 656 of file BattleSystem.cpp.

Here is the caller graph for this function:

◆ on_receive()

void BattleSystem::on_receive ( const Event::ExecuteBattle & msg)

ETL message handler that initializes and starts a new battle.

Acts as the initialization routine for the BattleSystem when an ExecuteBattle event is fired over the message bus.

This function dynamically allocates the active combat entities (Player, PartyMember, and Enemy objects) based on the profiles provided in the message payload. It then resets all internal state machine variables, clears any pending UI alerts

Finally, it calculates the initial turn order based on the battleStartCondition and advances the state machine to the first participant's initial phase.

Parameters
msgThe event payload containing the character/enemy profiles and encounter conditions.

Definition at line 10 of file BattleSystem.cpp.

Here is the call graph for this function:

◆ on_receive_unknown()

void BattleSystem::on_receive_unknown ( const etl::imessage & msg)
inline

Fallback handler for unhandled ETL messages.

Required by the ETL message router interface. Safely ignores any messages routed to the BattleSystem that do not have a specific handler.

Parameters
msgThe unhandled incoming message (unused).

Definition at line 96 of file BattleSystem.hpp.

◆ setMusicComponent()

void BattleSystem::setMusicComponent ( MusicComponent * music)

Definition at line 402 of file BattleSystem.cpp.

◆ setNextPhase()

void BattleSystem::setNextPhase ( BattlePhase nextPhase)
private

Safely transitions the state machine to a new BattlePhase.

Acts as a router to ensure UI alerts are displayed before phase changes. If there is text buffered in pendingAlert, it intercepts the transition, forcing the system into the ShowAlert phase and saving the intended next phase into alertReturnPhase.

Parameters
nextPhaseThe phase the state machine should enter after all alerts are cleared.

Definition at line 516 of file BattleSystem.cpp.

Here is the caller graph for this function:

◆ shutdown()

void BattleSystem::shutdown ( )
override

Definition at line 365 of file BattleSystem.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

void BattleSystem::update ( ae::q20_12_t )
override

Core update loop that processes the battle state machine and turn resolution.

Executes every engine tick to advance the current BattlePhase. This state machine governs both the user interface flow (via battleMenuCmpt) and the underlying combat mechanics. Key responsibilities include:

  • Menu Navigation: Handling hardware input for base actions, skill selection, and target filtering (e.g., preventing healing on max-HP targets, skipping dead entities).
  • Persona Mechanics: Managing mid-turn Persona switching and validating SP/HP resource costs before skill execution.
  • Combat Flow: Resolving attacks, evaluating "1 More" states to trigger All-Out Attack confirmations, and routing enemy AI behavior.
  • UI Alerts: Temporarily overriding the menu to display asynchronous combat messages using the pendingAlert buffer during the ShowAlert phase.
Parameters
dtFixed-point delta time passed from the aegis engine loop (currently unused).

Definition at line 72 of file BattleSystem.cpp.

Here is the call graph for this function:

◆ Singleton< BattleSystem >

friend class Singleton< BattleSystem >
friend

Definition at line 96 of file BattleSystem.hpp.

Member Data Documentation

◆ ACTION_ATTACK

u32 BattleSystem::ACTION_ATTACK = 0
staticconstexprprivate

Definition at line 106 of file BattleSystem.hpp.

◆ ACTION_GUARD

u32 BattleSystem::ACTION_GUARD = 1
staticconstexprprivate

Definition at line 107 of file BattleSystem.hpp.

◆ ACTION_PERSONA

u32 BattleSystem::ACTION_PERSONA = 2
staticconstexprprivate

Definition at line 108 of file BattleSystem.hpp.

◆ ACTION_SWITCH

u32 BattleSystem::ACTION_SWITCH = 3
staticconstexprprivate

Definition at line 109 of file BattleSystem.hpp.

◆ actions

std::array<ActionBase*, 4> BattleSystem::actions = {&attack, &guard, &persona, &switchPersona}
private

Definition at line 145 of file BattleSystem.hpp.

◆ alertReturnPhase

BattlePhase BattleSystem::alertReturnPhase = BattlePhase::Done
private

Definition at line 127 of file BattleSystem.hpp.

◆ allOutAttackWasPossibleThisKnockDown

bool BattleSystem::allOutAttackWasPossibleThisKnockDown = false
private

Definition at line 129 of file BattleSystem.hpp.

◆ attack

AttackAction BattleSystem::attack
private

Definition at line 140 of file BattleSystem.hpp.

◆ battleMenuCmpt

BattleMenu* BattleSystem::battleMenuCmpt = nullptr
private

Definition at line 242 of file BattleSystem.hpp.

◆ battleParticipants

etl::vector<BattleParticipant*, 13> BattleSystem::battleParticipants
private

Definition at line 132 of file BattleSystem.hpp.

◆ battleResult

Event::BattleResult BattleSystem::battleResult
private

Definition at line 114 of file BattleSystem.hpp.

◆ battleStartCondition

BattleStartCondition BattleSystem::battleStartCondition = BattleStartCondition::Even
private

Definition at line 137 of file BattleSystem.hpp.

◆ currentParticipantIndex

u32 BattleSystem::currentParticipantIndex = 0
private

Definition at line 117 of file BattleSystem.hpp.

◆ currentParticipantTurn

BattleParticipant* BattleSystem::currentParticipantTurn = nullptr
private

Definition at line 116 of file BattleSystem.hpp.

◆ enemies

etl::vector<Enemy*, 8> BattleSystem::enemies
private

Definition at line 133 of file BattleSystem.hpp.

◆ guard

Guard BattleSystem::guard
private

Definition at line 141 of file BattleSystem.hpp.

◆ musicCmpt

MusicComponent* BattleSystem::musicCmpt = nullptr
private

Definition at line 241 of file BattleSystem.hpp.

◆ partyMembers

etl::vector<PartyMember*, 4> BattleSystem::partyMembers
private

Definition at line 134 of file BattleSystem.hpp.

◆ pendingAlert

std::string BattleSystem::pendingAlert
private

Definition at line 126 of file BattleSystem.hpp.

◆ pendingPersonaSwitch

bool BattleSystem::pendingPersonaSwitch = false
private

Definition at line 122 of file BattleSystem.hpp.

◆ persona

PersonaAction BattleSystem::persona
private

Definition at line 142 of file BattleSystem.hpp.

◆ personaBeforeSwitch

PersonaBase* BattleSystem::personaBeforeSwitch = nullptr
private

Definition at line 124 of file BattleSystem.hpp.

◆ phase

BattlePhase BattleSystem::phase
private

Definition at line 113 of file BattleSystem.hpp.

◆ player

Player* BattleSystem::player = nullptr
private

Definition at line 135 of file BattleSystem.hpp.

◆ selectedBattleOption

int BattleSystem::selectedBattleOption = -1
private

Definition at line 119 of file BattleSystem.hpp.

◆ selectedSkill

Skill* BattleSystem::selectedSkill = nullptr
private

Definition at line 120 of file BattleSystem.hpp.

◆ switchedPersonaThisTurn

bool BattleSystem::switchedPersonaThisTurn = false
private

Definition at line 123 of file BattleSystem.hpp.

◆ switchPersona

SwitchPersona BattleSystem::switchPersona
private

Definition at line 143 of file BattleSystem.hpp.

◆ turnsTaken

u32 BattleSystem::turnsTaken = 0
private

Definition at line 111 of file BattleSystem.hpp.


The documentation for this class was generated from the following files: