/* author: YKMoon
* 状态机Mono版
* 初始化需要手动添加状态信息 AddState
* Update里面来调用状态机运行StateMachineUpdate(默认自动,想自定义就覆盖Update)M
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YKMoon
{
public class UFSMMono : YKMono
{
public FSMUpdateMode updateMode = FSMUpdateMode.Normal;
protected UFSMCore stateMachine {
get {
if(_stateMachine == null) {
_stateMachine = new UFSMCore(EarlyGlobalUpdate, LateGlobalUpdate);
}
return _stateMachine;
}
}
private UFSMCore _stateMachine;
public UFSMState currentState { get => stateMachine.currentState; set => stateMachine.currentState = value; }
public int lastState { get { return stateMachine.lastState; } }
public int currentStateID { get => stateMachine.currentStateID; set => stateMachine.currentStateID = value; }
///
/// 添加状态
///
/// true, 如果添加成功, false 添加失败.
/// 状态ID.
/// 进入状态时调用
/// 更新时调用
/// 出状态时调用.
public bool AddState(int stateID, Action onEnterState, Action onUpdate, Action onExitState)
{
return stateMachine.AddState(stateID, onEnterState, onUpdate, onExitState);
}
public bool AddState(T stateID, Action onEnterState, Action onUpdate, Action onExitState) where T: Enum
{
return stateMachine.AddState(Convert.ToInt32(stateID), onEnterState, onUpdate, onExitState);
}
protected virtual void Update()
{
if(updateMode == FSMUpdateMode.Normal) {
stateMachine.StateMachineUpdate(Time.deltaTime);
} else {
stateMachine.StateMachineUpdate(Time.unscaledDeltaTime);
}
}
protected virtual void EarlyGlobalUpdate(float deltaTime) { }
protected virtual void LateGlobalUpdate(float deltaTime) { }
}
}