Pause Menu Factory - UQcsse3200/2023-studio-3 GitHub Wiki

Introduction

One of the game's features is a pause menu that allows the player to pause the game. In order to create a pause menu and attach the required components, a PauseMenuFactory class has been implemented.

Pause menu creation createPauseMenu()

This method is used to create the pause menu, requiring a single GdxGame argument to allow the pause menu's buttons to change the game stage.

It begins with a preliminary check by calling previousPauseActive(), as seen in the next section. If previousPauseActive() returns true, the method aborts the attempt to make a pause menu, and returns null.

It creates a new entity, and attaches six components to give the entity pause menu functionality. The components are:

  1. PauseMenuButtonComponent: Displays the pause menu on screen. The pause menu consists of a background window with 4 buttons available:

    a) Continue, which removes the pause menu and continues the game.

    b) Settings, which switches the game to the pause screen.

    c) Planet Select, which switches the game to the planet select screen.

    d) Main Menu, which switches the game to the main menu screen.

  2. PauseMenuTimeStopComponent: Pauses the game when the pause menu is created, and resumes the game when the pause menu is deleted. 2 actions are done to implement this:

    a) To pause, it calls setEnabled(false) on all other entities registered in EntityService. To resume, it calls setEnabled(true) on all the previously paused entities still registered in EntityService.

    b) The toggleGamePause method from WaveService is called.

After attaching the components, the method registers the pause menu with the EntityService, and takes a record of its ID in the EntityService with the lastPauseMenuID private variable, and returns the created entity to the function that called the method.

Duplicate prevention previousPauseActive()

The PauseMenuTimeStopComponent does not function properly if multiple instances are active at once, and creating a pause menu is unnecessary if one is already active. Therefore, this method is used to assist createPauseMenu in detecting if the last pause menu created is still active, to prevent duplicates being made.

The method takes the lastPauseMenuID private variable, which is set by createPauseMenu() to equal the ID of the last pause menu that has been registered with EntityService, and checks the IDs of the entities currently registered with EntityService. If no match is found, the last pause menu created has been disposed, and the method returns false, allowing createPauseMenu() to create a new pause menu. If an ID match is found, the last pause menu made is still active, and the method returns true, preventing createPauseMenu() from making a duplicate.

The initial value of lastPauseMenuID is -1, an invalid ID number, to guarantee that the method does not find a match when createPauseMenu() is called for the first time.