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  * Base class for ccs.ActionObject
 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     _fTotalTime: 0,
 41     ctor: function () {
 42         this._actionNodeList = [];
 43         this._name = "";
 44         this._loop = false;
 45         this._pause = false;
 46         this._playing = false;
 47         this._unitTime = 0.1;
 48         this._currentTime = 0;
 49         this._fTotalTime = 0;
 50         this._scheduler = new cc.Scheduler();
 51         cc.director.getScheduler().scheduleUpdateForTarget(this._scheduler, 0, false);
 52     },
 53 
 54     /**
 55      * Sets name for object
 56      * @param {string} name
 57      */
 58     setName: function (name) {
 59         this._name = name;
 60     },
 61 
 62     /**
 63      * Gets name for object
 64      * @returns {string}
 65      */
 66     getName: function () {
 67         return this._name;
 68     },
 69 
 70     /**
 71      * Sets if the action will loop play.
 72      * @param {boolean} loop
 73      */
 74     setLoop: function (loop) {
 75         this._loop = loop;
 76     },
 77 
 78     /**
 79      * Gets if the action will loop play.
 80      * @returns {boolean}
 81      */
 82     getLoop: function () {
 83         return this._loop;
 84     },
 85 
 86     /**
 87      * Sets the time interval of frame.
 88      * @param {number} time
 89      */
 90     setUnitTime: function (time) {
 91         this._unitTime = time;
 92         var frameNum = this._actionNodeList.length;
 93         for (var i = 0; i < frameNum; i++) {
 94             var locActionNode = this._actionNodeList[i];
 95             locActionNode.setUnitTime(this._unitTime);
 96         }
 97     },
 98 
 99     /**
100      * Gets the time interval of frame.
101      * @returns {number}
102      */
103     getUnitTime: function () {
104         return this._unitTime;
105     },
106 
107     /**
108      * Gets the current time of frame.
109      * @returns {number}
110      */
111     getCurrentTime: function () {
112         return this._currentTime;
113     },
114 
115     /**
116      * Sets the current time of frame.
117      * @param time
118      */
119     setCurrentTime: function (time) {
120         this._currentTime = time;
121     },
122 
123     getTotalTime: function(){
124         return this._fTotalTime;
125     },
126 
127     /**
128      * Return if the action is playing.
129      * @returns {boolean}
130      */
131     isPlaying: function () {
132         return this._playing;
133     },
134 
135     /**
136      * Init properties with a json dictionary
137      * @param {Object} dic
138      * @param {Object} root
139      */
140     initWithDictionary: function (dic, root) {
141         this.setName(dic["name"]);
142         this.setLoop(dic["loop"]);
143         this.setUnitTime(dic["unittime"]);
144         var actionNodeList = dic["actionnodelist"];
145         var maxLength = 0;
146         for (var i = 0; i < actionNodeList.length; i++) {
147             var actionNode = new ccs.ActionNode();
148 
149             var actionNodeDic = actionNodeList[i];
150             actionNode.initWithDictionary(actionNodeDic, root);
151             actionNode.setUnitTime(this.getUnitTime());
152             this._actionNodeList.push(actionNode);
153             var length = actionNode.getLastFrameIndex() - actionNode.getFirstFrameIndex();
154             if(length > maxLength){
155                 maxLength = length;
156             }
157         }
158         this._fTotalTime = maxLength * this._unitTime;
159     },
160 
161     /**
162      * Adds a ActionNode to play the action.
163      * @param {ccs.ActionNode} node
164      */
165     addActionNode: function (node) {
166         if (!node) {
167             return;
168         }
169         this._actionNodeList.push(node);
170         node.setUnitTime(this._unitTime);
171     },
172 
173     /**
174      * Removes a ActionNode which play the action.
175      * @param {ccs.ActionNode} node
176      */
177     removeActionNode: function (node) {
178         if (node == null) {
179             return;
180         }
181         cc.arrayRemoveObject(this._actionNodeList, node);
182     },
183 
184     /**
185      * Play the action.
186      * @param {cc.CallFunc} fun
187      */
188     play: function (fun) {
189         this.stop();
190         this.updateToFrameByTime(0);
191         var frameNum = this._actionNodeList.length;
192         for (var i = 0; i < frameNum; i++) {
193             var locActionNode = this._actionNodeList[i];
194             locActionNode.playAction(fun);
195         }
196         if (this._loop) {
197             this._scheduler.scheduleCallbackForTarget(this, this.simulationActionUpdate, 0, cc.REPEAT_FOREVER, 0, false);
198     }
199     },
200 
201     /**
202      * pause the action.
203      */
204     pause: function () {
205         this._pause = true;
206     },
207 
208     /**
209      * stop the action.
210      */
211     stop: function () {
212         for (var i = 0; i < this._actionNodeList.length; i++) {
213             var locActionNode = this._actionNodeList[i];
214             locActionNode.stopAction();
215         }
216         this._scheduler.unscheduleCallbackForTarget(this, this.simulationActionUpdate);
217         this._pause = false;
218     },
219 
220     /**
221      * Method of update frame .
222      */
223     updateToFrameByTime: function (time) {
224         this._currentTime = time;
225         for (var i = 0; i < this._actionNodeList.length; i++) {
226             var locActionNode = this._actionNodeList[i];
227             locActionNode.updateActionToTimeLine(time);
228         }
229     },
230     simulationActionUpdate: function (dt) {
231         if (this._loop) {
232             var isEnd = true;
233             var actionNodeList = this._actionNodeList;
234             for (var i = 0; i < actionNodeList.length; i++) {
235                 var actionNode = actionNodeList[i];
236                 if (actionNode.isActionDoneOnce() == false) {
237                     isEnd = false;
238                     break;
239                 }
240             }
241             if (isEnd) {
242                 this.play();
243             }
244         }
245     }
246 });