SHADE_Y3/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs

81 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHADE;
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
{
public class StateMachine: Script
{
private Dictionary<Type, BaseState> stateDictionary;
public BaseState currentState = null;
public string currentStateName;
public void InitStateMachine(Dictionary<Type,BaseState> dictionary)
{
stateDictionary = dictionary;
currentState = stateDictionary.First().Value;
currentStateName = currentState.GetStateName();
currentState.OnEnter();
}
public bool HasState(Type type)
{
if(!type.IsSubclassOf(typeof(BaseState)))
{
return false;
}
else
{
return stateDictionary.ContainsKey(type);
}
}
public void SetState(Type type)
{
if (!type.IsSubclassOf(typeof(BaseState)))
{
return;
}
if (stateDictionary.ContainsKey(type))
{
currentState.OnExit();
currentState = stateDictionary[type];
currentState.OnEnter();
}
else
{
SetState(stateDictionary.First().Key);
}
}
public BaseState GetState(Type type)
{
if (!stateDictionary.ContainsKey(type))
return null;
return stateDictionary[type];
}
protected override void update()
{
if(currentState != null)
{
currentStateName = currentState.GetStateName().ToString();
currentState.Update();
}
}
public bool IsState(Type type)
{
return (currentState.GetType() == type);
}
}
}