CutsceneScreenDisplay - UQcsse3200/2024-studio-3 GitHub Wiki

Introduction

The CutsceneScreenDisplay class is responsible for managing the display and progression of text-based cutscenes in the game. It handles the creation of UI components like buttons and text boxes, as well as transitioning between different steps of a cutscene. The class is designed to facilitate smooth cutscene experiences, allowing players to advance through scenes or exit to the main menu.

Key Features

  • Next Scene Button: The "Next Scene" button allows players to manually advance to the next part of the cutscene or game level when clicked. It is connected to the cutsceneEnded event, which triggers a transition to the next scene.
  • Exit Button:The "Exit" button allows players to return to the main menu. It triggers the exitCutscene event, which transitions back to the main menu.
  • Cutscene Text: The cutscene text is managed in steps, with each step corresponding to a piece of dialogue or narrative. The class advances through the cutscene text, displaying one step at a time and triggering the end of the cutscene when all steps have been displayed.

Code Implementation

create()

This method sets up the UI components and initializes the text display for the cutscene. It creates buttons for advancing the cutscene and exiting to the main menu, adding them to the UI. The method also sets the initial cutscene text and triggers the first cutscene step.

@Override
public void create() {
    super.create();
    setupUI();

    textDisplay = new CutsceneTextDisplay();
    textDisplay.setVisible(false);
    stage.addActor(textDisplay.getTable());

    TextButton nextSceneBtn = new TextButton("Next Scene", skin);
    nextSceneBtn.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent changeEvent, Actor actor) {
            entity.getEvents().trigger("cutsceneEnded");
        }
    });

    TextButton exitButton = new TextButton("Exit", skin);
    exitButton.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent changeEvent, Actor actor) {
            entity.getEvents().trigger("exitCutscene");
        }
    });

    cutsceneText.add("Hello guys");
    createTextBox("Start text");
    advanceCutsceneStep();
}

advanceCutsceneStep()

This method handles moving to the next step in the cutscene. It checks whether there are more steps to display, updating the text display with the next piece of text. If all steps are complete, the cutscene ends, and the game transitions to the main game.

public void advanceCutsceneStep() {
    if (cutsceneStep < cutsceneText.size) {
        String text = cutsceneText.get(cutsceneStep);
        textDisplay.setText(text);
        cutsceneStep++;
    } else {
        textDisplay.setText("The cutscene ends.");
        startGame();  // Transition to the main game after the cutscene
    }
}

update()

This method listens for player input (specifically the space bar) to advance the cutscene. When the space bar is pressed, it triggers the next step in the cutscene.

@Override
public void update() {
    if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
        advanceCutsceneStep();
    }
}

createTextBox()

This method triggers an event to display a text box with the current step’s text. It sends the text to all entities in the game for rendering.

private void createTextBox(String text) {
    Array<Entity> entities = ServiceLocator.getEntityService().getEntities();
    for (Entity entity : entities) {
        entity.getEvents().trigger("SetText", text);
    }
}
⚠️ **GitHub.com Fallback** ⚠️