You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
|
||||
public enum CompareType {
|
||||
|
||||
Greater = 0,
|
||||
Less,
|
||||
Equal,
|
||||
NotEqual,
|
||||
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class FSMConditionData
|
||||
{
|
||||
|
||||
public float targetValue;
|
||||
|
||||
public string parameterName;
|
||||
|
||||
public CompareType compareType;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2becbadd938f3145bb5a6adec0f834a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
|
||||
public enum ParameterType {
|
||||
Float = 0,
|
||||
Int,
|
||||
Bool,
|
||||
Trigger
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class FSMParameterData
|
||||
{
|
||||
#region 字段
|
||||
public string name;
|
||||
|
||||
public float value;
|
||||
|
||||
public ParameterType parameterType;
|
||||
|
||||
public Action onValueChange;
|
||||
#endregion
|
||||
|
||||
#region 属性
|
||||
|
||||
public float Value {
|
||||
get { return value; }
|
||||
set {
|
||||
|
||||
if (this.value == value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.value = value;
|
||||
onValueChange?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c3e41aa1d7814c14592ab6fe0f173093
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,337 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
[System.Serializable]
|
||||
public class FSMStateScriptInfo
|
||||
{
|
||||
public string className;
|
||||
public string guid;
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class FSMStateNodeData
|
||||
{
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public Rect rect;
|
||||
|
||||
public void AddStateScript(MonoScript script)
|
||||
{
|
||||
string guid = AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath(script));
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
throw new System.Exception(string.Format("脚本添加失败,获取guid失败:{0}",script.name));
|
||||
foreach (var item in StateScripts)
|
||||
{
|
||||
if (item.guid.Equals(guid))
|
||||
throw new System.Exception("脚本重复添加!");
|
||||
}
|
||||
FSMStateScriptInfo info = new FSMStateScriptInfo();
|
||||
info.guid = guid;
|
||||
Type type = script.GetClass();
|
||||
if (type != null)
|
||||
{
|
||||
info.className = type.FullName;
|
||||
}
|
||||
StateScripts.Add(info);
|
||||
}
|
||||
|
||||
public void RefreshStateScripts(RuntimeFSMController controller)
|
||||
{
|
||||
|
||||
//EditorApplication.
|
||||
|
||||
List<FSMStateScriptInfo> invalid = new List<FSMStateScriptInfo>();
|
||||
|
||||
foreach (var item in StateScripts)
|
||||
{
|
||||
if (string.IsNullOrEmpty(item.guid) && !string.IsNullOrEmpty(item.className))
|
||||
item.guid = AssemblyTools.GetGUIDByStateClassFullName(item.className);
|
||||
|
||||
string path = AssetDatabase.GUIDToAssetPath(item.guid);
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
invalid.Add(item);
|
||||
continue;
|
||||
}
|
||||
MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>(path);
|
||||
if (script == null)
|
||||
{
|
||||
//Debug.LogFormat("添加无效脚本:{0} guid:{1}", script.name, path);
|
||||
//invalid.Add(item);
|
||||
continue;
|
||||
}
|
||||
Type type = script.GetClass();
|
||||
// 如果等于空或者没有继承自FSMState视为无效
|
||||
if (type == null)
|
||||
{
|
||||
//invalid.Add(item);
|
||||
continue;
|
||||
}
|
||||
|
||||
item.className = type.FullName;
|
||||
}
|
||||
|
||||
// 移除无效的脚本 ( 这些脚本可能已经被删除了 )
|
||||
foreach (var item in invalid)
|
||||
{
|
||||
StateScripts.Remove(item);
|
||||
}
|
||||
|
||||
if(controller != null)
|
||||
controller.Save();
|
||||
}
|
||||
|
||||
public void RemoveStateScript(MonoScript script)
|
||||
{
|
||||
Type type = script.GetClass();
|
||||
if (type == null) return;
|
||||
|
||||
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(script));
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
throw new System.Exception(string.Format("脚本添加失败,获取guid失败:{0}", script.name));
|
||||
|
||||
FSMStateScriptInfo info = null;
|
||||
|
||||
foreach (var item in StateScripts)
|
||||
{
|
||||
if (item.guid.Equals(guid)) {
|
||||
info = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (info == null) return;
|
||||
|
||||
StateScripts.Remove(info);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 是否是默认状态
|
||||
/// </summary>
|
||||
public bool defaultState;
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
public string name;
|
||||
|
||||
/// <summary>
|
||||
/// 状态脚本的名称
|
||||
/// </summary>
|
||||
public List<FSMStateScriptInfo> StateScripts = new List<FSMStateScriptInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态的父节点名称
|
||||
/// </summary>
|
||||
public List<string> parents;
|
||||
|
||||
/// <summary>
|
||||
/// 是否为子状态机
|
||||
/// </summary>
|
||||
public bool isSubStateMachine = false;
|
||||
|
||||
/// <summary>
|
||||
/// 是否为内置状态
|
||||
/// </summary>
|
||||
public bool isBuildInState = false;
|
||||
|
||||
/// <summary>
|
||||
/// 内置状态名称
|
||||
/// </summary>
|
||||
public string buildInStateName = null;
|
||||
|
||||
#region 属性
|
||||
|
||||
/// <summary>
|
||||
/// 判断当前状态是否为Any状态
|
||||
/// </summary>
|
||||
public bool IsAnyState
|
||||
{
|
||||
get
|
||||
{
|
||||
if (name.Equals(FSMConst.anyState))
|
||||
return true;
|
||||
|
||||
if (isBuildInState && buildInStateName.Equals(FSMConst.anyState))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断当前状态是否Entry状态
|
||||
/// </summary>
|
||||
public bool IsEntryState
|
||||
{
|
||||
get {
|
||||
|
||||
if (name.Equals(FSMConst.entryState))
|
||||
return true;
|
||||
|
||||
if (isBuildInState && buildInStateName.Equals(FSMConst.entryState))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否为返回上一层的按钮
|
||||
/// </summary>
|
||||
public bool IsUpState
|
||||
{
|
||||
get {
|
||||
|
||||
if (isBuildInState && buildInStateName.Equals(FSMConst.up))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string DisplayName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsAnyState)
|
||||
return FSMConst.anyState;
|
||||
if (IsEntryState)
|
||||
return FSMConst.entryState;
|
||||
if (IsUpState)
|
||||
{
|
||||
if (parents.Count > 1)
|
||||
return string.Format("(Up){0}", parents[parents.Count - 2]);
|
||||
else
|
||||
return "(Up)Base Layer";
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
public string ParentPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (parents == null || parents.Count == 0) return string.Empty;
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < parents.Count; i++) {
|
||||
sb.Append(parents[i]);
|
||||
if (i < parents.Count - 1)
|
||||
sb.Append("/");
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public string NextParentPath {
|
||||
get
|
||||
{
|
||||
List<string> next_parents = new List<string>();
|
||||
|
||||
if (parents != null && parents.Count != 0)
|
||||
next_parents.AddRange(parents);
|
||||
|
||||
next_parents.Add(name);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < next_parents.Count; i++)
|
||||
{
|
||||
sb.Append(next_parents[i]);
|
||||
if (i < next_parents.Count - 1)
|
||||
sb.Append("/");
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public string Parent
|
||||
{
|
||||
get {
|
||||
if (parents == null || parents.Count == 0) return string.Empty;
|
||||
|
||||
return parents[parents.Count - 1];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool CompareParent(FSMStateNodeData node)
|
||||
{
|
||||
if (BaseLayer() && node.BaseLayer()) return true;
|
||||
|
||||
if(parents == null || node.parents == null) return false;
|
||||
|
||||
if (node.parents.Count != this.parents.Count) return false;
|
||||
|
||||
for (int i = 0; i < parents.Count; i++)
|
||||
{
|
||||
if (!parents[i].Equals(node.parents[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CompareParent(List<string> parents) {
|
||||
|
||||
if (BaseLayer()) return parents == null || parents.Count == 0;
|
||||
|
||||
if (parents == null || this.parents == null) return false;
|
||||
|
||||
if (parents.Count != this.parents.Count) return false;
|
||||
|
||||
for (int i = 0; i < this.parents.Count; i++)
|
||||
{
|
||||
if (!this.parents[i].Equals(parents[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool BaseLayer()
|
||||
{
|
||||
return this.parents == null || this.parents.Count == 0;
|
||||
}
|
||||
|
||||
|
||||
public bool ContainsParent(string parent)
|
||||
{
|
||||
if(BaseLayer()) return false;
|
||||
return parents.Contains(parent);
|
||||
}
|
||||
|
||||
public bool BelongToParent(string parent)
|
||||
{
|
||||
if(BaseLayer()) return false;
|
||||
return parents[parents.Count - 1].Equals(parent);
|
||||
}
|
||||
|
||||
public void RenameParent(string oldParent, string newName) {
|
||||
int index = parents.IndexOf(oldParent);
|
||||
parents[index] = newName;
|
||||
}
|
||||
|
||||
public List<string> GetParent() {
|
||||
List<string> parents = new List<string>();
|
||||
if (parents == null) return parents;
|
||||
parents.AddRange(this.parents);
|
||||
return parents;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9a895c087d06e44eb52451b523e7fea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
|
||||
[System.Serializable]
|
||||
public class GroupCondition
|
||||
{
|
||||
public List<FSMConditionData> conditions = new List<FSMConditionData>();
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class FSMTransitionData
|
||||
{
|
||||
|
||||
public string fromStateName;
|
||||
public string toStateName;
|
||||
|
||||
public string Key
|
||||
{
|
||||
get {
|
||||
return string.Format("{0}:{1}",fromStateName,toStateName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<FSMConditionData> conditions = new List<FSMConditionData>();
|
||||
|
||||
public List<GroupCondition> group_conditions = new List<GroupCondition>();
|
||||
|
||||
public bool AutoSwtich = false;
|
||||
|
||||
public bool Empty
|
||||
{
|
||||
get {
|
||||
|
||||
if(conditions.Count != 0)
|
||||
return false;
|
||||
|
||||
foreach (GroupCondition condition in group_conditions) {
|
||||
if(condition.conditions.Count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a61269aeb473d154e9b6c9e33e8ec616
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,321 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
public class RuntimeFSMController : ScriptableObject
|
||||
{
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>
|
||||
/// 背景线的中心点坐标
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public Vector3 viewPosition = new Vector3(100,100,0);
|
||||
[HideInInspector]
|
||||
public Vector3 viewScale = Vector3.one * 0.8f;
|
||||
|
||||
/// <summary>
|
||||
/// 当前用户选择的状态机层级
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public List<string> Layers = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 实例化该对象的源文件的GUID
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public string originGUID;
|
||||
|
||||
public void AddLayer(string layer)
|
||||
{
|
||||
if(Layers.Contains(layer))
|
||||
return;
|
||||
Layers.Add(layer);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void RemoveLayer(int index, int count)
|
||||
{
|
||||
Layers.RemoveRange(index, count);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空当前用户选择的状态层级
|
||||
/// </summary>
|
||||
public void ClearLayer()
|
||||
{
|
||||
Layers.Clear();
|
||||
Save();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 所有的状态
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public List<FSMStateNodeData> states = new List<FSMStateNodeData>();
|
||||
|
||||
/// <summary>
|
||||
/// 所有的参数
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public List<FSMParameterData> parameters = new List<FSMParameterData>();
|
||||
|
||||
/// <summary>
|
||||
/// 保存所有的过渡
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public List<FSMTransitionData> transitions = new List<FSMTransitionData>();
|
||||
|
||||
|
||||
private Dictionary<string,FSMStateNodeData> states_dic = new Dictionary<string, FSMStateNodeData> ();
|
||||
private Dictionary<string,FSMParameterData> parameters_dic = new Dictionary<string, FSMParameterData>();
|
||||
private Dictionary<string, FSMStateNodeData> default_states_dic = new Dictionary<string, FSMStateNodeData>();
|
||||
private Dictionary<string, FSMTransitionData> transitions_dic = new Dictionary<string, FSMTransitionData>();
|
||||
|
||||
|
||||
public FSMStateNodeData GetStateNodeData(string name) {
|
||||
|
||||
if (name == null)
|
||||
return null;
|
||||
|
||||
if (states_dic.Count == 0) {
|
||||
foreach (FSMStateNodeData state in states)
|
||||
{
|
||||
if (states_dic.ContainsKey(state.name))
|
||||
throw new System.Exception(string.Format("状态名称重复:{0}", state.name));
|
||||
states_dic.Add (state.name, state);
|
||||
}
|
||||
}
|
||||
|
||||
if(states_dic.ContainsKey(name))
|
||||
return states_dic[name];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public FSMParameterData GetParameterData(string name)
|
||||
{
|
||||
if(name == null)
|
||||
return null;
|
||||
|
||||
if (parameters_dic.Count == 0) {
|
||||
foreach (var item in parameters)
|
||||
{
|
||||
if (parameters_dic.ContainsKey(item.name))
|
||||
throw new System.Exception(string.Format("参数名称重复:{0}", item.name));
|
||||
parameters_dic.Add (item.name, item);
|
||||
}
|
||||
}
|
||||
|
||||
if(parameters_dic.ContainsKey(name))
|
||||
return parameters_dic[name];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取某一个层级的默认状态
|
||||
/// </summary>
|
||||
/// <param name="parent"></param>
|
||||
/// <returns></returns>
|
||||
public FSMStateNodeData GetDefaultStateNodeData(string parent)
|
||||
{
|
||||
if (parent == null)
|
||||
return null;
|
||||
|
||||
if (default_states_dic.Count == 0)
|
||||
{
|
||||
foreach (var item in states)
|
||||
{
|
||||
if (!item.defaultState) continue;
|
||||
if (default_states_dic.ContainsKey(item.Parent))
|
||||
throw new System.Exception(string.Format("检测到层级:{0}有多个默认状态!",item.Parent));
|
||||
default_states_dic.Add(item.Parent, item);
|
||||
}
|
||||
}
|
||||
|
||||
if(default_states_dic.ContainsKey(parent))
|
||||
return default_states_dic[parent];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public FSMTransitionData GetTransitionData(string from, string to) {
|
||||
|
||||
if (transitions_dic.Count == 0) {
|
||||
foreach (var item in transitions)
|
||||
{
|
||||
transitions_dic.Add(item.Key, item);
|
||||
}
|
||||
}
|
||||
|
||||
string key = string.Format("{0}:{1}",from,to);
|
||||
|
||||
if(transitions_dic.ContainsKey(key))
|
||||
return transitions_dic[key];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
private Dictionary<string, List<FSMStateNodeData>> current_show_states = new Dictionary<string, List<FSMStateNodeData>>();
|
||||
private Dictionary<string, List<FSMTransitionData>> current_show_transitions = new Dictionary<string, List<FSMTransitionData>>();
|
||||
|
||||
|
||||
|
||||
public void AddState(FSMStateNodeData state) {
|
||||
ClearCache();
|
||||
states.Add(state);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void RemoveState(FSMStateNodeData state)
|
||||
{
|
||||
ClearCache();
|
||||
states.Remove(state);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void AddParameters(FSMParameterData data) {
|
||||
ClearCache();
|
||||
parameters.Add(data);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void RemoveParameters(FSMParameterData data) {
|
||||
ClearCache();
|
||||
parameters.Remove(data);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void AddTransition(FSMTransitionData data) {
|
||||
ClearCache();
|
||||
transitions.Add(data);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void RemoveTransition(FSMTransitionData data) {
|
||||
ClearCache();
|
||||
transitions.Remove(data);
|
||||
Save();
|
||||
}
|
||||
|
||||
|
||||
public void ClearCache()
|
||||
{
|
||||
states_dic.Clear();
|
||||
parameters_dic.Clear();
|
||||
default_states_dic.Clear();
|
||||
current_show_states.Clear();
|
||||
current_show_transitions.Clear();
|
||||
transitions_dic.Clear();
|
||||
}
|
||||
|
||||
|
||||
public List<FSMStateNodeData> GetCurrentShowStateNodeData(string parent)
|
||||
{
|
||||
|
||||
if (parent == null)
|
||||
return null;
|
||||
|
||||
if (current_show_states.Count == 0)
|
||||
{
|
||||
foreach (var item in states)
|
||||
{
|
||||
if (current_show_states.ContainsKey(item.Parent))
|
||||
{
|
||||
current_show_states[item.Parent].Add(item);
|
||||
}
|
||||
else {
|
||||
current_show_states.Add(item.Parent,new List<FSMStateNodeData> { item });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (current_show_states.ContainsKey(parent))
|
||||
{
|
||||
List<FSMStateNodeData> current_show_state = current_show_states[parent];
|
||||
if (current_show_state.Count == 0)
|
||||
ClearLayer();
|
||||
return current_show_state;
|
||||
}
|
||||
|
||||
ClearLayer();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<FSMTransitionData> GetCurrentShowTransitionData(string parent)
|
||||
{
|
||||
|
||||
if (parent == null)
|
||||
return null;
|
||||
|
||||
if (current_show_transitions.Count == 0) {
|
||||
foreach (var item in transitions)
|
||||
{
|
||||
FSMStateNodeData from = GetStateNodeData(item.fromStateName);
|
||||
FSMStateNodeData to = GetStateNodeData(item.toStateName);
|
||||
|
||||
if (!from.Parent.Equals(to.Parent))
|
||||
throw new System.Exception(string.Format("过渡的起始状态{0}和结束状态{1}不在同一个层级!",from.name,to.name));
|
||||
|
||||
if (current_show_transitions.ContainsKey(from.Parent))
|
||||
current_show_transitions[from.Parent].Add(item);
|
||||
else
|
||||
current_show_transitions.Add(from.Parent, new List<FSMTransitionData>() { item});
|
||||
}
|
||||
}
|
||||
|
||||
if(current_show_transitions.ContainsKey(parent))
|
||||
return current_show_transitions[parent];
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
public void Save() {
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
//UnityEditor.AssetDatabase.SaveAssets();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void RefreshStateScripts()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
foreach (var state in states)
|
||||
{
|
||||
state.RefreshStateScripts(this);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[InitializeOnLoadMethod]
|
||||
static void RefreshAllStateScripts()
|
||||
{
|
||||
string[] controllers = AssetDatabase.FindAssets("t:RuntimeFSMController");
|
||||
foreach (var item in controllers)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(item);
|
||||
RuntimeFSMController controller = AssetDatabase.LoadAssetAtPath<RuntimeFSMController>(path);
|
||||
controller.RefreshStateScripts();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6fbd4d875fc9c244aa5a60af9e992ab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,444 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// RuntimeFSMController 运行时的示例
|
||||
/// </summary>
|
||||
public class RuntimeFSMControllerInstance
|
||||
{
|
||||
|
||||
public string name;
|
||||
|
||||
|
||||
public RuntimeFSMController RuntimeFSMController;
|
||||
|
||||
// 所有的参数
|
||||
internal Dictionary<string, FSMParameterData> parameters = new Dictionary<string, FSMParameterData>();
|
||||
|
||||
[Tooltip("参数的默认值")]
|
||||
internal Dictionary<string, float> parameter_default = new Dictionary<string, float>();
|
||||
|
||||
// 所有的状态
|
||||
internal Dictionary<string, FSMStateNode> states = new Dictionary<string, FSMStateNode>();
|
||||
|
||||
internal List<FSMTransition> transitions = new List<FSMTransition>();
|
||||
|
||||
// 默认状态
|
||||
internal FSMStateNode defaultState { get; private set; } = null;
|
||||
|
||||
private Dictionary<string, FSMStateNode> current_states = new Dictionary<string, FSMStateNode>();
|
||||
|
||||
private List<FSMStateNode> current_states_array = new List<FSMStateNode>();
|
||||
|
||||
// 当前的过渡
|
||||
public FSMTransition currentTransition { get; private set; } = null;
|
||||
|
||||
public FSMController FSMController { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态切换计数(为了避免状态切换进入死循环)
|
||||
/// </summary>
|
||||
private Dictionary<string, int> state_count = new Dictionary<string, int>();
|
||||
|
||||
/// <summary>
|
||||
/// 当前的帧数
|
||||
/// </summary>
|
||||
private int currentSeconds = -1;
|
||||
|
||||
/// <summary>
|
||||
/// 记录Trigger触发的次数
|
||||
/// </summary>
|
||||
private Dictionary<string, int> trigger_count = new Dictionary<string, int>();
|
||||
|
||||
private List<string> trigerKeys = new List<string>();
|
||||
|
||||
public bool Started { get; private set; } = false;
|
||||
|
||||
|
||||
public RuntimeFSMControllerInstance(RuntimeFSMController runtimeFSMController, FSMController controller)
|
||||
{
|
||||
FSMController = controller;
|
||||
// 把数据复制一份
|
||||
if (runtimeFSMController == null) return;
|
||||
name = runtimeFSMController.name;
|
||||
// 默认不执行任何状态
|
||||
current_states.Clear();
|
||||
|
||||
RuntimeFSMController = GameObject.Instantiate(runtimeFSMController);
|
||||
#if UNITY_EDITOR
|
||||
RuntimeFSMController.originGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(runtimeFSMController));
|
||||
#endif
|
||||
RuntimeFSMController.name = runtimeFSMController.name;
|
||||
// 参数
|
||||
parameters.Clear();
|
||||
for (int i = 0; i < RuntimeFSMController.parameters.Count; i++)
|
||||
{
|
||||
FSMParameterData data = RuntimeFSMController.parameters[i];
|
||||
if (parameters.ContainsKey(data.name))
|
||||
continue;
|
||||
|
||||
data.onValueChange = null;
|
||||
parameters.Add(data.name, data);
|
||||
parameter_default.Add(data.name, data.Value);
|
||||
}
|
||||
|
||||
// 状态
|
||||
states.Clear();
|
||||
for (int i = 0; i < RuntimeFSMController.states.Count; i++)
|
||||
{
|
||||
FSMStateNodeData stateData = RuntimeFSMController.states[i];
|
||||
FSMStateNode state = new FSMStateNode(stateData, this);
|
||||
|
||||
if (states.ContainsKey(stateData.name)) continue;
|
||||
|
||||
if (stateData.defaultState && stateData.BaseLayer())
|
||||
{
|
||||
defaultState = state;
|
||||
}
|
||||
|
||||
states.Add(stateData.name, state);
|
||||
}
|
||||
// 过渡
|
||||
transitions.Clear();
|
||||
foreach (var item in runtimeFSMController.transitions)
|
||||
{
|
||||
FSMTransition transition = new FSMTransition(this, item);
|
||||
transitions.Add(transition);
|
||||
}
|
||||
|
||||
Started = false;
|
||||
}
|
||||
|
||||
|
||||
// 启动
|
||||
public void StartUp()
|
||||
{
|
||||
Started = true;
|
||||
// 找到默认状态 切换
|
||||
SwitchState(defaultState, null);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
// 切换到空状态 (当切换到空状态之后 就没有办法通过参数来切换状态)
|
||||
// 因为在判断条件是否满足时 会查询当前状态 如果当前状态为空 会直接return false;
|
||||
SwitchState(null, null);
|
||||
// 设置为非启动状态
|
||||
Started = false;
|
||||
// 重置参数
|
||||
ResetParamter();
|
||||
}
|
||||
internal void SwitchState(FSMStateNode state, FSMTransition transition)
|
||||
{
|
||||
//if (!Started) return;
|
||||
|
||||
int second = Mathf.FloorToInt(Time.time);
|
||||
|
||||
if (second != currentSeconds)
|
||||
{
|
||||
currentSeconds = second;
|
||||
state_count.Clear(); // 清空状态
|
||||
}
|
||||
// 添加状态
|
||||
if (state != null)
|
||||
AddStateCount(state.data.name);
|
||||
|
||||
string parent = state != null ? state.data.Parent : string.Empty;
|
||||
|
||||
// 退出当前状态
|
||||
ExitState(parent, state);
|
||||
EnterState(state);
|
||||
|
||||
currentTransition = transition;
|
||||
|
||||
// 检测当前状态是否有已经满足的条件
|
||||
CheckConditionAndSwitch();
|
||||
}
|
||||
|
||||
|
||||
private void CheckConditionAndSwitch()
|
||||
{
|
||||
// 检测当前的状态是否有已经满足条件的过渡
|
||||
foreach (var item in transitions)
|
||||
{
|
||||
// 检测条件是否满足
|
||||
if (item.CheckConditionAndSwitch())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ExitState(string parent, FSMStateNode nextState)
|
||||
{
|
||||
if (!current_states.ContainsKey(parent)) return;
|
||||
if (current_states[parent] == null) return;
|
||||
|
||||
FSMStateNode currentState = current_states[parent];
|
||||
|
||||
if (nextState != null)
|
||||
currentState.nextState = nextState.data.name;
|
||||
|
||||
if (nextState != null && nextState.data.CompareParent(currentState.data))
|
||||
nextState.lastState = currentState.data.name;
|
||||
|
||||
if (nextState != null)
|
||||
nextState.nextState = string.Empty; // 把下一个状态的值设置为空
|
||||
currentState.OnExit();
|
||||
|
||||
// 移除状态
|
||||
current_states.Remove(parent);
|
||||
|
||||
// 判断一下当前状态是否子状态机
|
||||
if (currentState.data.isSubStateMachine)
|
||||
{
|
||||
// 把子状态也退出掉
|
||||
ExitState(currentState.data.name, nextState);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnterState(FSMStateNode state)
|
||||
{
|
||||
if (state == null) return;
|
||||
|
||||
string parent = state.data.Parent;
|
||||
|
||||
if (current_states.ContainsKey(parent))
|
||||
current_states[parent] = state;
|
||||
else
|
||||
current_states.Add(parent, state);
|
||||
|
||||
state?.OnEnter();
|
||||
// 触发事件
|
||||
FSMController.onStateChange?.Invoke(RuntimeFSMController.name, state.data.name);
|
||||
|
||||
// 判断是否有子状态机
|
||||
if (state.data.isSubStateMachine)
|
||||
{
|
||||
// 进入到子状态机的默认状态
|
||||
FSMStateNodeData data = RuntimeFSMController.GetDefaultStateNodeData(state.data.name);
|
||||
if (data != null && states.ContainsKey(data.name))
|
||||
EnterState(states[data.name]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SetBool(string name, bool v)
|
||||
{
|
||||
SetParamter(name, v ? 1 : 0, ParameterType.Bool);
|
||||
}
|
||||
|
||||
public void SetFloat(string name, float v)
|
||||
{
|
||||
SetParamter(name, v, ParameterType.Float);
|
||||
}
|
||||
|
||||
public void SetInt(string name, int v)
|
||||
{
|
||||
SetParamter(name, v, ParameterType.Int);
|
||||
}
|
||||
|
||||
public void SetTrigger(string name)
|
||||
{
|
||||
FSMParameterData parameter;
|
||||
if (parameters.TryGetValue(name, out parameter))
|
||||
{
|
||||
if (parameter.parameterType != ParameterType.Trigger)
|
||||
return;
|
||||
|
||||
if (parameter.value == 1)
|
||||
{
|
||||
if (trigger_count.ContainsKey(name))
|
||||
trigger_count[name]++;
|
||||
else
|
||||
trigger_count.Add(name, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetParamter(name, 1, ParameterType.Trigger);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当Trigger触发后还原trigger
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
internal void ClearTrigger(string name)
|
||||
{
|
||||
SetParamter(name, 0, ParameterType.Trigger);
|
||||
}
|
||||
|
||||
public void ResetTrigger(string name)
|
||||
{
|
||||
if (trigger_count.ContainsKey(name))
|
||||
trigger_count[name] = 0;
|
||||
ClearTrigger(name);
|
||||
}
|
||||
|
||||
private void SetParamter(string name, float v, ParameterType type)
|
||||
{
|
||||
FSMParameterData parameter;
|
||||
if (parameters.TryGetValue(name, out parameter))
|
||||
{
|
||||
if (parameter.parameterType == type)
|
||||
parameter.Value = v;
|
||||
}
|
||||
}
|
||||
|
||||
internal float GetParamter(string name)
|
||||
{
|
||||
FSMParameterData parameter;
|
||||
if (parameters.TryGetValue(name, out parameter))
|
||||
return parameter.value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
internal int GetTriggerCount(string name)
|
||||
{
|
||||
|
||||
if (trigger_count.ContainsKey(name))
|
||||
return trigger_count[name];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public void Update()
|
||||
{
|
||||
|
||||
current_states_array.Clear();
|
||||
|
||||
foreach (var item in current_states.Keys)
|
||||
{
|
||||
if (current_states[item] == null) continue;
|
||||
current_states_array.Add(current_states[item]);
|
||||
}
|
||||
|
||||
foreach (var item in current_states_array)
|
||||
{
|
||||
if (!item.isRuning) continue;
|
||||
item.OnUpdate();
|
||||
}
|
||||
|
||||
// 更新Trigger
|
||||
UpdateTrigger();
|
||||
}
|
||||
|
||||
public void LateUpdate()
|
||||
{
|
||||
current_states_array.Clear();
|
||||
|
||||
foreach (var item in current_states.Keys)
|
||||
{
|
||||
if (current_states[item] == null) continue;
|
||||
current_states_array.Add(current_states[item]);
|
||||
}
|
||||
|
||||
foreach (var item in current_states_array)
|
||||
{
|
||||
if (!item.isRuning) continue;
|
||||
item.OnLateUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
current_states_array.Clear();
|
||||
|
||||
foreach (var item in current_states.Keys)
|
||||
{
|
||||
if (current_states[item] == null) continue;
|
||||
current_states_array.Add(current_states[item]);
|
||||
}
|
||||
|
||||
foreach (var item in current_states_array)
|
||||
{
|
||||
if (!item.isRuning) continue;
|
||||
item.OnFixedUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加计数
|
||||
/// </summary>
|
||||
/// <param name="stateName"></param>
|
||||
private void AddStateCount(string stateName)
|
||||
{
|
||||
if (state_count.ContainsKey(stateName))
|
||||
state_count[stateName]++;
|
||||
else
|
||||
state_count.Add(stateName, 0);
|
||||
|
||||
if (state_count[stateName] > 30)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append("游戏物体:").Append(FSMController.gameObject.name)
|
||||
.Append("状态机:").Append(this.RuntimeFSMController.name).Append("检测到状态:");
|
||||
foreach (var item in state_count.Keys)
|
||||
{
|
||||
if (state_count[item] >= 29)
|
||||
{
|
||||
stringBuilder.Append(item).Append(",");
|
||||
}
|
||||
}
|
||||
stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
||||
stringBuilder.Append("之间频繁切换,请检查状态之间的过渡是否同时满足?请设置合理的状态切换条件!");
|
||||
state_count.Clear();
|
||||
throw new System.Exception(stringBuilder.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void UpdateTrigger()
|
||||
{
|
||||
// 更新Trigger
|
||||
foreach (var item in trigger_count.Keys)
|
||||
{
|
||||
if (!trigerKeys.Contains(item))
|
||||
trigerKeys.Add(item);
|
||||
}
|
||||
|
||||
foreach (var item in trigerKeys)
|
||||
{
|
||||
if (!trigger_count.ContainsKey(item))
|
||||
continue;
|
||||
if (trigger_count[item] > 0 && GetParamter(item) == 0)
|
||||
{
|
||||
SetParamter(item, 1, ParameterType.Trigger);
|
||||
trigger_count[item]--;
|
||||
if (trigger_count[item] < 0)
|
||||
trigger_count[item] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FSMStateNode GetCurrentState(string parent)
|
||||
{
|
||||
|
||||
if (current_states.ContainsKey(parent))
|
||||
return current_states[parent];
|
||||
return null;
|
||||
}
|
||||
internal void ResetParamter()
|
||||
{
|
||||
foreach (var item in parameter_default.Keys)
|
||||
{
|
||||
FSMParameterData parameter;
|
||||
if (parameters.TryGetValue(item, out parameter))
|
||||
{
|
||||
parameter.Value = parameter_default[item];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2de5c4a2106a1974cb5a81ddd326e731
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user