Skip to content

Pausing and Resuming

Roy Browning edited this page Oct 7, 2021 · 9 revisions

Introduction

User can press the 'P' or 'ESC' key to pause the game at any time to pause all running components of the game. The pause menu background and UI elements are designed by Team 3 and Team 5 designers. There will be a restart, resume, main menu and settings. Each button taking you to the respective game state and/or game screen needed.

Code Implementation

Added triggerPauseResumeEvent(), which will pause the game updates and time, then switch screens, added to the event of a key press on ESC or P.

Added functionality for the UI elements: Resume, Restart, Settings and Exit to Main Menu on the new pause screen.

Example Pause Menu

Changes to KeyPlayerInputComponent

    case Keys.P:
            case Keys.ESCAPE:
                triggerPauseResumeEvent();
                return true;

Runs the pause method that change the game state so the game know that the game is being paused

 private void triggerPauseResumeEvent() {
        ServiceLocator.getGame().pause();
    }

@Override
  public void pause() {
    logger.info("Game paused");
    gameStatus = GAME_PAUSED;
  }

Logic for game states and status for pause menu being built

if (gameStatus == GAME_PAUSED) {
if (!builtPauseMenu) {
else if (gameStatus == GAME_RESUMING) {

public static final int GAME_RUNNING = 0;
public static final int GAME_PAUSED = 1;
public static final int GAME_RESUMING = 2;
private int gameStatus = GAME_RUNNING;

private boolean builtPauseMenu = false;

These provide conditions so the game knows all stages of the pausing and resuming of the game

Building pause menu

ResourceService resourceService = ServiceLocator.getResourceService();
        resourceService.loadTextures(pauseGameTextures);
        ServiceLocator.getResourceService().loadAll();

        //      Table table = new Table();
        table.setFillParent(true);
        //      Image bg = new Image(ServiceLocator.getResourceService()
        //              .getAsset("images/ui/screens/paused_screen.png", Texture.class));

        TextButton resumeBtn = new TextButton("Resume", new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")));
        TextButton restartBtn = new TextButton("Restart from Start", new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")));
        TextButton mainMenuBtn = new TextButton("Main Menu", new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")));
        TextButton settingsBtn = new TextButton("Settings", new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json")));

        // Trigger resume game
        resumeBtn.addListener(
                new ChangeListener() {
                  @Override
                  public void changed(ChangeEvent event, Actor actor) {
                    logger.debug("Resume button clicked");
                    mainGameEntity.getEvents().trigger("resume");
                  }
                });

        // Trigger restart game
        restartBtn.addListener(
                new ChangeListener() {
                  @Override
                  public void changed(ChangeEvent event, Actor actor) {
                    logger.debug("Restart button clicked");
                    mainGameEntity.getEvents().trigger("restart");
                  }
                });

        // Trigger to go to settings menu
        settingsBtn.addListener(
                new ChangeListener() {
                  @Override
                  public void changed(ChangeEvent event, Actor actor) {
                    logger.debug("Settings button clicked");
                    mainGameEntity.getEvents().trigger("settings");
                  }
                });

        // Trigger to go to main menu
        mainMenuBtn.addListener(
                new ChangeListener() {
                  @Override
                  public void changed(ChangeEvent event, Actor actor) {
                    logger.debug("Main menu button clicked");
                    mainGameEntity.getEvents().trigger("main_menu");
                  }
                });
        table.add(resumeBtn).padTop(50f);
        table.row();
        table.add(restartBtn).padTop(15f);
        table.row();
        table.add(settingsBtn).padTop(15f);
        table.row();
        table.add(mainMenuBtn).padTop(15f);
        table.setName("Pause Menu");
        ServiceLocator.getRenderService().getStage().addActor(table);
        ServiceLocator.getRenderService().getStage().draw();
        builtPauseMenu = true;
     /**
     * Restarts game, by resetting game by running start.
     */
    private void onRestart() {
        logger.info("Restart game");
        ServiceLocator.getGame().setScreen(GdxGame.ScreenType.MAIN_GAME);
        //TODO
        // How will restarting work
    }

    /**
     * Resumes game, by unpausing game state.
     */
    private void onResume() {
        logger.info("Resume game");
        ServiceLocator.getGame().setScreen(GdxGame.ScreenType.MAIN_GAME);
        ServiceLocator.getGame().resume();
    }

    /**
     * Opens settings menu.
     */
    private void onSettings() {
        logger.info("Launch settings screen");
        ServiceLocator.getGame().setScreen(GdxGame.ScreenType.SETTINGS);
    }

    /**
     * Opens main menu.
     */
    private void onMainMenu() {
        logger.info("Launch main menu screen");
        ServiceLocator.getGame().setScreen(GdxGame.ScreenType.MAIN_MENU);
    }

Possible Future Changes

In the comments in the last minute pull request ideas were given to clean up code further. The commenter wanted to complete this themselves, which will be great to get a second pair of eyes on the code to improve it.

Clone this wiki locally