StateGraph.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using XNode;
  4. using Sirenix.OdinInspector;
  5. using System.Text;
  6. namespace ChivaVR.State
  7. {
  8. [CreateAssetMenu(fileName = "StateGraph", menuName = "ChivaVR/Process/StateGraph")]
  9. public class StateGraph : NodeGraph
  10. {
  11. public EnterStateNode enter;
  12. public ExitStateNode exit;
  13. public delegate void StateEvent(StateGraph stateGraph);
  14. public StateEvent OnStateEnter;
  15. public StateEvent OnStateExit;
  16. /// <summary>
  17. /// 记分池
  18. /// </summary>
  19. private List<RunStateNode> noFinishedRunStateNodes;
  20. /// <summary>
  21. /// 提示池
  22. /// </summary>
  23. private List<RunStateNode> promptingNodesPool;
  24. [Button("更新RunStateNode")]
  25. public void UpdataRunStateNodeProcedureDescription()
  26. {
  27. foreach (var item in nodes)
  28. {
  29. RunStateNode tmpRunStateNode = item as RunStateNode;
  30. if(tmpRunStateNode)
  31. {
  32. tmpRunStateNode.UpdataCurrentProcedureDescription();
  33. }
  34. }
  35. }
  36. public void EnterStateGraph()
  37. {
  38. noFinishedRunStateNodes = new List<RunStateNode>();
  39. promptingNodesPool = new List<RunStateNode>();
  40. enter.OnEnter();
  41. OnStateEnter?.Invoke(this);
  42. }
  43. public void ExitStateGraph()
  44. {
  45. Debug.Log("ExitStateGraph" + OnStateExit.Target);
  46. if (OnStateExit != null)
  47. {
  48. Debug.Log(OnStateExit.Target);
  49. }
  50. OnStateExit?.Invoke(this);
  51. }
  52. public void InitState()
  53. {
  54. for (int i = 0; i < nodes.Count; i++)
  55. {
  56. StateNode node = nodes[i] as StateNode;
  57. if (node != null)
  58. {
  59. node.InitState();
  60. }
  61. }
  62. StateNode tmpNode = nodes[0] as StateNode;
  63. tmpNode.OnEnter();
  64. }
  65. /// <summary>
  66. /// 获取未完成状态描述
  67. /// </summary>
  68. /// <returns></returns>
  69. public string GetUnFinishedDescription()
  70. {
  71. StringBuilder stringBuilder = new StringBuilder();
  72. bool allFinished = true;
  73. //得分
  74. int score = 0;
  75. //总分
  76. int totalScore = 0;
  77. for (int i = 0; i < nodes.Count; i++)
  78. {
  79. RunStateNode stateNode = nodes[i] as RunStateNode;
  80. if (stateNode != null)
  81. {
  82. if (!stateNode.IsFinished)
  83. {
  84. stringBuilder.AppendLine("未完成:" + stateNode.CurrentProcedure.processBase.GetPBDescribe() + " -" + stateNode.score);
  85. allFinished = false;
  86. }
  87. else
  88. {
  89. score += stateNode.score;
  90. }
  91. totalScore += stateNode.score;
  92. }
  93. }
  94. if (allFinished)
  95. {
  96. stringBuilder.AppendLine("完成所有操作");
  97. }
  98. stringBuilder.AppendLine("总分:" + totalScore+" 得分:"+score);
  99. return stringBuilder.ToString();
  100. }
  101. /// <summary>
  102. /// 获取未完成步骤Step列表
  103. /// </summary>
  104. /// <returns></returns>
  105. public List<int> GetUnFinishedStepID()
  106. {
  107. List<int> unFinishedStepList = new List<int>();
  108. for (int i = 0; i < nodes.Count; i++)
  109. {
  110. RunStateNode stateNode = nodes[i] as RunStateNode;
  111. if (stateNode != null && !stateNode.IsFinished)
  112. {
  113. unFinishedStepList.Add(stateNode.currentProcedureStepID);
  114. }
  115. }
  116. return unFinishedStepList;
  117. }
  118. public static void CreateStateGraph()
  119. {
  120. #if UNITY_EDITOR
  121. //ScriptableObjectCreator.ShowDialog<StateGraph>("Assets/ChivaVR/Framework/1.LineProcess/Graph", obj =>
  122. //{
  123. //});
  124. #endif
  125. }
  126. /// <summary>
  127. /// 进入子节点
  128. /// </summary>
  129. public void OnNodeEnterState(RunStateNode node)
  130. {
  131. if (!promptingNodesPool.Contains(node)) promptingNodesPool.Add(node);
  132. List<RunStateNode> tmpNodes = node.GetEnterPortNoFinishedNode();
  133. tmpNodes.ForEach(tmpnode =>
  134. {
  135. if (!noFinishedRunStateNodes.Contains(tmpnode))
  136. {
  137. tmpnode.IsTiShi = true;
  138. noFinishedRunStateNodes.Add(tmpnode);
  139. if (promptingNodesPool.Contains(tmpnode)) promptingNodesPool.Remove(tmpnode);
  140. }
  141. });
  142. }
  143. /// <summary>
  144. /// 退出子节点
  145. /// </summary>
  146. public void OnNodeExitState(RunStateNode node)
  147. {
  148. noFinishedRunStateNodes.Remove(node);
  149. promptingNodesPool.Remove(node);
  150. }
  151. RunStateNode stateNode = null;
  152. /// <summary>
  153. /// 打开提示
  154. /// </summary>
  155. public void OpenHighliter()
  156. {
  157. RunStateNode tmpStateNode = null;
  158. promptingNodesPool.ForEach(node =>
  159. {
  160. if (tmpStateNode == null) tmpStateNode = node;
  161. if (tmpStateNode.currentProcedureStepID > node.currentProcedureStepID)
  162. {
  163. tmpStateNode = node;
  164. }
  165. });
  166. if (tmpStateNode != null && tmpStateNode != stateNode)
  167. {
  168. stateNode = tmpStateNode;
  169. }
  170. }
  171. private int GetTotalScores()
  172. {
  173. int totleScorce = 0;
  174. for (int i = 0; i < nodes.Count; i++)
  175. {
  176. RunStateNode node = nodes[i] as RunStateNode;
  177. totleScorce += node.score;
  178. }
  179. return totleScorce;
  180. }
  181. }
  182. [System.Serializable]
  183. public class StateGraphBase
  184. {
  185. public string description;
  186. [LabelText("流程图初始化步骤")]
  187. public int startInitStep;
  188. public StateGraph stateGraph;
  189. }
  190. }