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

Implemented a new package 'pausemenu' in the screens package, added filed 'PauseMenuActions', 'PauseMenuDisplay' and 'PauseMenuScreen'. 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 on the new pause screen.

Pause_Menu_Example_1

Changes to GdxGame

Added PAUSE_MENU as a screen type in GdxGame.

  public Screen newScreen(ScreenType screenType) {
    switch (screenType) {
      ...
      case PAUSE_MENU:
        return new PauseMenuScreen();
      ...
  }

  public enum ScreenType {
    MAIN_MENU, PAUSE_MENU, MAIN_GAME, SETTINGS, WIN_DEFAULT, LOSS_TIMED, LOSS_CAUGHT, CONTEXT, TITLE_SCREEN, LEADERBOARD
  }

Changes to KeyPlayerInputComponent

    case Keys.P:
            case Keys.ESCAPE:
                triggerPauseResumeEvent();
                return true;
 private void triggerPauseResumeEvent() {
        // If the screen is the main game it pauses the game, but if the screen is the 
 pause screen
        // it resumes the game.
        System.out.println(ServiceLocator.getGame().getScreen().toString());
        if (ServiceLocator.getGame().getScreen().getClass() ==
                MainGameScreen.class) {
            ServiceLocator.getGame().setScreen(GdxGame.ScreenType.PAUSE_MENU);
            ServiceLocator.getGame().pause();
        } else if (ServiceLocator.getGame().getScreen().getClass() ==
                PauseMenuScreen.class) {
            ServiceLocator.getGame().setScreen(GdxGame.ScreenType.MAIN_GAME);
            ServiceLocator.getGame().resume();
        }
    }

Making PauseMenuScreen, PauseMenuDisplay and PauseMenuActions.

Created similarly to MainMenu however adding unqiue buttons and actions associated.

  TextButton resumeBtn = new TextButton("Resume", skin);
        TextButton restartBtn = new TextButton("Restart from Start", skin);
        TextButton mainMenuBtn = new TextButton("Main Menu", skin);
        TextButton settingsBtn = new TextButton("Settings", skin);

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

        // Trigger restart game
        restartBtn.addListener(
                new ChangeListener() {
                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        logger.debug("Restart button clicked");
                        entity.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");
                        entity.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");
                        entity.getEvents().trigger("main_menu");
                    }
                });

        table.add(bg);
        table.row();
        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);
        stage.addActor(table);
    }
    @Override
    public void create() {
        entity.getEvents().addListener("resume", this::onResume);
        entity.getEvents().addListener("restart", this::onRestart);
        entity.getEvents().addListener("settings", this::onSettings);
        entity.getEvents().addListener("main_menu", this::onMainMenu);
    }
     /**
     * 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 next sprint it will integrate the settings better allowing for an alternative functionality of when the setting screen apply button is pressed based it will always switch to the previous screen.

In addition, the game state save for when pausing and switching screens will be implemented so the user doesn't lose progress on their game.

Clone this wiki locally