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 index 3d4a30111..41b15a844 100644 --- 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 @@ -36,9 +36,8 @@ public LevelWaves(int spawnDelay) { long currentTime = ServiceLocator.getTimeSource().getTime(); // Setting the timestamp for when the next mobs will spawn. - // Currently, the delay of mobs spawning after wave start - // is hardcoded but will fix in the next push. - ServiceLocator.getWaveService().setNextWaveTime(currentTime + 10000); + // Currently, the delay of mobs spawning after wave start. + ServiceLocator.getWaveService().setNextWaveTime(currentTime + (spawnDelay * 1000)); } /** 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 68946aeb2..41f031858 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 @@ -29,7 +29,8 @@ public WaveClass(HashMap entities) { * Get the entities that are part of this wave and randomise the order they are spawned * @return mobs for the wave in form of (mob name, mob health) */ - private List entitiesToWave() { + + public List entitiesToWave() { List enemies = new ArrayList<>(); for (Map.Entry set : entities.entrySet()) { for (int i = 0; i < set.getValue()[0]; i++) { 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 360509c73..b98d7079d 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 @@ -98,8 +98,8 @@ public void update() { long currentTime = ServiceLocator.getTimeSource().getTime(); // Setting the timestamp for when the next mobs will spawn. // Currently, the delay of mobs spawning after wave start - // is hardcoded but will fix in the next push. - ServiceLocator.getWaveService().setNextWaveTime(currentTime + 10000); + int spawnDelay = ServiceLocator.getWaveService().getSpawnDelay(); + ServiceLocator.getWaveService().setNextWaveTime(currentTime + (spawnDelay * 1000)); // Check if level has been completed - no more waves remaining if (currentWaveIndex == this.level.getNumWaves()) { @@ -120,10 +120,10 @@ public void update() { } } else { -// logger.info("{} enemies remaining in wave {}", ServiceLocator.getWaveService().getEnemyCount(), currentWaveIndex); -// logger.info("WAVE SERVICE NUMBER: Wave Number {}",ServiceLocator.getWaveService().getWaveCount()); -// logger.info("NEXT WAVE AT {}", ServiceLocator.getWaveService().getNextWaveTime()); -// logger.info("TIME IS {}", ServiceLocator.getTimeSource().getTime()); + //logger.info("{} enemies remaining in wave {}", ServiceLocator.getWaveService().getEnemyCount(), currentWaveIndex); + //logger.info("WAVE SERVICE NUMBER: Wave Number {}",ServiceLocator.getWaveService().getWaveCount()); + //logger.info("NEXT WAVE AT {}", ServiceLocator.getWaveService().getNextWaveTime()); + //logger.info("TIME IS {}", ServiceLocator.getTimeSource().getTime()); if (waveInProgress) { this.level.spawnWave(); } @@ -137,4 +137,12 @@ public void update() { public boolean isWaveInProgress() { return waveInProgress; } + + /** + * Gets the current wave index + * @return current wave index + */ + public int getCurrentWaveIndex() { + return currentWaveIndex; + } } 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 c3376095d..0c7bac198 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 @@ -6,6 +6,7 @@ import com.csse3200.game.components.tasks.waves.WaveTask; import com.csse3200.game.entities.Entity; import com.csse3200.game.screens.GameLevelData; +import com.csse3200.game.services.ServiceLocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -97,10 +98,16 @@ public static LevelWaves createLevel(int maxDiff, int maxWaves, int chosenLevel) String boss1 = "IceBoss"; // String boss1 = "PatrickBoss"; String boss2 = "PatrickBoss"; - // String boss3 = "IceBoss"; - //TODO change this to a fire boss in sprint 4 - String boss3 = "FireBoss"; - LevelWaves level = new LevelWaves(5); + //String boss3 = "IceBoss"; + + String boss3 = "FireBoss"; + + int spawnDelay = 5; + + // Create new level entity with spawn delay of 5 seconds + LevelWaves level = new LevelWaves(spawnDelay); + // Tell the waveService what the spawn delay for levels will be (for UI team). + ServiceLocator.getWaveService().setSpawnDelay(spawnDelay); ArrayList possibleMobs; diff --git a/source/core/src/main/com/csse3200/game/services/WaveService.java b/source/core/src/main/com/csse3200/game/services/WaveService.java index 0e50a7fc1..c00693fbd 100644 --- a/source/core/src/main/com/csse3200/game/services/WaveService.java +++ b/source/core/src/main/com/csse3200/game/services/WaveService.java @@ -17,6 +17,8 @@ public class WaveService { private long nextWaveTime; private final UIElementsDisplay display; + private int spawnDelay; + /** * Constructor for the Wave Service @@ -122,7 +124,19 @@ public void setNextWaveTime(long nextWaveTime) { } /** - * Used for adding this instance of UIElementsDisplay to the mainGameScreen. This is needed as update is performed + * Sets the spawn delay between levels + * @param spawnDelay representing the spawnDelay between levels. + */ + public void setSpawnDelay(int spawnDelay) {this.spawnDelay = spawnDelay;} + + /** + * Returns the spawn delay between levels + * @return (int) The spawn delay between levels. + */ + public int getSpawnDelay() {return this.spawnDelay;} + + + /* Used for adding this instance of UIElementsDisplay to the mainGameScreen. This is needed as update is performed * for this instance of the display. * @return the updating instance of UIElementsDisplay */ diff --git a/source/core/src/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java b/source/core/src/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java index 39ddd78ec..abf459c8f 100644 --- a/source/core/src/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java +++ b/source/core/src/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java @@ -1,7 +1,10 @@ package com.csse3200.game.components.tasks.waves; +import com.csse3200.game.components.tasks.waves.LevelWaves; +import com.csse3200.game.components.tasks.waves.WaveClass; import com.csse3200.game.entities.factories.WaveFactory; import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.services.GameTime; import com.csse3200.game.services.ServiceLocator; import org.junit.Test; import org.junit.jupiter.api.BeforeEach; @@ -10,8 +13,13 @@ import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; + +import static org.mockito.Mockito.when; + @Disabled + @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) class LevelWavesTest { @@ -19,21 +27,36 @@ class LevelWavesTest { LevelWaves levelWaves; WaveClass wave; ServiceLocator serviceLocator; + GameTime gameTime; @BeforeEach void setUp() { - levelWaves = (LevelWaves) WaveFactory.createWaves(); + levelWaves = mock(LevelWaves.class); wave = mock(WaveClass.class); + gameTime = mock(GameTime.class); } @Test public void testAddWave() { + when(levelWaves.getNumWaves()).thenReturn(1); + int size = levelWaves.getNumWaves(); levelWaves.addWave(wave); - assertEquals(3, levelWaves.waves.size()); + assertEquals(size + 1, levelWaves.getNumWaves()); } @Test - public void testSpawnWaveStart() { + public void testSpawnWave() { + levelWaves.setWaveIndex(1); + levelWaves.spawnWave(); + WaveClass thisWave = levelWaves.getWave(levelWaves.getWaveIndex()); + assertTrue(thisWave.getSize() > 0); + assertTrue(!thisWave.getMobs().isEmpty()); + } + @Test + public void testGetStartTime() { + when(levelWaves.getStartTime()).thenReturn(1000L); + assertEquals(1000L, levelWaves.getStartTime()); } + } diff --git a/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveClassTest.java b/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveClassTest.java index aa054e325..abc70a3a2 100644 --- a/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveClassTest.java +++ b/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveClassTest.java @@ -1,7 +1,51 @@ package com.csse3200.game.components.tasks.waves; + +import com.csse3200.game.components.tasks.waves.WaveClass; +import com.csse3200.game.extensions.GameExtension; +import org.junit.Assert; +import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Disabled; +@ExtendWith(GameExtension.class) +@ExtendWith(MockitoExtension.class) + @Disabled class WaveClassTest { + + HashMap waveContents; + WaveClass waveClass; + @BeforeEach + void setUp() { + waveContents = new HashMap<>(); + waveContents.put("Xeno", new int[]{2, 20}); + waveContents.put("DodgingDragon", new int[]{3, 40}); + waveContents.put("SplittingXeno", new int[]{5, 60}); + waveContents.put("DeflectXeno", new int[]{7, 80}); + waveClass = new WaveClass(waveContents); + } + + @Test + public void testGetMobs() { + List enemies = waveClass.getMobs(); + assertTrue(enemies.contains("Xeno") + && enemies.contains("DodgingDragon") + && enemies.contains("SplittingXeno") + && enemies.contains("DeflectXeno")); + } + + @Test + public void testGetSize() { + int size = waveClass.getSize(); + assertEquals(17, size); + } } diff --git a/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveTaskTest.java b/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveTaskTest.java index 7b95dedae..a47359ffc 100644 --- a/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveTaskTest.java +++ b/source/core/src/test/com/csse3200/game/components/tasks/waves/WaveTaskTest.java @@ -1,9 +1,13 @@ package com.csse3200.game.components.tasks.waves; import com.badlogic.gdx.audio.Sound; +import com.csse3200.game.ai.tasks.PriorityTask; +import com.csse3200.game.ai.tasks.Task; import com.csse3200.game.areas.ForestGameArea; import com.csse3200.game.areas.terrain.TerrainFactory; import com.csse3200.game.components.tasks.DroidCombatTask; +import com.csse3200.game.components.tasks.waves.LevelWaves; +import com.csse3200.game.components.tasks.waves.WaveTask; import com.csse3200.game.extensions.GameExtension; import com.csse3200.game.services.GameTime; import com.csse3200.game.services.ResourceService; @@ -52,10 +56,34 @@ public void testGetPriority() { } @Test - public void testStartWave() { + public void testStartFirstWave() { waveTask.start(); - assertEquals(1, waveTask.getPriority()); + assertEquals(10, waveTask.getPriority()); assertTrue(waveTask.isWaveInProgress()); + assertEquals(1, waveTask.getCurrentWaveIndex()); + } + + @Test + public void testIsWaveInProgress() { + waveTask.start(); + assertTrue(waveTask.isWaveInProgress()); + } + + @Test + public void testUpdateOnEmptyWaveAndNoNextWave() { + waveTask.start(); + ServiceLocator.getWaveService().setEnemyCount(0); + waveTask.update(); + assertTrue(ServiceLocator.getWaveService().isLevelCompleted()); + } + + @Test + public void testUpdateOnEmptyWaveAndNextWave() { + waveTask.start(); + int waveNumber = waveTask.getCurrentWaveIndex(); + ServiceLocator.getWaveService().setEnemyCount(0); + waveTask.update(); + assertTrue(waveNumber + 1 == waveTask.getCurrentWaveIndex()); } } diff --git a/source/core/src/test/com/csse3200/game/entities/factories/WaveFactoryTest.java b/source/core/src/test/com/csse3200/game/entities/factories/WaveFactoryTest.java index 8e43b601e..7b45d6c87 100644 --- a/source/core/src/test/com/csse3200/game/entities/factories/WaveFactoryTest.java +++ b/source/core/src/test/com/csse3200/game/entities/factories/WaveFactoryTest.java @@ -1,5 +1,6 @@ package com.csse3200.game.entities.factories; +import com.csse3200.game.components.tasks.waves.LevelWaves; import com.badlogic.gdx.assets.AssetManager; import com.csse3200.game.components.tasks.waves.LevelWaves; import com.csse3200.game.components.tasks.waves.WaveClass; diff --git a/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java new file mode 100644 index 000000000..6f33702c1 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java @@ -0,0 +1,56 @@ +package com.csse3200.game.services; + +import com.badlogic.gdx.audio.Sound; +import com.csse3200.game.areas.ForestGameArea; +import com.csse3200.game.components.tasks.waves.LevelWaves; +import com.csse3200.game.components.tasks.waves.WaveTask; +import com.csse3200.game.extensions.GameExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.extension.ExtendWith; + +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.physics.PhysicsService; +import com.csse3200.game.rendering.RenderService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; + +@Disabled +@ExtendWith(GameExtension.class) +public class WaveServiceTest { + + WaveTask waveTask; + ResourceService resourceService; + LevelWaves level; + + WaveService waveService; + @BeforeEach + void setUp() { + resourceService = ServiceLocator.getResourceService(); + waveService = ServiceLocator.getWaveService(); + GameTime globalTime = mock(GameTime.class); + level = mock(LevelWaves.class); + ServiceLocator.registerTimeSource(globalTime); + waveTask = new WaveTask(); + String[] sounds = waveTask.getSounds(); + resourceService.getAsset(sounds[0], Sound.class); + resourceService.getAsset(sounds[1], Sound.class); + } + + @Test + void shouldSetNextWaveTime() { + + waveTask.start(); + ServiceLocator.getWaveService().setEnemyCount(0); + waveTask.update(); + + assertTrue(ServiceLocator.getWaveService().getNextWaveTime() > 0); + + } + +}