Skip to content

ControlScreen Class Documentation

Rachit Chaurasia edited this page Oct 19, 2023 · 2 revisions

ControlScreen Class Documentation

Table of Contents

  1. Introduction
  2. Description
  3. Dependencies
  4. Usage
  5. Methods
  6. Example
  7. Contributors

Introduction

The ControlsScreen class represents a screen in your game that is responsible for displaying and managing control-related information to the player. This documentation provides an overview of the class, its purpose, and how to use it effectively in your game development project.

Description

Class Structure

The ControlsScreen class is a part of your game project and serves as a screen where players can access and learn about the game's controls and mechanics. It contains various components, including a tutorial image and an exit button, to provide a complete user experience.

Features

  • Display of a tutorial image that explains the game's controls and mechanics.
  • Exit button to allow players to return to the main game or previous screens.
  • Integration with other game systems, including rendering, physics, and input.

Implementation

image

Dependencies

The ControlsScreen class has the following dependencies:

  • A collection of texture files used for rendering various elements, such as buttons, images, and icons. These textures should be loaded and managed by the ResourceService or a similar service.

Example of Loading Dependencies

private static final String[] planetTextures = {
    "images/heart.png",
    "images/structure-icons/gate.png",
    // Add more texture paths as needed
};

Make sure to load these assets using a resource management system and unload them when they are no longer needed to free up memory resources.

Usage

To use the ControlsScreen class in your game, follow these steps:

  1. Create an instance of the ControlsScreen class, passing the main game instance and a name as parameters.
GdxGame game = ...; // Initialize your game instance
String screenName = "ControlsScreen";
ControlsScreen controlsScreen = new ControlsScreen(game, screenName);
  1. In the show method, you should register essential services, load required assets, and create the user interface components.

  2. Use the createUI method to create the user interface elements, including the tutorial image and exit button.

  3. Add any event listeners or functionality to the exit button to handle player interaction.

  4. Handle rendering, resizing, and disposing as needed.

Methods

ControlsScreen(GdxGame game, String name)

  • Constructor for creating a ControlsScreen instance.

show()

  • The show method is called when the screen is displayed. It should be used for initializing services, loading assets, creating UI elements, and setting up event listeners.

render(float delta)

  • The render method is called to update and render the screen. In this method, you should update physics, entities, and the renderer.

resize(int width, int height)

  • The resize method is called when the screen is resized. Ensure that you adjust the camera and renderer to match the new dimensions.

pause()

  • The pause method is called when the game is paused. You can implement any pause-related logic in this method.

resume()

  • The resume method is called when the game is resumed from a paused state. Implement any logic needed for resuming the game here.

dispose()

  • The dispose method is called to release resources and clean up the screen. You should clear the screen, release services, and unload assets in this method.

Example

Here's a simplified example of how to create and use the ControlsScreen in your game:

GdxGame game = ...; // Initialize your game instance
String screenName = "ControlsScreen";
ControlsScreen controlsScreen = new ControlsScreen(game, screenName);
controlsScreen.show();

This example demonstrates the basic usage of the ControlsScreen. You can customize it further based on your specific game requirements.

Code Implementation

  • ControlGameArea
// ControlGameArea.java
package com.csse3200.game.areas;

import com.badlogic.gdx.math.GridPoint2;
import com.csse3200.game.GdxGame;
import com.csse3200.game.areas.terrain.TerrainFactory;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.configs.PlayerConfig;
import com.csse3200.game.entities.factories.PlayerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Game area designed for controlling the game flow,
 * including the introduction of controls, entities and other features.
 */
public class ControlGameArea extends MapGameArea {

    private static final Logger logger = LoggerFactory.getLogger(ControlGameArea.class);
    private int stepIndex = 0;

    /**
     * Constructor for ControlGameArea.
     *
     * @param terrainFactory Factory to generate terrain for the game area.
     * @param game Reference to the main game instance.
     */
    public ControlGameArea(TerrainFactory terrainFactory, GdxGame game) {
        super("controls", "main_area", terrainFactory, game);
    }


    /**
     * Create the game area
     */
    @Override
    public void create() {
        super.create();
    }

    /**
     * Spawns a player entity into the game area.
     * The player's configuration is determined by the area's entity configuration.
     * If no configuration is found, a generic player is spawned.
     *
     * @return The created player entity.
     */
    public Entity spawnPlayer() {
        Entity newPlayer;
        PlayerConfig playerConfig = null;
        if (mapConfig.areaEntityConfig != null) {
            playerConfig = mapConfig.areaEntityConfig.getEntity(PlayerConfig.class);
        }

        if (playerConfig != null) {
            newPlayer = PlayerFactory.createPlayer(playerConfig);
        } else {
            logger.info("Player not found in config file - creating generic player");
            newPlayer = PlayerFactory.createPlayer();
        }

        if (playerConfig != null && playerConfig.position != null) {
            spawnEntityAt(newPlayer, playerConfig.position, true, true);
        } else {
            logger.info("Failed to load player position - created player at middle of map");
            //If no position specified spawn in middle of map.
            GridPoint2 pos = new GridPoint2(terrain.getMapBounds(0).x/2,terrain.getMapBounds(0).y/2);
            spawnEntityAt(newPlayer, pos, true, true);
        }
        return newPlayer;
    }
    
}

The implementation for this can be found in ControlGameArea and ControlsScreen classes.

Contributors

  • Yash Mittal (@YashMitttal} (s4823869)
  • Dev Gupta (@DRG31) (xdrgx)
Clone this wiki locally