1 /**************************************************************************** 2 Copyright (c) 2008-2010 Ricardo Quesada 3 Copyright (c) 2011-2012 cocos2d-x.org 4 Copyright (c) 2013-2014 Chukong Technologies 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 * @param {Number} duration 32 * @param {cc.Size} gridSize 33 */ 34 cc.GridAction = cc.ActionInterval.extend(/** @lends cc.GridAction# */{ 35 _gridSize:null, 36 _gridNodeTarget:null, 37 38 /** 39 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. 40 * @param {Number} duration 41 * @param {cc.Size} gridSize 42 */ 43 ctor:function(duration, gridSize){ 44 cc._checkWebGLRenderMode(); 45 cc.ActionInterval.prototype.ctor.call(this); 46 this._gridSize = cc.size(0,0); 47 48 gridSize && this.initWithDuration(duration, gridSize); 49 }, 50 51 _cacheTargetAsGridNode: function(){}, 52 53 /** 54 * to copy object with deep copy. 55 * returns a clone of action. 56 * 57 * @return {cc.Action} 58 */ 59 clone:function(){ 60 var action = new cc.GridAction(); 61 var locGridSize = this._gridSize; 62 action.initWithDuration(this._duration, cc.size(locGridSize.width, locGridSize.height)); 63 return action; 64 }, 65 66 /** 67 * called before the action start. It will also set the target. 68 * 69 * @param {cc.Node} target 70 */ 71 startWithTarget:function (target) { 72 cc.ActionInterval.prototype.startWithTarget.call(this, target); 73 cc.renderer.childrenOrderDirty = true; 74 var newGrid = this.getGrid(); 75 var t = this.target; 76 var targetGrid = t.grid; 77 if (targetGrid && targetGrid.getReuseGrid() > 0) { 78 var locGridSize = targetGrid.getGridSize(); 79 if (targetGrid.isActive() && (locGridSize.width === this._gridSize.width) && (locGridSize.height === this._gridSize.height)) 80 targetGrid.reuse(); 81 } else { 82 if (targetGrid && targetGrid.isActive()) 83 targetGrid.setActive(false); 84 t.grid = newGrid; 85 t.grid.setActive(true); 86 } 87 }, 88 89 /** 90 * Create a cc.ReverseTime action. Opposite with the original motion trajectory. 91 * @return {cc.ReverseTime} 92 */ 93 reverse:function () { 94 return new cc.ReverseTime(this); 95 }, 96 97 /** 98 * Initializes the action with size and duration. 99 * @param {Number} duration 100 * @param {cc.Size} gridSize 101 * @return {Boolean} 102 */ 103 initWithDuration:function (duration, gridSize) { 104 if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) { 105 this._gridSize.width = gridSize.width; 106 this._gridSize.height = gridSize.height; 107 return true; 108 } 109 return false; 110 }, 111 112 /** 113 * Returns the grid. 114 * @return {cc.GridBase} 115 */ 116 getGrid:function () { 117 // Abstract class needs implementation 118 cc.log("cc.GridAction.getGrid(): it should be overridden in subclass."); 119 } 120 }); 121 122 /** 123 * creates the action with size and duration 124 * @function 125 * @param {Number} duration 126 * @param {cc.Size} gridSize 127 * @return {cc.GridAction} 128 */ 129 cc.gridAction = function (duration, gridSize) { 130 return new cc.GridAction(duration, gridSize); 131 }; 132 133 /** 134 * Please use cc.gridAction instead. <br /> 135 * Creates the action with size and duration. 136 * @param {Number} duration 137 * @param {cc.Size} gridSize 138 * @return {cc.GridAction} 139 * @static 140 * @deprecated since v3.0 <br /> Please use cc.gridAction instead. 141 */ 142 cc.GridAction.create = cc.gridAction; 143 144 /** 145 * Base class for cc.Grid3D actions. <br/> 146 * Grid3D actions can modify a non-tiled grid. 147 * @class 148 * @extends cc.GridAction 149 */ 150 cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.Grid3DAction# */{ 151 152 /** 153 * returns the grid 154 * @return {cc.Grid3D} 155 */ 156 getGrid:function () { 157 return new cc.Grid3D(this._gridSize); 158 }, 159 160 /** 161 * returns the vertex than belongs to certain position in the grid. <br/> 162 * It will be deprecated in future, please use getVertex instead. 163 * @param {cc.Point} position 164 * @return {cc.Vertex3F} 165 */ 166 vertex:function (position) { 167 return this.getVertex(position); 168 }, 169 170 /** 171 * returns the vertex than belongs to certain position in the grid 172 * @param {cc.Point} position 173 * @return {cc.Vertex3F} 174 */ 175 getVertex: function(position){ 176 return this.target.grid.getVertex(position); 177 }, 178 179 /** 180 * returns the non-transformed vertex than belongs to certain position in the grid <br/> 181 * It will be deprecated in future, please use getVertex instead. 182 * @param {cc.Point} position 183 * @return {cc.Vertex3F} 184 */ 185 originalVertex:function (position) { 186 return this.getOriginalVertex(position); 187 }, 188 189 /** 190 * returns the non-transformed vertex than belongs to certain position in the grid 191 * @param {cc.Point} position 192 * @return {cc.Vertex3F} 193 */ 194 getOriginalVertex:function (position) { 195 return this.target.grid.originalVertex(position); 196 }, 197 198 /** 199 * sets a new vertex to a certain position of the grid 200 * @param {cc.Point} position 201 * @param {cc.Vertex3F} vertex 202 */ 203 setVertex:function (position, vertex) { 204 this.target.grid.setVertex(position, vertex); 205 } 206 }); 207 208 /** 209 * creates the action with size and duration 210 * @function 211 * @param {Number} duration 212 * @param {cc.Size} gridSize 213 * @return {cc.Grid3DAction} 214 */ 215 cc.grid3DAction = function (duration, gridSize) { 216 return new cc.Grid3DAction(duration, gridSize); 217 }; 218 /** 219 * Please use cc.grid3DAction instead. <br /> 220 * creates the action with size and duration. <br /> 221 * @param {Number} duration 222 * @param {cc.Size} gridSize 223 * @return {cc.Grid3DAction} 224 * @static 225 * @deprecated since v3.0 <br /> Please use cc.grid3DAction instead. 226 */ 227 cc.Grid3DAction.create = cc.grid3DAction; 228 229 /** 230 * Base class for cc.TiledGrid3D actions. 231 * @class 232 * @extends cc.GridAction 233 */ 234 cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{ 235 236 /** 237 * returns the tile that belongs to a certain position of the grid <br/> 238 * It will be deprecated in future, please use getTile instead. 239 * @param {cc.Point} position 240 * @return {cc.Quad3} 241 */ 242 tile:function (position) { 243 return this.getTile(position); 244 }, 245 246 /** 247 * returns the tile that belongs to a certain position of the grid 248 * @param {cc.Point} position 249 * @return {cc.Quad3} 250 */ 251 getTile:function (position) { 252 return this.target.grid.tile(position); 253 }, 254 255 /** 256 * returns the non-transformed tile that belongs to a certain position of the grid <br/> 257 * It will be deprecated in future, please use getOriginalTile instead. 258 * @param {cc.Point} position 259 * @return {cc.Quad3} 260 */ 261 originalTile:function (position) { 262 return this.getOriginalTile(position); 263 }, 264 265 /** 266 * returns the non-transformed tile that belongs to a certain position of the grid 267 * @param {cc.Point} position 268 * @return {cc.Quad3} 269 */ 270 getOriginalTile:function (position) { 271 return this.target.grid.originalTile(position); 272 }, 273 274 /** 275 * sets a new tile to a certain position of the grid 276 * @param {cc.Point} position 277 * @param {cc.Quad3} coords 278 */ 279 setTile:function (position, coords) { 280 this.target.grid.setTile(position, coords); 281 }, 282 283 /** 284 * returns the grid 285 * @return {cc.TiledGrid3D} 286 */ 287 getGrid:function () { 288 return new cc.TiledGrid3D(this._gridSize); 289 } 290 }); 291 292 /** 293 * Creates the action with duration and grid size 294 * @function 295 * @param {Number} duration 296 * @param {cc.Size} gridSize 297 * @return {cc.TiledGrid3DAction} 298 */ 299 cc.tiledGrid3DAction = function (duration, gridSize) { 300 return new cc.TiledGrid3DAction(duration, gridSize); 301 }; 302 303 /** 304 * Please use cc.tiledGrid3DAction instead 305 * Creates the action with duration and grid size 306 * @param {Number} duration 307 * @param {cc.Size} gridSize 308 * @return {cc.TiledGrid3DAction} 309 * @static 310 * @deprecated since v3.0 <br /> Please use cc.tiledGrid3DAction instead. 311 */ 312 cc.TiledGrid3DAction.create = cc.tiledGrid3DAction; 313 314 /** 315 * <p> 316 * cc.StopGrid action. <br/> 317 * @warning Don't call this action if another grid action is active. <br/> 318 * Call if you want to remove the the grid effect. Example: <br/> 319 * cc.sequence(Lens.action(...), cc.stopGrid(...), null); <br/> 320 * </p> 321 * @class 322 * @extends cc.ActionInstant 323 */ 324 cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{ 325 326 /** 327 * called before the action start. It will also set the target. 328 * 329 * @param {cc.Node} target 330 */ 331 startWithTarget:function (target) { 332 cc.ActionInstant.prototype.startWithTarget.call(this, target); 333 cc.renderer.childrenOrderDirty = true; 334 var grid = this.target.grid; 335 if (grid && grid.isActive()) 336 grid.setActive(false); 337 } 338 }); 339 340 /** 341 * Allocates and initializes the action 342 * @function 343 * @return {cc.StopGrid} 344 */ 345 cc.stopGrid = function () { 346 return new cc.StopGrid(); 347 }; 348 /** 349 * Please use cc.stopGrid instead 350 * Allocates and initializes the action 351 * @return {cc.StopGrid} 352 * @static 353 * @deprecated since v3.0 <br /> Please use cc.stopGrid instead. 354 */ 355 cc.StopGrid.create = cc.stopGrid; 356 357 /** 358 * cc.ReuseGrid action 359 * @class 360 * @extends cc.ActionInstant 361 * @param {Number} times 362 */ 363 cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{ 364 _times:null, 365 366 /** 367 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. 368 * @param {Number} times 369 */ 370 ctor: function(times) { 371 cc.ActionInstant.prototype.ctor.call(this); 372 times !== undefined && this.initWithTimes(times); 373 }, 374 375 /** 376 * initializes an action with the number of times that the current grid will be reused 377 * @param {Number} times 378 * @return {Boolean} 379 */ 380 initWithTimes:function (times) { 381 this._times = times; 382 return true; 383 }, 384 385 /** 386 * called before the action start. It will also set the target. 387 * 388 * @param {cc.Node} target 389 */ 390 startWithTarget:function (target) { 391 cc.ActionInstant.prototype.startWithTarget.call(this, target); 392 cc.renderer.childrenOrderDirty = true; 393 if (this.target.grid && this.target.grid.isActive()) 394 this.target.grid.setReuseGrid(this.target.grid.getReuseGrid() + this._times); 395 } 396 }); 397 398 /** 399 * creates an action with the number of times that the current grid will be reused 400 * @function 401 * @param {Number} times 402 * @return {cc.ReuseGrid} 403 */ 404 cc.reuseGrid = function (times) { 405 return new cc.ReuseGrid(times); 406 }; 407 /** 408 * Please use cc.reuseGrid instead 409 * creates an action with the number of times that the current grid will be reused 410 * @param {Number} times 411 * @return {cc.ReuseGrid} 412 * @static 413 * @deprecated since v3.0 <br /> Please use cc.reuseGrid instead. 414 */ 415 cc.ReuseGrid.create = cc.reuseGrid; 416