123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using QFramework;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Xml;
|
||
using UnityEngine;
|
||
using XMLTool;
|
||
|
||
public class StateMachineController : MonoSingleton<StateMachineController>
|
||
{
|
||
public class FsmInfo
|
||
{
|
||
public FSM<string> fsm;
|
||
public List<Transision> transisions = new List<Transision>();
|
||
}
|
||
public class Transision
|
||
{
|
||
public string from;
|
||
public string to;
|
||
public ICondition condition;
|
||
}
|
||
Dictionary<string, FsmInfo> FSMDitc = new Dictionary<string, FsmInfo>();
|
||
|
||
public bool isRun = false;
|
||
public override void OnSingletonInit()
|
||
{
|
||
base.OnSingletonInit();
|
||
TypeEventSystem.Global.Register<OnModuleStart>(OnStart).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
TypeEventSystem.Global.Register<OnModuleQuit>(OnQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
}
|
||
|
||
private void OnQuit(OnModuleQuit quit)
|
||
{
|
||
isRun = false;
|
||
FSMDitc.Clear();
|
||
}
|
||
|
||
private void OnStart(OnModuleStart start)
|
||
{
|
||
foreach (var item in Global.Instance.curModule.FSM)
|
||
{
|
||
FSM<string> fsm = new FSM<string>();
|
||
var fsmInfo = new FsmInfo();
|
||
string firstState = string.Empty;
|
||
foreach (var state in item.Value.StateDict)
|
||
{
|
||
if (string.IsNullOrEmpty(firstState))
|
||
{
|
||
firstState = state.Key;
|
||
}
|
||
IAction enterAct = null;
|
||
IAction exitAct = null;
|
||
|
||
|
||
fsm.State(state.Key).OnEnter(() =>
|
||
{
|
||
if (state.Value.Enter != null)
|
||
{
|
||
enterAct = ActionHelper.GetActionAndSub(state.Value.Enter.Action);
|
||
enterAct?.Start(this);
|
||
}
|
||
|
||
}).OnExit(() =>
|
||
{
|
||
if (enterAct != null)
|
||
{
|
||
enterAct.Deinit();
|
||
}
|
||
if (state.Value.Exit != null)
|
||
{
|
||
exitAct = ActionHelper.GetActionAndSub(state.Value.Exit.Action);
|
||
exitAct?.Start(this);
|
||
|
||
}
|
||
});
|
||
}
|
||
foreach (var transision in item.Value.Transisions)
|
||
{
|
||
var trans = new Transision();
|
||
trans.to = transision.To;
|
||
trans.from = transision.From;
|
||
trans.condition = ActionHelper.GetCondition(transision.Action);
|
||
fsmInfo.transisions.Add(trans);
|
||
}
|
||
fsmInfo.fsm = fsm;
|
||
fsmInfo.fsm.StartState(firstState);
|
||
if (FSMDitc.ContainsKey(item.Key)==false)
|
||
{
|
||
FSMDitc.Add(item.Key, fsmInfo);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("״̬<D7B4><CCAC>name<6D>ظ<EFBFBD>");
|
||
}
|
||
}
|
||
isRun = true;
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (isRun)
|
||
{
|
||
foreach (var item in FSMDitc)
|
||
{
|
||
var curState = item.Value.fsm.CurrentStateId;
|
||
foreach (var trans in item.Value.transisions)
|
||
{
|
||
if (trans.from == "any" || trans.from == item.Value.fsm.CurrentStateId)
|
||
{
|
||
if (trans.condition.Check())
|
||
{
|
||
item.Value.fsm.ChangeState(trans.to);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
}
|