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