CCActionManager.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * @class
  24. * @extends cc.Class
  25. */
  26. cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
  27. actions:null,
  28. target:null, //ccobject
  29. actionIndex:0,
  30. currentAction:null, //CCAction
  31. currentActionSalvaged:false,
  32. paused:false,
  33. hh:null, //ut hash handle
  34. /**
  35. * Constructor
  36. */
  37. ctor:function () {
  38. this.actions = [];
  39. this._target = null;
  40. this.actionIndex = 0;
  41. this.currentAction = null; //CCAction
  42. this.currentActionSalvaged = false;
  43. this.paused = false;
  44. this.hh = null; //ut hash handle
  45. }
  46. });
  47. /**
  48. * cc.ActionManager is a singleton that manages all the actions.<br/>
  49. * Normally you won't need to use this singleton directly. 99% of the cases you will use the CCNode interface,
  50. * which uses this singleton.
  51. * But there are some cases where you might need to use this singleton. <br/>
  52. * Examples:<br/>
  53. * - When you want to run an action where the target is different from a CCNode.<br/>
  54. * - When you want to pause / resume the actions<br/>
  55. * @class
  56. * @extends cc.Class
  57. */
  58. cc.ActionManager = cc.Class.extend({
  59. _hashTargets:null,
  60. _arrayTargets:null,
  61. _currentTarget:null,
  62. _currentTargetSalvaged:false,
  63. _searchElementByTarget:function (arr, target) {
  64. for (var k = 0; k < arr.length; k++) {
  65. if (target == arr[k].target)
  66. return arr[k];
  67. }
  68. return null;
  69. },
  70. /**
  71. * Constructor
  72. */
  73. ctor:function () {
  74. this._hashTargets = {};
  75. this._arrayTargets = [];
  76. this._currentTarget = null;
  77. this._currentTargetSalvaged = false;
  78. },
  79. /** Adds an action with a target.
  80. * If the target is already present, then the action will be added to the existing target.
  81. * If the target is not present, a new instance of this target will be created either paused or not, and the action will be added to the newly created target.
  82. * When the target is paused, the queued actions won't be 'ticked'.
  83. * @param {cc.Action} action
  84. * @param {cc.Node} target
  85. * @param {Boolean} paused
  86. */
  87. addAction:function (action, target, paused) {
  88. if(!action)
  89. throw "cc.ActionManager.addAction(): action must be non-null";
  90. if(!target)
  91. throw "cc.ActionManager.addAction(): action must be non-null";
  92. //check if the action target already exists
  93. var element = this._hashTargets[target.__instanceId];
  94. //if doesnt exists, create a hashelement and push in mpTargets
  95. if (!element) {
  96. element = new cc.HashElement();
  97. element.paused = paused;
  98. element.target = target;
  99. this._hashTargets[target.__instanceId] = element;
  100. this._arrayTargets.push(element);
  101. }
  102. //creates a array for that eleemnt to hold the actions
  103. this._actionAllocWithHashElement(element);
  104. element.actions.push(action);
  105. action.startWithTarget(target);
  106. },
  107. /**
  108. * Removes all actions from all the targets.
  109. */
  110. removeAllActions:function () {
  111. var locTargets = this._arrayTargets;
  112. for (var i = 0; i < locTargets.length; i++) {
  113. var element = locTargets[i];
  114. if (element)
  115. this.removeAllActionsFromTarget(element.target, true);
  116. }
  117. },
  118. /** Removes all actions from a certain target. <br/>
  119. * All the actions that belongs to the target will be removed.
  120. * @param {object} target
  121. * @param {boolean} forceDelete
  122. */
  123. removeAllActionsFromTarget:function (target, forceDelete) {
  124. // explicit null handling
  125. if (target == null)
  126. return;
  127. var element = this._hashTargets[target.__instanceId];
  128. if (element) {
  129. if (element.actions.indexOf(element.currentAction) !== -1 && !(element.currentActionSalvaged))
  130. element.currentActionSalvaged = true;
  131. element.actions.length = 0;
  132. if (this._currentTarget == element && !forceDelete) {
  133. this._currentTargetSalvaged = true;
  134. } else {
  135. this._deleteHashElement(element);
  136. }
  137. }
  138. },
  139. /** Removes an action given an action reference.
  140. * @param {cc.Action} action
  141. */
  142. removeAction:function (action) {
  143. // explicit null handling
  144. if (action == null)
  145. return;
  146. var target = action.getOriginalTarget();
  147. var element = this._hashTargets[target.__instanceId];
  148. if (element) {
  149. for (var i = 0; i < element.actions.length; i++) {
  150. if (element.actions[i] == action) {
  151. element.actions.splice(i, 1);
  152. break;
  153. }
  154. }
  155. } else {
  156. cc.log("cocos2d: removeAction: Target not found");
  157. }
  158. },
  159. /** Removes an action given its tag and the target
  160. * @param {Number} tag
  161. * @param {object} target
  162. */
  163. removeActionByTag:function (tag, target) {
  164. if(tag == cc.ACTION_TAG_INVALID)
  165. cc.log("cc.ActionManager.removeActionByTag(): an invalid tag");
  166. if(!target)
  167. throw "cc.ActionManager.removeActionByTag(): target must be non-null";
  168. var element = this._hashTargets[target.__instanceId];
  169. if (element) {
  170. var limit = element.actions.length;
  171. for (var i = 0; i < limit; ++i) {
  172. var action = element.actions[i];
  173. if (action && action.getTag() === tag && action.getOriginalTarget() == target) {
  174. this._removeActionAtIndex(i, element);
  175. break;
  176. }
  177. }
  178. }
  179. },
  180. /** Gets an action given its tag an a target
  181. * @param {Number} tag
  182. * @param {object} target
  183. * @return {cc.Action|Null} return the Action with the given tag on success
  184. */
  185. getActionByTag:function (tag, target) {
  186. if(tag == cc.ACTION_TAG_INVALID)
  187. cc.log("cc.ActionManager.getActionByTag(): an invalid tag");
  188. var element = this._hashTargets[target.__instanceId];
  189. if (element) {
  190. if (element.actions != null) {
  191. for (var i = 0; i < element.actions.length; ++i) {
  192. var action = element.actions[i];
  193. if (action && action.getTag() === tag)
  194. return action;
  195. }
  196. }
  197. cc.log("cocos2d : getActionByTag(tag =" + tag + "): Action not found");
  198. }
  199. return null;
  200. },
  201. /** Returns the numbers of actions that are running in a certain target. <br/>
  202. * Composable actions are counted as 1 action. <br/>
  203. * Example: <br/>
  204. * - If you are running 1 Sequence of 7 actions, it will return 1. <br/>
  205. * - If you are running 7 Sequences of 2 actions, it will return 7.
  206. * @param {object} target
  207. * @return {Number}
  208. */
  209. numberOfRunningActionsInTarget:function (target) {
  210. var element = this._hashTargets[target.__instanceId];
  211. if (element)
  212. return (element.actions) ? element.actions.length : 0;
  213. return 0;
  214. },
  215. /** Pauses the target: all running actions and newly added actions will be paused.
  216. * @param {object} target
  217. */
  218. pauseTarget:function (target) {
  219. var element = this._hashTargets[target.__instanceId];
  220. if (element)
  221. element.paused = true;
  222. },
  223. /** Resumes the target. All queued actions will be resumed.
  224. * @param {object} target
  225. */
  226. resumeTarget:function (target) {
  227. var element = this._hashTargets[target.__instanceId];
  228. if (element)
  229. element.paused = false;
  230. },
  231. /**
  232. * Pauses all running actions, returning a list of targets whose actions were paused.
  233. * @return {Array} a list of targets whose actions were paused.
  234. */
  235. pauseAllRunningActions:function(){
  236. var idsWithActions = [];
  237. var locTargets = this._arrayTargets;
  238. for(var i = 0; i< locTargets.length; i++){
  239. var element = locTargets[i];
  240. if(element && !element.paused){
  241. element.paused = true;
  242. idsWithActions.push(element.target);
  243. }
  244. }
  245. return idsWithActions;
  246. },
  247. /**
  248. * Resume a set of targets (convenience function to reverse a pauseAllRunningActions call)
  249. * @param {Array} targetsToResume
  250. */
  251. resumeTargets:function(targetsToResume){
  252. if(!targetsToResume)
  253. return;
  254. for(var i = 0 ; i< targetsToResume.length; i++){
  255. if(targetsToResume[i])
  256. this.resumeTarget(targetsToResume[i]);
  257. }
  258. },
  259. /** purges the shared action manager. It releases the retained instance. <br/>
  260. * because it uses this, so it can not be static
  261. */
  262. purgeSharedManager:function () {
  263. cc.Director.getInstance().getScheduler().unscheduleUpdateForTarget(this);
  264. },
  265. //protected
  266. _removeActionAtIndex:function (index, element) {
  267. var action = element.actions[index];
  268. if ((action == element.currentAction) && (!element.currentActionSalvaged))
  269. element.currentActionSalvaged = true;
  270. cc.ArrayRemoveObjectAtIndex(element.actions,index);
  271. // update actionIndex in case we are in tick. looping over the actions
  272. if (element.actionIndex >= index)
  273. element.actionIndex--;
  274. if (element.actions.length == 0) {
  275. if (this._currentTarget == element) {
  276. this._currentTargetSalvaged = true;
  277. } else {
  278. this._deleteHashElement(element);
  279. }
  280. }
  281. },
  282. _deleteHashElement:function (element) {
  283. if (element) {
  284. delete this._hashTargets[element.target.__instanceId];
  285. cc.ArrayRemoveObject(this._arrayTargets, element);
  286. element.actions = null;
  287. element.target = null;
  288. }
  289. },
  290. _actionAllocWithHashElement:function (element) {
  291. // 4 actions per Node by default
  292. if (element.actions == null) {
  293. element.actions = [];
  294. }
  295. },
  296. /**
  297. * @param {Number} dt delta time in seconds
  298. */
  299. update:function (dt) {
  300. var locTargets = this._arrayTargets , locCurrTarget;
  301. for (var elt = 0; elt < locTargets.length; elt++) {
  302. this._currentTarget = locTargets[elt];
  303. locCurrTarget = this._currentTarget;
  304. //this._currentTargetSalvaged = false;
  305. if (!locCurrTarget.paused) {
  306. // The 'actions' CCMutableArray may change while inside this loop.
  307. for (locCurrTarget.actionIndex = 0; locCurrTarget.actionIndex < locCurrTarget.actions.length;
  308. locCurrTarget.actionIndex++) {
  309. locCurrTarget.currentAction = locCurrTarget.actions[locCurrTarget.actionIndex];
  310. if (!locCurrTarget.currentAction)
  311. continue;
  312. locCurrTarget.currentActionSalvaged = false;
  313. locCurrTarget.currentAction.step(dt);
  314. if (locCurrTarget.currentActionSalvaged) {
  315. // The currentAction told the node to remove it. To prevent the action from
  316. // accidentally deallocating itself before finishing its step, we retained
  317. // it. Now that step is done, it's safe to release it.
  318. locCurrTarget.currentAction = null;//release
  319. } else if (locCurrTarget.currentAction.isDone()) {
  320. locCurrTarget.currentAction.stop();
  321. var action = locCurrTarget.currentAction;
  322. // Make currentAction nil to prevent removeAction from salvaging it.
  323. locCurrTarget.currentAction = null;
  324. this.removeAction(action);
  325. }
  326. locCurrTarget.currentAction = null;
  327. }
  328. }
  329. // elt, at this moment, is still valid
  330. // so it is safe to ask this here (issue #490)
  331. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  332. if (this._currentTargetSalvaged && locCurrTarget.actions.length === 0) {
  333. this._deleteHashElement(locCurrTarget);
  334. }
  335. }
  336. }
  337. });