diff --git a/source/core/assets/configs/Level1.json b/source/core/assets/configs/Level1.json new file mode 100644 index 000000000..f07bdf623 --- /dev/null +++ b/source/core/assets/configs/Level1.json @@ -0,0 +1,11 @@ +{ + "1": [ + {"Xeno": 5} + ], + "2": [ + {"Xeno": 7} + ], + "3" : [ + {"Xeno": 4} + ] +} \ No newline at end of file diff --git a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java index 1cb30384c..702f9d55d 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -181,7 +181,7 @@ public void create() { // Set up infrastructure for end game tracking player = spawnPlayer(); - waves = WaveFactory.createWave(); + waves = WaveFactory.createWaves(); spawnEntity(waves); waves.getEvents().addListener("spawnWave", this::spawnMob); diff --git a/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java b/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java new file mode 100644 index 000000000..d68d1c12f --- /dev/null +++ b/source/core/src/main/com/csse3200/game/components/tasks/waves/LevelWaves.java @@ -0,0 +1,56 @@ +package com.csse3200.game.components.tasks.waves; + +import com.badlogic.gdx.math.GridPoint2; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.services.GameTime; +import com.csse3200.game.services.ServiceLocator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Random; + +public class LevelWaves extends Entity { + List waves = new ArrayList<>(); + private float spawnDelay; + private GameTime gameTime; + private long startTime; + private Random rand = new Random(); + private int currentRandom = 0; + private int previousRandom = 0; + private int mobIndex; + private int waveIndex; + + public LevelWaves(int spawnDelay) { + this.spawnDelay = spawnDelay; + this.gameTime = ServiceLocator.getTimeSource(); + this.startTime = this.gameTime.getTime(); + this.mobIndex = 0; + this.waveIndex = 0; + } + + public void addWave(WaveClass wave) { + this.waves.add(wave); + } + + public WaveClass getWave(int index) { + return this.waves.get(index); + } + + public void spawnWave() { + if (gameTime.getTime() >= startTime + spawnDelay * 1000) { + do { + currentRandom = rand.nextInt(1, 7); + } while (currentRandom == previousRandom); + previousRandom = currentRandom; + GridPoint2 randomPos = new GridPoint2(19, currentRandom); + this.getEvents().trigger("spawnWave", waves.get(waveIndex) + .getMobs().get(mobIndex), randomPos); + startTime = gameTime.getTime(); + mobIndex++; + previousRandom = currentRandom; + } else if (mobIndex == waves.get(waveIndex).getSize()) { + this.getEvents().trigger("waveFinishedSpawning"); + } + } +} diff --git a/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveClass.java b/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveClass.java index 5370eeb70..f38d4e937 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveClass.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveClass.java @@ -8,22 +8,18 @@ import java.util.*; -public class WaveClass extends Entity { +public class WaveClass { private HashMap entities; - private float spawnDelay; private GameTime gameTime; private long startTime; private List wave; private Random rand = new Random(); - private int waveIndex; + private int mobIndex; - public WaveClass(HashMap entities, float spawnDelay) { + public WaveClass(HashMap entities) { this.entities = entities; - this.spawnDelay = spawnDelay; - this.gameTime = ServiceLocator.getTimeSource(); - this.startTime = this.gameTime.getTime(); this.wave = entitiesToWave(); - this.waveIndex = 0; + this.mobIndex = 0; } private List entitiesToWave() { @@ -37,24 +33,12 @@ private List entitiesToWave() { return enemies; } - public void spawnWave() { - System.out.println("This is triggering"); - if (gameTime.getTime() >= startTime + spawnDelay * 1000) { - GridPoint2 randomPos = new GridPoint2(19, rand.nextInt(1, 7)); - this.getEvents().trigger("spawnWave", wave.get(waveIndex), randomPos); - startTime = gameTime.getTime(); - waveIndex++; - } else if (waveIndex == wave.size()) { - this.getEvents().trigger("waveFinishedSpawning"); - } - } - public int getSize() { return this.wave.size(); } - public WaveClass getWave() { - return this; + public List getMobs() { + return this.wave; } } diff --git a/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveTask.java b/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveTask.java index 6d59d2742..d816969bf 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveTask.java @@ -13,7 +13,8 @@ public class WaveTask extends DefaultTask implements PriorityTask { private static final Logger logger = LoggerFactory.getLogger(WaveTask.class); - private List waves; + private LevelWaves level; + private WaveClass currentWave; private final GameTime globalTime; private int currentWaveIndex; private boolean waveInProgress; @@ -21,7 +22,6 @@ public class WaveTask extends DefaultTask implements PriorityTask { private float endTime = 0; private final float INITIAL_WAIT_INTERVAL = 10; private final int SPAWNING_INTERVAL = 10; - private WaveClass currentWave; public WaveTask() { this.globalTime = ServiceLocator.getTimeSource(); @@ -36,16 +36,14 @@ public int getPriority() { @Override public void start() { - System.out.println("this is starting"); super.start(); this.owner.getEntity().getEvents().addListener("waveFinishedSpawning", () -> waveInProgress = false); this.waveInProgress = true; - //this.currentWave = waves.get(currentWaveIndex); - WaveClass wave = (WaveClass) this.owner.getEntity(); - this.currentWave = wave.getWave(); + this.level = (LevelWaves) this.owner.getEntity(); + this.currentWave = level.getWave(currentWaveIndex); ServiceLocator.getWaveService().setEnemyCount(currentWave.getSize()); logger.info("Wave {} starting", currentWaveIndex); - endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000); + //endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000); } @Override @@ -54,12 +52,12 @@ public void update() { this.waveInProgress = true; logger.info("No enemies remaining, begin next wave"); currentWaveIndex++; - //this.currentWave = waves.get(currentWaveIndex); + this.currentWave = this.level.getWave(currentWaveIndex); //endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000L); // reset end time } else { logger.info("{} enemies remaining in wave {}", ServiceLocator.getWaveService().getEnemyCount(), currentWaveIndex); if (waveInProgress) { - currentWave.spawnWave(); + this.level.spawnWave(); } } } diff --git a/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java index 32fc21a52..6007ffb55 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/WaveFactory.java @@ -1,6 +1,7 @@ package com.csse3200.game.entities.factories; import com.csse3200.game.ai.tasks.AITaskComponent; +import com.csse3200.game.components.tasks.waves.LevelWaves; import com.csse3200.game.components.tasks.waves.WaveClass; import com.csse3200.game.components.tasks.waves.WaveTask; import com.csse3200.game.entities.Entity; @@ -13,13 +14,16 @@ public class WaveFactory { * Create a Wave entity. * @return entity */ - public static Entity createWave() { + public static Entity createWaves() { HashMap mobs = new HashMap<>(); mobs.put("Xeno", 5); + WaveClass wave = new WaveClass(mobs); + LevelWaves level = new LevelWaves(1); + level.addWave(wave); AITaskComponent aiComponent = new AITaskComponent() .addTask(new WaveTask()); - return new WaveClass(mobs, 1).addComponent(aiComponent); + return level.addComponent(aiComponent); } private WaveFactory() {