Pause And Resume functionality - UQcsse3200/2024-studio-1 GitHub Wiki

Introduction

The Pause and resume feature provides user the ability to pause the current game process (that is freezing all the entities) and continue playing after resume time in the future.

Overview

The "pause symbol" button and the pause functionality can be found in the main game area screen. The "resume" button and the resume functionality can be found in the pause menu.

This is Paused screen

Paused image

This is Resumed screen

Resumed image

Restart Option

The Restart feature allows players to reset the game to its initial state from the pause menu. This functionality provides a quick way for players to start over without having to exit and relaunch the game The "Restart" button can be found in the pause menu, accessible by pressing the pause button or the Esc key during gameplay. When activated, it resets all game progress and returns the player to the main game screen.

UML

pause wiki

Technical Information

The updated render function inside MainGameScreen.java is used to call renderer.render() method based on value of the isPaused variable

public void render(float delta) {
        ui.earlyUpdate();
        ui.update();

        if (isPaused) {
            renderer.render();
            return;
        }
        physicsEngine.update();
        ServiceLocator.getEntityService().update();
        ServiceLocator.getGameAreaService().update();
        renderer.render();
    }

MainGameExitDisplay functions

Created pauseGame, unpause & saveGame function in MainGameExitDisplay.java. Assign the isPaused value based on the function being called.

  public void pauseGame() {
    MainGameScreen.isPaused = true;
    stage.addActor(pauseTable);
    table.remove();
  }
  public void unpause() {
    MainGameScreen.isPaused = false;
    pauseTable.remove();
    stage.addActor(table);
  }
  public void saveGame() {
    Array<EntityCoordinates> entities = new Array<>();
    for (Entity entity : ServiceLocator.getEntityService().getEntities()) {
      if (entity.getName().equals("Main Player")) {
        player = entity;
        SavePlayerService savePlayerService = new SavePlayerService();
        savePlayerService.savePlayerState(player);
        //triggers to save map data and player location, listeners in MainGameArea
        player.getEvents().trigger("saveMapData");
        logger.info("Saved Successfully");
      }
    }
  }

MainGameExitDisplay listeners

Added listeners to pause button and they will trigger pauseGame(), unpause() & saveGame() respectively in MainGameExitDisplay.java file.

    pauseBtn.addListener(
      new ChangeListener() {
        @Override
        public void changed(ChangeEvent changeEvent, Actor actor) {
          pauseGame();
        }
      });
    resumeBtn.addListener(
      new ChangeListener() {
          @Override
          public void changed(ChangeEvent changeEvent, Actor actor) {
             unpause();
          }
      });
    saveBtn.addListener(
            new ChangeListener() {
              @Override
              public void changed(ChangeEvent changeEvent, Actor actor) {
                logger.info("Save button clicked");
                Label saveLabel = new Label("Game saved!", skin);
                pauseTable.add(saveLabel).padTop(BTN_SPACING);
                pauseTable.row();
                saveGame();
              }});

MainGameExitDisplay listeners that use MainGameActions functions

Added listeners to pause button and they will trigger onRestart() & onExit() in MainGameActions.java file respectively.

    restartBtn.addListener(
            new ChangeListener() {
              @Override
              public void changed(ChangeEvent changeEvent, Actor actor) {
                unpause();
                logger.debug("Restart button clicked");
                entity.getEvents().trigger("restart");
              }
            });
    exitBtn.addListener(
            new ChangeListener() {
              @Override
              public void changed(ChangeEvent changeEvent, Actor actor) {
                unpause();
                logger.debug("Exit button clicked");
                entity.getEvents().trigger("exit");
              }
            });