Skip to content

State Machine

jsutlive edited this page May 17, 2023 · 4 revisions

final class State Machine

Extends: N/A

Implements: N/A

Constructor:

Type Parameter Description
Time referenceTime Engine's physics clock
Boolean launchOnStart on startup, will play simulation (run state) if true, else will not play simulation (editor state)

Events

Accessibility Type Variable Name Description
public, static String onSaveStateInfo alert when a save point has been reached
public, static Boolean onStateMachineStateChange alert when the state is changed

Fields:

Accessibility Type Field Name Description
public State currentState System state object controlling response to update loop
public List allObjects objects that are added to the physics loop
public, static Time timer Reference to the simulation engine's physics timer

Methods

Accessibility Return Type Method Name Description
public void addEntityToList adds an entity to objects to be updated in the physics loop
public void removeEntityFromList removes an entity from objects to be updated in the physics loop
public void cycle reverses the allObjects list
public void selectEntityWithTag findObjectWithTag, and set this entity as "selected"
private void findObjectWithTag Return the first entity with the tag specified.
public void changeState move to a new state, given as input to this function.
public void handleSimulationToggle manages state changes and actions when receiving input regarding simulation start/stop
public void clearStateMachine removes all objects from state machine with exception of permanent objects (Settings, Camera)
public void loadModel load a preset model to the simulation using ModelLoader.
public void deactivate Closes major events and clears all simulation objects. Only called prior to shutdown.

State Machine and State Pattern for Data Management

States and Changing State

A state simply manages what actions will be performed during the update loop of the state machine. This way, we can easily control the program logic without having to make a cumbersome number of conditional statements. Additionally, this pattern makes the program more scalable, allowing for additional states to be created if necessary while limiting the amount of rewriting to be done to other scripts.

Simulation Timer and the Update Loop

In conjunction with the Entity class and methods, the simulation updates according to the fps set in the State Machine/ Engine timer. The diagram below shows the relationship between the object function calls, timing, and the update loop.

The Update Loop: As described in the Engine class, the timer "ticks" periodically as determined by the frames per second (fps) variable as set by the user when creating a "Time" timer object. When each of these "ticks" occurs, this leads to the state machine, and eventually the state, calling tick as well. From there, any function within tick will be called (specifically looped over objects in the allObjects list), thus leading to repeated, periodic actions being performed by objects in the state machine. This behavior typically only occurs in the Run State, check the documentation here for more specific descriptions on how this is managed.

Program Flow and Object State Calls