CCActionGrid.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #ifndef __ACTION_CCGRID_ACTION_H__
  24. #define __ACTION_CCGRID_ACTION_H__
  25. #include "2d/CCActionInterval.h"
  26. #include "2d/CCActionInstant.h"
  27. NS_CC_BEGIN
  28. class GridBase;
  29. class NodeGrid;
  30. /**
  31. * @addtogroup actions
  32. * @{
  33. */
  34. /**
  35. @class GridAction
  36. @brief Base class for Grid actions.
  37. @details Grid actions are the actions take effect on GridBase.
  38. */
  39. class CC_DLL GridAction : public ActionInterval
  40. {
  41. public:
  42. /**
  43. @brief Get the pointer of GridBase.
  44. @return The pointer of GridBase.
  45. */
  46. virtual GridBase* getGrid();
  47. // overrides
  48. virtual GridAction * clone() const override
  49. {
  50. CC_ASSERT(0);
  51. return nullptr;
  52. }
  53. virtual GridAction* reverse() const override;
  54. virtual void startWithTarget(Node *target) override;
  55. CC_CONSTRUCTOR_ACCESS:
  56. GridAction() {}
  57. virtual ~GridAction() {}
  58. /**
  59. * @brief Initializes the action with size and duration.
  60. * @param duration The duration of the GridAction. It's a value in seconds.
  61. * @param gridSize The size of the GridAction should be.
  62. * @return Return true when the initialization success, otherwise return false.
  63. */
  64. bool initWithDuration(float duration, const Size& gridSize);
  65. protected:
  66. Size _gridSize;
  67. NodeGrid* _gridNodeTarget;
  68. void cacheTargetAsGridNode();
  69. private:
  70. CC_DISALLOW_COPY_AND_ASSIGN(GridAction);
  71. };
  72. /**
  73. @brief Base class for Grid3D actions.
  74. @details Grid3D actions can modify a non-tiled grid.
  75. */
  76. class CC_DLL Grid3DAction : public GridAction
  77. {
  78. public:
  79. virtual GridBase* getGrid() override;
  80. /**
  81. * @brief Get the vertex that belongs to certain position in the grid.
  82. * @param position The position of the grid.
  83. * @return Return a pointer of vertex.
  84. * @js vertex
  85. * @lua NA
  86. */
  87. Vec3 getVertex(const Vec2& position) const;
  88. /** @deprecated Use getVertex() instead
  89. * @js NA
  90. * @lua NA
  91. */
  92. CC_DEPRECATED_ATTRIBUTE Vec3 vertex(const Vec2& position) { return getVertex(position); }
  93. /**
  94. * @brief Get the non-transformed vertex that belongs to certain position in the grid.
  95. * @param position The position of the grid.
  96. * @return Return a pointer of vertex.
  97. * @js originalVertex
  98. * @lua NA
  99. */
  100. Vec3 getOriginalVertex(const Vec2& position) const;
  101. /** @deprecated Use getOriginalVertex() instead
  102. * @js NA
  103. * @lua NA
  104. */
  105. CC_DEPRECATED_ATTRIBUTE Vec3 originalVertex(const Vec2& position) { return getOriginalVertex(position); }
  106. /**
  107. * @brief Set a new vertex to a certain position of the grid.
  108. * @param position The position of the grid.
  109. * @param vertex The vertex will be used on the certain position of grid.
  110. * @js setVertex
  111. * @lua NA
  112. */
  113. void setVertex(const Vec2& position, const Vec3& vertex);
  114. // Overrides
  115. virtual Grid3DAction * clone() const override
  116. {
  117. CC_ASSERT(0);
  118. return nullptr;
  119. }
  120. /**
  121. * @brief Get the effect grid rect.
  122. * @return Return the effect grid rect.
  123. */
  124. Rect getGridRect() const;
  125. };
  126. /**
  127. @brief Base class for TiledGrid3D actions.
  128. */
  129. class CC_DLL TiledGrid3DAction : public GridAction
  130. {
  131. public:
  132. /**
  133. * @brief Create the action with size and duration.
  134. * @param duration The duration of the action. It's a value in seconds.
  135. * @param gridSize Specify the grid size of the action.
  136. * @return A pointer of TiledGrid3DAction. If creation failed, return nil.
  137. * @lua NA
  138. */
  139. static TiledGrid3DAction* create(float duration, const Size& gridSize);
  140. /**
  141. * @brief Get the tile that belongs to a certain position of the grid.
  142. * @param position The position of the tile want to get.
  143. * @return A quadrilateral of the tile.
  144. * @js tile
  145. * @lua NA
  146. */
  147. Quad3 getTile(const Vec2& position) const;
  148. /** @deprecated Use getTile() instead
  149. * @js NA
  150. * @lua NA
  151. */
  152. CC_DEPRECATED_ATTRIBUTE Quad3 tile(const Vec2& position) { return getTile(position); }
  153. /**
  154. * @brief Get the non-transformed tile that belongs to a certain position of the grid.
  155. * @param position The position of the tile want to get.
  156. * @return A quadrilateral of the tile.
  157. * @js originalTile
  158. * @lua NA
  159. */
  160. Quad3 getOriginalTile(const Vec2& position) const;
  161. /** @deprecated Use getOriginalTile() instead.
  162. * @js NA
  163. * @lua NA
  164. */
  165. CC_DEPRECATED_ATTRIBUTE Quad3 originalTile(const Vec2& position) { return getOriginalTile(position); }
  166. /**
  167. * @brief Set a new tile to a certain position of the grid.
  168. * @param position The position of the tile.
  169. * @param coords The quadrilateral of the new tile.
  170. * @lua NA
  171. */
  172. void setTile(const Vec2& position, const Quad3& coords);
  173. /** returns the grid */
  174. virtual GridBase* getGrid() override;
  175. // Override
  176. virtual TiledGrid3DAction * clone() const override
  177. {
  178. CC_ASSERT(0);
  179. return nullptr;
  180. }
  181. };
  182. /**
  183. @brief AccelDeccelAmplitude action.
  184. @js NA
  185. */
  186. class CC_DLL AccelDeccelAmplitude : public ActionInterval
  187. {
  188. public:
  189. /**
  190. @brief Create the action with an inner action that has the amplitude property, and a duration time.
  191. @param action A pointer of the inner action.
  192. @param duration Specify the duration of the AccelDeccelAmplitude action.
  193. @return Return a pointer of AccelDeccelAmplitude action. When the creation failed, return nil.
  194. */
  195. static AccelDeccelAmplitude* create(Action *action, float duration);
  196. /**
  197. @brief Get the value of amplitude rate.
  198. @return the value of amplitude rate.
  199. */
  200. float getRate() const { return _rate; }
  201. /**
  202. @brief Set the value of amplitude rate.
  203. @param rate Specify the value of amplitude rate.
  204. */
  205. void setRate(float rate) { _rate = rate; }
  206. // Overrides
  207. virtual void startWithTarget(Node *target) override;
  208. virtual void update(float time) override;
  209. virtual AccelDeccelAmplitude* clone() const override;
  210. virtual AccelDeccelAmplitude* reverse() const override;
  211. CC_CONSTRUCTOR_ACCESS:
  212. AccelDeccelAmplitude() {}
  213. virtual ~AccelDeccelAmplitude();
  214. /**
  215. @brief Initializes the action with an inner action that has the amplitude property, and a duration time.
  216. @param action A pointer of the inner action.
  217. @param duration Specify the duration of the AccelDeccelAmplitude action.
  218. @return If the initialization success, return true; otherwise, return false.
  219. */
  220. bool initWithAction(Action *action, float duration);
  221. protected:
  222. float _rate;
  223. ActionInterval *_other;
  224. private:
  225. CC_DISALLOW_COPY_AND_ASSIGN(AccelDeccelAmplitude);
  226. };
  227. /**
  228. @brief AccelAmplitude action.
  229. @js NA
  230. */
  231. class CC_DLL AccelAmplitude : public ActionInterval
  232. {
  233. public:
  234. /**
  235. @brief Create the action with an inner action that has the amplitude property, and a duration time.
  236. @param action A pointer of the inner action.
  237. @param duration Specify the duration of the AccelAmplitude action.
  238. @return Return a pointer of AccelAmplitude action. When the creation failed, return nil.
  239. */
  240. static AccelAmplitude* create(Action *action, float duration);
  241. /**
  242. @brief Get the value of amplitude rate.
  243. @return The value of amplitude rate.
  244. */
  245. float getRate() const { return _rate; }
  246. /**
  247. @brief Set the value of amplitude rate.
  248. @param rate Specify the value of amplitude rate.
  249. */
  250. void setRate(float rate) { _rate = rate; }
  251. // Overrides
  252. virtual void startWithTarget(Node *target) override;
  253. virtual void update(float time) override;
  254. virtual AccelAmplitude* clone() const override;
  255. virtual AccelAmplitude* reverse() const override;
  256. CC_CONSTRUCTOR_ACCESS:
  257. AccelAmplitude() {}
  258. virtual ~AccelAmplitude();
  259. bool initWithAction(Action *action, float duration);
  260. protected:
  261. float _rate;
  262. ActionInterval *_other;
  263. private:
  264. CC_DISALLOW_COPY_AND_ASSIGN(AccelAmplitude);
  265. };
  266. /**
  267. @brief DeccelAmplitude action.
  268. @js NA
  269. */
  270. class CC_DLL DeccelAmplitude : public ActionInterval
  271. {
  272. public:
  273. /**
  274. @brief Creates the action with an inner action that has the amplitude property, and a duration time.
  275. @param action A pointer of the inner action.
  276. @param duration Specify the duration of the DeccelAmplitude action.
  277. @return Return a pointer of DeccelAmplitude. When the creation failed, return nil.
  278. */
  279. static DeccelAmplitude* create(Action *action, float duration);
  280. /**
  281. @brief Get the value of amplitude rate.
  282. @return The value of amplitude rate.
  283. */
  284. float getRate() const { return _rate; }
  285. /**
  286. @brief Set the value of amplitude rate.
  287. @param rate Specify the value.
  288. */
  289. void setRate(float rate) { _rate = rate; }
  290. // overrides
  291. virtual void startWithTarget(Node *target) override;
  292. virtual void update(float time) override;
  293. virtual DeccelAmplitude* clone() const override;
  294. virtual DeccelAmplitude* reverse() const override;
  295. CC_CONSTRUCTOR_ACCESS:
  296. DeccelAmplitude() {}
  297. virtual ~DeccelAmplitude();
  298. /**
  299. @brief Initializes the action with an inner action that has the amplitude property, and a duration time.
  300. @param action The pointer of inner action.
  301. @param duration The duration of the DeccelAmplitude action.
  302. @return If the initialization success, return true; otherwise, return false.
  303. */
  304. bool initWithAction(Action *action, float duration);
  305. protected:
  306. float _rate;
  307. ActionInterval *_other;
  308. private:
  309. CC_DISALLOW_COPY_AND_ASSIGN(DeccelAmplitude);
  310. };
  311. /**
  312. @brief StopGrid action.
  313. @warning Don't call this action if another grid action is active.
  314. Call if you want to remove the grid effect. Example:
  315. @code
  316. Sequence::create(Lens3D::create(...), StopGrid::create(), nullptr);
  317. @endcode
  318. */
  319. class CC_DLL StopGrid : public ActionInstant
  320. {
  321. public:
  322. /**
  323. @brief Create a StopGrid Action.
  324. @return Return a pointer of StopGrid. When the creation failed, return nil.
  325. */
  326. static StopGrid* create();
  327. // Overrides
  328. virtual void startWithTarget(Node *target) override;
  329. virtual StopGrid* clone() const override;
  330. virtual StopGrid* reverse() const override;
  331. CC_CONSTRUCTOR_ACCESS:
  332. StopGrid() {}
  333. virtual ~StopGrid() {}
  334. protected:
  335. NodeGrid* _gridNodeTarget;
  336. void cacheTargetAsGridNode();
  337. private:
  338. CC_DISALLOW_COPY_AND_ASSIGN(StopGrid);
  339. };
  340. /**
  341. @brief ReuseGrid action.
  342. */
  343. class CC_DLL ReuseGrid : public ActionInstant
  344. {
  345. public:
  346. /**
  347. @brief Create an action with the number of times that the current grid will be reused.
  348. @param times Specify times the grid will be reused.
  349. @return Return a pointer of ReuseGrid. When the creation failed, return nil.
  350. */
  351. static ReuseGrid* create(int times);
  352. // Override
  353. virtual void startWithTarget(Node *target) override;
  354. virtual ReuseGrid* clone() const override;
  355. virtual ReuseGrid* reverse() const override;
  356. CC_CONSTRUCTOR_ACCESS:
  357. ReuseGrid() {}
  358. virtual ~ReuseGrid() {}
  359. /**
  360. @brief Initializes an action with the number of times that the current grid will be reused.
  361. @param times Specify times the grid will be reused.
  362. @return If the initialization success, return true; otherwise, return false.
  363. */
  364. bool initWithTimes(int times);
  365. protected:
  366. NodeGrid* _gridNodeTarget;
  367. void cacheTargetAsGridNode();
  368. int _times;
  369. private:
  370. CC_DISALLOW_COPY_AND_ASSIGN(ReuseGrid);
  371. };
  372. // end of actions group
  373. /// @}
  374. NS_CC_END
  375. #endif // __ACTION_CCGRID_ACTION_H__