CCActionTiledGrid.cpp 22 KB

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