diff --git a/source/core/src/main/com/csse3200/game/components/maingame/MainGamePauseDisplay.java b/source/core/src/main/com/csse3200/game/components/maingame/MainGamePauseDisplay.java index eb8a0b273..cf2f129ed 100644 --- a/source/core/src/main/com/csse3200/game/components/maingame/MainGamePauseDisplay.java +++ b/source/core/src/main/com/csse3200/game/components/maingame/MainGamePauseDisplay.java @@ -19,11 +19,12 @@ public class MainGamePauseDisplay extends UIComponent { private static final Logger logger = LoggerFactory.getLogger(MainGamePauseDisplay.class); private static final float Z_INDEX = 2f; private Table table; - private GdxGame game; + private Entity waveSpawner; - public MainGamePauseDisplay(GdxGame screenSwitchHandle) { + public MainGamePauseDisplay(GdxGame screenSwitchHandle, Entity waveSpawner) { game = screenSwitchHandle; + this.waveSpawner = waveSpawner; } @Override @@ -45,7 +46,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Pause button clicked"); - PauseMenuFactory.createPauseMenu(game); + PauseMenuFactory.createPauseMenu(game, waveSpawner); } }); diff --git a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponent.java b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponent.java index 285fad8e2..6503add30 100644 --- a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponent.java +++ b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponent.java @@ -11,7 +11,14 @@ public class PauseMenuTimeStopComponent extends Component { private Array freezeList; - public PauseMenuTimeStopComponent() { + private Entity waveSpawner; + + /** + * Initialises the component. + * @param waveSpawner The entity that controls the wave spawning timer. + */ + public PauseMenuTimeStopComponent(Entity waveSpawner) { + this.waveSpawner = waveSpawner; } /** @@ -19,6 +26,7 @@ public PauseMenuTimeStopComponent() { */ @Override public void create() { + waveSpawner.getEvents().trigger("toggleWaveTimer"); freezeList = ServiceLocator.getEntityService().getEntities(); for (Entity pauseTarget : freezeList) { if (pauseTarget.getId() != getEntity().getId()) { @@ -33,6 +41,7 @@ public void create() { */ @Override public void dispose() { + waveSpawner.getEvents().trigger("toggleWaveTimer"); for (Entity pauseTarget : freezeList) { pauseTarget.setEnabled(true); } diff --git a/source/core/src/main/com/csse3200/game/components/tasks/SpawnWaveTask.java b/source/core/src/main/com/csse3200/game/components/tasks/SpawnWaveTask.java index d6c4a3d85..4150363aa 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/SpawnWaveTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/SpawnWaveTask.java @@ -12,6 +12,8 @@ public class SpawnWaveTask extends DefaultTask implements PriorityTask { private final GameTime globalTime; private long endTime = 0; private final int SPAWNING_INTERVAL = 10; + private boolean wavesPaused = false; + private long pauseTime = 0; public SpawnWaveTask() { this.globalTime = ServiceLocator.getTimeSource(); } @@ -25,13 +27,30 @@ public int getPriority() { public void start() { super.start(); endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000); + this.owner.getEntity().getEvents().addListener("toggleWaveTimer", this::toggleWaveTimer); } @Override public void update() { - if (globalTime.getTime() >= endTime) { - this.owner.getEntity().getEvents().trigger("spawnWave"); - endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000L); // reset end time + if (!wavesPaused) { + if (globalTime.getTime() >= endTime) { + this.owner.getEntity().getEvents().trigger("spawnWave"); + endTime = globalTime.getTime() + (SPAWNING_INTERVAL * 1000L); // reset end time + } + } + } + + /** + * Function for pausing the wave timer when the pause menu is opened, and resuming it when the pause menu is closed. + */ + private void toggleWaveTimer() { + if (!wavesPaused) { + pauseTime = globalTime.getTime(); + wavesPaused = true; + } else { + // Offsets the next wave spawn by however long the game was paused. + endTime += globalTime.getTime() - pauseTime; + wavesPaused = false; } } } diff --git a/source/core/src/main/com/csse3200/game/entities/factories/PauseMenuFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/PauseMenuFactory.java index fdb3a4204..bfd524ee7 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/PauseMenuFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/PauseMenuFactory.java @@ -12,11 +12,13 @@ public class PauseMenuFactory { /** * Creates the pause menu + * @param game The Gdx game instance that handles screen changes. + * @param waveSpawner The entity that handles the wave spawn timer * @return entity */ - public static Entity createPauseMenu(GdxGame game) { + public static Entity createPauseMenu(GdxGame game, Entity waveSpawner) { Entity pauseMenu = new Entity() - .addComponent(new PauseMenuTimeStopComponent()) + .addComponent(new PauseMenuTimeStopComponent(waveSpawner)) .addComponent(new PauseMenuRenderComponent()) .addComponent(new PauseMenuContinueButton()) .addComponent(new PauseMenuSettingsButton(game)) diff --git a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java index a56755b6e..77bdd47ea 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -202,7 +202,10 @@ private void createUI() { .addComponent(new MainGameActions(this.game)) .addComponent(new MainGameExitDisplay()) .addComponent(new MainGameLoseDisplay()) - .addComponent(new MainGamePauseDisplay(this.game)) + // WHERE'S THE WAVE SPAWNER, HOW IS THIS GAME FUNCTIONING WHEN IT DOESN'T EVEN MAKE A WAVE SPAWNER + // I'm told there's a new entity for the SpawnWaveTask, maybe it'll actually be accessible in this class. + // fyi there REALLY shouldn't be a null arg, but I need to push this before I can fix the issue. + .addComponent(new MainGamePauseDisplay(this.game, null)) .addComponent(new Terminal()) .addComponent(inputComponent) .addComponent(new TerminalDisplay());