CCActionTiledGrid.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies 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. #include "2d/CCActionTiledGrid.h"
  23. #include "2d/CCGrid.h"
  24. #include "2d/CCNodeGrid.h"
  25. #include "base/CCDirector.h"
  26. #include "base/ccMacros.h"
  27. NS_CC_BEGIN
  28. struct Tile
  29. {
  30. Vec2 position;
  31. Vec2 startPosition;
  32. Size delta;
  33. };
  34. // implementation of ShakyTiles3D
  35. ShakyTiles3D* ShakyTiles3D::create(float duration, const Size& gridSize, int range, bool shakeZ)
  36. {
  37. ShakyTiles3D *action = new (std::nothrow) ShakyTiles3D();
  38. if (action && action->initWithDuration(duration, gridSize, range, shakeZ))
  39. {
  40. action->autorelease();
  41. return action;
  42. }
  43. delete action;
  44. return nullptr;
  45. }
  46. bool ShakyTiles3D::initWithDuration(float duration, const Size& gridSize, int range, bool shakeZ)
  47. {
  48. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  49. {
  50. _randrange = range;
  51. _shakeZ = shakeZ;
  52. return true;
  53. }
  54. return false;
  55. }
  56. ShakyTiles3D* ShakyTiles3D::clone() const
  57. {
  58. // no copy constructor
  59. return ShakyTiles3D::create(_duration, _gridSize, _randrange, _shakeZ);
  60. }
  61. void ShakyTiles3D::update(float /*time*/)
  62. {
  63. int i, j;
  64. for (i = 0; i < _gridSize.width; ++i)
  65. {
  66. for (j = 0; j < _gridSize.height; ++j)
  67. {
  68. Quad3 coords = getOriginalTile(Vec2(i, j));
  69. // X
  70. coords.bl.x += ( rand() % (_randrange*2) ) - _randrange;
  71. coords.br.x += ( rand() % (_randrange*2) ) - _randrange;
  72. coords.tl.x += ( rand() % (_randrange*2) ) - _randrange;
  73. coords.tr.x += ( rand() % (_randrange*2) ) - _randrange;
  74. // Y
  75. coords.bl.y += ( rand() % (_randrange*2) ) - _randrange;
  76. coords.br.y += ( rand() % (_randrange*2) ) - _randrange;
  77. coords.tl.y += ( rand() % (_randrange*2) ) - _randrange;
  78. coords.tr.y += ( rand() % (_randrange*2) ) - _randrange;
  79. if (_shakeZ)
  80. {
  81. coords.bl.z += ( rand() % (_randrange*2) ) - _randrange;
  82. coords.br.z += ( rand() % (_randrange*2) ) - _randrange;
  83. coords.tl.z += ( rand() % (_randrange*2) ) - _randrange;
  84. coords.tr.z += ( rand() % (_randrange*2) ) - _randrange;
  85. }
  86. setTile(Vec2(i, j), coords);
  87. }
  88. }
  89. }
  90. // implementation of ShatteredTiles3D
  91. ShatteredTiles3D* ShatteredTiles3D::create(float duration, const Size& gridSize, int range, bool shatterZ)
  92. {
  93. ShatteredTiles3D *action = new (std::nothrow) ShatteredTiles3D();
  94. if (action && action->initWithDuration(duration, gridSize, range, shatterZ))
  95. {
  96. action->autorelease();
  97. return action;
  98. }
  99. delete action;
  100. return nullptr;
  101. }
  102. bool ShatteredTiles3D::initWithDuration(float duration, const Size& gridSize, int range, bool shatterZ)
  103. {
  104. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  105. {
  106. _once = false;
  107. _randrange = range;
  108. _shatterZ = shatterZ;
  109. return true;
  110. }
  111. return false;
  112. }
  113. ShatteredTiles3D* ShatteredTiles3D::clone() const
  114. {
  115. // no copy constructor
  116. return ShatteredTiles3D::create(_duration, _gridSize, _randrange, _shatterZ);
  117. }
  118. void ShatteredTiles3D::update(float /*time*/)
  119. {
  120. int i, j;
  121. if (_once == false)
  122. {
  123. for (i = 0; i < _gridSize.width; ++i)
  124. {
  125. for (j = 0; j < _gridSize.height; ++j)
  126. {
  127. Quad3 coords = getOriginalTile(Vec2(i ,j));
  128. // X
  129. coords.bl.x += ( rand() % (_randrange*2) ) - _randrange;
  130. coords.br.x += ( rand() % (_randrange*2) ) - _randrange;
  131. coords.tl.x += ( rand() % (_randrange*2) ) - _randrange;
  132. coords.tr.x += ( rand() % (_randrange*2) ) - _randrange;
  133. // Y
  134. coords.bl.y += ( rand() % (_randrange*2) ) - _randrange;
  135. coords.br.y += ( rand() % (_randrange*2) ) - _randrange;
  136. coords.tl.y += ( rand() % (_randrange*2) ) - _randrange;
  137. coords.tr.y += ( rand() % (_randrange*2) ) - _randrange;
  138. if (_shatterZ)
  139. {
  140. coords.bl.z += ( rand() % (_randrange*2) ) - _randrange;
  141. coords.br.z += ( rand() % (_randrange*2) ) - _randrange;
  142. coords.tl.z += ( rand() % (_randrange*2) ) - _randrange;
  143. coords.tr.z += ( rand() % (_randrange*2) ) - _randrange;
  144. }
  145. setTile(Vec2(i, j), coords);
  146. }
  147. }
  148. _once = true;
  149. }
  150. }
  151. // implementation of ShuffleTiles
  152. ShuffleTiles* ShuffleTiles::create(float duration, const Size& gridSize, unsigned int seed)
  153. {
  154. ShuffleTiles *action = new (std::nothrow) ShuffleTiles();
  155. if (action && action->initWithDuration(duration, gridSize, seed))
  156. {
  157. action->autorelease();
  158. return action;
  159. }
  160. delete action;
  161. return nullptr;
  162. }
  163. bool ShuffleTiles::initWithDuration(float duration, const Size& gridSize, unsigned int seed)
  164. {
  165. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  166. {
  167. _seed = seed;
  168. _tilesOrder = nullptr;
  169. _tiles = nullptr;
  170. return true;
  171. }
  172. return false;
  173. }
  174. ShuffleTiles* ShuffleTiles::clone() const
  175. {
  176. // no copy constructor
  177. return ShuffleTiles::create(_duration, _gridSize, _seed);
  178. }
  179. ShuffleTiles::~ShuffleTiles()
  180. {
  181. CC_SAFE_DELETE_ARRAY(_tilesOrder);
  182. CC_SAFE_DELETE_ARRAY(_tiles);
  183. }
  184. void ShuffleTiles::shuffle(unsigned int *array, unsigned int len)
  185. {
  186. for (int i = len - 1; i >= 0; i-- )
  187. {
  188. unsigned int j = rand() % (i+1);
  189. unsigned int v = array[i];
  190. array[i] = array[j];
  191. array[j] = v;
  192. }
  193. }
  194. Size ShuffleTiles::getDelta(const Size& pos) const
  195. {
  196. Vec2 pos2;
  197. unsigned int idx = pos.width * _gridSize.height + pos.height;
  198. pos2.x = (float)(_tilesOrder[idx] / (int)_gridSize.height);
  199. pos2.y = (float)(_tilesOrder[idx] % (int)_gridSize.height);
  200. return Size((int)(pos2.x - pos.width), (int)(pos2.y - pos.height));
  201. }
  202. void ShuffleTiles::placeTile(const Vec2& pos, Tile *t)
  203. {
  204. Quad3 coords = getOriginalTile(pos);
  205. Vec2 step = _gridNodeTarget->getGrid()->getStep();
  206. coords.bl.x += (int)(t->position.x * step.x);
  207. coords.bl.y += (int)(t->position.y * step.y);
  208. coords.br.x += (int)(t->position.x * step.x);
  209. coords.br.y += (int)(t->position.y * step.y);
  210. coords.tl.x += (int)(t->position.x * step.x);
  211. coords.tl.y += (int)(t->position.y * step.y);
  212. coords.tr.x += (int)(t->position.x * step.x);
  213. coords.tr.y += (int)(t->position.y * step.y);
  214. setTile(pos, coords);
  215. }
  216. void ShuffleTiles::startWithTarget(Node *target)
  217. {
  218. TiledGrid3DAction::startWithTarget(target);
  219. if (_seed != (unsigned int)-1)
  220. {
  221. std::srand(_seed);
  222. }
  223. _tilesCount = _gridSize.width * _gridSize.height;
  224. _tilesOrder = new unsigned int[_tilesCount];
  225. /**
  226. * Use k to loop. Because _tilesCount is unsigned int,
  227. * and i is used later for int.
  228. */
  229. for (unsigned int k = 0; k < _tilesCount; ++k)
  230. {
  231. _tilesOrder[k] = k;
  232. }
  233. shuffle(_tilesOrder, _tilesCount);
  234. _tiles = (struct Tile *)new Tile[_tilesCount];
  235. Tile *tileArray = (Tile*) _tiles;
  236. for (int i = 0; i < _gridSize.width; ++i)
  237. {
  238. for ( int j = 0; j < _gridSize.height; ++j)
  239. {
  240. tileArray->position.set((float)i, (float)j);
  241. tileArray->startPosition.set((float)i, (float)j);
  242. tileArray->delta = getDelta(Size(i, j));
  243. ++tileArray;
  244. }
  245. }
  246. }
  247. void ShuffleTiles::update(float time)
  248. {
  249. Tile *tileArray = (Tile*)_tiles;
  250. for (int i = 0; i < _gridSize.width; ++i)
  251. {
  252. for (int j = 0; j < _gridSize.height; ++j)
  253. {
  254. tileArray->position = Vec2((float)tileArray->delta.width, (float)tileArray->delta.height) * time;
  255. placeTile(Vec2(i, j), tileArray);
  256. ++tileArray;
  257. }
  258. }
  259. }
  260. // implementation of FadeOutTRTiles
  261. FadeOutTRTiles* FadeOutTRTiles::create(float duration, const Size& gridSize)
  262. {
  263. FadeOutTRTiles *action = new (std::nothrow) FadeOutTRTiles();
  264. if (action && action->initWithDuration(duration, gridSize))
  265. {
  266. action->autorelease();
  267. return action;
  268. }
  269. delete action;
  270. return nullptr;
  271. }
  272. FadeOutTRTiles* FadeOutTRTiles::clone() const
  273. {
  274. // no copy constructor
  275. return FadeOutTRTiles::create(_duration, _gridSize);
  276. }
  277. float FadeOutTRTiles::testFunc(const Size& pos, float time)
  278. {
  279. Vec2 n = Vec2((float)_gridSize.width, (float)_gridSize.height) * time;
  280. if ((n.x + n.y) == 0.0f)
  281. {
  282. return 1.0f;
  283. }
  284. return powf((pos.width + pos.height) / (n.x + n.y), 6);
  285. }
  286. void FadeOutTRTiles::turnOnTile(const Vec2& pos)
  287. {
  288. setTile(pos, getOriginalTile(pos));
  289. }
  290. void FadeOutTRTiles::turnOffTile(const Vec2& pos)
  291. {
  292. Quad3 coords;
  293. memset(&coords, 0, sizeof(Quad3));
  294. setTile(pos, coords);
  295. }
  296. void FadeOutTRTiles::transformTile(const Vec2& pos, float distance)
  297. {
  298. Quad3 coords = getOriginalTile(pos);
  299. Vec2 step = _gridNodeTarget->getGrid()->getStep();
  300. coords.bl.x += (step.x / 2) * (1.0f - distance);
  301. coords.bl.y += (step.y / 2) * (1.0f - distance);
  302. coords.br.x -= (step.x / 2) * (1.0f - distance);
  303. coords.br.y += (step.y / 2) * (1.0f - distance);
  304. coords.tl.x += (step.x / 2) * (1.0f - distance);
  305. coords.tl.y -= (step.y / 2) * (1.0f - distance);
  306. coords.tr.x -= (step.x / 2) * (1.0f - distance);
  307. coords.tr.y -= (step.y / 2) * (1.0f - distance);
  308. setTile(pos, coords);
  309. }
  310. void FadeOutTRTiles::update(float time)
  311. {
  312. for (int i = 0; i < _gridSize.width; ++i)
  313. {
  314. for (int j = 0; j < _gridSize.height; ++j)
  315. {
  316. float distance = testFunc(Size(i, j), time);
  317. if ( distance == 0 )
  318. {
  319. turnOffTile(Vec2(i, j));
  320. } else
  321. if (distance < 1)
  322. {
  323. transformTile(Vec2(i, j), distance);
  324. }
  325. else
  326. {
  327. turnOnTile(Vec2(i, j));
  328. }
  329. }
  330. }
  331. }
  332. // implementation of FadeOutBLTiles
  333. FadeOutBLTiles* FadeOutBLTiles::create(float duration, const Size& gridSize)
  334. {
  335. FadeOutBLTiles *action = new (std::nothrow) FadeOutBLTiles();
  336. if (action && action->initWithDuration(duration, gridSize))
  337. {
  338. action->autorelease();
  339. return action;
  340. }
  341. delete action;
  342. return nullptr;
  343. }
  344. FadeOutBLTiles* FadeOutBLTiles::clone() const
  345. {
  346. // no copy constructor
  347. return FadeOutBLTiles::create(_duration, _gridSize);
  348. }
  349. float FadeOutBLTiles::testFunc(const Size& pos, float time)
  350. {
  351. Vec2 n = Vec2((float)_gridSize.width, (float)_gridSize.height) * (1.0f - time);
  352. if ((pos.width + pos.height) == 0)
  353. {
  354. return 1.0f;
  355. }
  356. return powf((n.x + n.y) / (pos.width + pos.height), 6);
  357. }
  358. // implementation of FadeOutUpTiles
  359. FadeOutUpTiles* FadeOutUpTiles::create(float duration, const Size& gridSize)
  360. {
  361. FadeOutUpTiles *action = new (std::nothrow) FadeOutUpTiles();
  362. if (action && action->initWithDuration(duration, gridSize))
  363. {
  364. action->autorelease();
  365. return action;
  366. }
  367. delete action;
  368. return nullptr;
  369. }
  370. FadeOutUpTiles* FadeOutUpTiles::clone() const
  371. {
  372. // no copy constructor
  373. return FadeOutUpTiles::create(_duration, _gridSize);
  374. }
  375. float FadeOutUpTiles::testFunc(const Size& pos, float time)
  376. {
  377. Vec2 n = Vec2((float)_gridSize.width, (float)_gridSize.height) * time;
  378. if (n.y == 0.0f)
  379. {
  380. return 1.0f;
  381. }
  382. return powf(pos.height / n.y, 6);
  383. }
  384. void FadeOutUpTiles::transformTile(const Vec2& pos, float distance)
  385. {
  386. Quad3 coords = getOriginalTile(pos);
  387. Vec2 step = _gridNodeTarget->getGrid()->getStep();
  388. coords.bl.y += (step.y / 2) * (1.0f - distance);
  389. coords.br.y += (step.y / 2) * (1.0f - distance);
  390. coords.tl.y -= (step.y / 2) * (1.0f - distance);
  391. coords.tr.y -= (step.y / 2) * (1.0f - distance);
  392. setTile(pos, coords);
  393. }
  394. // implementation of FadeOutDownTiles
  395. FadeOutDownTiles* FadeOutDownTiles::create(float duration, const Size& gridSize)
  396. {
  397. FadeOutDownTiles *action = new (std::nothrow) FadeOutDownTiles();
  398. if (action && action->initWithDuration(duration, gridSize))
  399. {
  400. action->autorelease();
  401. return action;
  402. }
  403. delete action;
  404. return nullptr;
  405. }
  406. FadeOutDownTiles* FadeOutDownTiles::clone() const
  407. {
  408. // no copy constructor
  409. return FadeOutDownTiles::create(_duration, _gridSize);
  410. }
  411. float FadeOutDownTiles::testFunc(const Size& pos, float time)
  412. {
  413. Vec2 n = Vec2((float)_gridSize.width, (float)_gridSize.height) * (1.0f - time);
  414. return powf(n.y / (pos.height > 0.0f ? pos.height : 0.1f), 6);
  415. }
  416. // implementation of TurnOffTiles
  417. TurnOffTiles* TurnOffTiles::create(float duration, const Size& gridSize)
  418. {
  419. TurnOffTiles* action = new (std::nothrow) TurnOffTiles();
  420. if (action && action->initWithDuration(duration, gridSize, 0))
  421. {
  422. action->autorelease();
  423. return action;
  424. }
  425. delete action;
  426. return nullptr;
  427. }
  428. TurnOffTiles* TurnOffTiles::create(float duration, const Size& gridSize, unsigned int seed)
  429. {
  430. TurnOffTiles *action = new (std::nothrow) TurnOffTiles();
  431. if (action && action->initWithDuration(duration, gridSize, seed))
  432. {
  433. action->autorelease();
  434. return action;
  435. }
  436. delete action;
  437. return nullptr;
  438. }
  439. bool TurnOffTiles::initWithDuration(float duration, const Size& gridSize, unsigned int seed)
  440. {
  441. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  442. {
  443. _seed = seed;
  444. _tilesOrder = nullptr;
  445. return true;
  446. }
  447. return false;
  448. }
  449. TurnOffTiles* TurnOffTiles::clone() const
  450. {
  451. // no copy constructor
  452. return TurnOffTiles::create(_duration, _gridSize, _seed);
  453. }
  454. TurnOffTiles::~TurnOffTiles(void)
  455. {
  456. CC_SAFE_DELETE_ARRAY(_tilesOrder);
  457. }
  458. void TurnOffTiles::shuffle(unsigned int *array, unsigned int len)
  459. {
  460. for (int i = len - 1; i >= 0; i--)
  461. {
  462. unsigned int j = rand() % (i+1);
  463. unsigned int v = array[i];
  464. array[i] = array[j];
  465. array[j] = v;
  466. }
  467. }
  468. void TurnOffTiles::turnOnTile(const Vec2& pos)
  469. {
  470. setTile(pos, getOriginalTile(pos));
  471. }
  472. void TurnOffTiles::turnOffTile(const Vec2& pos)
  473. {
  474. Quad3 coords;
  475. memset(&coords, 0, sizeof(Quad3));
  476. setTile(pos, coords);
  477. }
  478. void TurnOffTiles::startWithTarget(Node *target)
  479. {
  480. TiledGrid3DAction::startWithTarget(target);
  481. if (_seed != (unsigned int)-1)
  482. {
  483. std::srand(_seed);
  484. }
  485. _tilesCount = _gridSize.width * _gridSize.height;
  486. _tilesOrder = new unsigned int[_tilesCount];
  487. for (unsigned int i = 0; i < _tilesCount; ++i)
  488. {
  489. _tilesOrder[i] = i;
  490. }
  491. shuffle(_tilesOrder, _tilesCount);
  492. }
  493. void TurnOffTiles::update(float time)
  494. {
  495. unsigned int l = (unsigned int)(time * (float)_tilesCount);
  496. unsigned int t = 0;
  497. for (unsigned int i = 0; i < _tilesCount; i++ )
  498. {
  499. t = _tilesOrder[i];
  500. Vec2 tilePos( (unsigned int)(t / _gridSize.height), t % (unsigned int)_gridSize.height );
  501. if ( i < l )
  502. {
  503. turnOffTile(tilePos);
  504. }
  505. else
  506. {
  507. turnOnTile(tilePos);
  508. }
  509. }
  510. }
  511. // implementation of WavesTiles3D
  512. WavesTiles3D* WavesTiles3D::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  513. {
  514. WavesTiles3D *action = new (std::nothrow) WavesTiles3D();
  515. if (action && action->initWithDuration(duration, gridSize, waves, amplitude))
  516. {
  517. action->autorelease();
  518. return action;
  519. }
  520. delete action;
  521. return nullptr;
  522. }
  523. bool WavesTiles3D::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  524. {
  525. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  526. {
  527. _waves = waves;
  528. _amplitude = amplitude;
  529. _amplitudeRate = 1.0f;
  530. return true;
  531. }
  532. return false;
  533. }
  534. WavesTiles3D* WavesTiles3D::clone() const
  535. {
  536. // no copy constructor
  537. return WavesTiles3D::create(_duration, _gridSize, _waves, _amplitude);
  538. }
  539. void WavesTiles3D::update(float time)
  540. {
  541. for (int i = 0; i < _gridSize.width; i++ )
  542. {
  543. for (int j = 0; j < _gridSize.height; j++ )
  544. {
  545. Quad3 coords = getOriginalTile(Vec2(i, j));
  546. coords.bl.z = (sinf(time * (float)M_PI *_waves * 2 +
  547. (coords.bl.y+coords.bl.x) * .01f) * _amplitude * _amplitudeRate );
  548. coords.br.z = coords.bl.z;
  549. coords.tl.z = coords.bl.z;
  550. coords.tr.z = coords.bl.z;
  551. setTile(Vec2(i, j), coords);
  552. }
  553. }
  554. }
  555. // implementation of JumpTiles3D
  556. JumpTiles3D* JumpTiles3D::create(float duration, const Size& gridSize, unsigned int numberOfJumps, float amplitude)
  557. {
  558. JumpTiles3D *action = new (std::nothrow) JumpTiles3D();
  559. if (action && action->initWithDuration(duration, gridSize, numberOfJumps, amplitude))
  560. {
  561. action->autorelease();
  562. return action;
  563. }
  564. delete action;
  565. return nullptr;
  566. }
  567. bool JumpTiles3D::initWithDuration(float duration, const Size& gridSize, unsigned int numberOfJumps, float amplitude)
  568. {
  569. if (TiledGrid3DAction::initWithDuration(duration, gridSize))
  570. {
  571. _jumps = numberOfJumps;
  572. _amplitude = amplitude;
  573. _amplitudeRate = 1.0f;
  574. return true;
  575. }
  576. return false;
  577. }
  578. JumpTiles3D* JumpTiles3D::clone() const
  579. {
  580. // no copy constructor
  581. return JumpTiles3D::create(_duration, _gridSize, _jumps, _amplitude);
  582. }
  583. void JumpTiles3D::update(float time)
  584. {
  585. float sinz = (sinf((float)M_PI * time * _jumps * 2) * _amplitude * _amplitudeRate );
  586. float sinz2 = (sinf((float)M_PI * (time * _jumps * 2 + 1)) * _amplitude * _amplitudeRate );
  587. for (int i = 0; i < _gridSize.width; i++ )
  588. {
  589. for (int j = 0; j < _gridSize.height; j++ )
  590. {
  591. Quad3 coords = getOriginalTile(Vec2(i, j));
  592. if ( ((i+j) % 2) == 0 )
  593. {
  594. coords.bl.z += sinz;
  595. coords.br.z += sinz;
  596. coords.tl.z += sinz;
  597. coords.tr.z += sinz;
  598. }
  599. else
  600. {
  601. coords.bl.z += sinz2;
  602. coords.br.z += sinz2;
  603. coords.tl.z += sinz2;
  604. coords.tr.z += sinz2;
  605. }
  606. setTile(Vec2(i, j), coords);
  607. }
  608. }
  609. }
  610. // implementation of SplitRows
  611. SplitRows* SplitRows::create(float duration, unsigned int nRows)
  612. {
  613. SplitRows *action = new (std::nothrow) SplitRows();
  614. if (action && action->initWithDuration(duration, nRows))
  615. {
  616. action->autorelease();
  617. return action;
  618. }
  619. delete action;
  620. return nullptr;
  621. }
  622. bool SplitRows::initWithDuration(float duration, unsigned int rows)
  623. {
  624. _rows = rows;
  625. return TiledGrid3DAction::initWithDuration(duration, Size(1, rows));
  626. }
  627. SplitRows* SplitRows::clone() const
  628. {
  629. // no copy constructor
  630. return SplitRows::create(_duration, _rows);
  631. }
  632. void SplitRows::startWithTarget(Node *target)
  633. {
  634. TiledGrid3DAction::startWithTarget(target);
  635. _winSize = Director::getInstance()->getWinSizeInPixels();
  636. }
  637. void SplitRows::update(float time)
  638. {
  639. for (unsigned int j = 0; j < _gridSize.height; ++j)
  640. {
  641. Quad3 coords = getOriginalTile(Vec2(0, j));
  642. float direction = 1;
  643. if ( (j % 2 ) == 0 )
  644. {
  645. direction = -1;
  646. }
  647. coords.bl.x += direction * _winSize.width * time;
  648. coords.br.x += direction * _winSize.width * time;
  649. coords.tl.x += direction * _winSize.width * time;
  650. coords.tr.x += direction * _winSize.width * time;
  651. setTile(Vec2(0, j), coords);
  652. }
  653. }
  654. // implementation of SplitCols
  655. SplitCols* SplitCols::create(float duration, unsigned int cols)
  656. {
  657. SplitCols *action = new (std::nothrow) SplitCols();
  658. if (action && action->initWithDuration(duration, cols))
  659. {
  660. action->autorelease();
  661. return action;
  662. }
  663. delete action;
  664. return nullptr;
  665. }
  666. bool SplitCols::initWithDuration(float duration, unsigned int cols)
  667. {
  668. _cols = cols;
  669. return TiledGrid3DAction::initWithDuration(duration, Size(cols, 1));
  670. }
  671. SplitCols* SplitCols::clone() const
  672. {
  673. // no copy constructor
  674. return SplitCols::create(_duration, _cols);
  675. }
  676. void SplitCols::startWithTarget(Node *target)
  677. {
  678. TiledGrid3DAction::startWithTarget(target);
  679. _winSize = Director::getInstance()->getWinSizeInPixels();
  680. }
  681. void SplitCols::update(float time)
  682. {
  683. for (unsigned int i = 0; i < _gridSize.width; ++i)
  684. {
  685. Quad3 coords = getOriginalTile(Vec2(i, 0));
  686. float direction = 1;
  687. if ( (i % 2 ) == 0 )
  688. {
  689. direction = -1;
  690. }
  691. coords.bl.y += direction * _winSize.height * time;
  692. coords.br.y += direction * _winSize.height * time;
  693. coords.tl.y += direction * _winSize.height * time;
  694. coords.tr.y += direction * _winSize.height * time;
  695. setTile(Vec2(i, 0), coords);
  696. }
  697. }
  698. NS_CC_END