Skip to content

Implementation Guide

rio edited this page Oct 18, 2022 · 7 revisions

Implementation of the Countdown Timer

This page explains how the countdown timer is implemented into the game screen.

Component

The display of the countdown timer is to be added to the main game screen via the MainGameScreen's createUI() method, therefore, a component will be created to represent the countdown display.

A countdownDisplay.java file was created in the components file of src/main, where libGDX GUI elements are created and added to the stage.

The class takes in the current game as a constructor, and declares initial variables including setting the game time limit. public countdownDisplay(GdxGame game) { super(); this.game = game; this.timeRemaining = 7260; //- (ServiceLocator.getTimeSource().getTime() / 1000); timeCount = 0; }

In the create() method, addActors() is called which creates a label to display the time remaining and added to the stage. The pause and resume buttons are also created and added to the stage.

` public Label counterLabel;

@Override
public void create() {
    super.create();
    addActors();
}

private void addActors() {

    counterLabel = new Label(String.valueOf(timeRemaining), skin);
    counterLabel.setPosition((float) (stage.getWidth() * 0.8), (float) (stage.getHeight() * 0.1));

    counterLabel.setFontScale(2);

    stage.addActor(counterLabel);

    Table pauseBtn = pauseButton();
    Table resumeBtn = resumeButton();
    stage.addActor(pauseBtn);
    stage.addActor(resumeBtn);

}

`

The Countdown

The basic countdown functionality is implemented in the update() method. This method is called each frame, and so to update the time, the time passed between two frames (getDeltaTime) is subtracted from the variable timeRemaining. The text on the label is also updated, to ensure the component displays the updated time remaining each frame.

` @Override public void update() { super.update(); timeCount = Gdx.graphics.getDeltaTime();

    if (this.timeRemaining <= 0) {
        counterLabel.setText("GAME OVER!");
    }

    if (timeRemaining>0 && stop==false) {
        this.timeRemaining -= timeCount;
        .
        .
        .
        // sets the text of the label to the current time remaining.
        counterLabel.setText(String.valueOf(equHours+":"+equMins+":"+equSeconds));

    }


}

`

Adding the component to the main game screen

The countdown component as implemented in countdownDisplay.java is added to the main game screen via the createUI() method in MainGameScreen.java.

` /**

  • Creates the main game's ui including components for rendering ui elements to the screen and
  • capturing and handling ui input. */ private void createUI() { logger.debug("Creating ui"); Stage stage = ServiceLocator.getRenderService().getStage(); InputComponent inputComponent = ServiceLocator.getInputService().getInputFactory().createForTerminal();
this.timeSinceStart = ServiceLocator.getTimeSource().getTime();
logger.info("time passed since game started: {}", this.timeSinceStart);

Entity ui = new Entity();
ui.addComponent(new InputDecorator(stage, 10))
    .addComponent(new PerformanceDisplay())
    .addComponent(new MainGameActions(this.game))
    .addComponent(new MainGameExitDisplay())
        .addComponent(new countdownDisplay(this.game))
    .addComponent(new Terminal())
    .addComponent(inputComponent)
    .addComponent(new TerminalDisplay());

ServiceLocator.getEntityService().register(ui);

} `

Pause/Resume/Restart

  • Pause When the player clicks on the pause button on the main game screen, a pop-up window titled "PAUSE" is displayed to the player. At the same time, the music and movement of the elements on the screen stop as well.

Creation of the paused button, and adding event listener on click which creates paused pop-up window (countdownDisplay.java): Screen Shot 2022-10-19 at 1 24 25

Changing the status of the game to paused, which will stop the game and music (MainGameScreen.java): Screen Shot 2022-10-19 at 1 26 49 Screen Shot 2022-10-19 at 1 25 10

Paused Pop-Up Window

A new instance of the PausedWindow.java class is created (the current game as the parameter) on click of the paused button on the main game screen, and the create() method is called to create the UI elements in addActors().

Screen Shot 2022-10-19 at 1 24 25

  • Resume The resume button is displayed in the paused pop-up window. When this button is clicked, the implementation simply does the opposite to what was done in the implementation of pausing the game.

Creation of the resume button in the paused pop-up window (PausedWindow.java in addActors()): Screen Shot 2022-10-19 at 1 30 50 Screen Shot 2022-10-19 at 1 31 00

Resuming the game:

Screen Shot 2022-10-19 at 1 27 59

  • Restart The restart button is accessed from the paused pop-up window just like the resume button. When the player clicks this button, the main game screen is reloaded, making the game restart.

PausedWindow.java Screen Shot 2022-10-19 at 1 31 08

The Lose Condition

When the countdown timer runs out, the timeRemaining is less than or equal to zero. In the countdownDisplay class, this condition is checked for in the update method, and when true, the game will be over as in the player has ran out of time and lost. Initially we had tried to setLose() (Team 3's method) and trigger "ending" event (Also Team 3's event) within this update method, however this caused a run time exception, so we had moved it to MainGameScreen.java where we created the method setterForCountDown().

countdownDisplay.java update() method checking timeRemaining is less than or equal to zero:

Screen Shot 2022-10-19 at 1 39 21

MainGameScreen.java --> timeTime variable is created where false means there is no more time remaining. Initially this is set to true, and is toggled to false when setterForCountDown() is called:

Screen Shot 2022-10-19 at 1 39 34

MainGameScreen.java --> when the timeTime variable is false (i.e. no more time remaining therefore game over), Team 3's setLose() method is used, and the screen type is set to ENDING to display the appropriate ending screen created by Team 3:

Screen Shot 2022-10-19 at 1 26 49

Table of Contents

Home

Game Design

User survey

Sprint 4

Eviction Menu and Win/lose Logic: Polishing tasks (Team 7)

Button Sounds and Ending Menu improve (Team 3)

Sound effect and Fixing the clue bug (Team 6)

Improvement of Enemy and Attack (Team 1)

Add Features When The Player Get Attacked and Overall UI Improvement (Team 8)

Sprint 1

Achievement System (Team 2)

Player Eviction Menu (Team 7)

Countdown Clock (Team 4)

Music (Team3)

Map (Team6)

Sprint 2

Player Eviction Menu (Team 7)

Character Design & Animation (Team 1)

Music (Team 3)

Inventory System and Consumables Items (Team 8)

Scenario design

Achievement System(team 2)

Storyline (Team 5)

Countdown Clock (Team 4)

Sprint 3

Ending Menu (Team 3)

NPC interaction (Team 2)

Win/lose Condition (Based on Eviction Menu) (Team 7)

Player Profile (Team 4)

Game Logo (Team 8)

Clue storage (Team 6)

Enemy Design and Attack (Team 1)

Scenario design for village(Team5)

Game design
Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally