CCAction.h 13 KB

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