Skip to content

Commit

Permalink
Added a counter to PauseMenuFactory to prevent duplicate pause menus …
Browse files Browse the repository at this point in the history
…being made.
  • Loading branch information
AlasdairS4698737 committed Sep 29, 2023
1 parent 036037c commit fc2cd97
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
}

0 comments on commit fc2cd97

Please sign in to comment.