CCActionManager.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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. * @example
  26. * var element = new cc.HashElement();
  27. */
  28. cc.HashElement = cc.Class.extend(/** @lends cc.HashElement# */{
  29. actions:null,
  30. target:null, //ccobject
  31. actionIndex:0,
  32. currentAction:null, //CCAction
  33. currentActionSalvaged:false,
  34. paused:false,
  35. hh:null, //ut hash handle
  36. /**
  37. * Constructor
  38. */
  39. ctor:function () {
  40. this.actions = [];
  41. this.target = null;
  42. this.actionIndex = 0;
  43. this.currentAction = null; //CCAction
  44. this.currentActionSalvaged = false;
  45. this.paused = false;
  46. this.hh = null; //ut hash handle
  47. }
  48. });
  49. /**
  50. * cc.ActionManager is a class that can manage actions.<br/>
  51. * Normally you won't need to use this class directly. 99% of the cases you will use the CCNode interface,
  52. * which uses this class's singleton object.
  53. * But there are some cases where you might need to use this class. <br/>
  54. * Examples:<br/>
  55. * - When you want to run an action where the target is different from a CCNode.<br/>
  56. * - When you want to pause / resume the actions<br/>
  57. * @class
  58. * @extends cc.Class
  59. * @example
  60. * var mng = new cc.ActionManager();
  61. */
  62. cc.ActionManager = cc.Class.extend(/** @lends cc.ActionManager# */{
  63. _hashTargets:null,
  64. _arrayTargets:null,
  65. _currentTarget:null,
  66. _currentTargetSalvaged:false,
  67. _searchElementByTarget:function (arr, target) {
  68. for (var k = 0; k < arr.length; k++) {
  69. if (target == arr[k].target)
  70. return arr[k];
  71. }
  72. return null;
  73. },
  74. ctor:function () {
  75. this._hashTargets = {};
  76. this._arrayTargets = [];
  77. this._currentTarget = null;
  78. this._currentTargetSalvaged = false;
  79. },
  80. /** Adds an action with a target.
  81. * If the target is already present, then the action will be added to the existing target.
  82. * 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.
  83. * When the target is paused, the queued actions won't be 'ticked'.
  84. * @param {cc.Action} action
  85. * @param {cc.Node} target
  86. * @param {Boolean} paused
  87. */
  88. addAction:function (action, target, paused) {
  89. if(!action)
  90. throw "cc.ActionManager.addAction(): action must be non-null";
  91. if(!target)
  92. throw "cc.ActionManager.addAction(): action must be non-null";
  93. //check if the action target already exists
  94. var element = this._hashTargets[target.__instanceId];
  95. //if doesnt exists, create a hashelement and push in mpTargets
  96. if (!element) {
  97. element = new cc.HashElement();
  98. element.paused = paused;
  99. element.target = target;
  100. this._hashTargets[target.__instanceId] = element;
  101. this._arrayTargets.push(element);
  102. }
  103. //creates a array for that eleemnt to hold the actions
  104. this._actionAllocWithHashElement(element);
  105. element.actions.push(action);
  106. action.startWithTarget(target);
  107. },
  108. /**
  109. * Removes all actions from all the targets.
  110. */
  111. removeAllActions:function () {
  112. var locTargets = this._arrayTargets;
  113. for (var i = 0; i < locTargets.length; i++) {
  114. var element = locTargets[i];
  115. if (element)
  116. this.removeAllActionsFromTarget(element.target, true);
  117. }
  118. },
  119. /** Removes all actions from a certain target. <br/>
  120. * All the actions that belongs to the target will be removed.
  121. * @param {object} target
  122. * @param {boolean} forceDelete
  123. */
  124. removeAllActionsFromTarget:function (target, forceDelete) {
  125. // explicit null handling
  126. if (target == null)
  127. return;
  128. var element = this._hashTargets[target.__instanceId];
  129. if (element) {
  130. if (element.actions.indexOf(element.currentAction) !== -1 && !(element.currentActionSalvaged))
  131. element.currentActionSalvaged = true;
  132. element.actions.length = 0;
  133. if (this._currentTarget == element && !forceDelete) {
  134. this._currentTargetSalvaged = true;
  135. } else {
  136. this._deleteHashElement(element);
  137. }
  138. }
  139. },
  140. /** Removes an action given an action reference.
  141. * @param {cc.Action} action
  142. */
  143. removeAction:function (action) {
  144. // explicit null handling
  145. if (action == null)
  146. return;
  147. var target = action.getOriginalTarget();
  148. var element = this._hashTargets[target.__instanceId];
  149. if (element) {
  150. for (var i = 0; i < element.actions.length; i++) {
  151. if (element.actions[i] == action) {
  152. element.actions.splice(i, 1);
  153. break;
  154. }
  155. }
  156. } else {
  157. cc.log(cc._LogInfos.ActionManager_removeAction);
  158. }
  159. },
  160. /** Removes an action given its tag and the target
  161. * @param {Number} tag
  162. * @param {object} target
  163. */
  164. removeActionByTag:function (tag, target) {
  165. if(tag == cc.ACTION_TAG_INVALID)
  166. cc.log(cc._LogInfos.ActionManager_addAction);
  167. cc.assert(target, cc._LogInfos.ActionManager_addAction);
  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._LogInfos.ActionManager_getActionByTag);
  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(cc._LogInfos.ActionManager_getActionByTag_2, tag);
  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.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. element.actions.splice(index, 1);
  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. //use for speed
  314. locCurrTarget.currentAction.step(dt * ( locCurrTarget.currentAction._speedMethod ? locCurrTarget.currentAction._speed : 1 ) );
  315. if (locCurrTarget.currentActionSalvaged) {
  316. // The currentAction told the node to remove it. To prevent the action from
  317. // accidentally deallocating itself before finishing its step, we retained
  318. // it. Now that step is done, it's safe to release it.
  319. locCurrTarget.currentAction = null;//release
  320. } else if (locCurrTarget.currentAction.isDone()) {
  321. locCurrTarget.currentAction.stop();
  322. var action = locCurrTarget.currentAction;
  323. // Make currentAction nil to prevent removeAction from salvaging it.
  324. locCurrTarget.currentAction = null;
  325. this.removeAction(action);
  326. }
  327. locCurrTarget.currentAction = null;
  328. }
  329. }
  330. // elt, at this moment, is still valid
  331. // so it is safe to ask this here (issue #490)
  332. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  333. if (this._currentTargetSalvaged && locCurrTarget.actions.length === 0) {
  334. this._deleteHashElement(locCurrTarget);
  335. }
  336. }
  337. }
  338. });