Spawning Bosses - UQcsse3200/2024-studio-2 GitHub Wiki
Overview
Handle the event triggers and event listeners which spawn the boss into the main game (After completing required quests).
Implementation
KeyboardPlayerInputComponent
, add temporary triggers key press for testing the events which spawns the boss "spawn...Boss".
In 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;
ForestGameArea
, the listeners "spawn...Boss" is added to listen for event which spawns the boss:
In @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;
}
}
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.
the method 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
https://github.com/user-attachments/assets/a0d78e65-9267-49a6-926c-17d8d36f4856