CCActionGrid.h 13 KB

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