修改状态机bug

This commit is contained in:
shenjianxing 2025-01-19 19:47:16 +08:00
parent cbf6cde0b0
commit 378ebcde83
8 changed files with 90 additions and 45 deletions

View File

@ -253,6 +253,8 @@ public class ActionHelper
/// <param name="condition"></param> /// <param name="condition"></param>
/// <returns></returns> /// <returns></returns>
public static ICondition GetCondition(XMLTool.ActionItem condition) public static ICondition GetCondition(XMLTool.ActionItem condition)
{
if (condition!=null)
{ {
switch (condition.Type) switch (condition.Type)
{ {
@ -276,6 +278,7 @@ public class ActionHelper
case "StrEvent": case "StrEvent":
return StrEventCondition.Allocate(condition.Value); return StrEventCondition.Allocate(condition.Value);
} }
}
return null; return null;
} }

View File

@ -48,31 +48,22 @@ public class StateMachineController : MonoSingleton<StateMachineController>
{ {
firstState = state.Key; firstState = state.Key;
} }
IAction enterAct = null; var vfState = new VfState();
IAction exitAct = null; vfState.OnEnter(() =>
fsm.State(state.Key).OnEnter(() =>
{ {
if (state.Value.Enter != null) vfState.enterAct = ActionHelper.GetActionAndSub(state.Value.Enter?.Action);
{ vfState.enterAct?.Start(this);
enterAct = ActionHelper.GetActionAndSub(state.Value.Enter.Action);
enterAct?.Start(this);
}
}).OnExit(() => }).OnExit(() =>
{ {
if (enterAct != null) if (vfState.enterAct != null)
{ {
enterAct.Deinit(); vfState.enterAct.Deinit();
} }
if (state.Value.Exit != null) vfState.exitAct = ActionHelper.GetActionAndSub(state.Value.Exit?.Action);
{ vfState.exitAct?.Start(this);
exitAct = ActionHelper.GetActionAndSub(state.Value.Exit.Action);
exitAct?.Start(this);
}
}); });
fsm.AddState(state.Key, vfState);
} }
foreach (var transision in item.Value.Transisions) foreach (var transision in item.Value.Transisions)
{ {
@ -102,14 +93,19 @@ public class StateMachineController : MonoSingleton<StateMachineController>
{ {
foreach (var item in FSMDitc) foreach (var item in FSMDitc)
{ {
var curState = item.Value.fsm.CurrentStateId; VfState state = item.Value.fsm.CurrentState as VfState;
if (state.enterAct == null || state.enterAct.Status == ActionStatus.Finished)
{
foreach (var trans in item.Value.transisions) foreach (var trans in item.Value.transisions)
{ {
if (trans.from == "any" || trans.from == item.Value.fsm.CurrentStateId) if ((trans.from == "any" && trans.to != item.Value.fsm.CurrentStateId) || trans.from == item.Value.fsm.CurrentStateId)
{ {
if (trans.condition.Check()) if (trans.condition == null || trans.condition.Check())
{ {
Debug.LogError(trans.from + "To:" + trans.to);
item.Value.fsm.ChangeState(trans.to); item.Value.fsm.ChangeState(trans.to);
break;
}
} }
} }
} }

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 6d7f9813c337725468c8739dba68c439 guid: 8b474fbd8dd5f4c49b70b10f5328d67e
folderAsset: yes folderAsset: yes
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5feaaac014488434681db44e2face542
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,14 @@
using QFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VfState : CustomState
{
public IAction enterAct = null;
public IAction exitAct = null;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a5f77690765e1f44b3f3ac7783076fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -35,6 +35,7 @@ public class Point3DItem : MonoBehaviour
} }
rotSpeed = data.rotateSpeed; rotSpeed = data.rotateSpeed;
gameObject.GetComponent<SpriteRenderer>().sortingOrder = data.order;
TypeEventSystem.Global.Register<OnPoint3DQuestionDestroy>(OnObjDestroy).UnRegisterWhenGameObjectDestroyed(gameObject); TypeEventSystem.Global.Register<OnPoint3DQuestionDestroy>(OnObjDestroy).UnRegisterWhenGameObjectDestroyed(gameObject);
} }

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Xml.Linq; using System.Xml.Linq;
using UnityEngine; using UnityEngine;
using static UnityEngine.Rendering.DebugUI;
namespace XMLTool namespace XMLTool
{ {
public class AppData public class AppData
@ -88,6 +89,7 @@ namespace XMLTool
public Vector3 rotate; public Vector3 rotate;
public Vector3 scale; public Vector3 scale;
public float rotateSpeed; public float rotateSpeed;
public int order = 1;
public string clickEvent; public string clickEvent;
} }
public List<Data> datas = new List<Data>(); public List<Data> datas = new List<Data>();
@ -1093,6 +1095,12 @@ namespace XMLTool
data.scale = Utility.GetVector3FromStrArray(item.Attribute("scale")?.Value); data.scale = Utility.GetVector3FromStrArray(item.Attribute("scale")?.Value);
} }
float.TryParse(item.Attribute("rotateSpeed")?.Value, out data.rotateSpeed); float.TryParse(item.Attribute("rotateSpeed")?.Value, out data.rotateSpeed);
var order = item.Attribute("order");
if (order != null)
{
int.TryParse(order.Value, out data.order);
}
data.clickEvent = item.Attribute("clickEvent")?.Value; data.clickEvent = item.Attribute("clickEvent")?.Value;
act.datas.Add(data); act.datas.Add(data);
} }
@ -1146,6 +1154,10 @@ namespace XMLTool
public static Condition ParseCondition(XElement action) public static Condition ParseCondition(XElement action)
{ {
if (action == null)
{
return null;
}
Condition newAction = null; Condition newAction = null;
string type = action.Attribute("type")?.Value; string type = action.Attribute("type")?.Value;
switch (type) switch (type)