123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System.Collections.Generic;
- using UnityEngine;
- using XNode;
- using Sirenix.OdinInspector;
- using System.Text;
- namespace ChivaVR.State
- {
- [CreateAssetMenu(fileName = "StateGraph", menuName = "ChivaVR/Process/StateGraph")]
- public class StateGraph : NodeGraph
- {
- public EnterStateNode enter;
- public ExitStateNode exit;
- public delegate void StateEvent(StateGraph stateGraph);
- public StateEvent OnStateEnter;
- public StateEvent OnStateExit;
- /// <summary>
- /// 记分池
- /// </summary>
- private List<RunStateNode> noFinishedRunStateNodes;
- /// <summary>
- /// 提示池
- /// </summary>
- private List<RunStateNode> promptingNodesPool;
- [Button("更新RunStateNode")]
- public void UpdataRunStateNodeProcedureDescription()
- {
- foreach (var item in nodes)
- {
- RunStateNode tmpRunStateNode = item as RunStateNode;
- if(tmpRunStateNode)
- {
- tmpRunStateNode.UpdataCurrentProcedureDescription();
- }
- }
- }
- public void EnterStateGraph()
- {
- noFinishedRunStateNodes = new List<RunStateNode>();
- promptingNodesPool = new List<RunStateNode>();
- enter.OnEnter();
- OnStateEnter?.Invoke(this);
- }
- public void ExitStateGraph()
- {
- Debug.Log("ExitStateGraph" + OnStateExit.Target);
- if (OnStateExit != null)
- {
- Debug.Log(OnStateExit.Target);
- }
- OnStateExit?.Invoke(this);
- }
- public void InitState()
- {
- for (int i = 0; i < nodes.Count; i++)
- {
- StateNode node = nodes[i] as StateNode;
- if (node != null)
- {
- node.InitState();
- }
- }
- StateNode tmpNode = nodes[0] as StateNode;
- tmpNode.OnEnter();
- }
-
- /// <summary>
- /// 获取未完成状态描述
- /// </summary>
- /// <returns></returns>
- public string GetUnFinishedDescription()
- {
- StringBuilder stringBuilder = new StringBuilder();
- bool allFinished = true;
- //得分
- int score = 0;
- //总分
- int totalScore = 0;
- for (int i = 0; i < nodes.Count; i++)
- {
- RunStateNode stateNode = nodes[i] as RunStateNode;
- if (stateNode != null)
- {
- if (!stateNode.IsFinished)
- {
- stringBuilder.AppendLine("未完成:" + stateNode.CurrentProcedure.processBase.GetPBDescribe() + " -" + stateNode.score);
- allFinished = false;
- }
- else
- {
- score += stateNode.score;
- }
- totalScore += stateNode.score;
- }
- }
- if (allFinished)
- {
- stringBuilder.AppendLine("完成所有操作");
- }
- stringBuilder.AppendLine("总分:" + totalScore+" 得分:"+score);
- return stringBuilder.ToString();
- }
- /// <summary>
- /// 获取未完成步骤Step列表
- /// </summary>
- /// <returns></returns>
- public List<int> GetUnFinishedStepID()
- {
- List<int> unFinishedStepList = new List<int>();
- for (int i = 0; i < nodes.Count; i++)
- {
- RunStateNode stateNode = nodes[i] as RunStateNode;
- if (stateNode != null && !stateNode.IsFinished)
- {
- unFinishedStepList.Add(stateNode.currentProcedureStepID);
- }
- }
- return unFinishedStepList;
- }
- public static void CreateStateGraph()
- {
- #if UNITY_EDITOR
- //ScriptableObjectCreator.ShowDialog<StateGraph>("Assets/ChivaVR/Framework/1.LineProcess/Graph", obj =>
- //{
- //});
- #endif
- }
- /// <summary>
- /// 进入子节点
- /// </summary>
- public void OnNodeEnterState(RunStateNode node)
- {
- if (!promptingNodesPool.Contains(node)) promptingNodesPool.Add(node);
-
- List<RunStateNode> tmpNodes = node.GetEnterPortNoFinishedNode();
- tmpNodes.ForEach(tmpnode =>
- {
- if (!noFinishedRunStateNodes.Contains(tmpnode))
- {
- tmpnode.IsTiShi = true;
-
- noFinishedRunStateNodes.Add(tmpnode);
- if (promptingNodesPool.Contains(tmpnode)) promptingNodesPool.Remove(tmpnode);
- }
- });
- }
- /// <summary>
- /// 退出子节点
- /// </summary>
- public void OnNodeExitState(RunStateNode node)
- {
- noFinishedRunStateNodes.Remove(node);
- promptingNodesPool.Remove(node);
- }
- RunStateNode stateNode = null;
- /// <summary>
- /// 打开提示
- /// </summary>
- public void OpenHighliter()
- {
- RunStateNode tmpStateNode = null;
- promptingNodesPool.ForEach(node =>
- {
- if (tmpStateNode == null) tmpStateNode = node;
- if (tmpStateNode.currentProcedureStepID > node.currentProcedureStepID)
- {
- tmpStateNode = node;
- }
- });
- if (tmpStateNode != null && tmpStateNode != stateNode)
- {
- stateNode = tmpStateNode;
- }
-
- }
- private int GetTotalScores()
- {
- int totleScorce = 0;
- for (int i = 0; i < nodes.Count; i++)
- {
- RunStateNode node = nodes[i] as RunStateNode;
- totleScorce += node.score;
- }
- return totleScorce;
- }
- }
- [System.Serializable]
- public class StateGraphBase
- {
- public string description;
- [LabelText("流程图初始化步骤")]
- public int startInitStep;
- public StateGraph stateGraph;
- }
- }
|