Class StateMachine<T extends State>

  • Type Parameters:
    T - The type of state which is being controlled by this state machine.
    All Implemented Interfaces:
    Updatable

    public class StateMachine<T extends State>
    extends java.lang.Object
    implements Updatable
    Represents a finite state machine. States can be identified by name or by the actual state object. States can have characteristics such as timed versus infinite, and interruptable versus permanent. The behavior of the state machine will depend on those characteristics, of both the current state and the requested new state.
    • Constructor Detail

      • StateMachine

        public StateMachine()
    • Method Detail

      • register

        public void register​(T state)
        Registers a state as a possible state for this state machine. If this is the first state to be registered, it is automatically set as the active state.
        Throws:
        java.lang.IllegalArgumentException - if the state has an invalid name, or if the state machine already contains a state with the same name.
      • changeState

        public boolean changeState​(T requestedState)
        Changes this state machine to the specified state. If the state is already active, or if the currently active state cannot be interrupted, this method does nothing. Returns true if the state was actually changed as a result of calling this method.

        This will also register the state with the state machine, if this is not already the case.

      • changeState

        public boolean changeState​(T requestedState,
                                   float duration)
        Changes this state machine to the specified state, which will remain active for the specified time. The requested duration overrides the duration indicated in the state itself. Once the state has finished, the state machine will switch to the next state. If the next state is null, the state machine will remain in the requested state indefinitely.

        If the state is already active, or if the currently active state cannot be interrupted, this method does nothing. Returns true if the state was actually changed as a result of calling this method.

        This will also register the state with the state machine, if this is not already the case.

      • update

        public void update​(float deltaTime)
        Description copied from interface: Updatable
        Updates this object for the current frame.
        Specified by:
        update in interface Updatable
        Parameters:
        deltaTime - Elapsed time since the last frame, in seconds.
      • resetActiveState

        public void resetActiveState()
      • getActiveState

        public T getActiveState()
      • getActiveStateName

        public java.lang.String getActiveStateName()
      • getActiveStateTime

        public float getActiveStateTime()
      • getActiveStateDuration

        public float getActiveStateDuration()
      • getPossibleStates

        public java.util.Set<T> getPossibleStates()
      • getPossibleState

        public T getPossibleState​(java.lang.String name)
      • hasState

        public boolean hasState​(java.lang.String name)
      • hasState

        public boolean hasState​(State state)