Combat Manager - UQcsse3200/2024-studio-2 GitHub Wiki
Overview
The CombatManager
class is the core controller for the turn-based combat system, managing all behind-the-scenes combat interactions between the player and enemy.
CombatManager extends the Component class, allowing it to be added as a part of combat screen and giving access to the same entity event calls that happen within the scope of combat.
Key Components
Move Combinations
Executes 16 unique player-enemy move combinations, ensuring all interactions—such as Attack vs Guard or Sleep vs Item—produce the correct combat outcomes each turn.
Enemy Move Generation
Dynamically selects enemy actions by mixing randomness and strategy, adjusting behaviour based on the combat situation (e.g., healing when health is low).
-
Default Probabilities:
- Attack: 30%
- Guard: 20%
- Sleep: 10%
-
Special Move (20% chance if):
- Enemy has a special move.
- Player has no status effect.
- Player didn’t use an item.
-
No Sleep: Enemy will not Sleep if its health and hunger are maxed.
-
Low Health/Hunger (below 30%):
- Guard: 40%
- Sleep: 60% (if available).
-
Low Player Health (below 30%): Chance of enemy attack increases to 70%.
-
Probabilities: Adjusted to sum up to 100%.
-
Move Selection: Chosen based on weighted probabilities, with a random fallback.
Status Effects
Manages and applies Bleed, Poisoned, and Shocked effects, tracking their duration and impact each round. These effects persist, draining health or causing other penalties, and CombatManager keeps track of all of these.
The effects of each Boss' Status Effect can be seen from the following wiki pages by Team 9:
Confusion Move Handling
If Confusion status is active, the player’s chosen move is replaced with a random one mid-turn, forcing unexpected actions and disrupting their strategy.
Dialogue Management
Generates detailed text for each combat round, summarising player and enemy actions along with narrative-driven status effects to maintain immersion.
Animation Signalling
Coordinates combat actions with animations by triggering events in sync with the game's state, ensuring smooth visual feedback during battles.
Usage
As of Sprint 4, in the context of the combat system and combat screen, a CombatManager
is instantiated when a new CombatScreen class is created, and is added as a component to it. It is also initialised beforehand so it can be passed into the CombatActions class (which is also a component of the CombatScreen's ui entity), as CombatActions requires it for logic passing.
public class CombatScreen extends ScreenAdapter {
...
/**
* Creates the main game's ui including components for rendering ui elements to the screen and
* capturing and handling ui input.
*/
private void createUI() {
...
// Initialise combat manager with instances of player and enemy to be passed into combat actions
CombatManager manager = new CombatManager(player, enemy);
Entity ui = new Entity();
ui.addComponent(new InputDecorator(stage, 10))
.addComponent(manager)
.addComponent(new CombatActions(
this.game, manager, oldScreen, oldScreenServices
))
...
ServiceLocator.getEntityService().register(ui);
}
UML Diagram: CombatManager
The UML diagram below illustrates the interactions between CombatManager and other classes.
CombatManager most notably interacts with the CombatStatsComponent
class, as this is the structure that stores the attacking and defending enemy's stats which are being affected.
CombatManager Class
Attributes:
player
: A reference to the player entity involved in the combat.enemy
: A reference to the enemy entity the player is fighting against.playerStats
: Stores the original combat stats of the player.enemyStats
: Stores the original combat stats of the enemy.playerAction
: The action chosen by the player during their turn.enemyAction
: The action selected by the enemy for its turn.playerMove
: A reference to the player's CombatMoveComponent. This will contain the player's CombatMoves initialised in the PlayerFactory.enemyMove
: A reference to the player's CombatMoveComponent. This will contain the enemy's CombatMoves initialised in the PlayerFactory.playerItem
: The item the player has selected to use during combat.playerItemIndex
: The index of the selected item in the player’s inventory.playerItemContext
: The context in which the selected item is being used.statusEffectDuration
: Tracks the duration of any status effects applied during combat.moveChangedByConfusion
: A flag indicating if the player’s move was altered by confusion status.
Functions
Initialisation & Setup
create()
: Initialises the combat manager and sets up the components for the battle.
Player Actions & Item Usage
-
onPlayerActionSelected(String playerActionStr)
: Processes the action chosen by the player, such as attacking or defending. This function takes in the parameter 'playerActionStr', which is passed in from CombatActions from a corresponding button trigger. It should be one of "ATTACK", "GUARD", "SLEEP", or "ITEMS". -
usePlayerItem(AbstractItem item, int index, ItemUsageContext context)
: Handles the logic for using an item from the player’s inventory during combat. Contains parameters relevant to theuseItem
function in the InventoryDisplay class. -
handlePlayerConfusion()
: Manages the effects of the confusion status, altering the player’s move.
Enemy Actions
selectEnemyMove()
: This function is where the enemy moveAction
is selected for each combat turn. The current implementation selects a move using weighted randomness, considering move availability, redundancy avoidance, and the enemy and player's current health and hunger stats, to select a move based on predicted player moves and keep combat engaging.
Status Effect Management
-
handleStatusEffects()
: Applies and updates all active status effects on entities during combat. -
handleBleed()
: Processes the mechanics of the "bleed" status effect. -
handlePoisoned()
: Manages the poisoned status on an entity. -
handleShocked()
: Handles the effects of the "shocked" status during the battle.
Combat Move Handling, Sequencing & Execution
executeMoveCombination(Action playerAction, Action enemyAction)
: Given a player action and enemy action, handles the unique move combination and relevant sequencing. The sequencing can be based on either the speed of the two entities, or the combination of moves themselves (using an item always goes first). Also executes the CombatMoves.checkSpecialMoveCombination()
: Checks if the player and enemy performed a special move combination.checkCombatEnd()
: Checks if the conditions to end the combat have been met.
Getters & Helper Functions
getPlayer()
: Returns the player entity involved in combat.getEnemy()
: Returns the enemy entity involved in combat.getPlayerStats()
: Retrieves the player's combat statistics.getEnemyStats()
: Retrieves the enemy's combat statistics.playerStatusEffects()
: Returns a string representing the player's active status effects.getMultiHitsLanded()
: Returns the number of successful multi-hits landed (between 0 and 4), as simulated by a Bernoulli distribution.
Sequence Diagram
The diagram outlines the core mechanics of a combat encounter, including the interaction between the Player, Enemy, and the CombatManager responsible for managing the battle.