CombatArea - UQcsse3200/2024-studio-2 GitHub Wiki

The CombatArea class represents the combat arena in the game where battles between the player and enemies take place. It is responsible for creating the combat environment, spawning entities, and managing the overall combat experience.

image

Class Diagram

classDiagram
    class GameArea {
        <<abstract>>
        +create()
        +dispose()
        +getPlayer(): Entity
        +getEnemies(): List<Entity>
        +getBosses(): List<Entity>
        +getFriendlyNPCs(): List<Entity>
        +getMinigameNPCs(): List<Entity>
    }
    class CombatArea {
        -GdxGame game
        -Entity player
        -Entity playerDisplay
        -GridPoint2 playerSpawn
        -CombatTerrainFactory combatTerrainFactory
        -Entity enemy
        -Entity enemyDisplay
        -KINGDOM kingdomType
        +CombatArea(Entity player, Entity enemy, GdxGame game, CombatTerrainFactory terrainFactory)
        +create()
        -displayUI()
        -spawnTerrain()
        -spawnPlayer()
        -spawnCameraInvisibleEntity()
        -spawnCombatEnemy()
        -spawnKangaBoss()
        -spawnWaterBoss()
        -spawnAirBoss()
        -spawnChicken()
        -spawnFrog()
        -spawnMonkey()
        -spawnBear()
        -spawnMacaw()
        -spawnBee()
        -spawnOctopus()
        -spawnPigeon()
        -spawnEel()
        -spawnBigSawfish()
        -spawnJoey()
        +startEnemyAnimation(CombatAnimation animation)
        +startPlayerAnimation(CombatAnimation animation)
        +playMusic()
        +pauseMusic()
        -loadAssets()
        +unloadAssets()
        +dispose()
    }
    GameArea <|-- CombatArea
Loading

Key Components

  1. Player: The player entity in the combat area.
  2. Enemy: The enemy entity that the player is fighting against.
  3. Terrain: The combat arena's background and environment.
  4. Kingdom Type: Determines the type of combat area (LAND, AIR, or WATER).

Main Functionality

Initialization

The CombatArea is initialized with the player, enemy, game instance, and terrain factory:

public CombatArea(Entity player, Entity enemy, GdxGame game, CombatTerrainFactory terrainFactory) {
    super();
    this.game = game;
    this.combatTerrainFactory = terrainFactory;
    this.player = player;
    this.enemy = enemy;
}

Creating the Combat Area

The create() method sets up the entire combat area:

@Override
public void create() {
    loadAssets();
    displayUI();
    spawnTerrain();
    spawnEnemy();
    spawnPlayer();
    playMusic();
}

Spawning Entities

The CombatArea class provides methods to spawn various entities:

  • spawnPlayer(): Spawns the player entity in the combat area.
  • spawnCombatEnemy(): Spawns a generic combat enemy.
  • spawnKangaBoss(), spawnWaterBoss(), spawnAirBoss(): Spawn boss entities.
  • spawnChicken(), spawnFrog(), spawnMonkey(), etc.: Spawn specific enemy types.

Example of spawning a chicken enemy:

private void spawnChicken() {
    Entity newEnemy = CombatAnimalFactory.createChickenCombatEnemy();
    spawnEntityAt(newEnemy, new GridPoint2(760, 190), true, true);
    this.enemyDisplay = newEnemy;
}

Animations

The class provides methods to start animations for both the player and enemy:

public void startEnemyAnimation(CombatAnimation animation) {
    switch (animation) {
        case IDLE:
            enemyDisplay.getEvents().trigger("idleLeft");
            break;
        case MOVE:
            enemyDisplay.getEvents().trigger("moveLeft");
            break;
    }
}

Music Management

The CombatArea class handles playing and pausing music for the combat scene:

public void playMusic() {
    AudioManager.stopMusic();
    AudioManager.playMusic(CombatAreaConfig.COMBATBACKGROUND_MUSIC, true);
}

public void pauseMusic() {
    AudioManager.stopMusic();
}

Sequence Diagram

sequenceDiagram
    participant Game
    participant CombatArea
    participant CombatTerrainFactory
    participant EntityFactory
    participant AudioManager

    Game->>CombatArea: new CombatArea(player, enemy, game, terrainFactory)
    Game->>CombatArea: create()
    CombatArea->>CombatArea: loadAssets()
    CombatArea->>CombatArea: displayUI()
    CombatArea->>CombatTerrainFactory: createBackgroundTerrain()
    CombatTerrainFactory-->>CombatArea: terrain
    CombatArea->>EntityFactory: createCombatEnemy()
    EntityFactory-->>CombatArea: enemy
    CombatArea->>EntityFactory: createCombatPlayer()
    EntityFactory-->>CombatArea: player
    CombatArea->>AudioManager: playMusic()
    Game->>CombatArea: startEnemyAnimation(IDLE)
    CombatArea->>CombatArea: enemyDisplay.getEvents().trigger("idleLeft")
    Game->>CombatArea: startPlayerAnimation(MOVE)
    CombatArea->>CombatArea: playerDisplay.getEvents().trigger("moveRight")
    Game->>CombatArea: dispose()
    CombatArea->>AudioManager: stopMusic()
    CombatArea->>CombatArea: unloadAssets()

Loading

Usage

To create and use a CombatArea:

  1. Initialize the CombatArea with the necessary parameters.
  2. Call the create() method to set up the combat environment.
  3. Use startEnemyAnimation() and startPlayerAnimation() to trigger animations during combat.
  4. Manage music with playMusic() and pauseMusic().
  5. Call dispose() when the combat area is no longer needed.

Example:

CombatTerrainFactory terrainFactory = new CombatTerrainFactory();
Entity player = PlayerFactory.createPlayer();
Entity enemy = EnemyFactory.createEnemy();
GdxGame game = new GdxGame();

CombatArea combatArea = new CombatArea(player, enemy, game, terrainFactory);
combatArea.create();

// During combat
combatArea.startPlayerAnimation(CombatArea.CombatAnimation.MOVE);
combatArea.startEnemyAnimation(CombatArea.CombatAnimation.IDLE);

// When combat is over
combatArea.dispose();

Notes

  • The CombatArea class extends the abstract GameArea class.
  • It uses a CombatTerrainFactory to create the combat environment.
  • Different enemy types can be spawned based on the game's requirements.
  • The class manages its own assets, including loading and unloading them.
  • Music and sound effects are handled through the AudioManager.
⚠️ **GitHub.com Fallback** ⚠️