Skip to content

Loading Display

Jay Thakkar edited this page Oct 2, 2024 · 7 revisions

LoadingDisplay

Overview

The LoadingDisplay class is a UI component responsible for displaying a loading screen during the game's resource loading process. It shows a progress indicator (represented by a moon graphic that fills as loading progresses) along with dynamic loading messages. These messages are displayed in a random order and shuffled after all messages are shown once.

Purpose

The LoadingDisplay is responsible for:

  • Displaying a progress bar in the form of a filling moon to represent the game's loading progress.
  • Presenting dynamic and randomized loading messages that change at regular intervals to entertain the player while loading occurs.
  • Ensures that the moon's progress and opacity are clamped between appropriate values (0 to 1) to prevent errors or overflows.

Structure

Constructor

The constructor initializes the LoadingDisplay with a progress bar (moon actor), a shuffled list of humorous loading messages, and sets the initial progress to zero. The messages are displayed in a random order to keep the loading process engaging.

public LoadingDisplay() {
    elapsedTime = 0;
    shuffleMessages();  // Shuffle the messages at the beginning
    currentMessageIndex = 0;
    currentMessage = shuffledMessages.get(currentMessageIndex);  // Start with the first message in the shuffled list
}

Fields

ProgressBar (MoonActor): A visual representation of the progress using a moon that fills up as resources are loaded. Label (loadingLabel): Displays the current loading message. shuffledMessages: A shuffled list of loading messages. currentMessageIndex: Tracks the index of the current message being displayed. elapsedTime: Tracks the overall time elapsed during the loading process. messageElapsedTime: Tracks how long the current message has been displayed to ensure messages change regularly.

Methods

create()

This method sets up the UI by adding actors to the stage, including the progress bar and loading message label.

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

addActors()

This method adds the moon actor and the loading message label to the stage using a Table layout. It ensures that both components are visually centered and aligned properly on the screen.

private void addActors() {
    // Create and add the moon actor
    moonActor = new MoonActor();
    moonActor.setOpacity(0.7f);  // Set the opacity to blend into the background
    stage.addActor(moonActor);

    loadingLabel = new Label(currentMessage, skin);
}

shuffleMessages()

This private method shuffles the loading messages to ensure they are displayed in a random order. After all messages are shown once, the list is reshuffled for the next cycle. Example:

    shuffledMessages = new LinkedList<>(loadingMessages);
    Collections.shuffle(shuffledMessages);  // Randomly shuffle the messages
}

update()

This method is called regularly to update the state of the loading screen. It updates the progress of the moon actor based on the elapsed time and resource loading progress, and it changes the loading message at predefined intervals (MESSAGE_INTERVAL).

@Override
public void update() {
    super.update();
    float deltaTime = ServiceLocator.getTimeSource().getDeltaTime();
    elapsedTime += deltaTime;
    messageElapsedTime += deltaTime;

    // Update loading messages at regular intervals
    if (messageElapsedTime >= MESSAGE_INTERVAL) {
        currentMessageIndex++;

        // If we've reached the end of the shuffled list, reshuffle
        if (currentMessageIndex >= shuffledMessages.size()) {
            shuffleMessages();
            currentMessageIndex = 0;
        }

        currentMessage = shuffledMessages.get(currentMessageIndex);
        loadingLabel.setText(currentMessage);
        messageElapsedTime = 0;
    }

    // Update the progress
    float progress = Math.min(elapsedTime / LOADING_DURATION, ServiceLocator.getResourceService().getProgress() / 100f);
    moonActor.setProgress(progress);  // Update moon actor progress
}

isLoadingFinished()

Checks if the loading process is complete by verifying whether the progress bar has reached its maximum value.

public boolean isLoadingFinished() {
    return progressBar.getValue() >= 1;
}

update()

This method updates the progress bar based on the elapsed time and the loading progress of the resources. It also changes the loading message at regular intervals (MESSAGE_INTERVAL).

@Override
public void update() {
    super.update();
    float deltaTime = Gdx.graphics.getDeltaTime();
    elapsedTime += deltaTime;
    messageElapsedTime += deltaTime;

    if (messageElapsedTime >= MESSAGE_INTERVAL) {
        int newMessageIndex;
        do {
            newMessageIndex = (int) (Math.random() * loadingMessages.length);
        } while (newMessageIndex == currentMessageIndex);

        currentMessageIndex = newMessageIndex;
        currentMessage = loadingMessages[currentMessageIndex];
        loadingLabel.setText(currentMessage);
        messageElapsedTime = 0;
    }

    float progress = Math.min(elapsedTime / LOADING_DURATION, ServiceLocator.getResourceService().getProgress() / 100f);
    progressBar.setValue(progress);
}

Getter functions

These methods include the getZIndex, getCurrentMessage, getAllMessages methods etc, primarily utilized for testing purposes.

draw(SpriteBatch batch)

The draw() method is overridden but left empty because the drawing is handled by the stage.

@Override
public void draw(SpriteBatch batch) {
    // draw is handled by the stage
}

dispose()

Disposes of the table and other resources when the loading display is no longer needed.

@Override
public void dispose() {
    table.clear();
    super.dispose();
}

Dependencies

The LoadingDisplay class depends on:

  • ProgressBar (MoonActor): Displays the progress of the game's resource loading using a filling moon.
  • ServiceLocator: Provides access to services, such as the ResourceService, to track the loading progress of game resources.
  • Label: Shows randomized loading messages that entertain players during loading.

Usage

To use the LoadingDisplay class:

  1. Instantiate the LoadingDisplay.
  2. Call the create() method to initialize and add it to the stage.
  3. Call the update() method regularly (e.g., in the game loop) to refresh the progress bar and loading messages.
  4. Check the loading status using isLoadingFinished().
LoadingDisplay loadingDisplay = new LoadingDisplay();
loadingDisplay.create();

// In your game loop:
loadingDisplay.update();
if (loadingDisplay.isLoadingFinished()) {
    // Switch to the main game screen
}

MoonActor

Overview

The MoonActor class represents a visual progress indicator where the moon fills up as the game loads. It also supports adjusting the opacity of the moon for visual effects.

Methods

setProgress(float progress): Sets the progress of the moon, clamped between 0 and 1 to avoid overflows.

Example:

public void setProgress(float progress) {
    // Clamp the progress between 0 and 1
    this.progress = Math.max(0, Math.min(progress, 1));  // Clamping logic
}

setOpacity(float opacity): Sets the opacity of the moon, clamped between 0 and 1 to control transparency.


public void setOpacity(float opacity) {
    this.opacity = Math.max(0, Math.min(opacity, 1));
}

Back

UML

image

Clone this wiki locally