1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 var cc = cc = cc || {};
 28 
 29 /** @brief Base class for Grid actions */
 30 cc.GridAction = cc.ActionInterval.extend({
 31     _gridSize:null,
 32     startWithTarget:function (target) {
 33         this._super(target);
 34         var newgrid = this.getGrid();
 35         var t = this._target;
 36         var targetGrid = t.getGrid();
 37         if (targetGrid && targetGrid.getReuseGrid() > 0) {
 38             if (targetGrid.isActive() && targetGrid.getGridSize().x == this._gridSize.x && targetGrid.getGridSize().y == this._gridSize.y) {
 39                 targetGrid.reuse();
 40             }
 41             else {
 42                 cc.Assert(0, "");
 43             }
 44         }
 45         else {
 46             if (targetGrid && targetGrid.isActive()) {
 47                 targetGrid.setActive(false);
 48             }
 49             t.setGrid(newgrid);
 50             t.getGrid().setActive(true);
 51         }
 52     },
 53 
 54     reverse:function () {
 55         return cc.ReverseTime.create(this);
 56     },
 57 
 58     /** initializes the action with size and duration */
 59     initWithSize:function (gridSize, duration) {
 60         if (this.initWithDuration(duration)) {
 61             this._gridSize = gridSize;
 62             return true;
 63         }
 64         return false;
 65     },
 66 
 67     /** returns the grid */
 68     getGrid:function () {
 69         // Abstract class needs implementation
 70         cc.Assert(0, "");
 71         return null;
 72     }
 73 });
 74 
 75 /** creates the action with size and duration */
 76 cc.GridAction.create = function (gridSize, duration) {
 77     var action = new cc.GridAction();
 78     action.initWithSize(gridSize, duration)
 79     return action;
 80 },
 81 
 82 /**
 83  @brief Base class for cc.Grid3D actions.
 84  Grid3D actions can modify a non-tiled grid.
 85  */
 86     cc.Grid3DAction = cc.GridAction.extend({
 87         /** returns the grid */
 88         getGrid:function () {
 89             return cc.Grid3D.create(this._gridSize);
 90         },
 91 
 92         /** returns the vertex than belongs to certain position in the grid */
 93         vertex:function (pos) {
 94             var g = this._target.getGrid();
 95             return g.vertex(pos);
 96         },
 97 
 98         /** returns the non-transformed vertex than belongs to certain position in the grid */
 99         originalVertex:function (pos) {
100             var g = this._target.getGrid();
101             return g.originalVertex(pos);
102         },
103 
104         /** sets a new vertex to a certain position of the grid */
105         setVertex:function (pos, vertex) {
106             var g = this._target.getGrid();
107             g.setVertex(pos, vertex);
108         }
109     });
110 /** creates the action with size and duration */
111 cc.Grid3DAction.create = function () {
112 
113 };
114 /** @brief Base class for cc.TiledGrid3D actions */
115 cc.TiledGrid3DAction = cc.GridAction.extend({
116     /** returns the tile that belongs to a certain position of the grid */
117     tile:function (pos) {
118         var g = this._target.getGrid();
119         return g.tile(pos);
120     },
121 
122     /** returns the non-transformed tile that belongs to a certain position of the grid */
123     originalTile:function (pos) {
124         var g = this._target.getGrid();
125         return g.originalTile(pos);
126     },
127 
128     /** sets a new tile to a certain position of the grid */
129     setTile:function (pos, coords) {
130         var g = this._target.getGrid();
131         return g.setTile(pos, coords);
132     },
133 
134     /** returns the grid */
135     getGrid:function () {
136         return cc.TiledGrid3D.create(this._gridSize);
137     }
138 });
139 
140 /** creates the action with size and duration */
141 cc.TiledGrid3DAction.create = function (gridSize, duration) {
142 
143 };
144 
145 /** @brief cc.AccelDeccelAmplitude action */
146 cc.AccelDeccelAmplitude = cc.ActionInterval.extend({
147     _rate:null,
148     _other:null,
149     /** initializes the action with an inner action that has the amplitude property, and a duration time */
150     initWithAction:function (action, duration) {
151         if (cc.ActionInterval.initWithDuration(duration)) {
152             this._rate = 1.0;
153             this._other = action;
154             return true;
155         }
156         return false;
157     },
158 
159     startWithTarget:function (target) {
160         cc.ActionInterval.startWithTarget(target);
161         this._other.startWithTarget(target);
162     },
163 
164     update:function (time) {
165         var f = time * 2;
166         if (f > 1) {
167             f -= 1;
168             f = 1 - f;
169         }
170         this._other.setAmplitudeRate(Math.pow(f, this._rate));
171     },
172 
173     reverse:function () {
174         return cc.AccelDeccelAmplitude.create(this._other.reverse(), this._duration);
175     },
176 
177     /** get amplitude rate */
178     getRate:function () {
179         return this._rate;
180     },
181 
182     /** set amplitude rate */
183     setRate:function (rate) {
184         this._rate = rate;
185     }
186 });
187 
188 /** creates the action with an inner action that has the amplitude property, and a duration time */
189 cc.AccelDeccelAmplitude.create = function (action, duration) {
190     var ret = new cc.AccelDeccelAmplitude();
191     return ret;
192 };
193 
194 /** @brief cc.AccelAmplitude action */
195 cc.AccelAmplitude = cc.ActionInterval.extend({
196     _rate:null,
197     _other:null,
198     /** initializes the action with an inner action that has the amplitude property, and a duration time */
199     initWithAction:function (action, duration) {
200         if (cc.ActionInterval.initWithDuration(duration)) {
201             this._rate = 1.0;
202             this._other = action;
203 
204             return true;
205         }
206         return false;
207     },
208 
209     /** get amplitude rate */
210     getRate:function () {
211         return this._rate;
212     },
213 
214     /** set amplitude rate */
215     setRate:function (rate) {
216         this._rate = rate;
217     },
218 
219     startWithTarget:function (target) {
220         cc.ActionInterval.startWithTarget(target);
221         this._other.startWithTarget(target);
222     },
223 
224     update:function (time) {
225 
226         this._other.setAmplitudeRate(Math.pow(time, this._rate));
227         this._other.update(time);
228     },
229 
230     reverse:function () {
231         return cc.AccelAmplitude.create(this._other.reverse(), this._duration);
232     }
233 });
234 
235 /** creates the action with an inner action that has the amplitude property, and a duration time */
236 cc.AccelAmplitude.create = function (action, duration) {
237     var ret = new cc.AccelAmplitude();
238     return ret;
239 };
240 
241 /** @brief cc.DeccelAmplitude action */
242 cc.DeccelAmplitude = cc.ActionInterval.extend({
243     _rate:null,
244     _other:null,
245     /** initializes the action with an inner action that has the amplitude property, and a duration time */
246     initWithAction:function (action, duration) {
247         if (cc.ActionInterval.initWithDuration(duration)) {
248             this._rate = 1.0;
249             this._other = action;
250             return true;
251         }
252 
253         return false;
254     },
255 
256     /** get amplitude rate */
257     getRate:function () {
258         return this._rate;
259     },
260 
261     /** set amplitude rate */
262     setRate:function (rate) {
263         this._rate = rate;
264     },
265 
266     startWithTarget:function (target) {
267         cc.ActionInterval.startWithTarget(target);
268         this._other.startWithTarget(target);
269     },
270 
271     update:function (time) {
272         this._other.setAmplitudeRate(Math.pow((1 - time), this._rate));
273         this._other.update(time);
274     },
275 
276     reverse:function () {
277         return cc.DeccelAmplitude.create(this._other.reverse(), this._duration);
278     }
279 });
280 
281 /** creates the action with an inner action that has the amplitude property, and a duration time */
282 cc.DeccelAmplitude.create = function (action, duration) {
283     var ret = new cc.DeccelAmplitude();
284     return ret;
285 };
286 
287 /** @brief cc.StopGrid action.
288  @warning Don't call this action if another grid action is active.
289  Call if you want to remove the the grid effect. Example:
290  cc.Sequence.create(Lens.action(...), cc.StopGrid.create(...), null);
291  */
292 cc.StopGrid = cc.ActionInstant.extend({
293     startWithTarget:function (target) {
294         this._super(target);
295         var grid = this._target.getGrid();
296         if (grid && grid.isActive()) {
297             grid.setActive(false);
298         }
299     }
300 });
301 
302 /** Allocates and initializes the action */
303 cc.StopGrid.create = function () {
304     var action = new cc.StopGrid();
305     return action;
306 };
307 
308 /** @brief cc.ReuseGrid action */
309 cc.ReuseGrid = cc.ActionInstant.extend({
310     _times:null,
311     /** initializes an action with the number of times that the current grid will be reused */
312     initWithTimes:function (times) {
313         this._times = times;
314 
315         return true;
316     },
317 
318     startWithTarget:function (target) {
319         cc.ActionInstant.startWithTarget(target);
320 
321         if (this._target.getGrid() && this._target.getGrid().isActive()) {
322             this._target.getGrid().setReuseGrid(this._target.getGrid().getReuseGrid() + this._times);
323         }
324     }
325 
326 });
327 
328 /** creates an action with the number of times that the current grid will be reused */
329 cc.ReuseGrid.create = function (times) {
330     var action = new cc.ReuseGrid();
331     return action;
332 };
333