From fb25a74bccefe445334de5d8980dd06263f0362e Mon Sep 17 00:00:00 2001 From: MajorDzaster Date: Mon, 25 Sep 2023 13:23:43 +1000 Subject: [PATCH] Added screen change functionality to pause menu buttons. Noted some crash issues re-entering the level. --- .../game/components/maingame/MainGamePauseDisplay.java | 9 ++++++++- .../components/pausemenu/PauseMenuMainMenuButton.java | 10 +++++++++- .../pausemenu/PauseMenuPlanetSelectButton.java | 10 +++++++++- .../components/pausemenu/PauseMenuSettingsButton.java | 10 +++++++++- .../game/entities/factories/PauseMenuFactory.java | 9 +++++---- .../main/com/csse3200/game/screens/MainGameScreen.java | 2 +- 6 files changed, 41 insertions(+), 9 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 b87877aa2..eb8a0b273 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 @@ -5,6 +5,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.csse3200.game.GdxGame; import com.csse3200.game.entities.Entity; import com.csse3200.game.entities.factories.PauseMenuFactory; import com.csse3200.game.ui.UIComponent; @@ -19,6 +20,12 @@ public class MainGamePauseDisplay extends UIComponent { private static final float Z_INDEX = 2f; private Table table; + private GdxGame game; + + public MainGamePauseDisplay(GdxGame screenSwitchHandle) { + game = screenSwitchHandle; + } + @Override public void create() { super.create(); @@ -38,7 +45,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Pause button clicked"); - PauseMenuFactory.createPauseMenu(); + PauseMenuFactory.createPauseMenu(game); } }); diff --git a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuMainMenuButton.java b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuMainMenuButton.java index 2f26c7b98..9b8dd208d 100644 --- a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuMainMenuButton.java +++ b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuMainMenuButton.java @@ -1,10 +1,13 @@ package com.csse3200.game.components.pausemenu; +import com.badlogic.gdx.Game; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.csse3200.game.GdxGame; import com.csse3200.game.ui.UIComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +20,11 @@ public class PauseMenuMainMenuButton extends UIComponent { private static final float Z_INDEX = 2f; private Table table; + private final GdxGame game; + + public PauseMenuMainMenuButton(GdxGame screenSwitchHandle) { + game = screenSwitchHandle; + } @Override public void create() { super.create(); @@ -36,7 +44,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Main menu button clicked"); - // open settings + game.setScreen(GdxGame.ScreenType.MAIN_MENU); } }); diff --git a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuPlanetSelectButton.java b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuPlanetSelectButton.java index 0ad08d5c0..f5d19f1bf 100644 --- a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuPlanetSelectButton.java +++ b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuPlanetSelectButton.java @@ -1,10 +1,13 @@ package com.csse3200.game.components.pausemenu; +import com.badlogic.gdx.Game; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.csse3200.game.GdxGame; import com.csse3200.game.ui.UIComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +20,11 @@ public class PauseMenuPlanetSelectButton extends UIComponent { private static final float Z_INDEX = 2f; private Table table; + private final GdxGame game; + + public PauseMenuPlanetSelectButton(GdxGame screenSwitchHandle) { + game = screenSwitchHandle; + } @Override public void create() { super.create(); @@ -36,7 +44,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Planet select button clicked"); - // open settings + game.setScreen(GdxGame.ScreenType.LEVEL_SELECT); } }); diff --git a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuSettingsButton.java b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuSettingsButton.java index a82f0f957..4338b57b6 100644 --- a/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuSettingsButton.java +++ b/source/core/src/main/com/csse3200/game/components/pausemenu/PauseMenuSettingsButton.java @@ -1,10 +1,12 @@ package com.csse3200.game.components.pausemenu; +import com.badlogic.gdx.Game; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.csse3200.game.GdxGame; import com.csse3200.game.ui.UIComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +19,12 @@ public class PauseMenuSettingsButton extends UIComponent { private static final float Z_INDEX = 2f; private Table table; + private final GdxGame game; + + public PauseMenuSettingsButton(GdxGame screenSwitchHandle) { + game = screenSwitchHandle; + } + @Override public void create() { super.create(); @@ -36,7 +44,7 @@ private void addActors() { @Override public void changed(ChangeEvent changeEvent, Actor actor) { logger.debug("Settings button clicked"); - // open settings + game.setScreen(GdxGame.ScreenType.SETTINGS); } }); 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 4d244ec2f..fdb3a4204 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 @@ -1,5 +1,6 @@ package com.csse3200.game.entities.factories; +import com.csse3200.game.GdxGame; import com.csse3200.game.components.pausemenu.*; import com.csse3200.game.entities.Entity; import com.csse3200.game.services.ServiceLocator; @@ -13,14 +14,14 @@ public class PauseMenuFactory { * Creates the pause menu * @return entity */ - public static Entity createPauseMenu() { + public static Entity createPauseMenu(GdxGame game) { Entity pauseMenu = new Entity() .addComponent(new PauseMenuTimeStopComponent()) .addComponent(new PauseMenuRenderComponent()) .addComponent(new PauseMenuContinueButton()) - .addComponent(new PauseMenuSettingsButton()) - .addComponent(new PauseMenuPlanetSelectButton()) - .addComponent(new PauseMenuMainMenuButton()); + .addComponent(new PauseMenuSettingsButton(game)) + .addComponent(new PauseMenuPlanetSelectButton(game)) + .addComponent(new PauseMenuMainMenuButton(game)); ServiceLocator.getEntityService().register(pauseMenu); return pauseMenu; } 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 651b031d7..a56755b6e 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,7 @@ private void createUI() { .addComponent(new MainGameActions(this.game)) .addComponent(new MainGameExitDisplay()) .addComponent(new MainGameLoseDisplay()) - .addComponent(new MainGamePauseDisplay()) + .addComponent(new MainGamePauseDisplay(this.game)) .addComponent(new Terminal()) .addComponent(inputComponent) .addComponent(new TerminalDisplay());