Skip to content

Spawning Bosses

Phurin Vanasrivilai edited this page Oct 17, 2024 · 3 revisions

Overview

Handle the event triggers and event listeners which spawn the boss into the main game (After completing required quests).

Implementation

In KeyboardPlayerInputComponent, add temporary triggers key press for testing the events which spawns the boss "spawn...Boss".

      case Keys.NUM_1: // TEMPORARY: Press 1 to spawn the land boss
        entity.getEvents().trigger("spawnLandBoss");
        return true;
      case Keys.NUM_2: // TEMPORARY: Press 2 to spawn the water boss
        entity.getEvents().trigger("spawnWaterBoss");
        return true;
      case Keys.NUM_3: // TEMPORARY: Press 3 to spawn the air boss
        entity.getEvents().trigger("spawnAirBoss");
        return true;

In ForestGameArea, the listeners "spawn...Boss" is added to listen for event which spawns the boss:

    @Override
    public void create() {
      .
      .
      .
      player.getEvents().addListener("spawnLandBoss", this::spawnKangarooBoss);
      player.getEvents().addListener("spawnWaterBoss", this::spawnWaterBoss);
      player.getEvents().addListener("spawnAirBoss", this::spawnAirBoss);
      .
      .
      .
    }

    private void spawnKangarooBoss() {
        if (!kangarooBossSpawned) {
            Entity kangarooBoss = BossFactory.createKangaBossEntity(player);
            kangarooBoss.getEvents().addListener("spawnJoey", this::spawnJoeyEnemy);
            spawnBossOnMap(kangarooBoss);
            bosses.add(kangarooBoss);
            kangarooBossSpawned = true;
        }
    }

    private void spawnWaterBoss() {
        if (!waterBossSpawned) {
            Entity waterBoss = BossFactory.createWaterBossEntity(player);
            waterBoss.getEvents().addListener("spawnWaterSpiral", this::spawnWaterSpiral);
            spawnBossOnMap(waterBoss);
            bosses.add(waterBoss);
            waterBossSpawned = true;
        }
    }

    private void spawnAirBoss() {
        if (!airBossSpawned) {
            Entity airBoss = BossFactory.createAirBossEntity(player);
            airBoss.getEvents().addListener("spawnWindGust", this::spawnWindGust);
            spawnBossOnMap(airBoss);
            bosses.add(airBoss);
            airBossSpawned = true;
        }
    }

the method spawnBossOnMap() determine which location the bosses will spawn in, usually will be around the gate going to the next area, with the exception for the Griffin (final boss) which spawns around the middle of its area.

    private void spawnBossOnMap(Entity entity) {
        GridPoint2 minPos = null;
        GridPoint2 maxPos = null;

        float tileSize = terrain.getTileSize();
        GridPoint2 tileBounds = terrain.getMapBounds(0);
        Vector2 worldBounds = new Vector2(tileBounds.x * tileSize, tileBounds.y * tileSize);

        int minGateX = (int) (worldBounds.x / 2) - 5;
        int maxGateX = (int) (worldBounds.x / 2) + 5;

        // Define spawn areas based on the boss type
        if (entity.getEnemyType() == Entity.EnemyType.KANGAROO) {
            // Spawn near the first barrier
            minPos = new GridPoint2(minGateX, (MAP_SIZE.y / 3 )- 10);
            maxPos = new GridPoint2(maxGateX, (MAP_SIZE.y / 3) - 5);
        } else if (entity.getEnemyType() == Entity.EnemyType.WATER_BOSS) {
            // Spawn near the second barrier
            minPos = new GridPoint2(minGateX, (MAP_SIZE.y / 3 * 2) - 10);
            maxPos = new GridPoint2(maxGateX, (MAP_SIZE.y / 3 * 2) - 5);
        } else if (entity.getEnemyType() == Entity.EnemyType.AIR_BOSS) {
            // Spawn at the top of the map
            minPos = new GridPoint2(minGateX, MAP_SIZE.y - 20);
            maxPos = new GridPoint2(maxGateX, MAP_SIZE.y - 15);
        }

        if (minPos != null) {
            // Generate a random position within the range
            GridPoint2 randomPos = RandomUtils.random(minPos, maxPos);
            // Spawn the entity at the calculated random position
            spawnEntityAt(entity, randomPos, true, true);
        }
    }

Testing Plan

Visual testing, with captioning to explain: https://youtu.be/Ny-1nAlynto

AttackOnAnimals._.Sprint.4.Boss._.Team.9.mp4

UML Diagrams

Sequence Diagram

Spawning Land Boss

ForestGameArea_spawnKangarooBoss

Spawning Water Boss

ForestGameArea_spawnWaterBoss

Spawning Air Boss

ForestGameArea_spawnAirBoss

Class Diagram

uml_boss_trigger

Clone this wiki locally