118 lines
3.7 KiB
C#
118 lines
3.7 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;
|
||
}
|
||
var vfState = new VfState();
|
||
vfState.OnEnter(() =>
|
||
{
|
||
vfState.enterAct = ActionHelper.GetActionAndSub(state.Value.Enter?.Action);
|
||
vfState.enterAct?.Start(this);
|
||
}).OnExit(() =>
|
||
{
|
||
if (vfState.enterAct != null)
|
||
{
|
||
vfState.enterAct.Deinit();
|
||
}
|
||
vfState.exitAct = ActionHelper.GetActionAndSub(state.Value.Exit?.Action);
|
||
vfState.exitAct?.Start(this);
|
||
|
||
});
|
||
fsm.AddState(state.Key, vfState);
|
||
}
|
||
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)
|
||
{
|
||
VfState state = item.Value.fsm.CurrentState as VfState;
|
||
if (state.enterAct == null || state.enterAct.Status == ActionStatus.Finished)
|
||
{
|
||
foreach (var trans in item.Value.transisions)
|
||
{
|
||
if ((trans.from == "any" && trans.to != item.Value.fsm.CurrentStateId) || trans.from == item.Value.fsm.CurrentStateId)
|
||
{
|
||
if (trans.condition == null || trans.condition.Check())
|
||
{
|
||
item.Value.fsm.ChangeState(trans.to);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
}
|