Loading Screen - UQcsse3200/2024-studio-2 GitHub Wiki
LoadingScreen
Overview
The LoadingScreen
class represents the loading screen in the game. It extends ScreenAdapter
and is responsible for rendering the loading display, managing input services, and transitioning to the main game screen when loading is finished. The class also initializes the necessary game services and disposes of resources when no longer needed.
Purpose
The LoadingScreen
class is responsible for:
- Displaying a loading screen with a background image and loading progress.
- Transitioning to the main game screen once loading is complete.
- Managing services such as input, rendering, and entity management.
- Handling resource cleanup after the loading screen is closed.
Structure
Constructor
The constructor initializes the LoadingScreen
by setting up the game services through the ServiceLocator
and creating the renderer. It also creates the UI elements for the loading screen.
public LoadingScreen(GdxGame game) {
this.game = game;
logger.debug("Initialising loading screen services");
ServiceLocator.registerInputService(new InputService());
ServiceLocator.registerResourceService(new ResourceService());
ServiceLocator.registerEntityService(new EntityService());
ServiceLocator.registerRenderService(new RenderService());
ServiceLocator.registerTimeSource(new GameTime());
renderer = RenderFactory.createRenderer();
renderer.getCamera().getEntity().setPosition(5f, 5f);
createUI();
}
Methods
render(float delta)
This method updates the entity service and renders the game screen. If the loading display has finished loading, it transitions to the main game screen.
@Override
public void render(float delta) {
ServiceLocator.getEntityService().update();
renderer.render();
if (loadingDisplay.isLoadingFinished()) {
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
}
resize(int width, int height)
Updates the renderer's viewport size when the screen is resized.
@Override
public void resize(int width, int height) {
renderer.resize(width, height);
}
dispose()
Disposes of the renderer and services to free up resources when the screen is no longer needed.
@Override
public void dispose() {
renderer.dispose();
ServiceLocator.getRenderService().dispose();
ServiceLocator.getEntityService().dispose();
ServiceLocator.clear();
}
createUI()
Creates the UI for the loading screen. This includes adding the background image and the loading display components.
private void createUI() {
logger.debug("Creating ui");
Stage stage = ServiceLocator.getRenderService().getStage();
Entity ui = new Entity();
BackgroundImage loadingScreenImage = new BackgroundImage("images/animal/shootingstar3.jpeg");
stage.addActor(loadingScreenImage);
loadingDisplay = new LoadingDisplay();
ui.addComponent(loadingDisplay).addComponent(new InputDecorator(stage, 10));
ServiceLocator.getEntityService().register(ui);
}
New Components and Features
MoonActor
The MoonActor class is responsible for rendering the moon that acts as the new loading progress indicator. The moon remains static at the center of the screen and gradually fills from the bottom to the top as the loading progresses. The moon also has adjustable opacity to blend with the background.
LoadingDisplay
The LoadingDisplay class now integrates the MoonActor instead of the progress bar. The moon fills up over a set duration and blends into the background using opacity settings. Text overlays appear in front of the moon to inform the user about the current loading stage.
Dependencies
This class relies on the following components:
- InputService: Manages input for the game.
- ResourceService: Manages game resources like textures and sounds.
- EntityService: Manages game entities.
- RenderService: Handles rendering the game screen.
- GameTime: Manages the in-game time system.
- LoadingDisplay: Manages the loading display and tracks loading progress.
- InputDecorator: Handles user input for the loading screen UI.
- Renderer: Renders the game visuals and manages the camera.
Usage
To use the LoadingScreen, instantiate it by passing the GdxGame instance and set it as the current screen in your game:
GdxGame game = new GdxGame();
LoadingScreen loadingScreen = new LoadingScreen(game);
game.setScreen(loadingScreen);