DungeonRPG/Scripts/Characters/StateMachine.cs
Bobby Lucero 7bc6454ce1 State Machine
(tutorial has us refactoring this monstrosity later)
2025-04-24 00:49:23 -04:00

35 lines
806 B
C#

using Godot;
using System;
using DungeonRPG.Scripts.General;
public partial class StateMachine : Node
{
[Export] private Node currentState;
[Export] private Node[] states;
public override void _Ready()
{
currentState.Notification(GameConstants.STATE_NOTIFICATION_ENABLE);
}
public void SwitchState<T>()
{
Node newState = null;
foreach (Node state in states)
{
if (state is T && currentState != state)
{
newState = state;
}
}
if (newState != null)
{
currentState.Notification(GameConstants.STATE_NOTIFICATION_DISABLE);
currentState = newState;
currentState.Notification(GameConstants.STATE_NOTIFICATION_ENABLE);
}
}
}