Skip to content

Commit

Permalink
Adds LevelWaves class, which can store and spawn waves
Browse files Browse the repository at this point in the history
  • Loading branch information
bojyyy committed Sep 27, 2023
1 parent e3ef337 commit 477e2fc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
11 changes: 11 additions & 0 deletions source/core/assets/configs/Level1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"1": [
{"Xeno": 5}
],
"2": [
{"Xeno": 7}
],
"3" : [
{"Xeno": 4}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<WaveClass> 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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
import java.util.*;


public class WaveClass extends Entity {
public class WaveClass {
private HashMap<String, Integer> entities;
private float spawnDelay;
private GameTime gameTime;
private long startTime;
private List<String> wave;
private Random rand = new Random();
private int waveIndex;
private int mobIndex;

public WaveClass(HashMap<String, Integer> entities, float spawnDelay) {
public WaveClass(HashMap<String, Integer> 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<String> entitiesToWave() {
Expand All @@ -37,24 +33,12 @@ private List<String> 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<String> getMobs() {
return this.wave;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

public class WaveTask extends DefaultTask implements PriorityTask {
private static final Logger logger = LoggerFactory.getLogger(WaveTask.class);
private List<WaveClass> waves;
private LevelWaves level;
private WaveClass currentWave;
private final GameTime globalTime;
private int currentWaveIndex;
private boolean waveInProgress;
private float startTime = 0;
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();
Expand All @@ -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
Expand All @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,13 +14,16 @@ public class WaveFactory {
* Create a Wave entity.
* @return entity
*/
public static Entity createWave() {
public static Entity createWaves() {
HashMap<String, Integer> 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() {
Expand Down

0 comments on commit 477e2fc

Please sign in to comment.