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 /** cc.TurnOffTiles action.<br/> 28 * Turn off the files in random order 29 * @class 30 * @extends cc.TiledGrid3DAction 31 */ 32 cc.TurnOffTiles = cc.TiledGrid3DAction.extend(/** @lends cc.TurnOffTiles# */{ 33 _seed:null, 34 _tilesCount:0, 35 _tilesOrder:[], 36 37 /** initializes the action with a random seed, the grid size and the duration 38 * @param {cc.GridSize} gridSize 39 * @param {Number} duration 40 * @param {Number} seed 41 * @return {Boolean} 42 */ 43 initWithSeed:function ( gridSize, duration, seed) { 44 if (this.initWithSize(gridSize, duration)) { 45 this._seed = seed; 46 this._tilesOrder = null; 47 48 return true; 49 } 50 51 return false; 52 }, 53 54 /** 55 * @param {Array} array 56 * @param {Number} len 57 */ 58 shuffle:function (array, len) { 59 var i; 60 for (i = len - 1; i >= 0; i--) { 61 var j = parseInt(Math.random() * (i + 1)); 62 var v = array[i]; 63 array[i] = array[j]; 64 array[j] = v; 65 } 66 }, 67 68 /** 69 * @param {cc.GridSize} pos 70 */ 71 turnOnTile:function (pos) { 72 this.setTile(pos, this.originalTile(pos)); 73 }, 74 75 /** 76 * @param {cc.GridSize}pos 77 */ 78 turnOffTile:function (pos) { 79 var coords = new cc.Quad3(); 80 this.setTile(pos, coords); 81 }, 82 83 /** 84 * @param {cc.Node} target 85 */ 86 startWithTarget:function (target) { 87 var i; 88 89 this._super(target); 90 91 if (this._seed != -1) { 92 parseInt(Math.random() * this._seed); 93 } 94 this._tilesCount = this._gridSize.x * this._gridSize.y; 95 this._tilesOrder = []; 96 97 for (i = 0; i < this._tilesCount; ++i) { 98 this._tilesOrder[i] = i; 99 } 100 101 this.shuffle(this._tilesOrder, this._tilesCount); 102 }, 103 104 /** 105 * @param {Number} time 106 */ 107 update:function (time) { 108 var i, l, t; 109 110 l = time * this._tilesCount; 111 112 for (i = 0; i < this._tilesCount; i++) { 113 t = this._tilesOrder[i]; 114 var tilePos = cc.g(t / this._gridSize.y, t % this._gridSize.y); 115 116 if (i < l) { 117 this.turnOffTile(tilePos); 118 } 119 else { 120 this.turnOnTile(tilePos); 121 } 122 } 123 } 124 125 }); 126 127 /** 128 * @param {cc.GridSize}gridSize 129 * @param {Number} duration 130 * @param {Number|Null} seed 131 * @return {cc.TurnOffTiles} 132 * @example 133 * // example 134 * // turnOffTiles without seed 135 * var toff = cc.TurnOffTiles.create(cc.g(x, y), this._duration); 136 * 137 * // turnOffTiles with seed 138 * var toff = cc.TurnOffTiles.create(cc.g(x, y), this._duration, 0); 139 */ 140 cc.TurnOffTiles.create = function (gridSize, duration, seed) { 141 var action = new cc.TurnOffTiles(); 142 if(arguments.length == 2) { 143 /** creates the action with the grid size and the duration */ 144 action.initWithSize(gridSize, duration); 145 } else if (arguments.length == 3) { 146 /** creates the action with the grid size, the duration and a random seed */ 147 action.initWithSeed(gridSize, duration, seed); 148 } 149 return action; 150 }; 151