1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * The Cocostudio's action object.
 28  * @class
 29  * @extends ccs.Class
 30  */
 31 ccs.ActionObject = ccs.Class.extend(/** @lends ccs.ActionObject# */{
 32     _actionNodeList: null,
 33     _name: "",
 34     _loop: false,
 35     _pause: false,
 36     _playing: false,
 37     _unitTime: 0,
 38     _currentTime: 0,
 39     _scheduler:null,
 40     _callback: null,
 41     _fTotalTime: 0,
 42 
 43     /**
 44      * Construction of ccs.ActionObject.
 45      */
 46     ctor: function () {
 47         this._actionNodeList = [];
 48         this._name = "";
 49         this._loop = false;
 50         this._pause = false;
 51         this._playing = false;
 52         this._unitTime = 0.1;
 53         this._currentTime = 0;
 54         this._fTotalTime = 0;
 55         this._scheduler = cc.director.getScheduler();
 56     },
 57 
 58     /**
 59      * Sets name to ccs.ActionObject
 60      * @param {string} name
 61      */
 62     setName: function (name) {
 63         this._name = name;
 64     },
 65 
 66     /**
 67      * Returns name fo ccs.ActionObject
 68      * @returns {string}
 69      */
 70     getName: function () {
 71         return this._name;
 72     },
 73 
 74     /**
 75      * Sets if the action will loop play.
 76      * @param {boolean} loop
 77      */
 78     setLoop: function (loop) {
 79         this._loop = loop;
 80     },
 81 
 82     /**
 83      * Returns if the action will loop play.
 84      * @returns {boolean}
 85      */
 86     getLoop: function () {
 87         return this._loop;
 88     },
 89 
 90     /**
 91      * Sets the time interval of frame.
 92      * @param {number} time
 93      */
 94     setUnitTime: function (time) {
 95         this._unitTime = time;
 96         var frameNum = this._actionNodeList.length;
 97         for (var i = 0; i < frameNum; i++) {
 98             var locActionNode = this._actionNodeList[i];
 99             locActionNode.setUnitTime(this._unitTime);
100         }
101     },
102 
103     /**
104      * Returns the time interval of frame.
105      * @returns {number} the time interval of frame
106      */
107     getUnitTime: function () {
108         return this._unitTime;
109     },
110 
111     /**
112      * Returns the current time of frame.
113      * @returns {number}
114      */
115     getCurrentTime: function () {
116         return this._currentTime;
117     },
118 
119     /**
120      * Sets the current time of frame.
121      * @param {Number} time the current time of frame
122      */
123     setCurrentTime: function (time) {
124         this._currentTime = time;
125     },
126 
127     /**
128      * Returns the total time of frame.
129      * @returns {number} the total time of frame
130      */
131     getTotalTime: function(){
132         return this._fTotalTime;
133     },
134 
135     /**
136      * Returns if the action is playing.
137      * @returns {boolean}  true if the action is playing, false the otherwise
138      */
139     isPlaying: function () {
140         return this._playing;
141     },
142 
143     /**
144      * Init properties with a json dictionary
145      * @param {Object} dic
146      * @param {Object} root
147      */
148     initWithDictionary: function (dic, root) {
149         this.setName(dic["name"]);
150         this.setLoop(dic["loop"]);
151         this.setUnitTime(dic["unittime"]);
152         var actionNodeList = dic["actionnodelist"];
153         var maxLength = 0;
154         for (var i = 0; i < actionNodeList.length; i++) {
155             var actionNode = new ccs.ActionNode();
156 
157             var actionNodeDic = actionNodeList[i];
158             actionNode.initWithDictionary(actionNodeDic, root);
159             actionNode.setUnitTime(this.getUnitTime());
160             this._actionNodeList.push(actionNode);
161             var length = actionNode.getLastFrameIndex() - actionNode.getFirstFrameIndex();
162             if(length > maxLength){
163                 maxLength = length;
164             }
165         }
166         this._fTotalTime = maxLength * this._unitTime;
167     },
168 
169     /**
170      * Adds a ActionNode to play the action.
171      * @param {ccs.ActionNode} node
172      */
173     addActionNode: function (node) {
174         if (!node)
175             return;
176         this._actionNodeList.push(node);
177         node.setUnitTime(this._unitTime);
178     },
179 
180     /**
181      * Removes a ActionNode which play the action.
182      * @param {ccs.ActionNode} node
183      */
184     removeActionNode: function (node) {
185         if (node == null)
186             return;
187         cc.arrayRemoveObject(this._actionNodeList, node);
188     },
189 
190     /**
191      * Plays the action.
192      * @param {cc.CallFunc} [fun]  Action Call Back
193      */
194     play: function (fun) {
195         this.stop();
196         this.updateToFrameByTime(0);
197         var locActionNodeList = this._actionNodeList;
198         var frameNum = locActionNodeList.length;
199         for (var i = 0; i < frameNum; i++) {
200             locActionNodeList[i].playAction(fun);
201         }
202         if (this._loop)
203             this._scheduler.scheduleCallbackForTarget(this, this.simulationActionUpdate, 0, cc.REPEAT_FOREVER, 0, false);
204         if(fun !== undefined)
205             this._callback = fun;
206     },
207 
208     /**
209      * Pauses the action.
210      */
211     pause: function () {
212         this._pause = true;
213     },
214 
215     /**
216      * Stop the action.
217      */
218     stop: function () {
219         var locActionNodeList = this._actionNodeList;
220         for (var i = 0; i < locActionNodeList.length; i++)
221             locActionNodeList[i].stopAction();
222         this._scheduler.unscheduleCallbackForTarget(this, this.simulationActionUpdate);
223         this._pause = false;
224     },
225 
226     /**
227      * Updates frame by time.
228      */
229     updateToFrameByTime: function (time) {
230         this._currentTime = time;
231         for (var i = 0; i < this._actionNodeList.length; i++) {
232             var locActionNode = this._actionNodeList[i];
233             locActionNode.updateActionToTimeLine(time);
234         }
235     },
236 
237     /**
238      * scheduler update function
239      * @param {Number} dt delta time
240      */
241     simulationActionUpdate: function (dt) {
242         var isEnd = true, locNodeList = this._actionNodeList;
243         for(var i = 0, len = locNodeList.length; i < len; i++) {
244             if (!locNodeList[i].isActionDoneOnce()){
245                 isEnd = false;
246                 break;
247             }
248         }
249 
250         if (isEnd){
251             if (this._callback != null)
252                 this._callback.execute();
253             if (this._loop)
254                 this.play();
255             else
256                 this._scheduler.unschedule(this.simulationActionUpdate, this);
257         }
258     }
259 });