/* author: YKMoon
* 状态机
* 初始化需要手动添加状态信息 AddState
* Update里面来调用状态机运行StateMachineUpdate
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YKMoon
{
public class UFSM
{
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 virtual void Update(float deltaTime, float unscaledDeltaTime)
{
if(updateMode == FSMUpdateMode.Normal) {
stateMachine.StateMachineUpdate(deltaTime);
} else {
stateMachine.StateMachineUpdate(unscaledDeltaTime);
}
}
protected virtual void EarlyGlobalUpdate(float deltaTime) { }
protected virtual void LateGlobalUpdate(float deltaTime) { }
public virtual void Reg()
{
UFSMManager.Reg(this);
}
public virtual void UnReg()
{
UFSMManager.UnReg(this);
}
}
}