-
Notifications
You must be signed in to change notification settings - Fork 7
Pause System
This documentation explains the code implementation and logic for pausing a game. We will use the provided code snippets as examples to demonstrate how pausing is achieved. In this example, we have two main components involved: OpenPauseComponent and PauseMenuActions, and a togglePauseAnimations method.
Iteration 1 (Basic, no messages) The first iteraiton of the pause menu aligns well with that of the main menu screen. We chose not to include a save game or restart functionality during this sprint, as we wanted to focus on the software design of pausing the game.
Iteration 2 (For sprint 3, made button and message additions)
The OpenPauseComponent is responsible for handling the pausing and unpausing of the game. It listens for a specific input event (in this case, the "escInput" event) to toggle the pause menu on and off. Code:
@Override
public void create() {
entity.getEvents().addListener("escInput", this::togglePauseMenu);
pauseOpen = false;
}`
create(): In the create() method, an event listener is added to the entity. This listener listens for the "escInput" event and associates it with the togglePauseMenu method.
private void togglePauseMenu() {
logger.info("togglepausemenu");
if (Boolean.TRUE.equals(pauseOpen)) {
closePauseMenu();
} else {
openPauseMenu();
}
}
togglePauseMenu(): When the "escInput" event is triggered, this method is called. It checks the current state of pauseOpen and either opens or closes the pause menu accordingly. As such:
public void openPauseMenu() {
logger.info("Opening pause window");
ServiceLocator.getPauseMenuArea().setPauseMenu();
pauseOpen = true;
}
public void closePauseMenu() {
logger.info("Closing pause window");
KeyboardPlayerInputComponent.clearMenuOpening();
ServiceLocator.getPauseMenuArea().disposePauseMenu();
pauseOpen = false;
}
PauseMenuActions is a component responsible for handling actions within the pause menu, such as exiting the game.
public class PauseMenuActions extends Component {
private static final Logger logger = LoggerFactory.getLogger(PauseMenuActions.class);
private GdxGame game;
private static boolean quitGame = false;
@Override
public void create() {
entity.getEvents().addListener("exit pressed", this::onExit);
}
}
The setPauseMenu
method is responsible for displaying a pause menu within a video game or application. This pause menu allows users to pause the game and access various options, including exiting to the main menu, loading a previous game state, saving the current game state, and resuming gameplay. Additionally, it provides a mechanism for displaying informative messages to the user. public void setPauseMenu()
The setPauseMenu
method begins by creating and displaying the pause menu when invoked. It includes several buttons and messages that enhance the player's control and interaction during paused gameplay. TextButton exitBtn = new TextButton("Exit", skin);
The "Exit" button allows the player to exit the game and return to the main menu. When clicked, it triggers the MainGameActions.exitToMainMenu()
method, providing a smooth transition from the game area to the main menu.
The "Save" button allows players to save their current game progress. When clicked, it displays a "Current Game Saved!" message using the saveMessageLabel
. Additionally, there is a resumeMessageLabel
that can show a "Previous Game Loaded!" message when the "Load Previous" button is clicked. These messages provide feedback to the player about the state of the game. TextButton loadBtn = new TextButton("Load Previous", skin);
The "Load Previous" button enables players to load a previously saved game state. When clicked, it displays a "Previous Game Loaded!" message using the resumeMessageLabel
and loads the saved game using ServiceLocator.getSaveLoadService().load()
The "Resume" button allows players to continue playing the game from where they left off. When clicked, it resumes gameplay and closes the pause menu. Any displayed messages, such as the save or resume messages, are hidden.
Overview The togglePauseAnimations method appears to be used for pausing animations in the game. It iterates through a list of components and pauses animations as needed.
togglePauseAnimations(boolean pausePlayer): This method takes a boolean parameter, pausePlayer, which determines whether the player's animations should be paused. It first checks if pausePlayer is false. If it is, the method iterates through the createdComponents and checks if certain specific components (e.g., KeyboardPlayerInputComponent, PlayerAnimationController, etc.) are present.
public void togglePauseAnimations(boolean pausePlayer) {
if (!pausePlayer) {
for (Component component : createdComponents) {
if (component instanceof KeyboardPlayerInputComponent ||
component instanceof PlayerAnimationController ||
component instanceof AnimalAnimationController ||
component instanceof GhostAnimationController ||
component instanceof TamableComponent ||
component instanceof ItemPickupComponent
) {
return; //
}
}
}
for (Component component : createdComponents) {
if (component instanceof PlayerAnimationController) {
return; // Don't pause if PlayerAnimationController is present
}
}
for (Component component : createdComponents) {
if (component instanceof AnimationRenderComponent) {
((AnimationRenderComponent) component).togglePauseAnimation();
}
}
}
If any of these components are found, the method returns early, indicating that animations should not be paused. If none of the specified components are present, it then checks if there is a PlayerAnimationController component. If so, it also returns early to avoid pausing the animations. Finally, if none of the above conditions are met, it iterates through the createdComponents again and calls the togglePauseAnimation() method on any AnimationRenderComponent present. This effectively pauses the animations.
To create a dimming effect for the background when displaying the pause menu, a semi-transparent overlay was created.
Image backgroundOverlay = new Image(new Texture(Gdx.files.internal("images/Pause_overlay.png")));
backgroundOverlay.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
backgroundOverlay.setColor(0, 0, 0, 0.8f);
stage.addActor(backgroundOverlay);
The overlay covers the entire screen by setting its size to the screen dimensions. The alpha value in setColor
controls the level of dimming. The backgroundOverlay
is added to the stage before adding the pause menu and buttons to create the dimming effect.
Here is a UML diagram for the classes discussed