1 /****************************************************************************
  2  Copyright (c) 2010-2013 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 /**
 28  * Base class for Grid actions
 29  * @class
 30  * @extends cc.ActionInterval
 31  */
 32 cc.GridAction = cc.ActionInterval.extend(/** @lends cc.GridAction# */{
 33     _gridSize:null,
 34 
 35     ctor:function(){
 36         cc.ActionInterval.prototype.ctor.call(this);
 37         this._gridSize = cc.size(0,0);
 38     },
 39 
 40     clone:function(){
 41         var action = new cc.GridAction();
 42         var locGridSize = this._gridSize;
 43         action.initWithDuration(this._duration, cc.size(locGridSize.width, locGridSize.height));
 44         return action;
 45     },
 46 
 47     startWithTarget:function (target) {
 48         cc.ActionInterval.prototype.startWithTarget.call(this, target);
 49         var newGrid = this.getGrid();
 50         var t = this._target;
 51         var targetGrid = t.getGrid();
 52         if (targetGrid && targetGrid.getReuseGrid() > 0) {
 53             var locGridSize = targetGrid.getGridSize();
 54             if (targetGrid.isActive() && (locGridSize.width == this._gridSize.width) && (locGridSize.height == this._gridSize.height))
 55                 targetGrid.reuse();
 56         } else {
 57             if (targetGrid && targetGrid.isActive())
 58                 targetGrid.setActive(false);
 59             t.setGrid(newGrid);
 60             t.getGrid().setActive(true);
 61         }
 62     },
 63 
 64     reverse:function () {
 65         return cc.ReverseTime.create(this);
 66     },
 67 
 68     /**
 69      * initializes the action with size and duration
 70      * @param {Number} duration
 71      * @param {cc.Size} gridSize
 72      * @return {Boolean}
 73      */
 74     initWithDuration:function (duration, gridSize) {
 75         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 76             this._gridSize.width = gridSize.width;
 77             this._gridSize.height = gridSize.height;
 78             return true;
 79         }
 80         return false;
 81     },
 82 
 83     /**
 84      * returns the grid
 85      * @return {cc.GridBase}
 86      */
 87     getGrid:function () {
 88         // Abstract class needs implementation
 89         cc.log("cc.GridAction.getGrid(): it should be overridden in subclass.");
 90     }
 91 });
 92 
 93 /**
 94  * creates the action with size and duration
 95  * @param {Number} duration
 96  * @param {cc.Size} gridSize
 97  * @return {cc.GridAction}
 98  */
 99 cc.GridAction.create = function (duration, gridSize) {
100     var action = new cc.GridAction();
101     action.initWithDuration(duration, gridSize);
102     return action;
103 };
104 
105 /**
106  * Base class for cc.Grid3D actions.                     <br/>
107  * Grid3D actions can modify a non-tiled grid.
108  * @class
109  * @extends cc.GridAction
110  */
111 cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.GridAction# */{
112     /**
113      * returns the grid
114      * @return {cc.GridBase}
115      */
116     getGrid:function () {
117         return cc.Grid3D.create(this._gridSize);
118     },
119 
120     /**
121      * returns the vertex than belongs to certain position in the grid
122      * @param {cc.Point} position
123      * @return {cc.Vertex3F}
124      */
125     vertex:function (position) {
126         return this._target.getGrid().vertex(position);
127     },
128 
129     /**
130      * returns the non-transformed vertex than belongs to certain position in the grid
131      * @param {cc.Point} position
132      * @return {*}
133      */
134     originalVertex:function (position) {
135         return this._target.getGrid().originalVertex(position);
136     },
137 
138     /**
139      * sets a new vertex to a certain position of the grid
140      * @param {cc.Point} position
141      * @param {cc.Vertex3F} vertex
142      */
143     setVertex:function (position, vertex) {
144         this._target.getGrid().setVertex(position, vertex);
145     }
146 });
147 
148 /**
149  * creates the action with size and duration
150  * @param {Number} duration
151  * @param {cc.Size} gridSize
152  * @return {cc.Grid3DAction}
153  */
154 cc.Grid3DAction.create = function (duration,gridSize) {
155     var action = new cc.Grid3DAction();
156     action.initWithDuration(duration,gridSize);
157     return action;
158 };
159 
160 /**
161  * Base class for cc.TiledGrid3D actions
162  * @class
163  * @extends cc.GridAction
164  */
165 cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{
166     /**
167      * returns the tile that belongs to a certain position of the grid
168      * @param {cc.Point} position
169      * @return {cc.Quad3}
170      */
171     tile:function (position) {
172         return this._target.getGrid().tile(position);
173     },
174 
175     /**
176      * returns the non-transformed tile that belongs to a certain position of the grid
177      * @param {cc.Point} position
178      * @return {cc.Quad3}
179      */
180     originalTile:function (position) {
181         return this._target.getGrid().originalTile(position);
182     },
183 
184     /**
185      * sets a new tile to a certain position of the grid
186      * @param {cc.Point} position
187      * @param {cc.Quad3} coords
188      */
189     setTile:function (position, coords) {
190         this._target.getGrid().setTile(position, coords);
191     },
192 
193     /**
194      * returns the grid
195      * @return {cc.GridBase}
196      */
197     getGrid:function () {
198         return cc.TiledGrid3D.create(this._gridSize);
199     }
200 });
201 
202 /**
203  * creates the action with size and duration
204  * @param {Number} duration
205  * @param {cc.Size} gridSize
206  * @return {cc.TiledGrid3DAction}
207  */
208 cc.TiledGrid3DAction.create = function (duration, gridSize) {
209      var ret = new cc.TiledGrid3DAction();
210     ret.initWithDuration(duration, gridSize);
211     return ret;
212 };
213 
214 /**
215  * <p>
216  * cc.StopGrid action.                                                               <br/>
217  * @warning Don't call this action if another grid action is active.                 <br/>
218  * Call if you want to remove the the grid effect. Example:                          <br/>
219  * cc.Sequence.create(Lens.action(...), cc.StopGrid.create(...), null);              <br/>
220  * </p>
221  * @class
222  * @extends cc.ActionInstant
223  */
224 cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{
225     startWithTarget:function (target) {
226         cc.ActionInstant.prototype.startWithTarget.call(this, target);
227         var grid = this._target.getGrid();
228         if (grid && grid.isActive())
229             grid.setActive(false);
230     }
231 });
232 
233 /**
234  * Allocates and initializes the action
235  * @return {cc.StopGrid}
236  */
237 cc.StopGrid.create = function () {
238     return new cc.StopGrid();
239 };
240 
241 /**
242  * cc.ReuseGrid action
243  * @class
244  * @extends cc.ActionInstant
245  */
246 cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{
247     _times:null,
248 
249     /**
250      * initializes an action with the number of times that the current grid will be reused
251      * @param {Number} times
252      * @return {Boolean}
253      */
254     initWithTimes:function (times) {
255         this._times = times;
256         return true;
257     },
258 
259     startWithTarget:function (target) {
260         cc.ActionInstant.prototype.startWithTarget.call(this, target);
261         if (this._target.getGrid() && this._target.getGrid().isActive())
262             this._target.getGrid().setReuseGrid(this._target.getGrid().getReuseGrid() + this._times);
263     }
264 });
265 
266 /**
267  * creates an action with the number of times that the current grid will be reused
268  * @param {Number} times
269  * @return {cc.ReuseGrid}
270  */
271 cc.ReuseGrid.create = function (times) {
272     return new cc.ReuseGrid();
273 };
274