Skip to content

Pause System

MatildaDamman edited this page Sep 12, 2023 · 44 revisions

Pause Game Logic and Implementation Documentation

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.

UI Design

Screenshot 2023-09-12 at 2 23 21 pm 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.

OpenPauseComponent

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.

`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

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);
}

}

TogglePauseAnimations

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 class OpenPauseComponent extends Component { private static Logger logger = LoggerFactory.getLogger(OpenPauseComponent.class); private Boolean pauseOpen;

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.

Clone this wiki locally