CCAction.cpp 7.5 KB

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