Skip to content

CombatActions

Sneha Zazan edited this page Oct 16, 2024 · 6 revisions

Overview

The CombatActions class is a crucial component in managing combat-related events and interactions within the game. It extends the Component class and is responsible for listening to various combat events, triggering appropriate responses, and managing transitions between different game states during and after combat.

Class Diagram

classDiagram
    class Component {
        +create()
        +dispose()
    }
    class CombatActions {
        -GdxGame game
        -CombatManager manager
        -Screen previousScreen
        -ServiceContainer previousServices
        +CombatActions(GdxGame, CombatManager, Screen, ServiceContainer)
        +create()
        -onCombatWin(Entity)
        -onCombatLoss(Entity)
        -onBossCombatWin(Entity)
        -onBossCombatLoss(Entity)
        -onAttack(Screen, ServiceContainer)
        -onGuard(Screen, ServiceContainer)
        -onSleep(Screen, ServiceContainer)
        -onItems(Screen, ServiceContainer)
        +dispose()
    }
    Component <|-- CombatActions
Loading

Key Components

  1. GdxGame: Represents the main game instance, used for screen transitions.
  2. CombatManager: Manages the combat logic and state.
  3. Screen: Represents the previous screen to return to after combat.
  4. ServiceContainer: Contains services used in the main game screen.

Initialization

The CombatActions class is initialized with the following parameters:

public CombatActions(GdxGame game, CombatManager manager, Screen previousScreen, ServiceContainer previousServices) {
    this.game = game;
    this.manager = manager;
    this.previousServices = previousServices;
    this.previousScreen = previousScreen;
}

Event Listeners

The create() method sets up various event listeners:

@Override
public void create() {
    entity.getEvents().addListener("combatWin", this::onCombatWin);
    entity.getEvents().addListener("combatLoss", this::onCombatLoss);
    entity.getEvents().addListener("bossCombatWin", this::onBossCombatWin);
    entity.getEvents().addListener("bossCombatLoss", this::onBossCombatLoss);
    entity.getEvents().addListener("Attack", this::onAttack);
    entity.getEvents().addListener("Guard", this::onGuard);
    entity.getEvents().addListener("Sleep", this::onSleep);
    entity.getEvents().addListener("Items", this::onItems);
    // ... other listeners ...
}

Key Methods

onCombatWin(Entity enemy)

Handles the event when the player wins a combat sequence:

private void onCombatWin(Entity enemy) {
    game.setEnemyWasBeaten(true);
    this.manager.getPlayer().getEvents().trigger("defeatedEnemy", this.manager.getEnemy());
    
    int enemyExp = enemy.getComponent(CombatStatsComponent.class).getExperience();
    manager.getPlayer().getComponent(CombatStatsComponent.class).addExperience(enemyExp);
    
    entity.getEvents().trigger("onCombatWin", manager.getPlayerStats());
    entity.getEvents().trigger("endOfCombatDialogue", enemy, true);
}

onCombatLoss(Entity enemy)

Handles the event when the player loses a combat sequence:

private void onCombatLoss(Entity enemy) {
    int lossExp = 0;
    int maxPlayerHealth = manager.getPlayerStats().getMaxHealth();
    int maxPlayerHunger = manager.getPlayerStats().getMaxHunger();
    int statusRestoreMult = 2;
    
    manager.getPlayerStats().setExperience(lossExp);
    this.manager.getPlayer().getComponent(PlayerInventoryDisplay.class).clearInventory();
    
    manager.getPlayerStats().setHealth(maxPlayerHealth/statusRestoreMult);
    manager.getPlayerStats().setHunger(maxPlayerHunger/statusRestoreMult);
    
    entity.getEvents().trigger("onCombatLoss", manager.getPlayerStats());
    entity.getEvents().trigger("endOfCombatDialogue", enemy, false);
}

Combat Action Methods

Methods like onAttack(), onGuard(), onSleep(), and onItems() handle specific combat actions:

private void onAttack(Screen screen, ServiceContainer container) {
    manager.onPlayerActionSelected("ATTACK");
    entity.getEvents().trigger("onAttack", manager.getPlayerStats(), manager.getEnemyStats());
}

private void onGuard(Screen screen, ServiceContainer container) {
    manager.onPlayerActionSelected("GUARD");
    entity.getEvents().trigger("onGuard", manager.getPlayerStats(), manager.getEnemyStats());
}

// Similar implementations for onSleep() and onItems()

Sequence Diagram

The following sequence diagram illustrates the flow of a typical combat action:

sequenceDiagram
    participant U as User
    participant CB as CombatButtonDisplay
    participant CA as CombatActions
    participant CM as CombatManager
    participant E as Entity
    participant CSD as CombatStatsDisplay

    U->>CB: Click Attack Button
    CB->>CA: Trigger "Attack" event
    CA->>CM: onPlayerActionSelected("ATTACK")
    CA->>E: Trigger "onAttack" event
    E->>CSD: Update display
    CM->>CA: Return combat result
    CA->>E: Trigger appropriate event (win/loss)
    E->>CSD: Update final stats

Loading

Usage

To use the CombatActions class:

  1. Create an instance of CombatActions with the required parameters.
  2. Add it as a component to the UI entity of the CombatScreen.
  3. The class will automatically handle combat-related events and transitions.

Example:

CombatActions combatActions = new CombatActions(game, combatManager, previousScreen, previousServices);
uiEntity.addComponent(combatActions);
Clone this wiki locally