Package nl.colorize.multimedialib.scene
Class StateMachine<T extends State>
- java.lang.Object
-
- nl.colorize.multimedialib.scene.StateMachine<T>
-
- 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 Summary
Constructors Constructor Description StateMachine()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanchangeState(T requestedState)Changes this state machine to the specified state.booleanchangeState(T requestedState, float duration)Changes this state machine to the specified state, which will remain active for the specified time.TgetActiveState()floatgetActiveStateDuration()java.lang.StringgetActiveStateName()floatgetActiveStateTime()TgetPossibleState(java.lang.String name)java.util.Set<T>getPossibleStates()booleanhasState(java.lang.String name)booleanhasState(State state)voidregister(T state)Registers a state as a possible state for this state machine.voidresetActiveState()voidupdate(float deltaTime)Updates this object for the current frame.
-
-
-
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 isnull, 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:UpdatableUpdates this object for the current frame.
-
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)
-
-