Class StateMachine<S>

java.lang.Object
nl.colorize.multimedialib.scene.StateMachine<S>
Type Parameters:
S - The type of state represented by this finite state machine. States are considered equal if stateA.equals(stateB). If the state implements the Updatable interface, the update method of the currently active state will be called when the state machine itself receives frame updates.
All Implemented Interfaces:
Updatable

public class StateMachine<S> extends Object implements Updatable
Finite state machine that allows a number of possible states, but can only have one currently active state at any point in time.

The state machine implements the Updatable interface, so it needs to receive frame updates in order to function. Requested states are stored in a queue, with the actual state change only occurring during the frame update.

By default, every state is allowed to transition into every other state. The state machine can optionally be configured to restrict allowed transitions based on the currently active state.

  • Constructor Summary

    Constructors
    Constructor
    Description
    StateMachine(S defaultState)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Restricts state transitions based on the specified predicate.
    void
    allowTransitions(nl.colorize.util.stats.TupleList<S,S> allowed)
    Restricts state transitions to only those included in the specified list.
    void
    forceState(S nextState)
    Forces this state machine into the specified state, clearing the queue so that the requested state becomes active during the next frame update.
     
     
    boolean
    requestState(S nextState)
    Requests the state machine to transition into the specified state at the earliest opportunity.
    boolean
    requestState(S nextState, float duration)
    Requests the state machine to transition into the specified state at the earliest opportunity.
    void
    update(float deltaTime)
    Updates this object for the current frame.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StateMachine

      public StateMachine(S defaultState)
  • Method Details

    • requestState

      public boolean requestState(S nextState)
      Requests the state machine to transition into the specified state at the earliest opportunity. Once active, the state will remain active until another state is requested.

      Returns a boolean indicating if the state machine allows a transition from the preceding state in the queue into the requested state.

    • requestState

      public boolean requestState(S nextState, float duration)
      Requests the state machine to transition into the specified state at the earliest opportunity. Once active, the state will remain active for the specified duration (in seconds).

      Returns a boolean indicating if the state machine allows a transition from the preceding state in the queue into the requested state.

    • forceState

      public void forceState(S nextState)
      Forces this state machine into the specified state, clearing the queue so that the requested state becomes active during the next frame update.
    • allowTransitions

      public void allowTransitions(BiPredicate<S,S> callback)
      Restricts state transitions based on the specified predicate. The first and second argument in the callback function refer to the current state and requested state respectively.
    • allowTransitions

      public void allowTransitions(nl.colorize.util.stats.TupleList<S,S> allowed)
      Restricts state transitions to only those included in the specified list. The first and second argument in each tuple refers to the current state and requested state respectively.
    • update

      public void update(float deltaTime)
      Description copied from interface: Updatable
      Updates this object for the current frame. deltaTime indicates the elapsed time since the last frame update, in seconds.
      Specified by:
      update in interface Updatable
    • getActiveState

      public S getActiveState()
    • getActiveStateTimer

      public Timer getActiveStateTimer()