Guest User

Untitled

a guest
Dec 18th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using JobTodo = AT.JobManager.JobRepeatBase.JobTodo;
  6. using JobToDoCondition = AT.JobManager.JobRepeatBase.JobToDoCondition;
  7. using JobSkipCondition = AT.JobManager.JobRepeatBase.JobSkipCondition;
  8. using JobAutoDropCondition = AT.JobManager.JobRepeatBase.JobAutoDropCondition;
  9. using JobExceptionCondition = AT.JobManager.JobRepeatBase.JobExceptionCondition;
  10. using JOB_STATE = AT.JobManager.JobRepeatBase.JOB_STATE;
  11.  
  12. #if UNITY_EDITOR
  13. using UnityEditor;
  14. namespace AT.JobManager
  15. {
  16. [CustomEditor(typeof(JobRepeatManager))]
  17. public class JobRepeatManagerEditor : Editor
  18. {
  19. private JobRepeatManager manager;
  20. public override void OnInspectorGUI()
  21. {
  22. manager = (JobRepeatManager)target;
  23.  
  24. EditorGUILayout.BeginHorizontal();
  25. if (GUILayout.Button("Add Empty Job"))
  26. {
  27. int EmptyJobCount = manager.JobList.FindAll(job => job.key.Contains("EmptyJob")).Count;
  28. manager.AddDelegateJob(string.Format("EmptyJob_{0}", EmptyJobCount), null);
  29. }
  30.  
  31. if (GUILayout.Button("Drop All Job"))
  32. {
  33. if(manager.JobList.Count > 0)
  34. {
  35. foreach(var jobItem in manager.JobList)
  36. {
  37. DestroyImmediate(jobItem.gameObject);
  38. }
  39. manager.JobList.Clear();
  40. }
  41. }
  42.  
  43. EditorApplication.update();
  44.  
  45. EditorGUILayout.EndHorizontal();
  46. DrawDefaultInspector();
  47. }
  48. }
  49. }
  50. #endif
  51.  
  52. namespace AT.JobManager
  53. {
  54. [ExecuteInEditMode]
  55. public class JobRepeatManager : SingletonBase<JobRepeatManager>
  56. {
  57. // Job Information Container - Job Data Struct With Delegate Job
  58. [SerializeField] protected List<JobRepeatBase> _jobList = new List<JobRepeatBase>();
  59. public List<JobRepeatBase> JobList { get { return _jobList; } }
  60.  
  61. // Coroutine Job Information Container - Job Data Struct With Coroutine Job
  62. [SerializeField] protected Dictionary<string, Coroutine> _coroutineJobList = new Dictionary<string, Coroutine>();
  63.  
  64. // Min DelayTime
  65. protected float m_MinDelayTime = 0.1f;
  66. public float MinDelayTime { get { return m_MinDelayTime; } set { m_MinDelayTime = value; } }
  67.  
  68. /// <summary>
  69. /// Job RepeatManager > Adding Job
  70. /// <para>key = JobKeyName,
  71. /// todo = ExecuteFunctionPointer,
  72. /// delay = Update Sequence Delay(Seconds),
  73. /// repeatCount = Total Execute Count,
  74. /// todoCondition = Execute Condition(Flag : true = Execute, false = Wait),
  75. /// skipCondition = Update Skip Condition(Flag : true = Skip, false = Wait),
  76. /// autoDropCondition = Job Drop Condition(Flag : true = Drop, false = MoveNext),
  77. /// exceptionCondition = Job Execute Exception Condition(Flag : true = Exception(like Skip), false = MoveNext)</para>
  78. /// </summary>
  79. public bool AddDelegateJob(string key, JobTodo toDo, float delay = 1.0f, int repeatCount = 0,
  80. JobToDoCondition toDoCondition = null, JobSkipCondition skipCondition = null,
  81. JobAutoDropCondition autoDropCondition = null, JobExceptionCondition exceptionCondition = null,
  82. object[] param = null, bool isImmediately = true)
  83. {
  84. // Already Registered Job Check
  85. if (JobList.Find(job => job.key.Equals(key)) != null)
  86. return false;
  87.  
  88. GameObject JobObject = new GameObject(key);
  89. JobObject.transform.parent = this.transform;
  90. JobRepeatBase newJob = JobObject.AddComponent<JobRepeatBase>();
  91. newJob.key = key;
  92. newJob.jobCoroutine = null;
  93. newJob.jobTodo = toDo;
  94. newJob.repeatDelay = delay;
  95. newJob.repeatCount = repeatCount;
  96. newJob.excuteCount = 0;
  97. newJob.jobToDoCheck = toDoCondition;
  98. newJob.jobSkipCheck = skipCondition;
  99. newJob.jobAutoDropCheck = autoDropCondition;
  100. newJob.jobExceptionCheck = exceptionCondition;
  101. newJob.state = JOB_STATE.JOB_STANBY;
  102. newJob.worker = CoJobHandle(key);
  103. newJob.parameter = param;
  104.  
  105. if(toDo == null)
  106. {
  107. Debug.LogWarningFormat("Are You Sure Adding Empty Job? Todo Parameter is null (key:{0})", key);
  108. newJob.state = JOB_STATE.JOB_EMPTY;
  109. }
  110.  
  111. newJob.repeatDelay = newJob.repeatDelay < m_MinDelayTime ? m_MinDelayTime : newJob.repeatDelay;
  112. JobList.Add(newJob);
  113.  
  114. if (isImmediately)
  115. {
  116. StartCoroutine(newJob.worker);
  117. }
  118.  
  119. return true;
  120. }
  121.  
  122. /// <summary>
  123. /// Job RepeatManager > Coroutine Type Adding Job
  124. /// <para>key = JobKeyName,
  125. /// todo = Coroutine Type Todo,
  126. /// delay = Update Sequence Delay(Seconds),
  127. /// repeatCount = Total Execute Count,
  128. /// todoCondition = Execute Condition(Flag : true = Execute, false = Wait),
  129. /// skipCondition = Update Skip Condition(Flag : true = Skip, false = Wait),
  130. /// autoDropCondition = Job Drop Condition(Flag : true = Drop, false = MoveNext),
  131. /// exceptionCondition = Job Execute Exception Condition(Flag : true = Exception(like Skip), false = MoveNext)</para>
  132. /// </summary>
  133. public bool AddCoroutineJob(string key, IEnumerator coroutineJobTodo, float delay = 1.0f, int repeatCount = 0,
  134. object[] param = null,
  135. JobToDoCondition toDoCondition = null, JobSkipCondition skipCondition = null,
  136. JobAutoDropCondition autoDropCondition = null, JobExceptionCondition exceptionCondition = null,
  137. bool isImmediately = true)
  138. {
  139. // Already Registered Job Check
  140. if (JobList.Find(job => job.key.Equals(key)) != null)
  141. return false;
  142.  
  143. GameObject JobObject = new GameObject(key);
  144. JobObject.transform.parent = this.transform;
  145. JobRepeatBase newJob = JobObject.AddComponent<JobRepeatBase>();
  146. newJob.key = key;
  147. newJob.jobCoroutine = coroutineJobTodo;
  148. newJob.jobTodo = null;
  149. newJob.repeatDelay = delay;
  150. newJob.repeatCount = repeatCount;
  151. newJob.excuteCount = 0;
  152. newJob.jobToDoCheck = toDoCondition;
  153. newJob.jobSkipCheck = skipCondition;
  154. newJob.jobAutoDropCheck = autoDropCondition;
  155. newJob.jobExceptionCheck = exceptionCondition;
  156. newJob.state = JOB_STATE.JOB_STANBY;
  157. newJob.worker = CoJobHandle(key);
  158. newJob.parameter = param;
  159.  
  160. if (coroutineJobTodo == null)
  161. {
  162. Debug.LogWarningFormat("Are You Sure Adding Empty Job? Todo Parameter is null (key:{0})", key);
  163. newJob.state = JOB_STATE.JOB_EMPTY;
  164. }
  165.  
  166. newJob.repeatDelay = newJob.repeatDelay < m_MinDelayTime ? m_MinDelayTime : newJob.repeatDelay;
  167. JobList.Add(newJob);
  168.  
  169. if (isImmediately)
  170. {
  171. StartCoroutine(newJob.worker);
  172. }
  173.  
  174. return true;
  175. }
  176.  
  177. private void Start()
  178. {
  179. StartCoroutine(CoAutoDropWorkers());
  180. }
  181.  
  182. public bool RemoveJob(string key)
  183. {
  184. JobRepeatBase findJob = JobList.Find(job => job.key.Equals(key));
  185. if (findJob == null)
  186. return false;
  187.  
  188. DestroyImmediate(findJob.gameObject);
  189. return JobList.Remove(findJob);
  190. }
  191.  
  192. public bool JobStart(string key)
  193. {
  194. JobRepeatBase findJob = JobList.Find(job => job.key.Equals(key));
  195. if (findJob == null)
  196. return false;
  197.  
  198. StopCoroutine(findJob.worker);
  199.  
  200. findJob.state = JOB_STATE.JOB_STANBY;
  201. StartCoroutine(findJob.worker);
  202. return true;
  203. }
  204.  
  205. public int JobDropAll()
  206. {
  207. int droppedJobCount = 0;
  208. if(JobList.Count > 0)
  209. {
  210. JobList.Clear();
  211. droppedJobCount = transform.childCount;
  212. transform.DestroyAllChildren();
  213. }
  214.  
  215. return droppedJobCount;
  216. }
  217.  
  218. public bool ChangeJobDelay(string key, float newDelay)
  219. {
  220. JobRepeatBase findJob = JobList.Find(job => job.key.Equals(key));
  221. if (findJob == null)
  222. return false;
  223.  
  224. findJob.repeatDelay = newDelay;
  225. StopCoroutine(findJob.worker);
  226.  
  227. findJob.state = JOB_STATE.JOB_STANBY;
  228. StartCoroutine(findJob.worker);
  229. return true;
  230. }
  231.  
  232. public bool ChangeRepeatCount(string key, int repeatCount)
  233. {
  234. JobRepeatBase findJob = JobList.Find(job => job.key.Equals(key));
  235. if (findJob == null)
  236. return false;
  237.  
  238. findJob.repeatCount = repeatCount;
  239. StopCoroutine(findJob.worker);
  240.  
  241. findJob.state = JOB_STATE.JOB_STANBY;
  242. StartCoroutine(findJob.worker);
  243. return true;
  244. }
  245.  
  246. public bool AddFunctionChain(string key, JobTodo Todo = null, JobToDoCondition toDoCheck = null,
  247. JobSkipCondition skipCondition = null, JobAutoDropCondition autoDropCondition = null, JobExceptionCondition exceptionCondition = null, bool isExecuteImmediately = true)
  248. {
  249. JobRepeatBase Job = JobList.Find(job => job.key.Equals(key));
  250. if (Job == null)
  251. return false;
  252.  
  253. StopCoroutine(Job.worker);
  254.  
  255. Job.jobTodo += Todo;
  256. Job.jobToDoCheck += toDoCheck;
  257. Job.jobSkipCheck += skipCondition;
  258. Job.jobAutoDropCheck += autoDropCondition;
  259. Job.jobExceptionCheck += exceptionCondition;
  260.  
  261. if (Job.jobTodo == null)
  262. {
  263. Job.state = JOB_STATE.JOB_EMPTY;
  264. return true;
  265. }
  266.  
  267. if (isExecuteImmediately)
  268. {
  269. Job.state = JOB_STATE.JOB_STANBY;
  270. StartCoroutine(Job.worker);
  271. }
  272.  
  273. return true;
  274. }
  275.  
  276. public bool RemoveFunctionChain(string key, JobTodo Todo = null, JobToDoCondition toDoCheck = null,
  277. JobSkipCondition skipCondition = null, JobAutoDropCondition autoDropCondition = null, JobExceptionCondition exceptionCondition = null, bool isExecuteImmediately = true)
  278. {
  279. JobRepeatBase Job = JobList.Find(job => job.key.Equals(key));
  280. if (Job == null)
  281. return false;
  282.  
  283. StopCoroutine(Job.worker);
  284.  
  285. Job.jobTodo -= Todo;
  286. Job.jobToDoCheck -= toDoCheck;
  287. Job.jobSkipCheck -= skipCondition;
  288. Job.jobAutoDropCheck -= autoDropCondition;
  289. Job.jobExceptionCheck -= exceptionCondition;
  290.  
  291. if (Job.jobTodo == null)
  292. {
  293. Job.state = JOB_STATE.JOB_EMPTY;
  294. return true;
  295. }
  296.  
  297. if (isExecuteImmediately)
  298. {
  299. Job.state = JOB_STATE.JOB_STANBY;
  300. StartCoroutine(Job.worker);
  301. }
  302. return true;
  303. }
  304.  
  305. public JobRepeatBase GetJobBase(string key)
  306. {
  307. return JobList.Find(x => x.key == key);
  308. }
  309.  
  310. private WaitForSeconds _dropManagingDelay = new WaitForSeconds(3.0f);
  311. private IEnumerator CoAutoDropWorkers()
  312. {
  313. while (gameObject.activeSelf)
  314. {
  315. var dropItems = JobList.FindAll(Job => Job.state == JOB_STATE.JOB_DROP);
  316. foreach(var dropItem in dropItems)
  317. {
  318. JobList.Remove(dropItem);
  319. DestroyImmediate(dropItem.gameObject);
  320. }
  321. yield return _dropManagingDelay;
  322. }
  323. }
  324.  
  325. private IEnumerator CoJobHandle(string key)
  326. {
  327. yield return null;
  328.  
  329. JobRepeatBase findJob = JobList.Find(x => x.key == key);
  330. if (findJob == null)
  331. yield break;
  332.  
  333. switch (findJob.state)
  334. {
  335. case JOB_STATE.JOB_EMPTY:
  336. yield break;
  337. case JOB_STATE.JOB_STANBY:
  338. if (findJob.jobExceptionCheck != null)
  339. {
  340. while (findJob.jobExceptionCheck(findJob.parameter))
  341. yield return null;
  342. }
  343.  
  344. if (findJob.jobSkipCheck != null)
  345. {
  346. while (findJob.jobSkipCheck(findJob.parameter))
  347. {
  348. findJob.state = JOB_STATE.JOB_HOLDING;
  349. yield return null;
  350. }
  351. findJob.state = JOB_STATE.JOB_STANBY;
  352. }
  353.  
  354. if (findJob.jobToDoCheck != null)
  355. {
  356. if (findJob.jobToDoCheck(findJob.parameter))
  357. {
  358. findJob.state = JOB_STATE.JOB_WORKING;
  359.  
  360. findJob.jobTodo?.Invoke(findJob.parameter);
  361. if (findJob.jobCoroutine != null)
  362. yield return findJob.jobCoroutine;
  363.  
  364. findJob.excuteCount++;
  365. if (findJob.excuteCount >= findJob.repeatCount && findJob.repeatCount != 0)
  366. findJob.state = JOB_STATE.JOB_DROP;
  367. else
  368. findJob.state = JOB_STATE.JOB_WAITING;
  369. }
  370. }
  371. else
  372. {
  373. findJob.state = JOB_STATE.JOB_WORKING;
  374. findJob.jobTodo?.Invoke(findJob.parameter);
  375. if (findJob.jobCoroutine != null)
  376. yield return findJob.jobCoroutine;
  377. findJob.excuteCount++;
  378. if (findJob.excuteCount >= findJob.repeatCount && findJob.repeatCount != 0)
  379. findJob.state = JOB_STATE.JOB_DROP;
  380. else
  381. findJob.state = JOB_STATE.JOB_WAITING;
  382. }
  383.  
  384. if (findJob.jobAutoDropCheck != null)
  385. {
  386. if (findJob.jobAutoDropCheck(findJob.parameter))
  387. {
  388. findJob.state = JOB_STATE.JOB_DROP;
  389. break;
  390. }
  391. }
  392. break;
  393. case JOB_STATE.JOB_WAITING:
  394. WaitForSeconds WaitForDelay = new WaitForSeconds(findJob.repeatDelay);
  395. yield return WaitForDelay;
  396. findJob.state = JOB_STATE.JOB_STANBY;
  397. break;
  398. case JOB_STATE.JOB_DROP:
  399. yield break;
  400. }
  401.  
  402. yield return StartCoroutine(CoJobHandle(findJob.key));
  403. }
  404. }
  405.  
  406. }
Add Comment
Please, Sign In to add comment