2024-12-14 18:27:59 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2024-12-30 16:14:35 +08:00
|
|
|
|
foreach (var item in Global.Instance.curModule.FSM)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-01-19 19:47:16 +08:00
|
|
|
|
var vfState = new VfState();
|
|
|
|
|
|
vfState.OnEnter(() =>
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
vfState.enterAct = ActionHelper.GetActionAndSub(state.Value.Enter?.Action);
|
|
|
|
|
|
vfState.enterAct?.Start(this);
|
2024-12-14 18:27:59 +08:00
|
|
|
|
}).OnExit(() =>
|
|
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
if (vfState.enterAct != null)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
vfState.enterAct.Deinit();
|
2024-12-14 18:27:59 +08:00
|
|
|
|
}
|
2025-01-19 19:47:16 +08:00
|
|
|
|
vfState.exitAct = ActionHelper.GetActionAndSub(state.Value.Exit?.Action);
|
|
|
|
|
|
vfState.exitAct?.Start(this);
|
2024-12-14 18:27:59 +08:00
|
|
|
|
|
|
|
|
|
|
});
|
2025-01-19 19:47:16 +08:00
|
|
|
|
fsm.AddState(state.Key, vfState);
|
2024-12-14 18:27:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
2025-01-19 19:47:16 +08:00
|
|
|
|
if (FSMDitc.ContainsKey(item.Key) == false)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
VfState state = item.Value.fsm.CurrentState as VfState;
|
|
|
|
|
|
if (state.enterAct == null || state.enterAct.Status == ActionStatus.Finished)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
foreach (var trans in item.Value.transisions)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
if ((trans.from == "any" && trans.to != item.Value.fsm.CurrentStateId) || trans.from == item.Value.fsm.CurrentStateId)
|
2024-12-14 18:27:59 +08:00
|
|
|
|
{
|
2025-01-19 19:47:16 +08:00
|
|
|
|
if (trans.condition == null || trans.condition.Check())
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError(trans.from + "To:" + trans.to);
|
|
|
|
|
|
item.Value.fsm.ChangeState(trans.to);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2024-12-14 18:27:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|