From 10024d3aa2ae3005060b5eb36447a5600af5ce29 Mon Sep 17 00:00:00 2001 From: max9753 Date: Tue, 17 Oct 2023 09:44:16 +1000 Subject: [PATCH 1/3] Fixed several code smells in WaveFactory.java --- .../game/entities/factories/WaveFactory.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) 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 53ea2007a..bf3c8f4be 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 @@ -130,9 +130,9 @@ public static LevelWaves createLevel(int chosenLevel) { int bossHealth; int minMobs; // Base health of the bosses - int LVL1_BOSS_BASE_HEALTH = 500; - int LVL2_BOSS_BASE_HEALTH = 1000; - int LVL3_BOSS_BASE_HEALTH = 2000; + final int LVL1_BOSS_BASE_HEALTH = 500; + final int LVL2_BOSS_BASE_HEALTH = 1000; + final int LVL3_BOSS_BASE_HEALTH = 2000; switch (chosenLevel) { case 2: @@ -155,7 +155,6 @@ public static LevelWaves createLevel(int chosenLevel) { break; } -// int totalMobs = 0; // Create mxWaves number of waves with mob stats increasing int atWave = 1; for (ArrayList wave : possibleMobs) { @@ -171,26 +170,23 @@ public static LevelWaves createLevel(int chosenLevel) { // Calculate the number of mobs for the wave if (leftToSort == 0) { num = minMobs - currentMobs; - System.out.println(num + " for " + mob + " at wave " + atWave); } else { num = rand.nextInt(minMobs - currentMobs - (2 * leftToSort) - 2) + 2; - System.out.println(num + " for " + mob + " at wave " + atWave); currentMobs += num; } // Calculate the health - int RANGE_BASE_HEALTH = 60; + final int RANGE_BASE_HEALTH = 60; int health = RANGE_BASE_HEALTH; if (MELEE_MOBS.contains(mob)) { // The base health for the different mobs - int MELEE_BASE_HEALTH = 80; + final int MELEE_BASE_HEALTH = 80; health = MELEE_BASE_HEALTH; } int[] mobStats = {num, health + (atWave * chosenLevel)}; mobs.put(mob, mobStats); leftToSort --; -// totalMobs += num; } minMobs ++; level.addWave(new WaveClass(mobs)); @@ -200,9 +196,7 @@ public static LevelWaves createLevel(int chosenLevel) { // Add boss wave HashMap bossMob = new HashMap<>(); bossMob.put(boss, new int[]{1, bossHealth}); -// totalMobs ++; -// ServiceLocator.getWaveService().setTotalMobs(totalMobs); level.addWave(new WaveClass(bossMob)); From b7a3f205b3124c8ee1e586c8a94f51748edab7ed Mon Sep 17 00:00:00 2001 From: max9753 Date: Tue, 17 Oct 2023 11:19:02 +1000 Subject: [PATCH 2/3] Added working + more tests to WaveServiceTest.java for the waveService --- .../game/services/WaveServiceTest.java | 84 ++++++++++++++----- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java index 6f33702c1..897128e83 100644 --- a/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java +++ b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java @@ -2,9 +2,12 @@ import com.badlogic.gdx.audio.Sound; import com.csse3200.game.areas.ForestGameArea; +import com.csse3200.game.components.maingame.UIElementsDisplay; import com.csse3200.game.components.tasks.waves.LevelWaves; import com.csse3200.game.components.tasks.waves.WaveTask; +import com.csse3200.game.currency.Scrap; import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.rendering.DebugRenderer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.extension.ExtendWith; @@ -16,41 +19,78 @@ 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.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; -@Disabled + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.mockito.junit.jupiter.MockitoExtension; + @ExtendWith(GameExtension.class) +@ExtendWith(MockitoExtension.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); + + UIElementsDisplay uiElementsDisplay = mock(UIElementsDisplay.class); + GameTime gameTime = mock(GameTime.class); + ServiceLocator.registerTimeSource(gameTime); + ServiceLocator.registerPhysicsService(new PhysicsService()); + RenderService render = new RenderService(); + render.setDebug(mock(DebugRenderer.class)); + ServiceLocator.registerRenderService(render); + ResourceService resourceService = mock(ResourceService.class); + ServiceLocator.registerResourceService(resourceService); + ServiceLocator.registerWaveService(new WaveService()); + } + + @Test + void testSetWaveCount() { + // setting the number of waves to be 5 will give you an index of 6 because the wave number + // goes from 1-6 instead of 0-5. Because players like to see "starting at level 1 not 0. + ServiceLocator.getWaveService().setWaveCount(5); + assertEquals(6, ServiceLocator.getWaveService().getWaveCount()); + } + + @Test + public void testSetLevelCompleted() { + ServiceLocator.getWaveService().setLevelCompleted(); + assertTrue(ServiceLocator.getWaveService().isLevelCompleted()); } @Test - void shouldSetNextWaveTime() { + void testSetNextLane() { + ServiceLocator.getWaveService().setNextLane(2); + assertEquals(2, ServiceLocator.getWaveService().getNextLane()); + } - waveTask.start(); - ServiceLocator.getWaveService().setEnemyCount(0); - waveTask.update(); + @Test + void testToggleDelay() { + ServiceLocator.getWaveService().toggleDelay(); + assertTrue(ServiceLocator.getWaveService().shouldSkip()); + } - assertTrue(ServiceLocator.getWaveService().getNextWaveTime() > 0); + @Test + void testSetTotalMobs() { + ServiceLocator.getWaveService().setTotalMobs(100); + assertEquals(100, ServiceLocator.getWaveService().totalMobs()); + assertEquals(100, ServiceLocator.getWaveService().remainingMobsForLevel()); + } + @Test + void testToggleGamePaused() { + // the toggle game paused method should flip the + // gamePaused variable + // This is checked with an assertEquals test. + boolean gamePaused = ServiceLocator.getWaveService().getGamePaused(); + ServiceLocator.getWaveService().toggleGamePause(); + assertEquals(!gamePaused, ServiceLocator.getWaveService().getGamePaused()); } + } From a22844d7c559b6864cdf2f8342e63730d7af3321 Mon Sep 17 00:00:00 2001 From: max9753 Date: Tue, 17 Oct 2023 11:21:49 +1000 Subject: [PATCH 3/3] Removed unused imports and fixed some smells. --- .../game/services/WaveServiceTest.java | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java index 897128e83..1c1b35260 100644 --- a/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java +++ b/source/core/src/test/com/csse3200/game/services/WaveServiceTest.java @@ -1,33 +1,15 @@ package com.csse3200.game.services; -import com.badlogic.gdx.audio.Sound; -import com.csse3200.game.areas.ForestGameArea; -import com.csse3200.game.components.maingame.UIElementsDisplay; -import com.csse3200.game.components.tasks.waves.LevelWaves; -import com.csse3200.game.components.tasks.waves.WaveTask; -import com.csse3200.game.currency.Scrap; import com.csse3200.game.extensions.GameExtension; import com.csse3200.game.rendering.DebugRenderer; 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.*; import static org.mockito.Mockito.mock; - - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; - -import org.junit.Before; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(GameExtension.class) @@ -36,8 +18,6 @@ public class WaveServiceTest { @BeforeEach void setUp() { - - UIElementsDisplay uiElementsDisplay = mock(UIElementsDisplay.class); GameTime gameTime = mock(GameTime.class); ServiceLocator.registerTimeSource(gameTime); ServiceLocator.registerPhysicsService(new PhysicsService());