-
Notifications
You must be signed in to change notification settings - Fork 9
Managing Game State
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.
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.
public void replaceResource(String resourceKey, int resourceAmount) {
this.put(resourceKey, resourceAmount);
}
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.
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);
}
Once the modification behaviour is defined and the listener configured the game state can be freely accessed through the GameStateObserver
using the Service Locator
.
Import the ServiceLocator
and GameStateObserver
classes into the project.
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.services.GameStateObserver;
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();
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);
Data can also be directly accessed straight from the game state through its key by calling getStateData
.
GameStateObserver stateObserver = ServiceLocator.getGameStateObserverService()
stateObserver.getStateData("Unobtanium");
Escape Earth Game
Interaction Controller and Interactable Components
Game and Entity Configuration Files
Loading Game Configuration Files