From 7435aafae566f9fb41abe9c367dc63ed8d2905b2 Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 3 Oct 2023 10:48:30 +1000 Subject: [PATCH 1/4] Wrote tests for waves test suite --- .../components/tasks/waves/WaveClass.java | 2 +- .../game/components/tasks/waves/WaveTask.java | 8 ++++ .../tasks/waves/LevelWavesTest.java | 40 ++++++++++++++-- .../components/tasks/waves/WaveClassTest.java | 47 ++++++++++++++++++- .../components/tasks/waves/WaveTaskTest.java | 36 ++++++++++++-- 5 files changed, 121 insertions(+), 12 deletions(-) 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 f5d3a9f41..cbe5fba2f 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 @@ -30,7 +30,7 @@ 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 */ - private List entitiesToWave() { + public List entitiesToWave() { List enemies = new ArrayList<>(); for (Map.Entry set : entities.entrySet()) { for (int i = 0; i < set.getValue(); 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 5821e6e01..01f578afe 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 @@ -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/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java b/source/core/src/test/com/csse3200/game/components/tasks/waves/LevelWavesTest.java index da0f54eff..b62fa0b6f 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; +package com.csse3200.game.components.tasks; +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; @@ -9,30 +12,57 @@ 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; @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) -class LevelWavesTest { +public class LevelWavesTest { LevelWaves levelWaves; WaveClass wave; ServiceLocator serviceLocator; + GameTime gameTime; @BeforeEach void setUp() { - levelWaves = (LevelWaves) WaveFactory.createWaves(); + levelWaves = new LevelWaves(2); 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 testGetNumWaves() { + assertTrue(levelWaves.getNumWaves() > 0); + } + + @Test + public void testGetStartTime() { + when(gameTime.getTime()).thenReturn(1000L); + assertEquals(1000L, levelWaves.getStartTime()); } + + @Test + public void fail() { + assertTrue(false); + } + } 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 e811e68ef..691a3e156 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,4 +1,47 @@ -package com.csse3200.game.components.tasks.waves; +package com.csse3200.game.components.tasks; -class WaveClassTest { +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; + +@ExtendWith(GameExtension.class) +@ExtendWith(MockitoExtension.class) +public class WaveClassTest { + + HashMap waveContents; + WaveClass waveClass; + @BeforeEach + void setUp() { + waveContents = new HashMap<>(); + waveContents.put("Xeno", 2); + waveContents.put("DodgingDragon", 3); + waveContents.put("SplittingXeno", 5); + waveContents.put("DeflectXeno", 7); + 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 0926c9e9b..155013e27 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; +package com.csse3200.game.components.tasks; 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; @@ -22,7 +26,7 @@ @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) -class WaveTaskTest { +public class WaveTaskTest { WaveTask waveTask; ResourceService resourceService; @@ -51,10 +55,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()); } } From 783c425981c242a3727b7e911d35784269923bfb Mon Sep 17 00:00:00 2001 From: BlairCannon97 Date: Tue, 3 Oct 2023 10:48:52 +1000 Subject: [PATCH 2/4] Wrote tests for waves test suite --- .../game/components/tasks/waves/LevelWavesTest.java | 13 ++++--------- .../game/components/tasks/waves/WaveClassTest.java | 4 ++-- .../game/components/tasks/waves/WaveTaskTest.java | 4 ++-- .../game/entities/factories/WaveFactoryTest.java | 3 ++- 4 files changed, 10 insertions(+), 14 deletions(-) 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 b62fa0b6f..653c6fd09 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,4 +1,4 @@ -package com.csse3200.game.components.tasks; +package com.csse3200.game.components.tasks.waves; import com.csse3200.game.components.tasks.waves.LevelWaves; import com.csse3200.game.components.tasks.waves.WaveClass; @@ -18,7 +18,7 @@ @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) -public class LevelWavesTest { +class LevelWavesTest { LevelWaves levelWaves; WaveClass wave; @@ -27,7 +27,7 @@ public class LevelWavesTest { @BeforeEach void setUp() { - levelWaves = new LevelWaves(2); + levelWaves = mock(LevelWaves.class); wave = mock(WaveClass.class); gameTime = mock(GameTime.class); } @@ -56,13 +56,8 @@ public void testGetNumWaves() { @Test public void testGetStartTime() { - when(gameTime.getTime()).thenReturn(1000L); + when(levelWaves.getStartTime()).thenReturn(1000L); assertEquals(1000L, levelWaves.getStartTime()); } - @Test - public void fail() { - assertTrue(false); - } - } 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 691a3e156..08abdfbb0 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,4 +1,4 @@ -package com.csse3200.game.components.tasks; +package com.csse3200.game.components.tasks.waves; import com.csse3200.game.components.tasks.waves.WaveClass; import com.csse3200.game.extensions.GameExtension; @@ -16,7 +16,7 @@ @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) -public class WaveClassTest { +class WaveClassTest { HashMap waveContents; WaveClass waveClass; 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 155013e27..a4dd28248 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,4 +1,4 @@ -package com.csse3200.game.components.tasks; +package com.csse3200.game.components.tasks.waves; import com.badlogic.gdx.audio.Sound; import com.csse3200.game.ai.tasks.PriorityTask; @@ -26,7 +26,7 @@ @ExtendWith(GameExtension.class) @ExtendWith(MockitoExtension.class) -public class WaveTaskTest { +class WaveTaskTest { WaveTask waveTask; ResourceService resourceService; 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 d154e1a6b..b205fa6c0 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.csse3200.game.extensions.GameExtension; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -11,6 +12,6 @@ class WaveFactoryTest { @BeforeEach void setUp() { - + LevelWaves levelWaves = (LevelWaves) WaveFactory.createWaves(); } } From 2699c1e2b0801fc4562c6c19aa43adc0f39a80b0 Mon Sep 17 00:00:00 2001 From: max9753 Date: Tue, 3 Oct 2023 12:06:28 +1000 Subject: [PATCH 3/4] Changed functionality of getNextWaveTime to work more dynamically. Previously spawnDelay was hardcoded. Spawn delay can also be accessed by the WaveService if the UI or other teams want to use it. Added WaveServiceTest.java as well. --- .../components/tasks/waves/LevelWaves.java | 5 +- .../game/components/tasks/waves/WaveTask.java | 12 ++-- .../game/entities/factories/WaveFactory.java | 9 ++- .../csse3200/game/services/WaveService.java | 15 +++++ .../game/services/WaveServiceTest.java | 56 +++++++++++++++++++ 5 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 source/core/src/test/com/csse3200/game/services/WaveServiceTest.java 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/WaveTask.java b/source/core/src/main/com/csse3200/game/components/tasks/waves/WaveTask.java index 8c75b46c5..0601fd239 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(); } 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 b54575d4e..572dff0a4 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; @@ -95,7 +96,13 @@ public static LevelWaves createLevel(int maxDiff, int maxWaves, int chosenLevel) String boss3 = "IceBoss"; //TODO change this to a fire boss in sprint 4 // String boss3 = "FireBoss"; - LevelWaves level = new LevelWaves(5); + + 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 cd26acf8b..adcd8177e 100644 --- a/source/core/src/main/com/csse3200/game/services/WaveService.java +++ b/source/core/src/main/com/csse3200/game/services/WaveService.java @@ -15,6 +15,8 @@ public class WaveService { private long nextWaveTime; + private int spawnDelay; + /** * Constructor for the Wave Service @@ -116,4 +118,17 @@ public long getNextWaveTime() { public void setNextWaveTime(long nextWaveTime) { this.nextWaveTime = nextWaveTime; } + + /** + * 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;} + } 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); + + } + +} From 456c8db5460a23a599eb33bcd8e205fe90d03b43 Mon Sep 17 00:00:00 2001 From: Samantha Sullivan Date: Tue, 3 Oct 2023 14:32:55 +1000 Subject: [PATCH 4/4] fixed parsing error --- .../core/src/main/com/csse3200/game/services/WaveService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2d019a465..c00693fbd 100644 --- a/source/core/src/main/com/csse3200/game/services/WaveService.java +++ b/source/core/src/main/com/csse3200/game/services/WaveService.java @@ -136,7 +136,7 @@ public void setNextWaveTime(long nextWaveTime) { public int getSpawnDelay() {return this.spawnDelay;} - * Used for adding this instance of UIElementsDisplay to the mainGameScreen. This is needed as update is performed + /* 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 */