Skip to content

Managing Game State

Henry Batt edited this page Aug 30, 2023 · 10 revisions

The management, access, and update of the global game state GameState is facilitated through the GameStateObserver. The observer acts as the only point of external interaction to modify the game state data.

Modification is obtained through Event triggers with the GameStateObserver acting as a Service Event Listener.

The two key components of the Observer are determining modification behaviour, and managing state data including updating and getting.

Modification Behaviour

1. State Modification Helper

The first step is to write a GameStateInteraction helper method to outline how and what needs to be modified in the game state.

The two primary ways are simply storing the value and overwriting existing data, or modifying the existing data.

Additional helpers must be written in the GameStateInteraction.java class file and be public methods of type void with up to three input parameters.

Some existing helpers already exist and can be used if appropriate and this step can be skipped.

i. Overwriting Data

public void replaceResource(String resourceKey, int resourceAmount) {
    this.put(resourceKey, resourceAmount);
}

ii. Modifying Existing Data

public void modifyResource(String resourceKey, int changeAmount) {
    Object value = gameState.get(resourceKey);
    int amount = value == null ? 0 : (int) value;
    this.put(resourceKey, amount + changeAmount);
}

Note: That method inputs don't have to be (key, value) but rather the key could be generated from an input - ie. resourceKey = "resource/" + resourceName; - to give a standard beginning header for all similar types and ensure that the name isn't already in use.

2. Registering Listener

Once the method has been created to change to the game state the listener must be configured for the modification call to occur.

In the GameStateObserver.java file there is the generateStateListeners method. A listener must be added with the desired name and the created helper method.

private void generateStateListeners() {
    this.addListener("resourceAdd", stateInteraction::updateResource);
}

Managing State Data

Once the modification behaviour is defined and the listener configured the game state can be freely accessed through the GameStateObserver using the Service Locator.

1. Accessing Through Service Locator

i. Importing

Import the ServiceLocator and GameStateObserver classes into the project.

import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.services.GameStateObserver;

ii. Accessing ServiceLocator

Initialising in ServiceLocator: An instance of the GameStateObserver needs to be initialised and registered with the ServiceLocator

GameStateObserver stateObserver = new GameStateObserver();
ServiceLocator.registerGameStateObserverService(stateObserver);

Note: Please note that the GameStateObserver has already been initialised as part of the MainGameScreen and does not need to be redone.

Accessing the ServiceLocator: Once initialised the GameStateObserver can then be accessed anytime when needed.

GameStateObserver stateObserver = ServiceLocator.getGameStateObserverService();

2. Updating Data (Calling Triggers)

Once accessed triggers can then be called to modify state data. This is done by triggering the specific event name outlined when registering the listener with appropriate parameters.

GameStateObserver stateObserver = ServiceLocator.getGameStateObserverService()
stateObserver.trigger("resourceAdd", "Unobtanium", 100);

3. Reading Data

Data can also be directly accessed straight from the game state through its key by calling getStateData.

GameStateObserver stateObserver = ServiceLocator.getGameStateObserverService()
stateObserver.getStateData("Unobtanium");
Clone this wiki locally