ActionManager.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System;
  4. using Urho;
  5. namespace Urho.Actions
  6. {
  7. public class ActionManager : IDisposable
  8. {
  9. internal class HashElement
  10. {
  11. public int ActionIndex;
  12. public List<ActionState> ActionStates;
  13. public ActionState CurrentActionState;
  14. public bool CurrentActionSalvaged;
  15. public bool Paused;
  16. public Node Target;
  17. }
  18. static Node[] tmpKeysArray = new Node[128];
  19. readonly Dictionary<Node, HashElement> targets = new Dictionary<Node, HashElement>();
  20. bool currentTargetSalvaged;
  21. HashElement currentTarget;
  22. bool targetsAvailable = false;
  23. #region Cleaning up
  24. ~ActionManager ()
  25. {
  26. this.Dispose(false);
  27. }
  28. public void Dispose()
  29. {
  30. this.Dispose(true);
  31. GC.SuppressFinalize(this);
  32. }
  33. protected virtual void Dispose(bool disposing)
  34. {
  35. if (disposing)
  36. {
  37. // Dispose of managed resources
  38. }
  39. this.RemoveAllActions();
  40. }
  41. #endregion Cleaning up
  42. public BaseAction GetAction(int tag, Node target)
  43. {
  44. Debug.Assert(tag != (int)ActionTag.Invalid);
  45. // Early out if we do not have any targets to search
  46. if (targets.Count == 0)
  47. return null;
  48. HashElement element;
  49. if (targets.TryGetValue(target, out element))
  50. {
  51. if (element.ActionStates != null)
  52. {
  53. int limit = element.ActionStates.Count;
  54. for (int i = 0; i < limit; i++)
  55. {
  56. var action = element.ActionStates[i].Action;
  57. if (action.Tag == tag)
  58. {
  59. return action;
  60. }
  61. }
  62. }
  63. }
  64. return null;
  65. }
  66. public ActionState GetActionState(int tag, Node target)
  67. {
  68. Debug.Assert(tag != (int)ActionTag.Invalid);
  69. // Early out if we do not have any targets to search
  70. if (targets.Count == 0)
  71. return null;
  72. HashElement element;
  73. if (targets.TryGetValue(target, out element))
  74. {
  75. if (element.ActionStates != null)
  76. {
  77. int limit = element.ActionStates.Count;
  78. for (int i = 0; i < limit; i++)
  79. {
  80. var actionState = element.ActionStates[i];
  81. if (actionState.Action.Tag == tag)
  82. {
  83. return actionState;
  84. }
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. public int NumberOfRunningActionsInTarget(Node target)
  91. {
  92. HashElement element;
  93. if (targets.TryGetValue(target, out element))
  94. {
  95. return (element.ActionStates != null) ? element.ActionStates.Count : 0;
  96. }
  97. return 0;
  98. }
  99. public void Update(float dt)
  100. {
  101. if (!targetsAvailable)
  102. return;
  103. int count = targets.Count;
  104. while (tmpKeysArray.Length < count)
  105. {
  106. tmpKeysArray = new Node[tmpKeysArray.Length * 2];
  107. }
  108. targets.Keys.CopyTo(tmpKeysArray, 0);
  109. for (int i = 0; i < count; i++)
  110. {
  111. HashElement elt;
  112. if (!targets.TryGetValue(tmpKeysArray[i], out elt))
  113. {
  114. continue;
  115. }
  116. if (elt.Target.IsDeleted)
  117. targets.Remove(elt.Target);
  118. currentTarget = elt;
  119. currentTargetSalvaged = false;
  120. if (!currentTarget.Paused)
  121. {
  122. // The 'actions' may change while inside this loop.
  123. for (currentTarget.ActionIndex = 0;
  124. currentTarget.ActionIndex < currentTarget.ActionStates.Count;
  125. currentTarget.ActionIndex++)
  126. {
  127. currentTarget.CurrentActionState = currentTarget.ActionStates[currentTarget.ActionIndex];
  128. if (currentTarget.CurrentActionState == null)
  129. {
  130. continue;
  131. }
  132. if (currentTarget.Target.IsDeleted)
  133. {
  134. break;
  135. }
  136. currentTarget.CurrentActionSalvaged = false;
  137. currentTarget.CurrentActionState.Step(dt);
  138. if (currentTarget.CurrentActionSalvaged)
  139. {
  140. // The currentAction told the node to remove it. To prevent the action from
  141. // aidentally deallocating itself before finishing its step, we retained
  142. // it. Now that step is done, it's safe to release it.
  143. //currentTarget->currentAction->release();
  144. }
  145. else if (currentTarget.CurrentActionState.IsDone)
  146. {
  147. currentTarget.CurrentActionState.Stop();
  148. var actionState = currentTarget.CurrentActionState;
  149. // Make currentAction nil to prevent removeAction from salvaging it.
  150. currentTarget.CurrentActionState = null;
  151. RemoveAction(actionState);
  152. }
  153. currentTarget.CurrentActionState = null;
  154. }
  155. }
  156. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  157. if (currentTargetSalvaged && currentTarget.ActionStates.Count == 0)
  158. {
  159. DeleteHashElement(currentTarget);
  160. }
  161. }
  162. currentTarget = null;
  163. }
  164. internal void DeleteHashElement(HashElement element)
  165. {
  166. element.ActionStates.Clear();
  167. targets.Remove(element.Target);
  168. element.Target = null;
  169. targetsAvailable = targets.Count > 0;
  170. }
  171. internal void ActionAllocWithHashElement(HashElement element)
  172. {
  173. if (element.ActionStates == null)
  174. {
  175. element.ActionStates = new List<ActionState>();
  176. }
  177. }
  178. #region Action running
  179. public void PauseTarget(Node target)
  180. {
  181. HashElement element;
  182. if (targets.TryGetValue(target, out element))
  183. {
  184. element.Paused = true;
  185. }
  186. }
  187. public void ResumeTarget(Node target)
  188. {
  189. HashElement element;
  190. if (targets.TryGetValue(target, out element))
  191. {
  192. element.Paused = false;
  193. }
  194. }
  195. public List<object> PauseAllRunningActions()
  196. {
  197. var idsWithActions = new List<object>();
  198. foreach (var element in targets.Values)
  199. {
  200. if (!element.Paused)
  201. {
  202. element.Paused = true;
  203. idsWithActions.Add(element.Target);
  204. }
  205. }
  206. return idsWithActions;
  207. }
  208. public void ResumeTargets(List<Node> targetsToResume)
  209. {
  210. for (int i = 0; i < targetsToResume.Count; i++)
  211. {
  212. ResumeTarget(targetsToResume[i]);
  213. }
  214. }
  215. #endregion Action running
  216. #region Adding/removing actions
  217. public ActionState AddAction(BaseAction action, Node target, bool paused = false)
  218. {
  219. Debug.Assert(action != null);
  220. Debug.Assert(target != null);
  221. HashElement element;
  222. if (!targets.TryGetValue(target, out element))
  223. {
  224. element = new HashElement();
  225. element.Paused = paused;
  226. element.Target = target;
  227. targets.Add(target, element);
  228. targetsAvailable = true;
  229. }
  230. ActionAllocWithHashElement(element);
  231. var isActionRunning = false;
  232. foreach (var existingState in element.ActionStates)
  233. {
  234. if (existingState.Action == action)
  235. {
  236. isActionRunning = true;
  237. break;
  238. }
  239. }
  240. Debug.Assert(!isActionRunning, "Action is already running for this target.");
  241. var state = action.StartAction(target);
  242. element.ActionStates.Add(state);
  243. return state;
  244. }
  245. public void RemoveAllActions()
  246. {
  247. if (!targetsAvailable)
  248. return;
  249. int count = targets.Count;
  250. if (tmpKeysArray.Length < count)
  251. {
  252. tmpKeysArray = new Node[tmpKeysArray.Length * 2];
  253. }
  254. targets.Keys.CopyTo(tmpKeysArray, 0);
  255. for (int i = 0; i < count; i++)
  256. {
  257. RemoveAllActionsFromTarget(tmpKeysArray[i]);
  258. }
  259. }
  260. public void RemoveAllActionsFromTarget(Node target)
  261. {
  262. if (target == null)
  263. {
  264. return;
  265. }
  266. HashElement element;
  267. if (targets.TryGetValue(target, out element))
  268. {
  269. if (element.ActionStates.Contains(element.CurrentActionState) && (!element.CurrentActionSalvaged))
  270. {
  271. element.CurrentActionSalvaged = true;
  272. }
  273. element.ActionStates.Clear();
  274. if (currentTarget == element)
  275. {
  276. currentTargetSalvaged = true;
  277. }
  278. else
  279. {
  280. DeleteHashElement(element);
  281. }
  282. }
  283. }
  284. public void RemoveAction(ActionState actionState)
  285. {
  286. if (actionState == null || actionState.OriginalTarget == null)
  287. {
  288. return;
  289. }
  290. var target = actionState.OriginalTarget;
  291. HashElement element;
  292. if (targets.TryGetValue(target, out element))
  293. {
  294. int i = element.ActionStates.IndexOf(actionState);
  295. if (i != -1)
  296. {
  297. RemoveActionAtIndex(i, element);
  298. }
  299. }
  300. }
  301. internal void RemoveActionAtIndex(int index, HashElement element)
  302. {
  303. var action = element.ActionStates[index];
  304. if (action == element.CurrentActionState && (!element.CurrentActionSalvaged))
  305. {
  306. element.CurrentActionSalvaged = true;
  307. }
  308. element.ActionStates.RemoveAt(index);
  309. // update actionIndex in case we are in tick. looping over the actions
  310. if (element.ActionIndex >= index)
  311. {
  312. element.ActionIndex--;
  313. }
  314. if (element.ActionStates.Count == 0)
  315. {
  316. if (currentTarget == element)
  317. {
  318. currentTargetSalvaged = true;
  319. }
  320. else
  321. {
  322. DeleteHashElement(element);
  323. }
  324. }
  325. }
  326. internal void RemoveAction(BaseAction action, Node target)
  327. {
  328. if (action == null || target == null)
  329. return;
  330. HashElement element;
  331. if (targets.TryGetValue(target, out element))
  332. {
  333. int limit = element.ActionStates.Count;
  334. bool actionFound = false;
  335. for (int i = 0; i < limit; i++)
  336. {
  337. var actionState = element.ActionStates[i];
  338. if (actionState.Action == action && actionState.OriginalTarget == target)
  339. {
  340. RemoveActionAtIndex(i, element);
  341. actionFound = true;
  342. break;
  343. }
  344. }
  345. }
  346. }
  347. public void RemoveAction(int tag, Node target)
  348. {
  349. Debug.Assert((tag != (int)ActionTag.Invalid));
  350. Debug.Assert(target != null);
  351. // Early out if we do not have any targets to search
  352. if (targets.Count == 0)
  353. return;
  354. HashElement element;
  355. if (targets.TryGetValue(target, out element))
  356. {
  357. int limit = element.ActionStates.Count;
  358. bool tagFound = false;
  359. for (int i = 0; i < limit; i++)
  360. {
  361. var actionState = element.ActionStates[i];
  362. if (actionState.Action.Tag == tag && actionState.OriginalTarget == target)
  363. {
  364. RemoveActionAtIndex(i, element);
  365. tagFound = true;
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. #endregion Adding/removing actions
  372. }
  373. }