CCAction.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include "2d/CCAction.h"
  25. #include "2d/CCActionInterval.h"
  26. #include "2d/CCNode.h"
  27. #include "base/CCDirector.h"
  28. #include "base/ccUTF8.h"
  29. NS_CC_BEGIN
  30. //
  31. // Action Base Class
  32. //
  33. Action::Action()
  34. :_originalTarget(nullptr)
  35. ,_target(nullptr)
  36. ,_tag(Action::INVALID_TAG)
  37. ,_flags(0)
  38. {
  39. #if CC_ENABLE_SCRIPT_BINDING
  40. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  41. _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
  42. #endif
  43. }
  44. Action::~Action()
  45. {
  46. CCLOGINFO("deallocing Action: %p - tag: %i", this, _tag);
  47. }
  48. std::string Action::description() const
  49. {
  50. return StringUtils::format("<Action | Tag = %d", _tag);
  51. }
  52. void Action::startWithTarget(Node *aTarget)
  53. {
  54. _originalTarget = _target = aTarget;
  55. }
  56. void Action::stop()
  57. {
  58. _target = nullptr;
  59. }
  60. bool Action::isDone() const
  61. {
  62. return true;
  63. }
  64. void Action::step(float /*dt*/)
  65. {
  66. CCLOG("[Action step]. override me");
  67. }
  68. void Action::update(float /*time*/)
  69. {
  70. CCLOG("[Action update]. override me");
  71. }
  72. //
  73. // Speed
  74. //
  75. Speed::Speed()
  76. : _speed(0.0)
  77. , _innerAction(nullptr)
  78. {
  79. }
  80. Speed::~Speed()
  81. {
  82. CC_SAFE_RELEASE(_innerAction);
  83. }
  84. Speed* Speed::create(ActionInterval* action, float speed)
  85. {
  86. Speed *ret = new (std::nothrow) Speed();
  87. if (ret && ret->initWithAction(action, speed))
  88. {
  89. ret->autorelease();
  90. return ret;
  91. }
  92. CC_SAFE_DELETE(ret);
  93. return nullptr;
  94. }
  95. bool Speed::initWithAction(ActionInterval *action, float speed)
  96. {
  97. CCASSERT(action != nullptr, "action must not be NULL");
  98. if (action == nullptr)
  99. {
  100. log("Speed::initWithAction error: action is nullptr!");
  101. return false;
  102. }
  103. action->retain();
  104. _innerAction = action;
  105. _speed = speed;
  106. return true;
  107. }
  108. Speed *Speed::clone() const
  109. {
  110. // no copy constructor
  111. if (_innerAction)
  112. return Speed::create(_innerAction->clone(), _speed);
  113. return nullptr;
  114. }
  115. void Speed::startWithTarget(Node* target)
  116. {
  117. if (target && _innerAction)
  118. {
  119. Action::startWithTarget(target);
  120. _innerAction->startWithTarget(target);
  121. }
  122. else
  123. log("Speed::startWithTarget error: target(%p) or _innerAction(%p) is nullptr!", target, _innerAction);
  124. }
  125. void Speed::stop()
  126. {
  127. if (_innerAction)
  128. _innerAction->stop();
  129. Action::stop();
  130. }
  131. void Speed::step(float dt)
  132. {
  133. _innerAction->step(dt * _speed);
  134. }
  135. bool Speed::isDone() const
  136. {
  137. return _innerAction->isDone();
  138. }
  139. Speed *Speed::reverse() const
  140. {
  141. if (_innerAction)
  142. return Speed::create(_innerAction->reverse(), _speed);
  143. return nullptr;
  144. }
  145. void Speed::setInnerAction(ActionInterval *action)
  146. {
  147. if (_innerAction != action)
  148. {
  149. CC_SAFE_RELEASE(_innerAction);
  150. _innerAction = action;
  151. CC_SAFE_RETAIN(_innerAction);
  152. }
  153. }
  154. //
  155. // Follow
  156. //
  157. Follow::~Follow()
  158. {
  159. CC_SAFE_RELEASE(_followedNode);
  160. }
  161. Follow* Follow::create(Node *followedNode, const Rect& rect/* = Rect::ZERO*/)
  162. {
  163. return createWithOffset(followedNode, 0.0, 0.0,rect);
  164. }
  165. Follow* Follow::createWithOffset(Node* followedNode,float xOffset,float yOffset,const Rect& rect/*= Rect::ZERO*/){
  166. Follow *follow = new (std::nothrow) Follow();
  167. bool valid;
  168. if(follow)
  169. valid = follow->initWithTargetAndOffset(followedNode, xOffset, yOffset,rect);
  170. if (follow && valid)
  171. {
  172. follow->autorelease();
  173. return follow;
  174. }
  175. delete follow;
  176. return nullptr;
  177. }
  178. Follow* Follow::clone() const
  179. {
  180. // no copy constructor
  181. return Follow::createWithOffset(_followedNode, _offsetX,_offsetY,_worldRect);
  182. }
  183. Follow* Follow::reverse() const
  184. {
  185. return clone();
  186. }
  187. bool Follow::initWithTargetAndOffset(Node *followedNode, float xOffset,float yOffset,const Rect& rect)
  188. {
  189. CCASSERT(followedNode != nullptr, "FollowedNode can't be NULL");
  190. if(followedNode == nullptr)
  191. {
  192. log("Follow::initWithTarget error: followedNode is nullptr!");
  193. return false;
  194. }
  195. followedNode->retain();
  196. _followedNode = followedNode;
  197. _worldRect = rect;
  198. _boundarySet = !rect.equals(Rect::ZERO);
  199. _boundaryFullyCovered = false;
  200. Size winSize = Director::getInstance()->getWinSize();
  201. _fullScreenSize.set(winSize.width, winSize.height);
  202. _halfScreenSize = _fullScreenSize * 0.5f;
  203. _offsetX=xOffset;
  204. _offsetY=yOffset;
  205. _halfScreenSize.x += _offsetX;
  206. _halfScreenSize.y += _offsetY;
  207. if (_boundarySet)
  208. {
  209. _leftBoundary = -((rect.origin.x+rect.size.width) - _fullScreenSize.x);
  210. _rightBoundary = -rect.origin.x ;
  211. _topBoundary = -rect.origin.y;
  212. _bottomBoundary = -((rect.origin.y+rect.size.height) - _fullScreenSize.y);
  213. if(_rightBoundary < _leftBoundary)
  214. {
  215. // screen width is larger than world's boundary width
  216. //set both in the middle of the world
  217. _rightBoundary = _leftBoundary = (_leftBoundary + _rightBoundary) / 2;
  218. }
  219. if(_topBoundary < _bottomBoundary)
  220. {
  221. // screen width is larger than world's boundary width
  222. //set both in the middle of the world
  223. _topBoundary = _bottomBoundary = (_topBoundary + _bottomBoundary) / 2;
  224. }
  225. if( (_topBoundary == _bottomBoundary) && (_leftBoundary == _rightBoundary) )
  226. {
  227. _boundaryFullyCovered = true;
  228. }
  229. }
  230. return true;
  231. }
  232. bool Follow::initWithTarget(Node *followedNode, const Rect& rect /*= Rect::ZERO*/){
  233. return initWithTargetAndOffset(followedNode, 0.0, 0.0,rect);
  234. }
  235. void Follow::step(float /*dt*/)
  236. {
  237. if(_boundarySet)
  238. {
  239. // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
  240. if(_boundaryFullyCovered)
  241. {
  242. return;
  243. }
  244. Vec2 tempPos = _halfScreenSize - _followedNode->getPosition();
  245. _target->setPosition(clampf(tempPos.x, _leftBoundary, _rightBoundary),
  246. clampf(tempPos.y, _bottomBoundary, _topBoundary));
  247. }
  248. else
  249. {
  250. _target->setPosition(_halfScreenSize - _followedNode->getPosition());
  251. }
  252. }
  253. bool Follow::isDone() const
  254. {
  255. return ( !_followedNode->isRunning() );
  256. }
  257. void Follow::stop()
  258. {
  259. _target = nullptr;
  260. Action::stop();
  261. }
  262. NS_CC_END