119 lines
2.4 KiB
C#
119 lines
2.4 KiB
C#
|
using SHADE;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
public abstract class StateMachine : BaseComponent
|
|||
|
{
|
|||
|
private Dictionary<Type, BaseState> stateDictionary;
|
|||
|
public BaseState currentState = null;
|
|||
|
public string currentStateName;
|
|||
|
public string currentAnimName;
|
|||
|
|
|||
|
public StateMachine(uint entity) : base(entity) { }
|
|||
|
|
|||
|
public void InitStateMachine(Dictionary<Type, BaseState> dictionary)
|
|||
|
{
|
|||
|
|
|||
|
stateDictionary = dictionary;
|
|||
|
currentState = stateDictionary.First().Value;
|
|||
|
currentStateName = currentState.GetStateName();
|
|||
|
currentAnimName = currentState.GetAnimName();
|
|||
|
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];
|
|||
|
}
|
|||
|
|
|||
|
public void Update()
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
{
|
|||
|
currentStateName = currentState.GetStateName();
|
|||
|
currentAnimName = currentState.GetAnimName();
|
|||
|
currentState.Update();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
public bool IsState(Type type)
|
|||
|
{
|
|||
|
return (currentState.GetType() == type);
|
|||
|
}
|
|||
|
|
|||
|
public void onCollisionEnter(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onCollisionEnter(info);
|
|||
|
}
|
|||
|
|
|||
|
public void onCollisionStay(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onCollisionStay(info);
|
|||
|
}
|
|||
|
|
|||
|
public void onCollisionExit(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onCollisionExit(info);
|
|||
|
}
|
|||
|
|
|||
|
public void onTriggerEnter(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onTriggerEnter(info);
|
|||
|
}
|
|||
|
|
|||
|
public void onTriggerStay(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onTriggerStay(info);
|
|||
|
}
|
|||
|
|
|||
|
public void onTriggerExit(CollisionInfo info)
|
|||
|
{
|
|||
|
if (currentState != (null))
|
|||
|
currentState.onTriggerExit(info);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|