ProcessManagement.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. namespace ChivaXR
  2. {
  3. using ChivaVR.State;
  4. using Sirenix.OdinInspector;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using UnityEngine;
  10. public class ProcessManagement : SerializedMonoSingleton<ProcessManagement>
  11. {
  12. /// <summary>
  13. /// 初始步骤
  14. /// </summary>
  15. public int awakeStepID = 1;
  16. /// <summary>
  17. /// 初始化运行
  18. /// </summary>
  19. public bool awakePlay = true;
  20. [LabelText("自动进入下一步骤")]
  21. public bool autoEnterNextProcess = true;
  22. /// <summary>
  23. /// 当前步骤
  24. /// </summary>
  25. public int currentStepID = 1;
  26. /// <summary>
  27. /// 当前步骤在List中的索引
  28. /// </summary>
  29. public int CurrentListID
  30. {
  31. get
  32. {
  33. if ((currentStepID - 1) >= 0 && ((currentStepID - 1) < processes.Count))
  34. {
  35. return currentStepID - 1;
  36. }
  37. Debug.LogError("当前步骤超出List范围");
  38. return 0;
  39. }
  40. }
  41. /// <summary>
  42. /// 当前List步骤数量
  43. /// </summary>
  44. public int CurrentListsCount
  45. {
  46. get
  47. {
  48. return processes.Count;
  49. }
  50. }
  51. /// <summary>
  52. /// 流程列表
  53. /// </summary>
  54. public List<ProcessElement> processes = new List<ProcessElement>();
  55. public GameObject processListParent;
  56. public bool setAwakeStep = false;
  57. public int addProcedureStep; //添加流程步骤
  58. public bool setAddNumber = false; //是否开启自定义添加流程
  59. public int delateProcedureStep; //删除流程步骤
  60. public bool setDelateNumber = false; //是否开启删除流程
  61. public float delayTime = 0; //每步流程延迟加载时间 学习模式+0.5秒过度时长
  62. /// <summary>
  63. /// 使用提示
  64. /// </summary>
  65. //public bool useTips = true;
  66. /// <summary>
  67. /// 使用流程图
  68. /// </summary>
  69. public bool useGraph;
  70. public StateGraph stateGraph;
  71. public delegate void ProcessEventHandler(int stepID);
  72. public ProcessEventHandler EnterProcessEvent;
  73. public delegate void processElementActiveHandler(ProcessElement processElement);
  74. /// <summary>
  75. /// 步骤激活
  76. /// </summary>
  77. public processElementActiveHandler processElementActiveEvent;
  78. /// <summary>
  79. /// 步骤结束
  80. /// </summary>
  81. public processElementActiveHandler processElementDisActiveEvent;
  82. /// <summary>
  83. /// 步骤预处理结束
  84. /// </summary>
  85. public processElementActiveHandler preprocessFnishedEvent;
  86. /// <summary>
  87. /// 流程结束
  88. /// </summary>
  89. public System.Action processFinishEvent;
  90. public void Start()
  91. {
  92. if (awakePlay)
  93. {
  94. if (useGraph) InitStateGraph();
  95. else InitProcess();
  96. }
  97. }
  98. protected override void OnDestory()
  99. {
  100. base.OnDestory();
  101. AnimationCoroutine.Instance.StopAllCoroutines();
  102. }
  103. public void InitStateGraph()
  104. {
  105. if (stateGraph == null)
  106. {
  107. Debug.LogError("无流程图,请检查!!!");
  108. return;
  109. }
  110. stateGraph.InitState();
  111. }
  112. /// <summary>
  113. /// 初始化流程
  114. /// </summary>
  115. /// <param name="editorMode">是否为编辑器运行状态</param>
  116. public void InitProcess()
  117. {
  118. currentStepID = awakeStepID;
  119. if (processes[CurrentListID] == null) return;
  120. ActiveCurrentProcess();
  121. }
  122. /// <summary>
  123. /// 激活当前流程
  124. /// </summary>
  125. public void ActiveCurrentProcess()
  126. {
  127. if ((CurrentListID) < processes.Count)//执行的下一流程
  128. {
  129. processes[CurrentListID].Enter();
  130. processElementActiveEvent?.Invoke(processes[CurrentListID]);
  131. //EnterProcessEvent?.Invoke(CurrentListID);
  132. switch (OperateSetting.Instance.m_CurrentOperationMode)
  133. {
  134. case OperationMode.Learn:
  135. case OperationMode.Practice:
  136. case OperationMode.Exam:
  137. case OperationMode.Challenge:
  138. EnterProcessEvent?.Invoke(CurrentListID);
  139. break;
  140. default:
  141. break;
  142. }
  143. Debug.Log("加载执行:" + processes[CurrentListID].name);
  144. }
  145. else
  146. {
  147. Debug.LogError("当前流程超出List");
  148. }
  149. }
  150. public void EnterNextProcess()
  151. {
  152. if (!autoEnterNextProcess) { return; }
  153. if (useGraph) return;
  154. processElementDisActiveEvent?.Invoke(processes[CurrentListID]);
  155. if (currentStepID + 1 > processes.Count)
  156. {
  157. Debug.Log("完成所有流程");
  158. processFinishEvent?.Invoke();
  159. return;
  160. }
  161. if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Challenge) return;
  162. if (delayTime != 0)
  163. {
  164. StopAllCoroutines();
  165. StartCoroutine(DelayEnterNextProcess(delayTime));
  166. }
  167. else
  168. {
  169. currentStepID++;
  170. ActiveCurrentProcess();
  171. }
  172. }
  173. IEnumerator DelayEnterNextProcess(float time)
  174. {
  175. yield return new WaitForSeconds(time);
  176. currentStepID++;
  177. ActiveCurrentProcess();
  178. }
  179. public void PreProcessState()
  180. {
  181. JumpProcessState(currentStepID - 1);
  182. }
  183. public void NextProcessState()
  184. {
  185. JumpProcessState(currentStepID + 1);
  186. }
  187. /// <summary>
  188. /// 跳步
  189. /// </summary>
  190. /// <param name="targetStepID">目标步骤ID</param>
  191. public void JumpProcessState(int targetStepID)
  192. {
  193. //步骤错误
  194. if (targetStepID < 1 || targetStepID > CurrentListsCount)
  195. {
  196. //防止第一步动画跳入两次
  197. GetProcessElement(CurrentListID).QuitHalfWay();
  198. GetProcessElement(CurrentListID).SetEnterState();
  199. Debug.LogError("流程跳转:无目标步骤");
  200. return;
  201. }
  202. if (targetStepID == currentStepID)
  203. {
  204. //重置当前步骤状态
  205. GetProcessElement(CurrentListID).QuitHalfWay();
  206. GetProcessElement(CurrentListID).SetEnterState();
  207. }
  208. //向前跳步
  209. else if (targetStepID < currentStepID)
  210. {
  211. for (int i = currentStepID - 1; i >= targetStepID - 1; i--)
  212. {
  213. if (i.Equals(currentStepID - 1))
  214. {
  215. GetProcessElement(i).QuitHalfWay();
  216. if (currentStepID != targetStepID)
  217. {
  218. GetProcessElement(i).SetEnterState(false);
  219. }
  220. else
  221. {
  222. GetProcessElement(i).SetEnterState(true);
  223. }
  224. }
  225. else if (i.Equals(targetStepID - 1))
  226. {
  227. GetProcessElement(i).SetEnterState(true);
  228. }
  229. else
  230. {
  231. GetProcessElement(i).SetEnterState(false);
  232. }
  233. }
  234. }
  235. //向后跳步
  236. else
  237. {
  238. for (int i = currentStepID - 1; i <= targetStepID - 1; i++)
  239. {
  240. if (i.Equals(currentStepID - 1))
  241. {
  242. GetProcessElement(i).QuitHalfWay();
  243. }
  244. if (i.Equals(targetStepID - 1))
  245. {
  246. GetProcessElement(i).SetEnterState();
  247. }
  248. else
  249. {
  250. GetProcessElement(i).SetExitState();
  251. }
  252. }
  253. }
  254. awakeStepID = targetStepID;
  255. currentStepID = targetStepID;
  256. }
  257. /// <summary>
  258. /// 停止当前步骤
  259. /// </summary>
  260. public void StopCurrentProcess()
  261. {
  262. GetProcessElement(CurrentListID).QuitHalfWay();
  263. }
  264. /// <summary>
  265. ///初始化步骤状态
  266. /// </summary>
  267. public void InitStepState()
  268. {
  269. for (int i = processes.Count - 1; i >= 0; i--)
  270. {
  271. if (i == 0)
  272. {
  273. processes[i].SetEnterState(true);
  274. }
  275. else
  276. {
  277. processes[i].SetEnterState(false);
  278. }
  279. }
  280. Debug.Log("初始化跳转至第一步");
  281. awakeStepID = 1;
  282. currentStepID = 1;
  283. }
  284. /// <summary>
  285. /// 根据步骤获取Procedure
  286. /// </summary>
  287. /// <param name="_step"></param>
  288. /// <returns></returns>
  289. private ProcessElement GetProcessElement(int listID)
  290. {
  291. try
  292. {
  293. return processes[listID];
  294. }
  295. catch
  296. {
  297. return null;
  298. }
  299. }
  300. /// <summary>
  301. /// 获取当前步骤Procedure
  302. /// </summary>
  303. /// <param name="_step"></param>
  304. /// <returns></returns>
  305. public ProcessElement GetCurrentProcess()
  306. {
  307. try
  308. {
  309. return processes[CurrentListID];
  310. }
  311. catch
  312. {
  313. return null;
  314. }
  315. }
  316. #region 更删改查
  317. /// <summary>
  318. /// 更新流程列表
  319. /// </summary>
  320. public void UpdateProcesses()
  321. {
  322. processes.Clear();
  323. processes = processListParent.GetComponentsInChildren<ProcessElement>().ToList();
  324. processes = processes.OrderBy(target => target.stepID).ToList();
  325. for (int i = 0; i < processes.Count; i++)
  326. {
  327. processes[i].transform.SetSiblingIndex(processes[i].stepID - 1);
  328. processes[i].SetName();
  329. }
  330. }
  331. /// <summary>
  332. /// 添加流程
  333. /// </summary>
  334. public void AddProcess()
  335. {
  336. ProcessElement process = new GameObject().AddComponent<ProcessElement>();
  337. process.transform.parent = processListParent.transform;
  338. process.transform.position = Vector3.zero;
  339. process.stepID = addProcedureStep;
  340. process.SetName();
  341. processes.Insert(process.stepID - 1, process);
  342. process.transform.SetSiblingIndex(process.stepID - 1);
  343. for (int i = addProcedureStep; i < processes.Count; i++)
  344. {
  345. if (processes[i] == null)
  346. continue;
  347. processes[i].stepID = i + 1;
  348. processes[i].SetName();
  349. }
  350. UpdateProcesses();
  351. }
  352. /// <summary>
  353. /// 添加流程
  354. /// </summary>
  355. public ProcessElement AddProcess1()
  356. {
  357. ProcessElement process = new GameObject().AddComponent<ProcessElement>();
  358. if (processListParent == null) processListParent = this.gameObject;
  359. process.transform.parent = processListParent.transform;
  360. process.transform.position = Vector3.zero;
  361. process.stepID = processes.Count + 1;
  362. process.SetName();
  363. processes.Insert(process.stepID - 1, process);
  364. process.transform.SetSiblingIndex(process.stepID - 1);
  365. for (int i = addProcedureStep; i < processes.Count; i++)
  366. {
  367. if (processes[i] == null) continue;
  368. processes[i].stepID = i + 1;
  369. processes[i].SetName();
  370. }
  371. UpdateProcesses();
  372. return process;
  373. }
  374. /// <summary>
  375. /// 删除流程
  376. /// </summary>
  377. public void DelateProcess()
  378. {
  379. if (!setDelateNumber) return;
  380. DestroyImmediate(processes[delateProcedureStep - 1].gameObject);
  381. processes.RemoveAt(delateProcedureStep - 1);
  382. if ((delateProcedureStep - 1) < processes.Count)
  383. {
  384. for (int i = delateProcedureStep - 1; i < processes.Count; i++)
  385. {
  386. if (processes[i] == null)
  387. continue;
  388. processes[i].stepID = i + 1;
  389. processes[i].SetName();
  390. }
  391. }
  392. UpdateProcesses();
  393. }
  394. #endregion
  395. }
  396. }