CCActionGrid.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/CCActionGrid.h"
  23. #include "2d/CCGrid.h"
  24. #include "2d/CCNodeGrid.h"
  25. #include "base/CCDirector.h"
  26. NS_CC_BEGIN
  27. // implementation of GridAction
  28. bool GridAction::initWithDuration(float duration, const Size& gridSize)
  29. {
  30. if (ActionInterval::initWithDuration(duration))
  31. {
  32. _gridSize = gridSize;
  33. return true;
  34. }
  35. return false;
  36. }
  37. void GridAction::startWithTarget(Node *target)
  38. {
  39. ActionInterval::startWithTarget(target);
  40. cacheTargetAsGridNode();
  41. GridBase *targetGrid = _gridNodeTarget->getGrid();
  42. if (targetGrid && targetGrid->getReuseGrid() > 0)
  43. {
  44. if (targetGrid->isActive() && targetGrid->getGridSize().width == _gridSize.width
  45. && targetGrid->getGridSize().height == _gridSize.height)
  46. {
  47. targetGrid->reuse();
  48. }
  49. else
  50. {
  51. CCASSERT(0, "Invalid grid parameters!");
  52. }
  53. }
  54. else
  55. {
  56. if (targetGrid && targetGrid->isActive())
  57. {
  58. targetGrid->setActive(false);
  59. }
  60. auto newgrid = this->getGrid();
  61. _gridNodeTarget->setGrid(newgrid);
  62. _gridNodeTarget->getGrid()->setActive(true);
  63. }
  64. }
  65. void GridAction::cacheTargetAsGridNode()
  66. {
  67. _gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
  68. CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
  69. }
  70. GridAction* GridAction::reverse() const
  71. {
  72. // FIXME: This conversion isn't safe.
  73. return (GridAction*)ReverseTime::create( this->clone() );
  74. }
  75. GridBase* GridAction::getGrid()
  76. {
  77. // Abstract class needs implementation
  78. CCASSERT(0, "Subclass should implement this method!");
  79. return nullptr;
  80. }
  81. // implementation of Grid3DAction
  82. GridBase* Grid3DAction::getGrid()
  83. {
  84. return Grid3D::create(_gridSize, _gridNodeTarget->getGridRect());
  85. }
  86. Vec3 Grid3DAction::getVertex(const Vec2& position) const
  87. {
  88. Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
  89. return g->getVertex(position);
  90. }
  91. Vec3 Grid3DAction::getOriginalVertex(const Vec2& position) const
  92. {
  93. Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
  94. return g->getOriginalVertex(position);
  95. }
  96. void Grid3DAction::setVertex(const Vec2& position, const Vec3& vertex)
  97. {
  98. Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
  99. g->setVertex(position, vertex);
  100. }
  101. Rect Grid3DAction::getGridRect() const
  102. {
  103. Grid3D *g = (Grid3D*)_gridNodeTarget->getGrid();
  104. return g->getGridRect();
  105. }
  106. // implementation of TiledGrid3DAction
  107. GridBase* TiledGrid3DAction::getGrid(void)
  108. {
  109. return TiledGrid3D::create(_gridSize, _gridNodeTarget->getGridRect());
  110. }
  111. Quad3 TiledGrid3DAction::getTile(const Vec2& pos) const
  112. {
  113. TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
  114. return g->getTile(pos);
  115. }
  116. Quad3 TiledGrid3DAction::getOriginalTile(const Vec2& pos) const
  117. {
  118. TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
  119. return g->getOriginalTile(pos);
  120. }
  121. void TiledGrid3DAction::setTile(const Vec2& pos, const Quad3& coords)
  122. {
  123. TiledGrid3D *g = (TiledGrid3D*)_gridNodeTarget->getGrid();
  124. return g->setTile(pos, coords);
  125. }
  126. // implementation AccelDeccelAmplitude
  127. AccelDeccelAmplitude* AccelDeccelAmplitude::create(Action *action, float duration)
  128. {
  129. AccelDeccelAmplitude *ret = new (std::nothrow) AccelDeccelAmplitude();
  130. if (ret && ret->initWithAction(action, duration))
  131. {
  132. ret->autorelease();
  133. return ret;
  134. }
  135. delete ret;
  136. return nullptr;
  137. }
  138. bool AccelDeccelAmplitude::initWithAction(Action *action, float duration)
  139. {
  140. if (ActionInterval::initWithDuration(duration))
  141. {
  142. _rate = 1.0f;
  143. _other = (ActionInterval*)(action);
  144. action->retain();
  145. return true;
  146. }
  147. return false;
  148. }
  149. AccelDeccelAmplitude* AccelDeccelAmplitude::clone() const
  150. {
  151. // no copy constructor
  152. if (_other)
  153. return AccelDeccelAmplitude::create(_other->clone(), _rate);
  154. return nullptr;
  155. }
  156. AccelDeccelAmplitude::~AccelDeccelAmplitude()
  157. {
  158. CC_SAFE_RELEASE(_other);
  159. }
  160. void AccelDeccelAmplitude::startWithTarget(Node *target)
  161. {
  162. ActionInterval::startWithTarget(target);
  163. _other->startWithTarget(target);
  164. }
  165. void AccelDeccelAmplitude::update(float time)
  166. {
  167. float f = time * 2;
  168. if (f > 1)
  169. {
  170. f -= 1;
  171. f = 1 - f;
  172. }
  173. ((AccelDeccelAmplitude*)(_other))->setAmplitudeRate(powf(f, _rate));
  174. }
  175. AccelDeccelAmplitude* AccelDeccelAmplitude::reverse() const
  176. {
  177. if (_other)
  178. return AccelDeccelAmplitude::create(_other->reverse(), _duration);
  179. return nullptr;
  180. }
  181. // implementation of AccelAmplitude
  182. AccelAmplitude* AccelAmplitude::create(Action *action, float duration)
  183. {
  184. AccelAmplitude *ret = new (std::nothrow) AccelAmplitude();
  185. if (ret && ret->initWithAction(action, duration))
  186. {
  187. ret->autorelease();
  188. return ret;
  189. }
  190. delete ret;
  191. return nullptr;
  192. }
  193. bool AccelAmplitude::initWithAction(Action *action, float duration)
  194. {
  195. if (ActionInterval::initWithDuration(duration))
  196. {
  197. _rate = 1.0f;
  198. _other = (ActionInterval*)(action);
  199. action->retain();
  200. return true;
  201. }
  202. return false;
  203. }
  204. AccelAmplitude* AccelAmplitude::clone() const
  205. {
  206. // no copy constructor
  207. if (_other)
  208. return AccelAmplitude::create(_other->clone(), _duration);
  209. return nullptr;
  210. }
  211. AccelAmplitude::~AccelAmplitude()
  212. {
  213. CC_SAFE_DELETE(_other);
  214. }
  215. void AccelAmplitude::startWithTarget(Node *target)
  216. {
  217. ActionInterval::startWithTarget(target);
  218. _other->startWithTarget(target);
  219. }
  220. void AccelAmplitude::update(float time)
  221. {
  222. ((AccelAmplitude*)(_other))->setAmplitudeRate(powf(time, _rate));
  223. _other->update(time);
  224. }
  225. AccelAmplitude* AccelAmplitude::reverse() const
  226. {
  227. if (_other)
  228. return AccelAmplitude::create(_other->reverse(), _duration);
  229. return nullptr;
  230. }
  231. // DeccelAmplitude
  232. DeccelAmplitude* DeccelAmplitude::create(Action *action, float duration)
  233. {
  234. DeccelAmplitude *ret = new (std::nothrow) DeccelAmplitude();
  235. if (ret && ret->initWithAction(action, duration))
  236. {
  237. ret->autorelease();
  238. return ret;
  239. }
  240. delete ret;
  241. return nullptr;
  242. }
  243. bool DeccelAmplitude::initWithAction(Action *action, float duration)
  244. {
  245. if (ActionInterval::initWithDuration(duration))
  246. {
  247. _rate = 1.0f;
  248. _other = (ActionInterval*)(action);
  249. action->retain();
  250. return true;
  251. }
  252. return false;
  253. }
  254. DeccelAmplitude::~DeccelAmplitude()
  255. {
  256. CC_SAFE_RELEASE(_other);
  257. }
  258. void DeccelAmplitude::startWithTarget(Node *target)
  259. {
  260. ActionInterval::startWithTarget(target);
  261. _other->startWithTarget(target);
  262. }
  263. void DeccelAmplitude::update(float time)
  264. {
  265. ((DeccelAmplitude*)(_other))->setAmplitudeRate(powf((1 - time), _rate));
  266. _other->update(time);
  267. }
  268. DeccelAmplitude* DeccelAmplitude::clone() const
  269. {
  270. // no copy constructor
  271. if (_other)
  272. return DeccelAmplitude::create(_other->clone(), _duration);
  273. return nullptr;
  274. }
  275. DeccelAmplitude* DeccelAmplitude::reverse() const
  276. {
  277. return DeccelAmplitude::create(_other->reverse(), _duration);
  278. }
  279. // implementation of StopGrid
  280. void StopGrid::startWithTarget(Node *target)
  281. {
  282. ActionInstant::startWithTarget(target);
  283. cacheTargetAsGridNode();
  284. GridBase *grid = _gridNodeTarget->getGrid();
  285. if (grid && grid->isActive())
  286. {
  287. grid->setActive(false);
  288. }
  289. }
  290. void StopGrid::cacheTargetAsGridNode()
  291. {
  292. _gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
  293. CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
  294. }
  295. StopGrid* StopGrid::create()
  296. {
  297. StopGrid* action = new (std::nothrow) StopGrid();
  298. if (action)
  299. {
  300. action->autorelease();
  301. return action;
  302. }
  303. delete action;
  304. return nullptr;
  305. }
  306. StopGrid* StopGrid::clone() const
  307. {
  308. return StopGrid::create();
  309. }
  310. StopGrid* StopGrid::reverse() const
  311. {
  312. // no reverse, just clone it
  313. return this->clone();
  314. }
  315. // implementation of ReuseGrid
  316. ReuseGrid* ReuseGrid::create(int times)
  317. {
  318. ReuseGrid *action = new (std::nothrow) ReuseGrid();
  319. if (action && action->initWithTimes(times))
  320. {
  321. action->autorelease();
  322. return action;
  323. }
  324. delete action;
  325. return nullptr;
  326. }
  327. bool ReuseGrid::initWithTimes(int times)
  328. {
  329. _times = times;
  330. return true;
  331. }
  332. void ReuseGrid::startWithTarget(Node *target)
  333. {
  334. ActionInstant::startWithTarget(target);
  335. cacheTargetAsGridNode();
  336. if (_gridNodeTarget->getGrid() && _gridNodeTarget->getGrid()->isActive())
  337. {
  338. _gridNodeTarget->getGrid()->setReuseGrid(_gridNodeTarget->getGrid()->getReuseGrid() + _times);
  339. }
  340. }
  341. void ReuseGrid::cacheTargetAsGridNode()
  342. {
  343. _gridNodeTarget = dynamic_cast<NodeGrid*> (_target);
  344. CCASSERT(_gridNodeTarget, "GridActions can only used on NodeGrid");
  345. }
  346. ReuseGrid* ReuseGrid::clone() const
  347. {
  348. return ReuseGrid::create(_times);
  349. }
  350. ReuseGrid* ReuseGrid::reverse() const
  351. {
  352. // no reverse, just clone it
  353. return this->clone();
  354. }
  355. NS_CC_END