FiniteStateMachine.cs
Description
This script defines the Player's Finite State Machine system, and handles the initialisation & changing of the active state.
Script
namespace Player.FSM
{
public abstract class FiniteStateMachine
{
private FsmState _initialState;
public FsmState CurrentState { get; set; }
public FsmState PreviousState { get; set; }
public void Initialize(FsmState startingState)
{
CurrentState = startingState;
CurrentState.Enter();
}
public void ChangeState(FsmState newState)
{
PreviousState = CurrentState;
CurrentState?.Exit();
CurrentState = newState;
CurrentState?.Enter();
}
}
}
Public Methods
- ChangeState()
Changes the current state of the Finite State Machine. Stores a reference to the previous state, exits it, updates the current state and then enters the new state.
- Initialize()
Sets the current state of the Finite State Machine to the state passed through as an input, and then enters that state.
Variables
- CurrentState
The current state of the Finite State Machine.
- PreviousState
The previous state of the Finite State Machine.
Last modified: 30 April 2024