Skip to content

Combat Screen

Paul edited this page Oct 21, 2024 · 7 revisions

Overview

Upon collision with an enemy, a new instance of CombatScreen is created in the GdxGame class, with the player and enemy (as instances of the Entity class) involved in the collision being passed in. An instance of CombatScreen is created with the following essential components:

  • CombatArea (extends GameArea)
  • CombatActions: central event listener and handler.
  • CombatManager: manages combat logic.
  • CombatExitDisplay: exit buttons to return to the main game.
  • CombatStatsDisplay: UI for player and enemy's stats.
  • CombatButtonDisplay: Buttons used to select player's combat move.

Class Diagram

Here's an updated class diagram showing the key components of the Combat Screen:

classDiagram
    class CombatScreen {
        -GdxGame game
        -PhysicsEngine physicsEngine
        -Screen oldScreen
        -ServiceContainer oldScreenServices
        -Entity player
        -Entity enemy
        -CombatArea combatArea
        +render(float delta)
        +resize(int width, int height)
        +pause()
        +resume()
        +dispose()
        -loadAssets()
        -unloadAssets()
        -createUI()
        -checkEnemyDeath()
    }
    class CombatArea {
        +create()
        +spawnTerrain()
    }
    class CombatManager {
        +CombatManager(Entity player, Entity enemy)
    }
    class CombatActions {
        +CombatActions(GdxGame game, CombatManager manager, Screen oldScreen, ServiceContainer oldScreenServices)
    }
    class CombatStatsDisplay {
        +CombatStatsDisplay(CombatStatsComponent playerStats, CombatStatsComponent enemyStats)
    }
    class CombatButtonDisplay {
        +CombatButtonDisplay(Screen oldScreen, ServiceContainer oldScreenServices, CombatArea combatArea)
    }

    CombatScreen --> CombatArea
    CombatScreen --> CombatManager
    CombatScreen --> CombatActions
    CombatScreen --> CombatStatsDisplay
    CombatScreen --> CombatButtonDisplay
Loading

Entering Combat Sequence of Events

This section covers how a combat screen is triggered and resolved. This example will use The interaction between Player and default game Entity Ghost and will reference from the simplified UML class diagram below: The sequence of events is as followed:

  • The event system is used to trigger an event "startCombat" that signals the start of moving over to the Combat Screen. The trigger "startCombat" is attached to the TouchAttackComponent that the Ghost has as a component by default
  • The PlayerActions which has a listener for "startCombat" that the Player has as a component by default that calls the function startCombat(Entity enemyToFight) which calls game.addCombatScreen(enemyToFight)
  • a new Screen CombatScreen is created in GdxGame that saves the state of the game before setting the screen to be CombatScreen

Here's a sequence diagram illustrating this process:

sequenceDiagram
    participant Player
    participant Ghost
    participant TouchAttackComponent
    participant PlayerActions
    participant GdxGame
    participant CombatScreen

    Ghost->>TouchAttackComponent: Collision detected
    TouchAttackComponent->>PlayerActions: Trigger "startCombat" event
    PlayerActions->>PlayerActions: startCombat(Entity enemyToFight)
    PlayerActions->>GdxGame: addCombatScreen(enemyToFight)
    GdxGame->>CombatScreen: new CombatScreen(game, oldScreen, services, player, enemy)
    CombatScreen->>CombatScreen: createUI()
    CombatScreen->>CombatScreen: loadAssets()
    GdxGame->>CombatScreen: setScreen()
Loading

Combat Screen

The combat screen is the screen that will be loaded when players are required to engage in a fight or combat sequence against enemies. It will be loaded from the main game screen once the player comes into contact with an enemy character. This is achieved through adding a listener to Class PlayerActions and adding a trigger to another Entity's component such as the class TouchAttackComponent.

Class Combat Screen

  • Loads combat screen and instantiates resource, physics, and rendering services.
  • Contains code for button functionality and will link button functionality to combat logic function/class later on
  • Utilises CombatGameArea to render individual components to screen e.g. entities, obstacles, and assets
  • see Rough Wireframe Design for Combat Screen below

Here's a simplified example of the CombatScreen class:

public class CombatScreen extends ResizableScreen {
    private final GdxGame game;
    private final PhysicsEngine physicsEngine;
    private final Entity player;
    private final Entity enemy;
    private final CombatArea combatArea;

    public CombatScreen(GdxGame game, Screen screen, ServiceContainer container, Entity player, Entity enemy) {
        // ... initialization
    }

    @Override
    public void render(float delta) {
        if (!isPaused) {
            physicsEngine.update();
            super.render(delta);
            checkEnemyDeath();
        }
    }

    private void createUI() {
        // ... create UI components
    }

    private void checkEnemyDeath() {
        if (enemyCombatStats.getHealth() <= 0) {
            game.setScreen(new EnemyTransitionCutSceneScreen(game, this.enemy));
        }
    }
}

Class Combat Game Area

  • Handles spawning of individual entities and obstacles to combat screen

Associated Files Added

  • source/core/assets/images/land_background.png
  • source/core/assets/images/water_background.png
  • source/core/assets/images/air_background.png

Final Combat Screen Design - example below is for the Water Kingdom image

Asset Management

The CombatScreen handles loading and unloading of combat-related assets:

private static final String[] combatTextures = {
    "images/heart.png", "images/PauseOverlay/TitleBG.png", "images/PauseOverlay/Button.png",
    // ... (other textures)
};

private void loadAssets() {
    ResourceService resourceService = ServiceLocator.getResourceService();
    resourceService.loadTextures(combatTextures);
    ServiceLocator.getResourceService().loadAll();
}

private void unloadAssets() {
    ResourceService resourceService = ServiceLocator.getResourceService();
    resourceService.unloadAssets(combatTextures);
}

Video Testing

Extending Combat Functionality

To add new combat actions or enemy types:

  1. Create new action classes that implement a common interface (e.g., CombatAction).
  2. Add new buttons or UI elements to the CombatButtonDisplay for new actions.
  3. Implement the logic for new actions in the CombatManager class.
  4. For new enemy types, create new Entity subclasses with appropriate components and AI behavior.

By understanding these components and their interactions, developers can easily modify and extend the combat system to add new features or adjust existing gameplay mechanics.

Clone this wiki locally