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