CCActionTiledGrid.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * cc.ShakyTiles3D action
  24. * @class
  25. * @extends cc.TiledGrid3DAction
  26. */
  27. cc.ShakyTiles3D = cc.TiledGrid3DAction.extend(/** @lends cc.ShakyTiles3D# */{
  28. _randRange:0,
  29. _shakeZ:false,
  30. ctor:function () {
  31. cc.GridAction.prototype.ctor.call(this);
  32. this._randRange = 0;
  33. this._shakeZ = false;
  34. },
  35. /**
  36. * initializes the action with a range, whether or not to shake Z vertices, a grid size, and duration
  37. * @param {Number} duration
  38. * @param {cc.Size} gridSize
  39. * @param {Number} range
  40. * @param {Boolean} shakeZ
  41. * @return {Boolean}
  42. */
  43. initWithDuration:function (duration, gridSize, range, shakeZ) {
  44. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  45. this._randRange = range;
  46. this._shakeZ = shakeZ;
  47. return true;
  48. }
  49. return false;
  50. },
  51. update:function (time) {
  52. var locGridSize = this._gridSize, locRandRange = this._randRange;
  53. var locPos = cc.p(0, 0);
  54. for (var i = 0; i < locGridSize.width; ++i) {
  55. for (var j = 0; j < locGridSize.height; ++j) {
  56. locPos.x = i;
  57. locPos.y = j;
  58. var coords = this.originalTile(locPos);
  59. // X
  60. coords.bl.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  61. coords.br.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  62. coords.tl.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  63. coords.tr.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  64. // Y
  65. coords.bl.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  66. coords.br.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  67. coords.tl.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  68. coords.tr.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  69. if (this._shakeZ) {
  70. coords.bl.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  71. coords.br.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  72. coords.tl.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  73. coords.tr.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  74. }
  75. this.setTile(locPos, coords);
  76. }
  77. }
  78. }
  79. });
  80. /**
  81. * creates the action with a range, whether or not to shake Z vertices, a grid size, and duration
  82. * @param {Number} duration
  83. * @param {cc.Size} gridSize
  84. * @param {Number} range
  85. * @param {Boolean} shakeZ
  86. * @return {cc.ShakyTiles3D}
  87. */
  88. cc.ShakyTiles3D.create = function (duration, gridSize, range, shakeZ) {
  89. var action = new cc.ShakyTiles3D();
  90. action.initWithDuration(duration, gridSize, range, shakeZ);
  91. return action;
  92. };
  93. /**
  94. * cc.ShatteredTiles3D action
  95. * @class
  96. * @extends cc.TiledGrid3DAction
  97. */
  98. cc.ShatteredTiles3D = cc.TiledGrid3DAction.extend(/** @lends cc.ShatteredTiles3D# */{
  99. _randRange:0,
  100. _once:false,
  101. _shatterZ:false,
  102. ctor:function () {
  103. cc.GridAction.prototype.ctor.call(this);
  104. this._randRange = 0;
  105. this._shakeZ = false;
  106. this._once = false;
  107. },
  108. /**
  109. * initializes the action with a range, whether or not to shatter Z vertices, a grid size and duration
  110. * @param {Number} duration
  111. * @param {cc.Size} gridSize
  112. * @param {Number} range
  113. * @param {Boolean} shatterZ
  114. * @return {Boolean}
  115. */
  116. initWithDuration:function (duration, gridSize, range, shatterZ) {
  117. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  118. this._once = false;
  119. this._randRange = range;
  120. this._shatterZ = shatterZ;
  121. return true;
  122. }
  123. return false;
  124. },
  125. update:function (time) {
  126. if (this._once === false) {
  127. var locGridSize = this._gridSize, locRandRange = this._randRange;
  128. var coords, locPos = cc.p(0, 0);
  129. for (var i = 0; i < locGridSize.width; ++i) {
  130. for (var j = 0; j < locGridSize.height; ++j) {
  131. locPos.x = i;
  132. locPos.y = j;
  133. coords = this.originalTile(locPos);
  134. // X
  135. coords.bl.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  136. coords.br.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  137. coords.tl.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  138. coords.tr.x += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  139. // Y
  140. coords.bl.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  141. coords.br.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  142. coords.tl.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  143. coords.tr.y += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  144. if (this._shatterZ) {
  145. coords.bl.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  146. coords.br.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  147. coords.tl.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  148. coords.tr.z += ( cc.rand() % (locRandRange * 2) ) - locRandRange;
  149. }
  150. this.setTile(locPos, coords);
  151. }
  152. }
  153. this._once = true;
  154. }
  155. }
  156. });
  157. /**
  158. * creates the action with a range, whether of not to shatter Z vertices, a grid size and duration
  159. * @param {Number} duration
  160. * @param {cc.Size} gridSize
  161. * @param {Number} range
  162. * @param {Boolean} shatterZ
  163. * @return {cc.ShatteredTiles3D}
  164. */
  165. cc.ShatteredTiles3D.create = function (duration, gridSize, range, shatterZ) {
  166. var action = new cc.ShatteredTiles3D();
  167. action.initWithDuration(duration, gridSize, range, shatterZ);
  168. return action;
  169. };
  170. /**
  171. * A Tile composed of position, startPosition and delta
  172. * @Class
  173. * @constructor
  174. * @param {cc.Point} [position=cc.Point_ZERO]
  175. * @param {cc.Point} [startPosition=cc.Point_ZERO]
  176. * @param {cc.Size} [delta=cc.Point_ZERO]
  177. */
  178. cc.Tile = function (position, startPosition, delta) {
  179. this.position = position || cc.POINT_ZERO;
  180. this.startPosition = startPosition || cc.POINT_ZERO;
  181. this.delta = delta || cc.POINT_ZERO;
  182. };
  183. /**
  184. * cc.ShuffleTiles action, Shuffle the tiles in random order
  185. * @class
  186. * @extends cc.TiledGrid3DAction
  187. */
  188. cc.ShuffleTiles = cc.TiledGrid3DAction.extend(/** @lends cc.ShuffleTiles# */{
  189. _seed:0,
  190. _tilesCount:0,
  191. _tilesOrder:null,
  192. _tiles:null,
  193. ctor:function () {
  194. cc.GridAction.prototype.ctor.call(this);
  195. this._tilesOrder = [];
  196. this._tiles = [];
  197. this._seed = 0;
  198. this._tilesCount = 0;
  199. },
  200. /**
  201. * initializes the action with a random seed, the grid size and the duration
  202. * @param {Number} duration
  203. * @param {cc.Size} gridSize
  204. * @param {Number} seed
  205. * @return {Boolean}
  206. */
  207. initWithDuration:function (duration, gridSize, seed) {
  208. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  209. this._seed = seed;
  210. this._tilesOrder.length = 0;
  211. this._tiles.length = 0;
  212. return true;
  213. }
  214. return false;
  215. },
  216. /**
  217. * shuffle
  218. * @param {Array} array
  219. * @param {Number} len
  220. */
  221. shuffle:function (array, len) {
  222. for (var i = len - 1; i >= 0; i--) {
  223. var j = 0 | (cc.rand() % (i + 1));
  224. var v = array[i];
  225. array[i] = array[j];
  226. array[j] = v;
  227. }
  228. },
  229. /**
  230. * get Delta
  231. * @param {cc.Size} pos
  232. */
  233. getDelta:function (pos) {
  234. var locGridSize = this._gridSize;
  235. var idx = pos.width * locGridSize.height + pos.height;
  236. return cc.size(((this._tilesOrder[idx] / locGridSize.height) - pos.width),
  237. ((this._tilesOrder[idx] % locGridSize.height) - pos.height));
  238. },
  239. /**
  240. * place Tile
  241. * @param {cc.Point} pos
  242. * @param {cc.Tile} tile
  243. */
  244. placeTile:function (pos, tile) {
  245. var coords = this.originalTile(pos);
  246. var step = this._target.getGrid().getStep();
  247. var locPosition = tile.position;
  248. coords.bl.x += (locPosition.x * step.x);
  249. coords.bl.y += (locPosition.y * step.y);
  250. coords.br.x += (locPosition.x * step.x);
  251. coords.br.y += (locPosition.y * step.y);
  252. coords.tl.x += (locPosition.x * step.x);
  253. coords.tl.y += (locPosition.y * step.y);
  254. coords.tr.x += (locPosition.x * step.x);
  255. coords.tr.y += (locPosition.y * step.y);
  256. this.setTile(pos, coords);
  257. },
  258. /**
  259. * start with target
  260. * @param {cc.Node} target
  261. */
  262. startWithTarget:function (target) {
  263. cc.TiledGrid3DAction.prototype.startWithTarget.call(this, target);
  264. var locGridSize = this._gridSize;
  265. this._tilesCount = locGridSize.width * locGridSize.height;
  266. var locTilesOrder = this._tilesOrder;
  267. locTilesOrder.length = 0;
  268. /**
  269. * Use k to loop. Because m_nTilesCount is unsigned int,
  270. * and i is used later for int.
  271. */
  272. for (var k = 0; k < this._tilesCount; ++k)
  273. locTilesOrder[k] = k;
  274. this.shuffle(locTilesOrder, this._tilesCount);
  275. var locTiles = this._tiles ;
  276. locTiles.length = 0;
  277. var tileIndex = 0, tempSize = cc.size(0,0);
  278. for (var i = 0; i < locGridSize.width; ++i) {
  279. for (var j = 0; j < locGridSize.height; ++j) {
  280. locTiles[tileIndex] = new cc.Tile();
  281. locTiles[tileIndex].position = cc.p(i, j);
  282. locTiles[tileIndex].startPosition = cc.p(i, j);
  283. tempSize.width = i;
  284. tempSize.height = j;
  285. locTiles[tileIndex].delta = this.getDelta(tempSize);
  286. ++tileIndex;
  287. }
  288. }
  289. },
  290. update:function (time) {
  291. var tileIndex = 0, locGridSize = this._gridSize, locTiles = this._tiles;
  292. var selTile, locPos = cc.p(0, 0);
  293. for (var i = 0; i < locGridSize.width; ++i) {
  294. for (var j = 0; j < locGridSize.height; ++j) {
  295. locPos.x = i;
  296. locPos.y = j;
  297. selTile = locTiles[tileIndex];
  298. selTile.position.x = selTile.delta.width * time;
  299. selTile.position.y = selTile.delta.height * time;
  300. this.placeTile(locPos, selTile);
  301. ++tileIndex;
  302. }
  303. }
  304. }
  305. });
  306. /**
  307. * creates the action with a random seed, the grid size and the duration
  308. * @param {Number} duration
  309. * @param {cc.Size} gridSize
  310. * @param {Number} seed
  311. * @return {cc.ShuffleTiles}
  312. */
  313. cc.ShuffleTiles.create = function (duration, gridSize, seed) {
  314. var action = new cc.ShuffleTiles();
  315. action.initWithDuration(duration, gridSize, seed);
  316. return action;
  317. };
  318. /**
  319. * cc.FadeOutTRTiles action. Fades out the tiles in a Top-Right direction
  320. * @class
  321. * @extends cc.TiledGrid3DAction
  322. */
  323. cc.FadeOutTRTiles = cc.TiledGrid3DAction.extend(/** @lends cc.FadeOutTRTiles# */{
  324. /**
  325. * @param {cc.Size} pos
  326. * @param {Number} time
  327. */
  328. testFunc:function (pos, time) {
  329. var locX = this._gridSize.width * time;
  330. var locY = this._gridSize.height * time;
  331. if ((locX + locY) == 0.0)
  332. return 1.0;
  333. return Math.pow((pos.width + pos.height) / (locX + locY), 6);
  334. },
  335. /**
  336. * turn on Tile
  337. * @param {cc.Point} pos
  338. */
  339. turnOnTile:function (pos) {
  340. this.setTile(pos, this.originalTile(pos));
  341. },
  342. /**
  343. * turn Off Tile
  344. * @param {cc.Point} pos
  345. */
  346. turnOffTile:function (pos) {
  347. this.setTile(pos, new cc.Quad3());
  348. },
  349. /**
  350. * transform tile
  351. * @param {cc.Point} pos
  352. * @param {Number} distance
  353. */
  354. transformTile:function (pos, distance) {
  355. var coords = this.originalTile(pos);
  356. var step = this._target.getGrid().getStep();
  357. coords.bl.x += (step.x / 2) * (1.0 - distance);
  358. coords.bl.y += (step.y / 2) * (1.0 - distance);
  359. coords.br.x -= (step.x / 2) * (1.0 - distance);
  360. coords.br.y += (step.y / 2) * (1.0 - distance);
  361. coords.tl.x += (step.x / 2) * (1.0 - distance);
  362. coords.tl.y -= (step.y / 2) * (1.0 - distance);
  363. coords.tr.x -= (step.x / 2) * (1.0 - distance);
  364. coords.tr.y -= (step.y / 2) * (1.0 - distance);
  365. this.setTile(pos, coords);
  366. },
  367. update:function (time) {
  368. var locGridSize = this._gridSize;
  369. var locPos = cc.p(0, 0), locSize = cc.size(0, 0), distance;
  370. for (var i = 0; i < locGridSize.width; ++i) {
  371. for (var j = 0; j < locGridSize.height; ++j) {
  372. locPos.x = i;
  373. locPos.y = j;
  374. locSize.width = i;
  375. locSize.height = j;
  376. distance = this.testFunc(locSize, time);
  377. if (distance == 0)
  378. this.turnOffTile(locPos);
  379. else if (distance < 1)
  380. this.transformTile(locPos, distance);
  381. else
  382. this.turnOnTile(locPos);
  383. }
  384. }
  385. }
  386. });
  387. /**
  388. * creates the action with the grid size and the duration
  389. * @param duration
  390. * @param gridSize
  391. * @return {cc.FadeOutTRTiles}
  392. */
  393. cc.FadeOutTRTiles.create = function (duration, gridSize) {
  394. var action = new cc.FadeOutTRTiles();
  395. action.initWithDuration(duration, gridSize);
  396. return action;
  397. };
  398. /**
  399. * cc.FadeOutBLTiles action. Fades out the tiles in a Bottom-Left direction
  400. * @class
  401. * @extends cc.FadeOutTRTiles
  402. */
  403. cc.FadeOutBLTiles = cc.FadeOutTRTiles.extend(/** @lends cc.FadeOutBLTiles# */{
  404. /**
  405. * @param {cc.Size} pos
  406. * @param {Number} time
  407. */
  408. testFunc:function (pos, time) {
  409. var locX = this._gridSize.width * (1.0 - time);
  410. var locY = this._gridSize.height * (1.0 - time);
  411. if ((pos.width + pos.height) == 0)
  412. return 1.0;
  413. return Math.pow((locX + locY) / (pos.width + pos.height), 6);
  414. }
  415. });
  416. /**
  417. * creates the action with the grid size and the duration
  418. * @param duration
  419. * @param gridSize
  420. * @return {cc.FadeOutBLTiles}
  421. */
  422. cc.FadeOutBLTiles.create = function (duration, gridSize) {
  423. var action = new cc.FadeOutBLTiles();
  424. action.initWithDuration(duration, gridSize);
  425. return action;
  426. };
  427. /**
  428. * cc.FadeOutUpTiles action. Fades out the tiles in upwards direction
  429. * @class
  430. * @extends cc.FadeOutTRTiles
  431. */
  432. cc.FadeOutUpTiles = cc.FadeOutTRTiles.extend(/** @lends cc.FadeOutUpTiles# */{
  433. testFunc:function (pos, time) {
  434. var locY = this._gridSize.height * time;
  435. if (locY == 0.0)
  436. return 1.0;
  437. return Math.pow(pos.height / locY, 6);
  438. },
  439. transformTile:function (pos, distance) {
  440. var coords = this.originalTile(pos);
  441. var step = this._target.getGrid().getStep();
  442. coords.bl.y += (step.y / 2) * (1.0 - distance);
  443. coords.br.y += (step.y / 2) * (1.0 - distance);
  444. coords.tl.y -= (step.y / 2) * (1.0 - distance);
  445. coords.tr.y -= (step.y / 2) * (1.0 - distance);
  446. this.setTile(pos, coords);
  447. }
  448. });
  449. /**
  450. * creates the action with the grid size and the duration
  451. * @param {Number} duration
  452. * @param {cc.Size} gridSize
  453. * @return {cc.FadeOutUpTiles}
  454. */
  455. cc.FadeOutUpTiles.create = function (duration, gridSize) {
  456. var action = new cc.FadeOutUpTiles();
  457. action.initWithDuration(duration, gridSize);
  458. return action;
  459. };
  460. /**
  461. * cc.FadeOutDownTiles action. Fades out the tiles in downwards direction
  462. * @class
  463. * @extends cc.FadeOutUpTiles
  464. */
  465. cc.FadeOutDownTiles = cc.FadeOutUpTiles.extend(/** @lends cc.FadeOutDownTiles# */{
  466. testFunc:function (pos, time) {
  467. var locY = this._gridSize.height * (1.0 - time);
  468. if (pos.height == 0)
  469. return 1.0;
  470. return Math.pow(locY / pos.height, 6);
  471. }
  472. });
  473. /**
  474. * creates the action with the grid size and the duration
  475. * @param {Number} duration
  476. * @param {cc.Size} gridSize
  477. * @return {cc.FadeOutDownTiles}
  478. */
  479. cc.FadeOutDownTiles.create = function (duration, gridSize) {
  480. var action = new cc.FadeOutDownTiles();
  481. action.initWithDuration(duration, gridSize);
  482. return action;
  483. };
  484. /**
  485. * cc.TurnOffTiles action.<br/>
  486. * Turn off the files in random order
  487. * @class
  488. * @extends cc.TiledGrid3DAction
  489. */
  490. cc.TurnOffTiles = cc.TiledGrid3DAction.extend(/** @lends cc.TurnOffTiles# */{
  491. _seed:null,
  492. _tilesCount:0,
  493. _tilesOrder:null,
  494. ctor:function () {
  495. cc.GridAction.prototype.ctor.call(this);
  496. this._tilesOrder = [];
  497. this._seed = null;
  498. this._tilesCount = 0;
  499. },
  500. /** initializes the action with a random seed, the grid size and the duration
  501. * @param {Number} duration
  502. * @param {cc.Size} gridSize
  503. * @param {Number} seed
  504. * @return {Boolean}
  505. */
  506. initWithDuration:function (duration, gridSize, seed) {
  507. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  508. this._seed = seed;
  509. this._tilesOrder.length = 0;
  510. return true;
  511. }
  512. return false;
  513. },
  514. /**
  515. * @param {Array} array
  516. * @param {Number} len
  517. */
  518. shuffle:function (array, len) {
  519. for (var i = len - 1; i >= 0; i--) {
  520. var j = 0 | (cc.rand() % (i + 1));
  521. var v = array[i];
  522. array[i] = array[j];
  523. array[j] = v;
  524. }
  525. },
  526. /**
  527. * @param {cc.Point} pos
  528. */
  529. turnOnTile:function (pos) {
  530. this.setTile(pos, this.originalTile(pos));
  531. },
  532. /**
  533. * @param {cc.Point} pos
  534. */
  535. turnOffTile:function (pos) {
  536. this.setTile(pos, new cc.Quad3());
  537. },
  538. /**
  539. * @param {cc.Node} target
  540. */
  541. startWithTarget:function (target) {
  542. cc.TiledGrid3DAction.prototype.startWithTarget.call(this, target);
  543. this._tilesCount = this._gridSize.width * this._gridSize.height;
  544. var locTilesOrder = this._tilesOrder;
  545. locTilesOrder.length = 0;
  546. for (var i = 0; i < this._tilesCount; ++i)
  547. locTilesOrder[i] = i;
  548. this.shuffle(locTilesOrder, this._tilesCount);
  549. },
  550. /**
  551. * @param {Number} time
  552. */
  553. update:function (time) {
  554. var l = 0 | (time * this._tilesCount), locGridSize = this._gridSize;
  555. var t,tilePos = cc.p(0,0), locTilesOrder = this._tilesOrder;
  556. for (var i = 0; i < this._tilesCount; i++) {
  557. t = locTilesOrder[i];
  558. tilePos.x = 0 | (t / locGridSize.height);
  559. tilePos.y = t % (0 | locGridSize.height);
  560. if (i < l)
  561. this.turnOffTile(tilePos);
  562. else
  563. this.turnOnTile(tilePos);
  564. }
  565. }
  566. });
  567. /**
  568. * creates the action with a random seed, the grid size and the duration
  569. * @param {Number} duration
  570. * @param {cc.Size} gridSize
  571. * @param {Number|Null} [seed=0]
  572. * @return {cc.TurnOffTiles}
  573. * @example
  574. * // example
  575. * // turnOffTiles without seed
  576. * var toff = cc.TurnOffTiles.create(this._duration, cc.size(x, y));
  577. *
  578. * // turnOffTiles with seed
  579. * var toff = cc.TurnOffTiles.create(this._duration, cc.size(x, y), 0);
  580. */
  581. cc.TurnOffTiles.create = function (duration, gridSize, seed) {
  582. seed = seed || 0;
  583. var action = new cc.TurnOffTiles();
  584. action.initWithDuration(duration, gridSize, seed);
  585. return action;
  586. };
  587. /**
  588. * cc.WavesTiles3D action.
  589. * @class
  590. * @extends cc.TiledGrid3DAction
  591. */
  592. cc.WavesTiles3D = cc.TiledGrid3DAction.extend(/** @lends cc.WavesTiles3D# */{
  593. _waves:0,
  594. _amplitude:0,
  595. _amplitudeRate:0,
  596. ctor:function () {
  597. cc.GridAction.prototype.ctor.call(this);
  598. this._waves = 0;
  599. this._amplitude = 0;
  600. this._amplitudeRate = 0;
  601. },
  602. /**
  603. * get amplitude of waves
  604. * @return {Number}
  605. */
  606. getAmplitude:function () {
  607. return this._amplitude;
  608. },
  609. /**
  610. * set amplitude of waves
  611. * @param {Number} amplitude
  612. */
  613. setAmplitude:function (amplitude) {
  614. this._amplitude = amplitude;
  615. },
  616. /**
  617. * get amplitude rate of waves
  618. * @return {Number}
  619. */
  620. getAmplitudeRate:function () {
  621. return this._amplitudeRate;
  622. },
  623. /**
  624. * set amplitude rate of waves
  625. * @param {Number} amplitudeRate
  626. */
  627. setAmplitudeRate:function (amplitudeRate) {
  628. this._amplitudeRate = amplitudeRate;
  629. },
  630. /**
  631. * initializes the action with a number of waves, the waves amplitude, the grid size and the duration
  632. * @param {Number} duration
  633. * @param {cc.Size} gridSize
  634. * @param {Number} waves
  635. * @param {Number} amplitude
  636. * @return {Boolean}
  637. */
  638. initWithDuration:function (duration, gridSize, waves, amplitude) {
  639. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  640. this._waves = waves;
  641. this._amplitude = amplitude;
  642. this._amplitudeRate = 1.0;
  643. return true;
  644. }
  645. return false;
  646. },
  647. update:function (time) {
  648. var locGridSize = this._gridSize, locWaves = this._waves, locAmplitude = this._amplitude, locAmplitudeRate = this._amplitudeRate;
  649. var locPos = cc.p(0, 0), coords;
  650. for (var i = 0; i < locGridSize.width; i++) {
  651. for (var j = 0; j < locGridSize.height; j++) {
  652. locPos.x = i;
  653. locPos.y = j;
  654. coords = this.originalTile(locPos);
  655. coords.bl.z = (Math.sin(time * Math.PI * locWaves * 2 +
  656. (coords.bl.y + coords.bl.x) * 0.01) * locAmplitude * locAmplitudeRate);
  657. coords.br.z = coords.bl.z;
  658. coords.tl.z = coords.bl.z;
  659. coords.tr.z = coords.bl.z;
  660. this.setTile(locPos, coords);
  661. }
  662. }
  663. }
  664. });
  665. /**
  666. * creates the action with a number of waves, the waves amplitude, the grid size and the duration
  667. * @param {Number} duration
  668. * @param {cc.Size} gridSize
  669. * @param {Number} waves
  670. * @param {Number} amplitude
  671. * @return {cc.WavesTiles3D}
  672. */
  673. cc.WavesTiles3D.create = function (duration, gridSize, waves, amplitude) {
  674. var action = new cc.WavesTiles3D();
  675. action.initWithDuration(duration, gridSize, waves, amplitude);
  676. return action;
  677. };
  678. /**
  679. * cc.JumpTiles3D action. A sin function is executed to move the tiles across the Z axis
  680. * @class
  681. * @extends cc.TiledGrid3DAction
  682. */
  683. cc.JumpTiles3D = cc.TiledGrid3DAction.extend(/** @lends cc.JumpTiles3D# */{
  684. _jumps:0,
  685. _amplitude:0,
  686. _amplitudeRate:0,
  687. ctor:function () {
  688. cc.GridAction.prototype.ctor.call(this);
  689. this._jumps = 0;
  690. this._amplitude = 0;
  691. this._amplitudeRate = 0;
  692. },
  693. /**
  694. * get amplitude of the sin
  695. * @return {Number}
  696. */
  697. getAmplitude:function () {
  698. return this._amplitude;
  699. },
  700. /**
  701. * set amplitude of the sin
  702. * @param {Number} amplitude
  703. */
  704. setAmplitude:function (amplitude) {
  705. this._amplitude = amplitude;
  706. },
  707. /**
  708. * get amplitude rate
  709. * @return {Number}
  710. */
  711. getAmplitudeRate:function () {
  712. return this._amplitudeRate;
  713. },
  714. /**
  715. * set amplitude rate
  716. * @param amplitudeRate
  717. */
  718. setAmplitudeRate:function (amplitudeRate) {
  719. this._amplitudeRate = amplitudeRate;
  720. },
  721. /**
  722. * initializes the action with the number of jumps, the sin amplitude, the grid size and the duration
  723. * @param {Number} duration
  724. * @param {cc.Size} gridSize
  725. * @param {Number} numberOfJumps
  726. * @param {Number} amplitude
  727. */
  728. initWithDuration:function (duration, gridSize, numberOfJumps, amplitude) {
  729. if (cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, gridSize)) {
  730. this._jumps = numberOfJumps;
  731. this._amplitude = amplitude;
  732. this._amplitudeRate = 1.0;
  733. return true;
  734. }
  735. return false;
  736. },
  737. update:function (time) {
  738. var sinz = (Math.sin(Math.PI * time * this._jumps * 2) * this._amplitude * this._amplitudeRate );
  739. var sinz2 = (Math.sin(Math.PI * (time * this._jumps * 2 + 1)) * this._amplitude * this._amplitudeRate );
  740. var locGridSize = this._gridSize;
  741. var locGrid = this._target.getGrid();
  742. var coords, locPos = cc.p(0, 0);
  743. for (var i = 0; i < locGridSize.width; i++) {
  744. for (var j = 0; j < locGridSize.height; j++) {
  745. locPos.x = i;
  746. locPos.y = j;
  747. //hack for html5
  748. //var coords = this.originalTile(cc.p(i, j));
  749. coords = locGrid.originalTile(locPos);
  750. if (((i + j) % 2) == 0) {
  751. coords.bl.z += sinz;
  752. coords.br.z += sinz;
  753. coords.tl.z += sinz;
  754. coords.tr.z += sinz;
  755. } else {
  756. coords.bl.z += sinz2;
  757. coords.br.z += sinz2;
  758. coords.tl.z += sinz2;
  759. coords.tr.z += sinz2;
  760. }
  761. //hack for html5
  762. //this.setTile(cc.p(i, j), coords);
  763. locGrid.setTile(locPos, coords);
  764. }
  765. }
  766. }
  767. });
  768. /**
  769. * creates the action with the number of jumps, the sin amplitude, the grid size and the duration
  770. * @param {Number} duration
  771. * @param {cc.Size} gridSize
  772. * @param {Number} numberOfJumps
  773. * @param {Number} amplitude
  774. * @return {cc.JumpTiles3D}
  775. */
  776. cc.JumpTiles3D.create = function (duration, gridSize, numberOfJumps, amplitude) {
  777. var action = new cc.JumpTiles3D();
  778. action.initWithDuration(duration, gridSize, numberOfJumps, amplitude);
  779. return action;
  780. };
  781. /**
  782. * cc.SplitRows action
  783. * @class
  784. * @extends cc.TiledGrid3DAction
  785. */
  786. cc.SplitRows = cc.TiledGrid3DAction.extend(/** @lends cc.SplitRows# */{
  787. _rows:0,
  788. _winSize:null,
  789. ctor:function () {
  790. cc.GridAction.prototype.ctor.call(this);
  791. this._rows = 0;
  792. this._winSize = null;
  793. },
  794. /**
  795. * initializes the action with the number of rows to split and the duration
  796. * @param {Number} duration
  797. * @param {Number} rows
  798. * @return {Boolean}
  799. */
  800. initWithDuration:function (duration, rows) {
  801. this._rows = rows;
  802. return cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, cc.size(1, rows));
  803. },
  804. update:function (time) {
  805. var locGridSize = this._gridSize, locWinSizeWidth = this._winSize.width;
  806. var coords, direction, locPos = cc.p(0, 0);
  807. for (var j = 0; j < locGridSize.height; ++j) {
  808. locPos.y = j;
  809. coords = this.originalTile(locPos);
  810. direction = 1;
  811. if ((j % 2 ) == 0)
  812. direction = -1;
  813. coords.bl.x += direction * locWinSizeWidth * time;
  814. coords.br.x += direction * locWinSizeWidth * time;
  815. coords.tl.x += direction * locWinSizeWidth * time;
  816. coords.tr.x += direction * locWinSizeWidth * time;
  817. this.setTile(locPos, coords);
  818. }
  819. },
  820. startWithTarget:function (target) {
  821. cc.TiledGrid3DAction.prototype.startWithTarget.call(this, target);
  822. this._winSize = cc.Director.getInstance().getWinSizeInPixels();
  823. }
  824. });
  825. /**
  826. * creates the action with the number of rows to split and the duration
  827. * @param {Number} duration
  828. * @param {Number} rows
  829. * @return {cc.SplitRows}
  830. */
  831. cc.SplitRows.create = function (duration, rows) {
  832. var action = new cc.SplitRows();
  833. action.initWithDuration(duration, rows);
  834. return action;
  835. };
  836. /**
  837. * cc.SplitCols action
  838. * @class
  839. * @extends cc.TiledGrid3DAction
  840. */
  841. cc.SplitCols = cc.TiledGrid3DAction.extend(/** @lends cc.SplitCols# */{
  842. _cols:0,
  843. _winSize:null,
  844. ctor:function () {
  845. cc.GridAction.prototype.ctor.call(this);
  846. this._cols = 0;
  847. this._winSize = null;
  848. },
  849. /**
  850. * initializes the action with the number of columns to split and the duration
  851. * @param {Number} duration
  852. * @param {Number} cols
  853. * @return {Boolean}
  854. */
  855. initWithDuration:function (duration, cols) {
  856. this._cols = cols;
  857. return cc.TiledGrid3DAction.prototype.initWithDuration.call(this, duration, cc.size(cols, 1));
  858. },
  859. update:function (time) {
  860. var locGridSizeWidth = this._gridSize.width, locWinSizeHeight = this._winSize.height;
  861. var coords, direction, locPos = cc.p(0, 0);
  862. for (var i = 0; i < locGridSizeWidth; ++i) {
  863. locPos.x = i;
  864. coords = this.originalTile(locPos);
  865. direction = 1;
  866. if ((i % 2 ) == 0)
  867. direction = -1;
  868. coords.bl.y += direction * locWinSizeHeight * time;
  869. coords.br.y += direction * locWinSizeHeight * time;
  870. coords.tl.y += direction * locWinSizeHeight * time;
  871. coords.tr.y += direction * locWinSizeHeight * time;
  872. this.setTile(locPos, coords);
  873. }
  874. },
  875. /**
  876. * @param {cc.Node} target
  877. */
  878. startWithTarget:function (target) {
  879. cc.TiledGrid3DAction.prototype.startWithTarget.call(this, target);
  880. this._winSize = cc.Director.getInstance().getWinSizeInPixels();
  881. }
  882. });
  883. /**
  884. * creates the action with the number of columns to split and the duration
  885. * @param {Number} duration
  886. * @param {Number} cols
  887. * @return {cc.SplitCols}
  888. */
  889. cc.SplitCols.create = function (duration, cols) {
  890. var action = new cc.SplitCols();
  891. action.initWithDuration(duration, cols);
  892. return action;
  893. };