123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- namespace ChivaXR
- {
- using ChivaVR.State;
- using Sirenix.OdinInspector;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using UnityEngine;
- public class ProcessManagement : SerializedMonoSingleton<ProcessManagement>
- {
- /// <summary>
- /// 初始步骤
- /// </summary>
- public int awakeStepID = 1;
- /// <summary>
- /// 初始化运行
- /// </summary>
- public bool awakePlay = true;
- [LabelText("自动进入下一步骤")]
- public bool autoEnterNextProcess = true;
- /// <summary>
- /// 当前步骤
- /// </summary>
- public int currentStepID = 1;
- /// <summary>
- /// 当前步骤在List中的索引
- /// </summary>
- public int CurrentListID
- {
- get
- {
- if ((currentStepID - 1) >= 0 && ((currentStepID - 1) < processes.Count))
- {
- return currentStepID - 1;
- }
- Debug.LogError("当前步骤超出List范围");
- return 0;
- }
- }
- /// <summary>
- /// 当前List步骤数量
- /// </summary>
- public int CurrentListsCount
- {
- get
- {
- return processes.Count;
- }
- }
- /// <summary>
- /// 流程列表
- /// </summary>
- public List<ProcessElement> processes = new List<ProcessElement>();
- public GameObject processListParent;
- public bool setAwakeStep = false;
- public int addProcedureStep; //添加流程步骤
- public bool setAddNumber = false; //是否开启自定义添加流程
- public int delateProcedureStep; //删除流程步骤
- public bool setDelateNumber = false; //是否开启删除流程
- public float delayTime = 0; //每步流程延迟加载时间 学习模式+0.5秒过度时长
- /// <summary>
- /// 使用提示
- /// </summary>
- //public bool useTips = true;
- /// <summary>
- /// 使用流程图
- /// </summary>
- public bool useGraph;
- public StateGraph stateGraph;
- public delegate void ProcessEventHandler(int stepID);
- public ProcessEventHandler EnterProcessEvent;
- public delegate void processElementActiveHandler(ProcessElement processElement);
- /// <summary>
- /// 步骤激活
- /// </summary>
- public processElementActiveHandler processElementActiveEvent;
- /// <summary>
- /// 步骤结束
- /// </summary>
- public processElementActiveHandler processElementDisActiveEvent;
- /// <summary>
- /// 步骤预处理结束
- /// </summary>
- public processElementActiveHandler preprocessFnishedEvent;
- /// <summary>
- /// 流程结束
- /// </summary>
- public System.Action processFinishEvent;
- public void Start()
- {
- if (awakePlay)
- {
- if (useGraph) InitStateGraph();
- else InitProcess();
- }
- }
- protected override void OnDestory()
- {
- base.OnDestory();
- AnimationCoroutine.Instance.StopAllCoroutines();
- }
- public void InitStateGraph()
- {
- if (stateGraph == null)
- {
- Debug.LogError("无流程图,请检查!!!");
- return;
- }
- stateGraph.InitState();
- }
- /// <summary>
- /// 初始化流程
- /// </summary>
- /// <param name="editorMode">是否为编辑器运行状态</param>
- public void InitProcess()
- {
- currentStepID = awakeStepID;
- if (processes[CurrentListID] == null) return;
- ActiveCurrentProcess();
- }
- /// <summary>
- /// 激活当前流程
- /// </summary>
- public void ActiveCurrentProcess()
- {
- if ((CurrentListID) < processes.Count)//执行的下一流程
- {
- processes[CurrentListID].Enter();
- processElementActiveEvent?.Invoke(processes[CurrentListID]);
- //EnterProcessEvent?.Invoke(CurrentListID);
- switch (OperateSetting.Instance.m_CurrentOperationMode)
- {
- case OperationMode.Learn:
- case OperationMode.Practice:
- case OperationMode.Exam:
- case OperationMode.Challenge:
- EnterProcessEvent?.Invoke(CurrentListID);
- break;
- default:
- break;
- }
- Debug.Log("加载执行:" + processes[CurrentListID].name);
- }
- else
- {
- Debug.LogError("当前流程超出List");
- }
- }
- public void EnterNextProcess()
- {
- if (!autoEnterNextProcess) { return; }
- if (useGraph) return;
- processElementDisActiveEvent?.Invoke(processes[CurrentListID]);
- if (currentStepID + 1 > processes.Count)
- {
- Debug.Log("完成所有流程");
- processFinishEvent?.Invoke();
- return;
- }
- if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Challenge) return;
- if (delayTime != 0)
- {
- StopAllCoroutines();
- StartCoroutine(DelayEnterNextProcess(delayTime));
- }
- else
- {
- currentStepID++;
- ActiveCurrentProcess();
- }
- }
- IEnumerator DelayEnterNextProcess(float time)
- {
- yield return new WaitForSeconds(time);
- currentStepID++;
- ActiveCurrentProcess();
- }
- public void PreProcessState()
- {
- JumpProcessState(currentStepID - 1);
- }
- public void NextProcessState()
- {
- JumpProcessState(currentStepID + 1);
- }
- /// <summary>
- /// 跳步
- /// </summary>
- /// <param name="targetStepID">目标步骤ID</param>
- public void JumpProcessState(int targetStepID)
- {
- //步骤错误
- if (targetStepID < 1 || targetStepID > CurrentListsCount)
- {
- //防止第一步动画跳入两次
- GetProcessElement(CurrentListID).QuitHalfWay();
- GetProcessElement(CurrentListID).SetEnterState();
- Debug.LogError("流程跳转:无目标步骤");
- return;
- }
- if (targetStepID == currentStepID)
- {
- //重置当前步骤状态
- GetProcessElement(CurrentListID).QuitHalfWay();
- GetProcessElement(CurrentListID).SetEnterState();
- }
- //向前跳步
- else if (targetStepID < currentStepID)
- {
- for (int i = currentStepID - 1; i >= targetStepID - 1; i--)
- {
- if (i.Equals(currentStepID - 1))
- {
- GetProcessElement(i).QuitHalfWay();
- if (currentStepID != targetStepID)
- {
- GetProcessElement(i).SetEnterState(false);
- }
- else
- {
- GetProcessElement(i).SetEnterState(true);
- }
- }
- else if (i.Equals(targetStepID - 1))
- {
- GetProcessElement(i).SetEnterState(true);
- }
- else
- {
- GetProcessElement(i).SetEnterState(false);
- }
- }
- }
- //向后跳步
- else
- {
- for (int i = currentStepID - 1; i <= targetStepID - 1; i++)
- {
- if (i.Equals(currentStepID - 1))
- {
- GetProcessElement(i).QuitHalfWay();
- }
- if (i.Equals(targetStepID - 1))
- {
- GetProcessElement(i).SetEnterState();
- }
- else
- {
- GetProcessElement(i).SetExitState();
- }
- }
- }
- awakeStepID = targetStepID;
- currentStepID = targetStepID;
- }
- /// <summary>
- /// 停止当前步骤
- /// </summary>
- public void StopCurrentProcess()
- {
- GetProcessElement(CurrentListID).QuitHalfWay();
- }
- /// <summary>
- ///初始化步骤状态
- /// </summary>
- public void InitStepState()
- {
- for (int i = processes.Count - 1; i >= 0; i--)
- {
- if (i == 0)
- {
- processes[i].SetEnterState(true);
- }
- else
- {
- processes[i].SetEnterState(false);
- }
- }
- Debug.Log("初始化跳转至第一步");
- awakeStepID = 1;
- currentStepID = 1;
- }
- /// <summary>
- /// 根据步骤获取Procedure
- /// </summary>
- /// <param name="_step"></param>
- /// <returns></returns>
- private ProcessElement GetProcessElement(int listID)
- {
- try
- {
- return processes[listID];
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 获取当前步骤Procedure
- /// </summary>
- /// <param name="_step"></param>
- /// <returns></returns>
- public ProcessElement GetCurrentProcess()
- {
- try
- {
- return processes[CurrentListID];
- }
- catch
- {
- return null;
- }
- }
- #region 更删改查
- /// <summary>
- /// 更新流程列表
- /// </summary>
- public void UpdateProcesses()
- {
- processes.Clear();
- processes = processListParent.GetComponentsInChildren<ProcessElement>().ToList();
- processes = processes.OrderBy(target => target.stepID).ToList();
- for (int i = 0; i < processes.Count; i++)
- {
- processes[i].transform.SetSiblingIndex(processes[i].stepID - 1);
- processes[i].SetName();
- }
- }
- /// <summary>
- /// 添加流程
- /// </summary>
- public void AddProcess()
- {
- ProcessElement process = new GameObject().AddComponent<ProcessElement>();
- process.transform.parent = processListParent.transform;
- process.transform.position = Vector3.zero;
- process.stepID = addProcedureStep;
- process.SetName();
- processes.Insert(process.stepID - 1, process);
- process.transform.SetSiblingIndex(process.stepID - 1);
- for (int i = addProcedureStep; i < processes.Count; i++)
- {
- if (processes[i] == null)
- continue;
- processes[i].stepID = i + 1;
- processes[i].SetName();
- }
- UpdateProcesses();
- }
- /// <summary>
- /// 添加流程
- /// </summary>
- public ProcessElement AddProcess1()
- {
- ProcessElement process = new GameObject().AddComponent<ProcessElement>();
- if (processListParent == null) processListParent = this.gameObject;
- process.transform.parent = processListParent.transform;
- process.transform.position = Vector3.zero;
- process.stepID = processes.Count + 1;
- process.SetName();
- processes.Insert(process.stepID - 1, process);
- process.transform.SetSiblingIndex(process.stepID - 1);
- for (int i = addProcedureStep; i < processes.Count; i++)
- {
- if (processes[i] == null) continue;
- processes[i].stepID = i + 1;
- processes[i].SetName();
- }
- UpdateProcesses();
- return process;
- }
- /// <summary>
- /// 删除流程
- /// </summary>
- public void DelateProcess()
- {
- if (!setDelateNumber) return;
- DestroyImmediate(processes[delateProcedureStep - 1].gameObject);
- processes.RemoveAt(delateProcedureStep - 1);
- if ((delateProcedureStep - 1) < processes.Count)
- {
- for (int i = delateProcedureStep - 1; i < processes.Count; i++)
- {
- if (processes[i] == null)
- continue;
- processes[i].stepID = i + 1;
- processes[i].SetName();
- }
- }
- UpdateProcesses();
- }
- #endregion
- }
- }
|