Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StackFSM : IDisposable {
- // Default state
- private readonly Action<float> _defaultState;
- // FSM Stack
- private readonly Stack<Action<float>> _actionStack = new Stack<Action<float>>();
- // Disposing bool
- private bool _disposing;
- // Constructor
- public StackFSM(Action<float> defaultState) {
- _defaultState = defaultState;
- }
- // Get the current state from the stack
- public Action<float> GetCurrentState() {
- // Return the state at the top of the stack or return the default state if empty
- return _actionStack.Count > 0 ? _actionStack.Peek() : _defaultState;
- }
- // Push a new state to the stack
- public void PushState(Action<float> actionState) {
- _actionStack.Push(actionState);
- }
- // Pop a state from the Stack
- public void PopState() {
- _actionStack.Pop();
- }
- // Update the state machine
- public void Update(float deltaTime) {
- var state = GetCurrentState();
- state?.Invoke(deltaTime);
- }
- // Dispose of the class
- public void Dispose() {
- // Exit if already disposing
- if (_disposing) return;
- // Clear all actions from the stack
- _actionStack.Clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement