From fc2cd975e26d46e2370bc0dfbe7cc0194baff0ef Mon Sep 17 00:00:00 2001 From: MajorDzaster Date: Fri, 29 Sep 2023 12:47:06 +1000 Subject: [PATCH] Added a counter to PauseMenuFactory to prevent duplicate pause menus being made. --- .../maingame/MainGamePauseDisplay.java | 2 +- .../pausemenu/PauseMenuTimeStopComponent.java | 3 ++ .../entities/factories/PauseMenuFactory.java | 40 +++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) 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..9284f1492 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 @@ -45,7 +45,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Pause button clicked"); - PauseMenuFactory.createPauseMenu(game); + PauseMenuFactory.createPauseMenu(game, false); } }); 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..41aadee4c 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 @@ -3,6 +3,7 @@ import com.csse3200.game.components.Component; import com.csse3200.game.entities.Entity; import com.badlogic.gdx.utils.Array; +import com.csse3200.game.entities.factories.PauseMenuFactory; import com.csse3200.game.services.ServiceLocator; /** @@ -30,9 +31,11 @@ public void create() { /** * Handles the un-pausing of the game entities when the pause menu is closed. + * Also notifies the pause menu factory that the pause menu is being disposed. */ @Override public void dispose() { + PauseMenuFactory.decrementPauseMenuCount(); for (Entity pauseTarget : freezeList) { pauseTarget.setEnabled(true); } 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 71248198c..a0dc1ad9d 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 @@ -10,22 +10,36 @@ * Factory to create the pause menu and attach its components. */ public class PauseMenuFactory { + private static int pauseMenusActive = 0; /** * Creates the pause menu - * @return entity + * @param game The Gdx Game that handles screen changes + * @param duplicateOverride If false, will not create a pause menu if any currently exist + * @return entity, or null if no entity was created */ - public static Entity createPauseMenu(GdxGame game) { - Entity pauseMenu = new Entity() - .addComponent(new PauseMenuTimeStopComponent()) - .addComponent(new PauseMenuContinueButton()) - .addComponent(new PauseMenuSettingsButton(game)) - .addComponent(new PauseMenuPlanetSelectButton(game)) - .addComponent(new PauseMenuMainMenuButton(game)) - .addComponent(new TextureRenderComponent("images/ui/Sprites/UI_Glass_Toggle_Bar_01a.png")); - pauseMenu.setScale(8, 8); - pauseMenu.setPosition(6.3f, 2f); - ServiceLocator.getEntityService().register(pauseMenu); - return pauseMenu; + public static Entity createPauseMenu(GdxGame game, boolean duplicateOverride) { + if (pauseMenusActive == 0 || duplicateOverride) { + Entity pauseMenu = new Entity() + .addComponent(new PauseMenuTimeStopComponent()) + .addComponent(new PauseMenuContinueButton()) + .addComponent(new PauseMenuSettingsButton(game)) + .addComponent(new PauseMenuPlanetSelectButton(game)) + .addComponent(new PauseMenuMainMenuButton(game)) + .addComponent(new TextureRenderComponent("images/ui/Sprites/UI_Glass_Toggle_Bar_01a.png")); + pauseMenu.setScale(8, 8); + pauseMenu.setPosition(6.3f, 2f); + ServiceLocator.getEntityService().register(pauseMenu); + pauseMenusActive++; + return pauseMenu; + } + return null; + } + + /** + * Called when a pause menu is disposed to decrement the count of active pause menus. + */ + public static void decrementPauseMenuCount() { + pauseMenusActive--; } }