CCAction.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __ACTIONS_CCACTION_H__
  25. #define __ACTIONS_CCACTION_H__
  26. #include "base/CCRef.h"
  27. #include "math/CCGeometry.h"
  28. #include "base/CCScriptSupport.h"
  29. NS_CC_BEGIN
  30. class Node;
  31. enum {
  32. kActionUpdate
  33. };
  34. /**
  35. * @addtogroup actions
  36. * @{
  37. */
  38. /**
  39. * @brief Base class for Action objects.
  40. */
  41. class CC_DLL Action : public Ref, public Clonable
  42. {
  43. public:
  44. /** Default tag used for all the actions. */
  45. static const int INVALID_TAG = -1;
  46. /**
  47. * @js NA
  48. * @lua NA
  49. */
  50. virtual std::string description() const;
  51. /** Returns a clone of action.
  52. *
  53. * @return A clone action.
  54. */
  55. virtual Action* clone() const
  56. {
  57. CC_ASSERT(0);
  58. return nullptr;
  59. }
  60. /** Returns a new action that performs the exact reverse of the action.
  61. *
  62. * @return A new action that performs the exact reverse of the action.
  63. * @js NA
  64. */
  65. virtual Action* reverse() const
  66. {
  67. CC_ASSERT(0);
  68. return nullptr;
  69. }
  70. /** Return true if the action has finished.
  71. *
  72. * @return Is true if the action has finished.
  73. */
  74. virtual bool isDone() const;
  75. /** Called before the action start. It will also set the target.
  76. *
  77. * @param target A certain target.
  78. */
  79. virtual void startWithTarget(Node *target);
  80. /**
  81. * Called after the action has finished. It will set the 'target' to nil.
  82. * IMPORTANT: You should never call "Action::stop()" manually. Instead, use: "target->stopAction(action);".
  83. */
  84. virtual void stop();
  85. /** Called every frame with it's delta time, dt in seconds. DON'T override unless you know what you are doing.
  86. *
  87. * @param dt In seconds.
  88. */
  89. virtual void step(float dt);
  90. /**
  91. * Called once per frame. time a value between 0 and 1.
  92. * For example:
  93. * - 0 Means that the action just started.
  94. * - 0.5 Means that the action is in the middle.
  95. * - 1 Means that the action is over.
  96. *
  97. * @param time A value between 0 and 1.
  98. */
  99. virtual void update(float time);
  100. /** Return certain target.
  101. *
  102. * @return A certain target.
  103. */
  104. Node* getTarget() const { return _target; }
  105. /** The action will modify the target properties.
  106. *
  107. * @param target A certain target.
  108. */
  109. void setTarget(Node *target) { _target = target; }
  110. /** Return a original Target.
  111. *
  112. * @return A original Target.
  113. */
  114. Node* getOriginalTarget() const { return _originalTarget; }
  115. /**
  116. * Set the original target, since target can be nil.
  117. * Is the target that were used to run the action. Unless you are doing something complex, like ActionManager, you should NOT call this method.
  118. * The target is 'assigned', it is not 'retained'.
  119. * @since v0.8.2
  120. *
  121. * @param originalTarget Is 'assigned', it is not 'retained'.
  122. */
  123. void setOriginalTarget(Node *originalTarget) { _originalTarget = originalTarget; }
  124. /** Returns a tag that is used to identify the action easily.
  125. *
  126. * @return A tag.
  127. */
  128. int getTag() const { return _tag; }
  129. /** Changes the tag that is used to identify the action easily.
  130. *
  131. * @param tag Used to identify the action easily.
  132. */
  133. void setTag(int tag) { _tag = tag; }
  134. /** Returns a flag field that is used to group the actions easily.
  135. *
  136. * @return A tag.
  137. */
  138. unsigned int getFlags() const { return _flags; }
  139. /** Changes the flag field that is used to group the actions easily.
  140. *
  141. * @param flags Used to group the actions easily.
  142. */
  143. void setFlags(unsigned int flags) { _flags = flags; }
  144. CC_CONSTRUCTOR_ACCESS:
  145. Action();
  146. virtual ~Action();
  147. protected:
  148. Node *_originalTarget;
  149. /**
  150. * The "target".
  151. * The target will be set with the 'startWithTarget' method.
  152. * When the 'stop' method is called, target will be set to nil.
  153. * The target is 'assigned', it is not 'retained'.
  154. */
  155. Node *_target;
  156. /** The action tag. An identifier of the action. */
  157. int _tag;
  158. /** The action flag field. To categorize action into certain groups.*/
  159. unsigned int _flags;
  160. #if CC_ENABLE_SCRIPT_BINDING
  161. ccScriptType _scriptType; ///< type of script binding, lua or javascript
  162. #endif
  163. private:
  164. CC_DISALLOW_COPY_AND_ASSIGN(Action);
  165. };
  166. /** @class FiniteTimeAction
  167. * @brief
  168. * Base class actions that do have a finite time duration.
  169. * Possible actions:
  170. * - An action with a duration of 0 seconds.
  171. * - An action with a duration of 35.5 seconds.
  172. * Infinite time actions are valid.
  173. */
  174. class CC_DLL FiniteTimeAction : public Action
  175. {
  176. public:
  177. /** Get duration in seconds of the action.
  178. *
  179. * @return The duration in seconds of the action.
  180. */
  181. float getDuration() const { return _duration; }
  182. /** Set duration in seconds of the action.
  183. *
  184. * @param duration In seconds of the action.
  185. */
  186. void setDuration(float duration) { _duration = duration; }
  187. //
  188. // Overrides
  189. //
  190. virtual FiniteTimeAction* reverse() const override
  191. {
  192. CC_ASSERT(0);
  193. return nullptr;
  194. }
  195. virtual FiniteTimeAction* clone() const override
  196. {
  197. CC_ASSERT(0);
  198. return nullptr;
  199. }
  200. CC_CONSTRUCTOR_ACCESS:
  201. FiniteTimeAction()
  202. : _duration(0)
  203. {}
  204. virtual ~FiniteTimeAction(){}
  205. protected:
  206. //! Duration in seconds.
  207. float _duration;
  208. private:
  209. CC_DISALLOW_COPY_AND_ASSIGN(FiniteTimeAction);
  210. };
  211. class ActionInterval;
  212. class RepeatForever;
  213. /** @class Speed
  214. * @brief Changes the speed of an action, making it take longer (speed>1)
  215. * or shorter (speed<1) time.
  216. * Useful to simulate 'slow motion' or 'fast forward' effect.
  217. * @warning This action can't be Sequenceable because it is not an IntervalAction.
  218. */
  219. class CC_DLL Speed : public Action
  220. {
  221. public:
  222. /** Create the action and set the speed.
  223. *
  224. * @param action An action.
  225. * @param speed The action speed.
  226. */
  227. static Speed* create(ActionInterval* action, float speed);
  228. /** Return the speed.
  229. *
  230. * @return The action speed.
  231. */
  232. float getSpeed() const { return _speed; }
  233. /** Alter the speed of the inner function in runtime.
  234. *
  235. * @param speed Alter the speed of the inner function in runtime.
  236. */
  237. void setSpeed(float speed) { _speed = speed; }
  238. /** Replace the interior action.
  239. *
  240. * @param action The new action, it will replace the running action.
  241. */
  242. void setInnerAction(ActionInterval *action);
  243. /** Return the interior action.
  244. *
  245. * @return The interior action.
  246. */
  247. ActionInterval* getInnerAction() const { return _innerAction; }
  248. //
  249. // Override
  250. //
  251. virtual Speed* clone() const override;
  252. virtual Speed* reverse() const override;
  253. virtual void startWithTarget(Node* target) override;
  254. virtual void stop() override;
  255. /**
  256. * @param dt in seconds.
  257. */
  258. virtual void step(float dt) override;
  259. /** Return true if the action has finished.
  260. *
  261. * @return Is true if the action has finished.
  262. */
  263. virtual bool isDone() const override;
  264. CC_CONSTRUCTOR_ACCESS:
  265. Speed();
  266. virtual ~Speed(void);
  267. /** Initializes the action. */
  268. bool initWithAction(ActionInterval *action, float speed);
  269. protected:
  270. float _speed;
  271. ActionInterval *_innerAction;
  272. private:
  273. CC_DISALLOW_COPY_AND_ASSIGN(Speed);
  274. };
  275. /** @class Follow
  276. * @brief Follow is an action that "follows" a node.
  277. * Eg:
  278. * @code
  279. * layer->runAction(Follow::create(hero));
  280. * @endcode
  281. * Instead of using Camera as a "follower", use this action instead.
  282. * @since v0.99.2
  283. */
  284. class CC_DLL Follow : public Action
  285. {
  286. public:
  287. /**
  288. * Creates the action with a set boundary or with no boundary.
  289. *
  290. * @param followedNode The node to be followed.
  291. * @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
  292. * with no boundary.
  293. */
  294. static Follow* create(Node *followedNode, const Rect& rect = Rect::ZERO);
  295. /**
  296. * Creates the action with a set boundary or with no boundary with offsets.
  297. *
  298. * @param followedNode The node to be followed.
  299. * @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
  300. * with no boundary.
  301. * @param xOffset The horizontal offset from the center of the screen from which the
  302. * node is to be followed.It can be positive,negative or zero.If
  303. * set to zero the node will be horizontally centered followed.
  304. * @param yOffset The vertical offset from the center of the screen from which the
  305. * node is to be followed.It can be positive,negative or zero.
  306. * If set to zero the node will be vertically centered followed.
  307. * If both xOffset and yOffset are set to zero,then the node will be horizontally and vertically centered followed.
  308. */
  309. static Follow* createWithOffset(Node* followedNode,float xOffset,float yOffset,const Rect& rect = Rect::ZERO);
  310. /** Return boundarySet.
  311. *
  312. * @return Return boundarySet.
  313. */
  314. bool isBoundarySet() const { return _boundarySet; }
  315. /** Alter behavior - turn on/off boundary.
  316. *
  317. * @param value Turn on/off boundary.
  318. */
  319. void setBoundarySet(bool value) { _boundarySet = value; }
  320. /** @deprecated Alter behavior - turn on/off boundary.
  321. *
  322. * @param value Turn on/off boundary.
  323. */
  324. CC_DEPRECATED_ATTRIBUTE void setBoudarySet(bool value) { setBoundarySet(value); }
  325. //
  326. // Override
  327. //
  328. virtual Follow* clone() const override;
  329. virtual Follow* reverse() const override;
  330. /**
  331. * @param dt in seconds.
  332. * @js NA
  333. */
  334. virtual void step(float dt) override;
  335. virtual bool isDone() const override;
  336. virtual void stop() override;
  337. CC_CONSTRUCTOR_ACCESS:
  338. /**
  339. * @js ctor
  340. */
  341. Follow()
  342. : _followedNode(nullptr)
  343. , _boundarySet(false)
  344. , _boundaryFullyCovered(false)
  345. , _leftBoundary(0.0)
  346. , _rightBoundary(0.0)
  347. , _topBoundary(0.0)
  348. , _bottomBoundary(0.0)
  349. , _offsetX(0.0)
  350. , _offsetY(0.0)
  351. , _worldRect(Rect::ZERO)
  352. {}
  353. /**
  354. * @js NA
  355. * @lua NA
  356. */
  357. virtual ~Follow();
  358. /**
  359. * Initializes the action with a set boundary or with no boundary.
  360. *
  361. * @param followedNode The node to be followed.
  362. * @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
  363. * with no boundary.
  364. */
  365. bool initWithTarget(Node *followedNode, const Rect& rect = Rect::ZERO);
  366. /**
  367. * Initializes the action with a set boundary or with no boundary with offsets.
  368. *
  369. * @param followedNode The node to be followed.
  370. * @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
  371. * with no boundary.
  372. * @param xOffset The horizontal offset from the center of the screen from which the
  373. * node is to be followed.It can be positive,negative or zero.If
  374. * set to zero the node will be horizontally centered followed.
  375. * @param yOffset The vertical offset from the center of the screen from which the
  376. * node is to be followed.It can be positive,negative or zero.
  377. * If set to zero the node will be vertically centered followed.
  378. * If both xOffset and yOffset are set to zero,then the node will be horizontally and vertically centered followed.
  379. */
  380. bool initWithTargetAndOffset(Node *followedNode,float xOffset,float yOffset,const Rect& rect = Rect::ZERO);
  381. protected:
  382. /** Node to follow. */
  383. Node *_followedNode;
  384. /** Whether camera should be limited to certain area. */
  385. bool _boundarySet;
  386. /** If screen size is bigger than the boundary - update not needed. */
  387. bool _boundaryFullyCovered;
  388. /** Fast access to the screen dimensions. */
  389. Vec2 _halfScreenSize;
  390. Vec2 _fullScreenSize;
  391. /** World boundaries. */
  392. float _leftBoundary;
  393. float _rightBoundary;
  394. float _topBoundary;
  395. float _bottomBoundary;
  396. /** Horizontal (x) and vertical (y) offset values. */
  397. float _offsetX;
  398. float _offsetY;
  399. Rect _worldRect;
  400. private:
  401. CC_DISALLOW_COPY_AND_ASSIGN(Follow);
  402. };
  403. // end of actions group
  404. /// @}
  405. NS_CC_END
  406. #endif // __ACTIONS_CCACTION_H__