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; /// /// 记分池 /// private List noFinishedRunStateNodes; /// /// 提示池 /// private List 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(); promptingNodesPool = new List(); 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(); } /// /// 获取未完成状态描述 /// /// 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(); } /// /// 获取未完成步骤Step列表 /// /// public List GetUnFinishedStepID() { List unFinishedStepList = new List(); 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("Assets/ChivaVR/Framework/1.LineProcess/Graph", obj => //{ //}); #endif } /// /// 进入子节点 /// public void OnNodeEnterState(RunStateNode node) { if (!promptingNodesPool.Contains(node)) promptingNodesPool.Add(node); List tmpNodes = node.GetEnterPortNoFinishedNode(); tmpNodes.ForEach(tmpnode => { if (!noFinishedRunStateNodes.Contains(tmpnode)) { tmpnode.IsTiShi = true; noFinishedRunStateNodes.Add(tmpnode); if (promptingNodesPool.Contains(tmpnode)) promptingNodesPool.Remove(tmpnode); } }); } /// /// 退出子节点 /// public void OnNodeExitState(RunStateNode node) { noFinishedRunStateNodes.Remove(node); promptingNodesPool.Remove(node); } RunStateNode stateNode = null; /// /// 打开提示 /// 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; } }