CCActionInterval.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  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/CCActionInterval.h"
  24. #include <stdarg.h>
  25. #include "2d/CCSprite.h"
  26. #include "2d/CCNode.h"
  27. #include "2d/CCSpriteFrame.h"
  28. #include "2d/CCActionInstant.h"
  29. #include "base/CCDirector.h"
  30. #include "base/CCEventCustom.h"
  31. #include "base/CCEventDispatcher.h"
  32. #include "platform/CCStdC.h"
  33. #include "base/CCScriptSupport.h"
  34. NS_CC_BEGIN
  35. // Extra action for making a Sequence or Spawn when only adding one action to it.
  36. class ExtraAction : public FiniteTimeAction
  37. {
  38. public:
  39. static ExtraAction* create();
  40. virtual ExtraAction* clone() const;
  41. virtual ExtraAction* reverse(void) const;
  42. virtual void update(float time);
  43. virtual void step(float dt);
  44. };
  45. ExtraAction* ExtraAction::create()
  46. {
  47. ExtraAction* ret = new (std::nothrow) ExtraAction();
  48. if (ret)
  49. {
  50. ret->autorelease();
  51. }
  52. return ret;
  53. }
  54. ExtraAction* ExtraAction::clone() const
  55. {
  56. // no copy constructor
  57. return ExtraAction::create();
  58. }
  59. ExtraAction* ExtraAction::reverse() const
  60. {
  61. return ExtraAction::create();
  62. }
  63. void ExtraAction::update(float /*time*/)
  64. {
  65. }
  66. void ExtraAction::step(float /*dt*/)
  67. {
  68. }
  69. //
  70. // IntervalAction
  71. //
  72. bool ActionInterval::initWithDuration(float d)
  73. {
  74. _duration = d;
  75. // prevent division by 0
  76. // This comparison could be in step:, but it might decrease the performance
  77. // by 3% in heavy based action games.
  78. if (_duration <= FLT_EPSILON)
  79. {
  80. _duration = FLT_EPSILON;
  81. }
  82. _elapsed = 0;
  83. _firstTick = true;
  84. return true;
  85. }
  86. bool ActionInterval::sendUpdateEventToScript(float dt, Action *actionObject)
  87. {
  88. #if CC_ENABLE_SCRIPT_BINDING
  89. if (_scriptType == kScriptTypeJavascript)
  90. {
  91. if (ScriptEngineManager::sendActionEventToJS(actionObject, kActionUpdate, (void *)&dt))
  92. return true;
  93. }
  94. #endif
  95. return false;
  96. }
  97. bool ActionInterval::isDone() const
  98. {
  99. // fix #14936 _duration is not 0, but _elapsed is 0.
  100. return (_elapsed + FLT_EPSILON) >= _duration;
  101. }
  102. void ActionInterval::step(float dt)
  103. {
  104. if (_firstTick)
  105. {
  106. _firstTick = false;
  107. _elapsed = 0;
  108. }
  109. else
  110. {
  111. _elapsed += dt;
  112. }
  113. float updateDt = MAX (0, // needed for rewind. elapsed could be negative
  114. MIN(1, _elapsed / _duration)
  115. );
  116. if (sendUpdateEventToScript(updateDt, this)) return;
  117. this->update(updateDt);
  118. }
  119. void ActionInterval::setAmplitudeRate(float /*amp*/)
  120. {
  121. // Abstract class needs implementation
  122. CCASSERT(0, "Subclass should implement this method!");
  123. }
  124. float ActionInterval::getAmplitudeRate()
  125. {
  126. // Abstract class needs implementation
  127. CCASSERT(0, "Subclass should implement this method!");
  128. return 0;
  129. }
  130. void ActionInterval::startWithTarget(Node *target)
  131. {
  132. FiniteTimeAction::startWithTarget(target);
  133. _elapsed = 0.0f;
  134. _firstTick = true;
  135. }
  136. //
  137. // Sequence
  138. //
  139. Sequence* Sequence::createWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo)
  140. {
  141. Sequence *sequence = new (std::nothrow) Sequence();
  142. if (sequence && sequence->initWithTwoActions(actionOne, actionTwo))
  143. {
  144. sequence->autorelease();
  145. return sequence;
  146. }
  147. delete sequence;
  148. return nullptr;
  149. }
  150. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  151. Sequence* Sequence::variadicCreate(FiniteTimeAction *action1, ...)
  152. {
  153. va_list params;
  154. va_start(params, action1);
  155. Sequence *ret = Sequence::createWithVariableList(action1, params);
  156. va_end(params);
  157. return ret;
  158. }
  159. #else
  160. Sequence* Sequence::create(FiniteTimeAction *action1, ...)
  161. {
  162. va_list params;
  163. va_start(params, action1);
  164. Sequence *ret = Sequence::createWithVariableList(action1, params);
  165. va_end(params);
  166. return ret;
  167. }
  168. #endif
  169. Sequence* Sequence::createWithVariableList(FiniteTimeAction *action1, va_list args)
  170. {
  171. FiniteTimeAction *now;
  172. FiniteTimeAction *prev = action1;
  173. bool bOneAction = true;
  174. while (action1)
  175. {
  176. now = va_arg(args, FiniteTimeAction*);
  177. if (now)
  178. {
  179. prev = createWithTwoActions(prev, now);
  180. bOneAction = false;
  181. }
  182. else
  183. {
  184. // If only one action is added to Sequence, make up a Sequence by adding a simplest finite time action.
  185. if (bOneAction)
  186. {
  187. prev = createWithTwoActions(prev, ExtraAction::create());
  188. }
  189. break;
  190. }
  191. }
  192. return ((Sequence*)prev);
  193. }
  194. Sequence* Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions)
  195. {
  196. Sequence* seq = new (std::nothrow) Sequence;
  197. if (seq && seq->init(arrayOfActions))
  198. {
  199. seq->autorelease();
  200. return seq;
  201. }
  202. delete seq;
  203. return nullptr;
  204. }
  205. bool Sequence::init(const Vector<FiniteTimeAction*>& arrayOfActions)
  206. {
  207. auto count = arrayOfActions.size();
  208. if (count == 0)
  209. return false;
  210. if (count == 1)
  211. return initWithTwoActions(arrayOfActions.at(0), ExtraAction::create());
  212. // else size > 1
  213. auto prev = arrayOfActions.at(0);
  214. for (int i = 1; i < count-1; ++i)
  215. {
  216. prev = createWithTwoActions(prev, arrayOfActions.at(i));
  217. }
  218. return initWithTwoActions(prev, arrayOfActions.at(count-1));
  219. }
  220. bool Sequence::initWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo)
  221. {
  222. CCASSERT(actionOne != nullptr, "actionOne can't be nullptr!");
  223. CCASSERT(actionTwo != nullptr, "actionTwo can't be nullptr!");
  224. if (actionOne == nullptr || actionTwo == nullptr)
  225. {
  226. log("Sequence::initWithTwoActions error: action is nullptr!!");
  227. return false;
  228. }
  229. float d = actionOne->getDuration() + actionTwo->getDuration();
  230. ActionInterval::initWithDuration(d);
  231. _actions[0] = actionOne;
  232. actionOne->retain();
  233. _actions[1] = actionTwo;
  234. actionTwo->retain();
  235. return true;
  236. }
  237. Sequence* Sequence::clone() const
  238. {
  239. // no copy constructor
  240. if (_actions[0] && _actions[1])
  241. {
  242. return Sequence::create(_actions[0]->clone(), _actions[1]->clone(), nullptr);
  243. } else {
  244. return nullptr;
  245. }
  246. }
  247. Sequence::Sequence()
  248. : _split(0)
  249. {
  250. _actions[0] = nullptr;
  251. _actions[1] = nullptr;
  252. }
  253. Sequence::~Sequence()
  254. {
  255. CC_SAFE_RELEASE(_actions[0]);
  256. CC_SAFE_RELEASE(_actions[1]);
  257. }
  258. void Sequence::startWithTarget(Node *target)
  259. {
  260. if (target == nullptr)
  261. {
  262. log("Sequence::startWithTarget error: target is nullptr!");
  263. return;
  264. }
  265. if (_actions[0] == nullptr || _actions[1] == nullptr)
  266. {
  267. log("Sequence::startWithTarget error: _actions[0] or _actions[1] is nullptr!");
  268. return;
  269. }
  270. if (_duration > FLT_EPSILON)
  271. // fix #14936 - FLT_EPSILON (instant action) / very fast duration (0.001) leads to worng split, that leads to call instant action few times
  272. _split = _actions[0]->getDuration() > FLT_EPSILON ? _actions[0]->getDuration() / _duration : 0;
  273. ActionInterval::startWithTarget(target);
  274. _last = -1;
  275. }
  276. void Sequence::stop(void)
  277. {
  278. // Issue #1305
  279. if( _last != - 1 && _actions[_last])
  280. {
  281. _actions[_last]->stop();
  282. }
  283. ActionInterval::stop();
  284. }
  285. void Sequence::update(float t)
  286. {
  287. int found = 0;
  288. float new_t = 0.0f;
  289. if( t < _split )
  290. {
  291. // action[0]
  292. found = 0;
  293. if( _split != 0 )
  294. new_t = t / _split;
  295. else
  296. new_t = 1;
  297. }
  298. else
  299. {
  300. // action[1]
  301. found = 1;
  302. if ( _split == 1 )
  303. new_t = 1;
  304. else
  305. new_t = (t-_split) / (1 - _split );
  306. }
  307. if ( found==1 )
  308. {
  309. if( _last == -1 )
  310. {
  311. // action[0] was skipped, execute it.
  312. _actions[0]->startWithTarget(_target);
  313. if (!(sendUpdateEventToScript(1.0f, _actions[0])))
  314. _actions[0]->update(1.0f);
  315. _actions[0]->stop();
  316. }
  317. else if( _last == 0 )
  318. {
  319. // switching to action 1. stop action 0.
  320. if (!(sendUpdateEventToScript(1.0f, _actions[0])))
  321. _actions[0]->update(1.0f);
  322. _actions[0]->stop();
  323. }
  324. }
  325. else if(found==0 && _last==1 )
  326. {
  327. // Reverse mode ?
  328. // FIXME: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode"
  329. // since it will require a hack to know if an action is on reverse mode or not.
  330. // "step" should be overridden, and the "reverseMode" value propagated to inner Sequences.
  331. if (!(sendUpdateEventToScript(0, _actions[1])))
  332. _actions[1]->update(0);
  333. _actions[1]->stop();
  334. }
  335. // Last action found and it is done.
  336. if( found == _last && _actions[found]->isDone() )
  337. {
  338. return;
  339. }
  340. // Last action found and it is done
  341. if( found != _last )
  342. {
  343. _actions[found]->startWithTarget(_target);
  344. }
  345. if (!(sendUpdateEventToScript(new_t, _actions[found])))
  346. _actions[found]->update(new_t);
  347. _last = found;
  348. }
  349. Sequence* Sequence::reverse() const
  350. {
  351. if (_actions[0] && _actions[1])
  352. return Sequence::createWithTwoActions(_actions[1]->reverse(), _actions[0]->reverse());
  353. else
  354. return nullptr;
  355. }
  356. //
  357. // Repeat
  358. //
  359. Repeat* Repeat::create(FiniteTimeAction *action, unsigned int times)
  360. {
  361. Repeat* repeat = new (std::nothrow) Repeat();
  362. if (repeat && repeat->initWithAction(action, times))
  363. {
  364. repeat->autorelease();
  365. return repeat;
  366. }
  367. delete repeat;
  368. return nullptr;
  369. }
  370. bool Repeat::initWithAction(FiniteTimeAction *action, unsigned int times)
  371. {
  372. float d = action->getDuration() * times;
  373. if (action && ActionInterval::initWithDuration(d))
  374. {
  375. _times = times;
  376. _innerAction = action;
  377. action->retain();
  378. _actionInstant = dynamic_cast<ActionInstant*>(action) ? true : false;
  379. //an instant action needs to be executed one time less in the update method since it uses startWithTarget to execute the action
  380. // minggo: instant action doesn't execute action in Repeat::startWithTarget(), so comment it.
  381. // if (_actionInstant)
  382. // {
  383. // _times -=1;
  384. // }
  385. _total = 0;
  386. return true;
  387. }
  388. return false;
  389. }
  390. Repeat* Repeat::clone(void) const
  391. {
  392. // no copy constructor
  393. return Repeat::create(_innerAction->clone(), _times);
  394. }
  395. Repeat::~Repeat(void)
  396. {
  397. CC_SAFE_RELEASE(_innerAction);
  398. }
  399. void Repeat::startWithTarget(Node *target)
  400. {
  401. _total = 0;
  402. _nextDt = _innerAction->getDuration()/_duration;
  403. ActionInterval::startWithTarget(target);
  404. _innerAction->startWithTarget(target);
  405. }
  406. void Repeat::stop(void)
  407. {
  408. _innerAction->stop();
  409. ActionInterval::stop();
  410. }
  411. // issue #80. Instead of hooking step:, hook update: since it can be called by any
  412. // container action like Repeat, Sequence, Ease, etc..
  413. void Repeat::update(float dt)
  414. {
  415. if (dt >= _nextDt)
  416. {
  417. while (dt >= _nextDt && _total < _times)
  418. {
  419. if (!(sendUpdateEventToScript(1.0f, _innerAction)))
  420. _innerAction->update(1.0f);
  421. _total++;
  422. _innerAction->stop();
  423. _innerAction->startWithTarget(_target);
  424. _nextDt = _innerAction->getDuration()/_duration * (_total+1);
  425. }
  426. // fix for issue #1288, incorrect end value of repeat
  427. if (std::abs(dt - 1.0f) < FLT_EPSILON && _total < _times)
  428. {
  429. if (!(sendUpdateEventToScript(1.0f, _innerAction)))
  430. _innerAction->update(1.0f);
  431. _total++;
  432. }
  433. // don't set an instant action back or update it, it has no use because it has no duration
  434. if (!_actionInstant)
  435. {
  436. if (_total == _times)
  437. {
  438. // minggo: inner action update is invoked above, don't have to invoke it here
  439. // if (!(sendUpdateEventToScript(1, _innerAction)))
  440. // _innerAction->update(1);
  441. _innerAction->stop();
  442. }
  443. else
  444. {
  445. // issue #390 prevent jerk, use right update
  446. if (!(sendUpdateEventToScript(dt - (_nextDt - _innerAction->getDuration()/_duration), _innerAction)))
  447. _innerAction->update(dt - (_nextDt - _innerAction->getDuration()/_duration));
  448. }
  449. }
  450. }
  451. else
  452. {
  453. if (!(sendUpdateEventToScript(fmodf(dt * _times,1.0f), _innerAction)))
  454. _innerAction->update(fmodf(dt * _times,1.0f));
  455. }
  456. }
  457. bool Repeat::isDone(void) const
  458. {
  459. return _total == _times;
  460. }
  461. Repeat* Repeat::reverse() const
  462. {
  463. return Repeat::create(_innerAction->reverse(), _times);
  464. }
  465. //
  466. // RepeatForever
  467. //
  468. RepeatForever::~RepeatForever()
  469. {
  470. CC_SAFE_RELEASE(_innerAction);
  471. }
  472. RepeatForever *RepeatForever::create(ActionInterval *action)
  473. {
  474. RepeatForever *ret = new (std::nothrow) RepeatForever();
  475. if (ret && ret->initWithAction(action))
  476. {
  477. ret->autorelease();
  478. return ret;
  479. }
  480. delete ret;
  481. return nullptr;
  482. }
  483. bool RepeatForever::initWithAction(ActionInterval *action)
  484. {
  485. CCASSERT(action != nullptr, "action can't be nullptr!");
  486. if (action == nullptr)
  487. {
  488. log("RepeatForever::initWithAction error:action is nullptr!");
  489. return false;
  490. }
  491. action->retain();
  492. _innerAction = action;
  493. return true;
  494. }
  495. RepeatForever *RepeatForever::clone() const
  496. {
  497. // no copy constructor
  498. return RepeatForever::create(_innerAction->clone());
  499. }
  500. void RepeatForever::startWithTarget(Node* target)
  501. {
  502. ActionInterval::startWithTarget(target);
  503. _innerAction->startWithTarget(target);
  504. }
  505. void RepeatForever::step(float dt)
  506. {
  507. _innerAction->step(dt);
  508. if (_innerAction->isDone())
  509. {
  510. float diff = _innerAction->getElapsed() - _innerAction->getDuration();
  511. if (diff > _innerAction->getDuration())
  512. diff = fmodf(diff, _innerAction->getDuration());
  513. _innerAction->startWithTarget(_target);
  514. // to prevent jerk. issue #390, 1247
  515. _innerAction->step(0.0f);
  516. _innerAction->step(diff);
  517. }
  518. }
  519. bool RepeatForever::isDone() const
  520. {
  521. return false;
  522. }
  523. RepeatForever *RepeatForever::reverse() const
  524. {
  525. return RepeatForever::create(_innerAction->reverse());
  526. }
  527. //
  528. // Spawn
  529. //
  530. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  531. Spawn* Spawn::variadicCreate(FiniteTimeAction *action1, ...)
  532. {
  533. va_list params;
  534. va_start(params, action1);
  535. Spawn *ret = Spawn::createWithVariableList(action1, params);
  536. va_end(params);
  537. return ret;
  538. }
  539. #else
  540. Spawn* Spawn::create(FiniteTimeAction *action1, ...)
  541. {
  542. va_list params;
  543. va_start(params, action1);
  544. Spawn *ret = Spawn::createWithVariableList(action1, params);
  545. va_end(params);
  546. return ret;
  547. }
  548. #endif
  549. Spawn* Spawn::createWithVariableList(FiniteTimeAction *action1, va_list args)
  550. {
  551. FiniteTimeAction *now;
  552. FiniteTimeAction *prev = action1;
  553. bool oneAction = true;
  554. while (action1)
  555. {
  556. now = va_arg(args, FiniteTimeAction*);
  557. if (now)
  558. {
  559. prev = createWithTwoActions(prev, now);
  560. oneAction = false;
  561. }
  562. else
  563. {
  564. // If only one action is added to Spawn, make up a Spawn by adding a simplest finite time action.
  565. if (oneAction)
  566. {
  567. prev = createWithTwoActions(prev, ExtraAction::create());
  568. }
  569. break;
  570. }
  571. }
  572. return ((Spawn*)prev);
  573. }
  574. Spawn* Spawn::create(const Vector<FiniteTimeAction*>& arrayOfActions)
  575. {
  576. Spawn* ret = new (std::nothrow) Spawn;
  577. if (ret && ret->init(arrayOfActions))
  578. {
  579. ret->autorelease();
  580. return ret;
  581. }
  582. delete ret;
  583. return nullptr;
  584. }
  585. Spawn* Spawn::createWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2)
  586. {
  587. Spawn *spawn = new (std::nothrow) Spawn();
  588. if (spawn && spawn->initWithTwoActions(action1, action2))
  589. {
  590. spawn->autorelease();
  591. return spawn;
  592. }
  593. delete spawn;
  594. return nullptr;
  595. }
  596. bool Spawn::init(const Vector<FiniteTimeAction*>& arrayOfActions)
  597. {
  598. auto count = arrayOfActions.size();
  599. if (count == 0)
  600. return false;
  601. if (count == 1)
  602. return initWithTwoActions(arrayOfActions.at(0), ExtraAction::create());
  603. // else count > 1
  604. auto prev = arrayOfActions.at(0);
  605. for (int i = 1; i < count-1; ++i)
  606. {
  607. prev = createWithTwoActions(prev, arrayOfActions.at(i));
  608. }
  609. return initWithTwoActions(prev, arrayOfActions.at(count-1));
  610. }
  611. bool Spawn::initWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2)
  612. {
  613. CCASSERT(action1 != nullptr, "action1 can't be nullptr!");
  614. CCASSERT(action2 != nullptr, "action2 can't be nullptr!");
  615. if (action1 == nullptr || action2 == nullptr)
  616. {
  617. log("Spawn::initWithTwoActions error: action is nullptr!");
  618. return false;
  619. }
  620. bool ret = false;
  621. float d1 = action1->getDuration();
  622. float d2 = action2->getDuration();
  623. if (ActionInterval::initWithDuration(MAX(d1, d2)))
  624. {
  625. _one = action1;
  626. _two = action2;
  627. if (d1 > d2)
  628. {
  629. _two = Sequence::createWithTwoActions(action2, DelayTime::create(d1 - d2));
  630. }
  631. else if (d1 < d2)
  632. {
  633. _one = Sequence::createWithTwoActions(action1, DelayTime::create(d2 - d1));
  634. }
  635. _one->retain();
  636. _two->retain();
  637. ret = true;
  638. }
  639. return ret;
  640. }
  641. Spawn* Spawn::clone(void) const
  642. {
  643. // no copy constructor
  644. if (_one && _two)
  645. return Spawn::createWithTwoActions(_one->clone(), _two->clone());
  646. else
  647. return nullptr;
  648. }
  649. Spawn::Spawn()
  650. : _one(nullptr)
  651. , _two(nullptr)
  652. {
  653. }
  654. Spawn::~Spawn(void)
  655. {
  656. CC_SAFE_RELEASE(_one);
  657. CC_SAFE_RELEASE(_two);
  658. }
  659. void Spawn::startWithTarget(Node *target)
  660. {
  661. if (target == nullptr)
  662. {
  663. log("Spawn::startWithTarget error: target is nullptr!");
  664. return;
  665. }
  666. if (_one == nullptr || _two == nullptr)
  667. {
  668. log("Spawn::startWithTarget error: _one or _two is nullptr!");
  669. return;
  670. }
  671. ActionInterval::startWithTarget(target);
  672. _one->startWithTarget(target);
  673. _two->startWithTarget(target);
  674. }
  675. void Spawn::stop(void)
  676. {
  677. if (_one)
  678. _one->stop();
  679. if (_two)
  680. _two->stop();
  681. ActionInterval::stop();
  682. }
  683. void Spawn::update(float time)
  684. {
  685. if (_one)
  686. {
  687. if (!(sendUpdateEventToScript(time, _one)))
  688. _one->update(time);
  689. }
  690. if (_two)
  691. {
  692. if (!(sendUpdateEventToScript(time, _two)))
  693. _two->update(time);
  694. }
  695. }
  696. Spawn* Spawn::reverse() const
  697. {
  698. if (_one && _two)
  699. return Spawn::createWithTwoActions(_one->reverse(), _two->reverse());
  700. return nullptr;
  701. }
  702. //
  703. // RotateTo
  704. //
  705. RotateTo* RotateTo::create(float duration, float dstAngle)
  706. {
  707. RotateTo* rotateTo = new (std::nothrow) RotateTo();
  708. if (rotateTo && rotateTo->initWithDuration(duration, dstAngle, dstAngle))
  709. {
  710. rotateTo->autorelease();
  711. return rotateTo;
  712. }
  713. delete rotateTo;
  714. return nullptr;
  715. }
  716. RotateTo* RotateTo::create(float duration, float dstAngleX, float dstAngleY)
  717. {
  718. RotateTo* rotateTo = new (std::nothrow) RotateTo();
  719. if (rotateTo && rotateTo->initWithDuration(duration, dstAngleX, dstAngleY))
  720. {
  721. rotateTo->autorelease();
  722. return rotateTo;
  723. }
  724. delete rotateTo;
  725. return nullptr;
  726. }
  727. RotateTo* RotateTo::create(float duration, const Vec3& dstAngle3D)
  728. {
  729. RotateTo* rotateTo = new (std::nothrow) RotateTo();
  730. if(rotateTo && rotateTo->initWithDuration(duration, dstAngle3D))
  731. {
  732. rotateTo->autorelease();
  733. return rotateTo;
  734. }
  735. delete rotateTo;
  736. return nullptr;
  737. }
  738. RotateTo::RotateTo()
  739. : _is3D(false)
  740. {
  741. }
  742. bool RotateTo::initWithDuration(float duration, float dstAngleX, float dstAngleY)
  743. {
  744. if (ActionInterval::initWithDuration(duration))
  745. {
  746. _dstAngle.x = dstAngleX;
  747. _dstAngle.y = dstAngleY;
  748. return true;
  749. }
  750. return false;
  751. }
  752. bool RotateTo::initWithDuration(float duration, const Vec3& dstAngle3D)
  753. {
  754. if (ActionInterval::initWithDuration(duration))
  755. {
  756. _dstAngle = dstAngle3D;
  757. _is3D = true;
  758. return true;
  759. }
  760. return false;
  761. }
  762. RotateTo* RotateTo::clone(void) const
  763. {
  764. // no copy constructor
  765. auto a = new (std::nothrow) RotateTo();
  766. if(_is3D)
  767. a->initWithDuration(_duration, _dstAngle);
  768. else
  769. a->initWithDuration(_duration, _dstAngle.x, _dstAngle.y);
  770. a->autorelease();
  771. return a;
  772. }
  773. void RotateTo::calculateAngles(float &startAngle, float &diffAngle, float dstAngle)
  774. {
  775. if (startAngle > 0)
  776. {
  777. startAngle = fmodf(startAngle, 360.0f);
  778. }
  779. else
  780. {
  781. startAngle = fmodf(startAngle, -360.0f);
  782. }
  783. diffAngle = dstAngle - startAngle;
  784. if (diffAngle > 180)
  785. {
  786. diffAngle -= 360;
  787. }
  788. if (diffAngle < -180)
  789. {
  790. diffAngle += 360;
  791. }
  792. }
  793. void RotateTo::startWithTarget(Node *target)
  794. {
  795. ActionInterval::startWithTarget(target);
  796. if (_is3D)
  797. {
  798. _startAngle = _target->getRotation3D();
  799. }
  800. else
  801. {
  802. _startAngle.x = _target->getRotationSkewX();
  803. _startAngle.y = _target->getRotationSkewY();
  804. }
  805. calculateAngles(_startAngle.x, _diffAngle.x, _dstAngle.x);
  806. calculateAngles(_startAngle.y, _diffAngle.y, _dstAngle.y);
  807. calculateAngles(_startAngle.z, _diffAngle.z, _dstAngle.z);
  808. }
  809. void RotateTo::update(float time)
  810. {
  811. if (_target)
  812. {
  813. if(_is3D)
  814. {
  815. _target->setRotation3D(Vec3(
  816. _startAngle.x + _diffAngle.x * time,
  817. _startAngle.y + _diffAngle.y * time,
  818. _startAngle.z + _diffAngle.z * time
  819. ));
  820. }
  821. else
  822. {
  823. #if CC_USE_PHYSICS
  824. if (_startAngle.x == _startAngle.y && _diffAngle.x == _diffAngle.y)
  825. {
  826. _target->setRotation(_startAngle.x + _diffAngle.x * time);
  827. }
  828. else
  829. {
  830. _target->setRotationSkewX(_startAngle.x + _diffAngle.x * time);
  831. _target->setRotationSkewY(_startAngle.y + _diffAngle.y * time);
  832. }
  833. #else
  834. _target->setRotationSkewX(_startAngle.x + _diffAngle.x * time);
  835. _target->setRotationSkewY(_startAngle.y + _diffAngle.y * time);
  836. #endif // CC_USE_PHYSICS
  837. }
  838. }
  839. }
  840. RotateTo *RotateTo::reverse() const
  841. {
  842. CCASSERT(false, "RotateTo doesn't support the 'reverse' method");
  843. return nullptr;
  844. }
  845. //
  846. // RotateBy
  847. //
  848. RotateBy* RotateBy::create(float duration, float deltaAngle)
  849. {
  850. RotateBy *rotateBy = new (std::nothrow) RotateBy();
  851. if (rotateBy && rotateBy->initWithDuration(duration, deltaAngle))
  852. {
  853. rotateBy->autorelease();
  854. return rotateBy;
  855. }
  856. delete rotateBy;
  857. return nullptr;
  858. }
  859. RotateBy* RotateBy::create(float duration, float deltaAngleX, float deltaAngleY)
  860. {
  861. RotateBy *rotateBy = new (std::nothrow) RotateBy();
  862. if (rotateBy && rotateBy->initWithDuration(duration, deltaAngleX, deltaAngleY))
  863. {
  864. rotateBy->autorelease();
  865. return rotateBy;
  866. }
  867. delete rotateBy;
  868. return nullptr;
  869. }
  870. RotateBy* RotateBy::create(float duration, const Vec3& deltaAngle3D)
  871. {
  872. RotateBy *rotateBy = new (std::nothrow) RotateBy();
  873. if(rotateBy && rotateBy->initWithDuration(duration, deltaAngle3D))
  874. {
  875. rotateBy->autorelease();
  876. return rotateBy;
  877. }
  878. delete rotateBy;
  879. return nullptr;
  880. }
  881. RotateBy::RotateBy()
  882. : _is3D(false)
  883. {
  884. }
  885. bool RotateBy::initWithDuration(float duration, float deltaAngle)
  886. {
  887. if (ActionInterval::initWithDuration(duration))
  888. {
  889. _deltaAngle.x = _deltaAngle.y = deltaAngle;
  890. return true;
  891. }
  892. return false;
  893. }
  894. bool RotateBy::initWithDuration(float duration, float deltaAngleX, float deltaAngleY)
  895. {
  896. if (ActionInterval::initWithDuration(duration))
  897. {
  898. _deltaAngle.x = deltaAngleX;
  899. _deltaAngle.y = deltaAngleY;
  900. return true;
  901. }
  902. return false;
  903. }
  904. bool RotateBy::initWithDuration(float duration, const Vec3& deltaAngle3D)
  905. {
  906. if (ActionInterval::initWithDuration(duration))
  907. {
  908. _deltaAngle = deltaAngle3D;
  909. _is3D = true;
  910. return true;
  911. }
  912. return false;
  913. }
  914. RotateBy* RotateBy::clone() const
  915. {
  916. // no copy constructor
  917. auto a = new (std::nothrow) RotateBy();
  918. if(_is3D)
  919. a->initWithDuration(_duration, _deltaAngle);
  920. else
  921. a->initWithDuration(_duration, _deltaAngle.x, _deltaAngle.y);
  922. a->autorelease();
  923. return a;
  924. }
  925. void RotateBy::startWithTarget(Node *target)
  926. {
  927. ActionInterval::startWithTarget(target);
  928. if(_is3D)
  929. {
  930. _startAngle = target->getRotation3D();
  931. }
  932. else
  933. {
  934. _startAngle.x = target->getRotationSkewX();
  935. _startAngle.y = target->getRotationSkewY();
  936. }
  937. }
  938. void RotateBy::update(float time)
  939. {
  940. // FIXME: shall I add % 360
  941. if (_target)
  942. {
  943. if(_is3D)
  944. {
  945. Vec3 v;
  946. v.x = _startAngle.x + _deltaAngle.x * time;
  947. v.y = _startAngle.y + _deltaAngle.y * time;
  948. v.z = _startAngle.z + _deltaAngle.z * time;
  949. _target->setRotation3D(v);
  950. }
  951. else
  952. {
  953. #if CC_USE_PHYSICS
  954. if (_startAngle.x == _startAngle.y && _deltaAngle.x == _deltaAngle.y)
  955. {
  956. _target->setRotation(_startAngle.x + _deltaAngle.x * time);
  957. }
  958. else
  959. {
  960. _target->setRotationSkewX(_startAngle.x + _deltaAngle.x * time);
  961. _target->setRotationSkewY(_startAngle.y + _deltaAngle.y * time);
  962. }
  963. #else
  964. _target->setRotationSkewX(_startAngle.x + _deltaAngle.x * time);
  965. _target->setRotationSkewY(_startAngle.y + _deltaAngle.y * time);
  966. #endif // CC_USE_PHYSICS
  967. }
  968. }
  969. }
  970. RotateBy* RotateBy::reverse() const
  971. {
  972. if(_is3D)
  973. {
  974. Vec3 v;
  975. v.x = - _deltaAngle.x;
  976. v.y = - _deltaAngle.y;
  977. v.z = - _deltaAngle.z;
  978. return RotateBy::create(_duration, v);
  979. }
  980. else
  981. {
  982. return RotateBy::create(_duration, -_deltaAngle.x, -_deltaAngle.y);
  983. }
  984. }
  985. //
  986. // MoveBy
  987. //
  988. MoveBy* MoveBy::create(float duration, const Vec2& deltaPosition)
  989. {
  990. return MoveBy::create(duration, Vec3(deltaPosition.x, deltaPosition.y, 0));
  991. }
  992. MoveBy* MoveBy::create(float duration, const Vec3 &deltaPosition)
  993. {
  994. MoveBy *ret = new (std::nothrow) MoveBy();
  995. if (ret && ret->initWithDuration(duration, deltaPosition))
  996. {
  997. ret->autorelease();
  998. return ret;
  999. }
  1000. delete ret;
  1001. return nullptr;
  1002. }
  1003. bool MoveBy::initWithDuration(float duration, const Vec2& deltaPosition)
  1004. {
  1005. return MoveBy::initWithDuration(duration, Vec3(deltaPosition.x, deltaPosition.y, 0));
  1006. }
  1007. bool MoveBy::initWithDuration(float duration, const Vec3& deltaPosition)
  1008. {
  1009. bool ret = false;
  1010. if (ActionInterval::initWithDuration(duration))
  1011. {
  1012. _positionDelta = deltaPosition;
  1013. _is3D = true;
  1014. ret = true;
  1015. }
  1016. return ret;
  1017. }
  1018. MoveBy* MoveBy::clone() const
  1019. {
  1020. // no copy constructor
  1021. return MoveBy::create(_duration, _positionDelta);
  1022. }
  1023. void MoveBy::startWithTarget(Node *target)
  1024. {
  1025. ActionInterval::startWithTarget(target);
  1026. _previousPosition = _startPosition = target->getPosition3D();
  1027. }
  1028. MoveBy* MoveBy::reverse() const
  1029. {
  1030. return MoveBy::create(_duration, -_positionDelta);
  1031. }
  1032. void MoveBy::update(float t)
  1033. {
  1034. if (_target)
  1035. {
  1036. #if CC_ENABLE_STACKABLE_ACTIONS
  1037. Vec3 currentPos = _target->getPosition3D();
  1038. Vec3 diff = currentPos - _previousPosition;
  1039. _startPosition = _startPosition + diff;
  1040. Vec3 newPos = _startPosition + (_positionDelta * t);
  1041. _target->setPosition3D(newPos);
  1042. _previousPosition = newPos;
  1043. #else
  1044. _target->setPosition3D(_startPosition + _positionDelta * t);
  1045. #endif // CC_ENABLE_STACKABLE_ACTIONS
  1046. }
  1047. }
  1048. //
  1049. // MoveTo
  1050. //
  1051. MoveTo* MoveTo::create(float duration, const Vec2& position)
  1052. {
  1053. return MoveTo::create(duration, Vec3(position.x, position.y, 0));
  1054. }
  1055. MoveTo* MoveTo::create(float duration, const Vec3& position)
  1056. {
  1057. MoveTo *ret = new (std::nothrow) MoveTo();
  1058. if (ret && ret->initWithDuration(duration, position))
  1059. {
  1060. ret->autorelease();
  1061. return ret;
  1062. }
  1063. delete ret;
  1064. return nullptr;
  1065. }
  1066. bool MoveTo::initWithDuration(float duration, const Vec2& position)
  1067. {
  1068. return initWithDuration(duration, Vec3(position.x, position.y, 0));
  1069. }
  1070. bool MoveTo::initWithDuration(float duration, const Vec3& position)
  1071. {
  1072. bool ret = false;
  1073. if (ActionInterval::initWithDuration(duration))
  1074. {
  1075. _endPosition = position;
  1076. ret = true;
  1077. }
  1078. return ret;
  1079. }
  1080. MoveTo* MoveTo::clone() const
  1081. {
  1082. // no copy constructor
  1083. return MoveTo::create(_duration, _endPosition);
  1084. }
  1085. void MoveTo::startWithTarget(Node *target)
  1086. {
  1087. MoveBy::startWithTarget(target);
  1088. _positionDelta = _endPosition - target->getPosition3D();
  1089. }
  1090. MoveTo* MoveTo::reverse() const
  1091. {
  1092. CCASSERT(false, "reverse() not supported in MoveTo");
  1093. return nullptr;
  1094. }
  1095. //
  1096. // SkewTo
  1097. //
  1098. SkewTo* SkewTo::create(float t, float sx, float sy)
  1099. {
  1100. SkewTo *skewTo = new (std::nothrow) SkewTo();
  1101. if (skewTo && skewTo->initWithDuration(t, sx, sy))
  1102. {
  1103. skewTo->autorelease();
  1104. return skewTo;
  1105. }
  1106. delete skewTo;
  1107. return nullptr;
  1108. }
  1109. bool SkewTo::initWithDuration(float t, float sx, float sy)
  1110. {
  1111. bool bRet = false;
  1112. if (ActionInterval::initWithDuration(t))
  1113. {
  1114. _endSkewX = sx;
  1115. _endSkewY = sy;
  1116. bRet = true;
  1117. }
  1118. return bRet;
  1119. }
  1120. SkewTo* SkewTo::clone() const
  1121. {
  1122. // no copy constructor
  1123. return SkewTo::create(_duration, _endSkewX, _endSkewY);
  1124. }
  1125. SkewTo* SkewTo::reverse() const
  1126. {
  1127. CCASSERT(false, "reverse() not supported in SkewTo");
  1128. return nullptr;
  1129. }
  1130. void SkewTo::startWithTarget(Node *target)
  1131. {
  1132. ActionInterval::startWithTarget(target);
  1133. _startSkewX = target->getSkewX();
  1134. if (_startSkewX > 0)
  1135. {
  1136. _startSkewX = fmodf(_startSkewX, 180.f);
  1137. }
  1138. else
  1139. {
  1140. _startSkewX = fmodf(_startSkewX, -180.f);
  1141. }
  1142. _deltaX = _endSkewX - _startSkewX;
  1143. if (_deltaX > 180)
  1144. {
  1145. _deltaX -= 360;
  1146. }
  1147. if (_deltaX < -180)
  1148. {
  1149. _deltaX += 360;
  1150. }
  1151. _startSkewY = target->getSkewY();
  1152. if (_startSkewY > 0)
  1153. {
  1154. _startSkewY = fmodf(_startSkewY, 360.f);
  1155. }
  1156. else
  1157. {
  1158. _startSkewY = fmodf(_startSkewY, -360.f);
  1159. }
  1160. _deltaY = _endSkewY - _startSkewY;
  1161. if (_deltaY > 180)
  1162. {
  1163. _deltaY -= 360;
  1164. }
  1165. if (_deltaY < -180)
  1166. {
  1167. _deltaY += 360;
  1168. }
  1169. }
  1170. void SkewTo::update(float t)
  1171. {
  1172. _target->setSkewX(_startSkewX + _deltaX * t);
  1173. _target->setSkewY(_startSkewY + _deltaY * t);
  1174. }
  1175. SkewTo::SkewTo()
  1176. : _skewX(0.0)
  1177. , _skewY(0.0)
  1178. , _startSkewX(0.0)
  1179. , _startSkewY(0.0)
  1180. , _endSkewX(0.0)
  1181. , _endSkewY(0.0)
  1182. , _deltaX(0.0)
  1183. , _deltaY(0.0)
  1184. {
  1185. }
  1186. //
  1187. // SkewBy
  1188. //
  1189. SkewBy* SkewBy::create(float t, float sx, float sy)
  1190. {
  1191. SkewBy *skewBy = new (std::nothrow) SkewBy();
  1192. if (skewBy && skewBy->initWithDuration(t, sx, sy))
  1193. {
  1194. skewBy->autorelease();
  1195. return skewBy;
  1196. }
  1197. delete skewBy;
  1198. return nullptr;
  1199. }
  1200. SkewBy * SkewBy::clone() const
  1201. {
  1202. // no copy constructor
  1203. return SkewBy::create(_duration, _skewX, _skewY);
  1204. }
  1205. bool SkewBy::initWithDuration(float t, float deltaSkewX, float deltaSkewY)
  1206. {
  1207. bool ret = false;
  1208. if (SkewTo::initWithDuration(t, deltaSkewX, deltaSkewY))
  1209. {
  1210. _skewX = deltaSkewX;
  1211. _skewY = deltaSkewY;
  1212. ret = true;
  1213. }
  1214. return ret;
  1215. }
  1216. void SkewBy::startWithTarget(Node *target)
  1217. {
  1218. SkewTo::startWithTarget(target);
  1219. _deltaX = _skewX;
  1220. _deltaY = _skewY;
  1221. _endSkewX = _startSkewX + _deltaX;
  1222. _endSkewY = _startSkewY + _deltaY;
  1223. }
  1224. SkewBy* SkewBy::reverse() const
  1225. {
  1226. return SkewBy::create(_duration, -_skewX, -_skewY);
  1227. }
  1228. ResizeTo* ResizeTo::create(float duration, const cocos2d::Size& final_size)
  1229. {
  1230. ResizeTo *ret = new (std::nothrow) ResizeTo();
  1231. if (ret)
  1232. {
  1233. if (ret->initWithDuration(duration, final_size))
  1234. {
  1235. ret->autorelease();
  1236. }
  1237. else
  1238. {
  1239. delete ret;
  1240. ret = nullptr;
  1241. }
  1242. }
  1243. return ret;
  1244. }
  1245. ResizeTo* ResizeTo::clone(void) const
  1246. {
  1247. // no copy constructor
  1248. ResizeTo* a = new (std::nothrow) ResizeTo();
  1249. a->initWithDuration(_duration, _finalSize);
  1250. a->autorelease();
  1251. return a;
  1252. }
  1253. void ResizeTo::startWithTarget(cocos2d::Node* target)
  1254. {
  1255. ActionInterval::startWithTarget(target);
  1256. _initialSize = target->getContentSize();
  1257. _sizeDelta = _finalSize - _initialSize;
  1258. }
  1259. void ResizeTo::update(float time)
  1260. {
  1261. if (_target)
  1262. {
  1263. auto new_size = _initialSize + (_sizeDelta * time);
  1264. _target->setContentSize(new_size);
  1265. }
  1266. }
  1267. bool ResizeTo::initWithDuration(float duration, const cocos2d::Size& final_size)
  1268. {
  1269. if (cocos2d::ActionInterval::initWithDuration(duration))
  1270. {
  1271. _finalSize = final_size;
  1272. return true;
  1273. }
  1274. return false;
  1275. }
  1276. //
  1277. // ResizeBy
  1278. //
  1279. ResizeBy* ResizeBy::create(float duration, const cocos2d::Size& deltaSize)
  1280. {
  1281. ResizeBy *ret = new (std::nothrow) ResizeBy();
  1282. if (ret)
  1283. {
  1284. if (ret->initWithDuration(duration, deltaSize))
  1285. {
  1286. ret->autorelease();
  1287. }
  1288. else
  1289. {
  1290. delete ret;
  1291. ret = nullptr;
  1292. }
  1293. }
  1294. return ret;
  1295. }
  1296. ResizeBy* ResizeBy::clone() const
  1297. {
  1298. // no copy constructor
  1299. auto a = new (std::nothrow) ResizeBy();
  1300. a->initWithDuration(_duration, _sizeDelta);
  1301. a->autorelease();
  1302. return a;
  1303. }
  1304. void ResizeBy::startWithTarget(Node *target)
  1305. {
  1306. ActionInterval::startWithTarget(target);
  1307. _previousSize = _startSize = target->getContentSize();
  1308. }
  1309. ResizeBy* ResizeBy::reverse() const
  1310. {
  1311. cocos2d::Size newSize(-_sizeDelta.width, -_sizeDelta.height);
  1312. return ResizeBy::create(_duration, newSize);
  1313. }
  1314. void ResizeBy::update(float t)
  1315. {
  1316. if (_target)
  1317. {
  1318. _target->setContentSize(_startSize + (_sizeDelta * t));
  1319. }
  1320. }
  1321. bool ResizeBy::initWithDuration(float duration, const cocos2d::Size& deltaSize)
  1322. {
  1323. bool ret = false;
  1324. if (ActionInterval::initWithDuration(duration))
  1325. {
  1326. _sizeDelta = deltaSize;
  1327. ret = true;
  1328. }
  1329. return ret;
  1330. }
  1331. //
  1332. // JumpBy
  1333. //
  1334. JumpBy* JumpBy::create(float duration, const Vec2& position, float height, int jumps)
  1335. {
  1336. JumpBy *jumpBy = new (std::nothrow) JumpBy();
  1337. if (jumpBy && jumpBy->initWithDuration(duration, position, height, jumps))
  1338. {
  1339. jumpBy->autorelease();
  1340. return jumpBy;
  1341. }
  1342. delete jumpBy;
  1343. return nullptr;
  1344. }
  1345. bool JumpBy::initWithDuration(float duration, const Vec2& position, float height, int jumps)
  1346. {
  1347. CCASSERT(jumps>=0, "Number of jumps must be >= 0");
  1348. if (jumps < 0)
  1349. {
  1350. log("JumpBy::initWithDuration error: Number of jumps must be >= 0");
  1351. return false;
  1352. }
  1353. if (ActionInterval::initWithDuration(duration) && jumps>=0)
  1354. {
  1355. _delta = position;
  1356. _height = height;
  1357. _jumps = jumps;
  1358. return true;
  1359. }
  1360. return false;
  1361. }
  1362. JumpBy* JumpBy::clone() const
  1363. {
  1364. // no copy constructor
  1365. return JumpBy::create(_duration, _delta, _height, _jumps);
  1366. }
  1367. void JumpBy::startWithTarget(Node *target)
  1368. {
  1369. ActionInterval::startWithTarget(target);
  1370. _previousPos = _startPosition = target->getPosition();
  1371. }
  1372. void JumpBy::update(float t)
  1373. {
  1374. // parabolic jump (since v0.8.2)
  1375. if (_target)
  1376. {
  1377. float frac = fmodf( t * _jumps, 1.0f );
  1378. float y = _height * 4 * frac * (1 - frac);
  1379. y += _delta.y * t;
  1380. float x = _delta.x * t;
  1381. #if CC_ENABLE_STACKABLE_ACTIONS
  1382. Vec2 currentPos = _target->getPosition();
  1383. Vec2 diff = currentPos - _previousPos;
  1384. _startPosition = diff + _startPosition;
  1385. Vec2 newPos = _startPosition + Vec2(x,y);
  1386. _target->setPosition(newPos);
  1387. _previousPos = newPos;
  1388. #else
  1389. _target->setPosition(_startPosition + Vec2(x,y));
  1390. #endif // !CC_ENABLE_STACKABLE_ACTIONS
  1391. }
  1392. }
  1393. JumpBy* JumpBy::reverse() const
  1394. {
  1395. return JumpBy::create(_duration, Vec2(-_delta.x, -_delta.y),
  1396. _height, _jumps);
  1397. }
  1398. //
  1399. // JumpTo
  1400. //
  1401. JumpTo* JumpTo::create(float duration, const Vec2& position, float height, int jumps)
  1402. {
  1403. JumpTo *jumpTo = new (std::nothrow) JumpTo();
  1404. if (jumpTo && jumpTo->initWithDuration(duration, position, height, jumps))
  1405. {
  1406. jumpTo->autorelease();
  1407. return jumpTo;
  1408. }
  1409. delete jumpTo;
  1410. return nullptr;
  1411. }
  1412. bool JumpTo::initWithDuration(float duration, const Vec2& position, float height, int jumps)
  1413. {
  1414. CCASSERT(jumps>=0, "Number of jumps must be >= 0");
  1415. if (jumps < 0)
  1416. {
  1417. log("JumpTo::initWithDuration error:Number of jumps must be >= 0");
  1418. return false;
  1419. }
  1420. if (ActionInterval::initWithDuration(duration) && jumps>=0)
  1421. {
  1422. _endPosition = position;
  1423. _height = height;
  1424. _jumps = jumps;
  1425. return true;
  1426. }
  1427. return false;
  1428. }
  1429. JumpTo* JumpTo::clone() const
  1430. {
  1431. // no copy constructor
  1432. return JumpTo::create(_duration, _endPosition, _height, _jumps);
  1433. }
  1434. JumpTo* JumpTo::reverse() const
  1435. {
  1436. CCASSERT(false, "reverse() not supported in JumpTo");
  1437. return nullptr;
  1438. }
  1439. void JumpTo::startWithTarget(Node *target)
  1440. {
  1441. JumpBy::startWithTarget(target);
  1442. _delta.set(_endPosition.x - _startPosition.x, _endPosition.y - _startPosition.y);
  1443. }
  1444. // Bezier cubic formula:
  1445. // ((1 - t) + t)3 = 1
  1446. // Expands to ...
  1447. // (1 - t)3 + 3t(1-t)2 + 3t2(1 - t) + t3 = 1
  1448. static inline float bezierat( float a, float b, float c, float d, float t )
  1449. {
  1450. return (powf(1-t,3) * a +
  1451. 3*t*(powf(1-t,2))*b +
  1452. 3*powf(t,2)*(1-t)*c +
  1453. powf(t,3)*d );
  1454. }
  1455. //
  1456. // BezierBy
  1457. //
  1458. BezierBy* BezierBy::create(float t, const ccBezierConfig& c)
  1459. {
  1460. BezierBy *bezierBy = new (std::nothrow) BezierBy();
  1461. if (bezierBy && bezierBy->initWithDuration(t, c))
  1462. {
  1463. bezierBy->autorelease();
  1464. return bezierBy;
  1465. }
  1466. delete bezierBy;
  1467. return nullptr;
  1468. }
  1469. bool BezierBy::initWithDuration(float t, const ccBezierConfig& c)
  1470. {
  1471. if (ActionInterval::initWithDuration(t))
  1472. {
  1473. _config = c;
  1474. return true;
  1475. }
  1476. return false;
  1477. }
  1478. void BezierBy::startWithTarget(Node *target)
  1479. {
  1480. ActionInterval::startWithTarget(target);
  1481. _previousPosition = _startPosition = target->getPosition();
  1482. }
  1483. BezierBy* BezierBy::clone() const
  1484. {
  1485. // no copy constructor
  1486. return BezierBy::create(_duration, _config);
  1487. }
  1488. void BezierBy::update(float time)
  1489. {
  1490. if (_target)
  1491. {
  1492. float xa = 0;
  1493. float xb = _config.controlPoint_1.x;
  1494. float xc = _config.controlPoint_2.x;
  1495. float xd = _config.endPosition.x;
  1496. float ya = 0;
  1497. float yb = _config.controlPoint_1.y;
  1498. float yc = _config.controlPoint_2.y;
  1499. float yd = _config.endPosition.y;
  1500. float x = bezierat(xa, xb, xc, xd, time);
  1501. float y = bezierat(ya, yb, yc, yd, time);
  1502. #if CC_ENABLE_STACKABLE_ACTIONS
  1503. Vec2 currentPos = _target->getPosition();
  1504. Vec2 diff = currentPos - _previousPosition;
  1505. _startPosition = _startPosition + diff;
  1506. Vec2 newPos = _startPosition + Vec2(x,y);
  1507. _target->setPosition(newPos);
  1508. _previousPosition = newPos;
  1509. #else
  1510. _target->setPosition( _startPosition + Vec2(x,y));
  1511. #endif // !CC_ENABLE_STACKABLE_ACTIONS
  1512. }
  1513. }
  1514. BezierBy* BezierBy::reverse() const
  1515. {
  1516. ccBezierConfig r;
  1517. r.endPosition = -_config.endPosition;
  1518. r.controlPoint_1 = _config.controlPoint_2 + (-_config.endPosition);
  1519. r.controlPoint_2 = _config.controlPoint_1 + (-_config.endPosition);
  1520. BezierBy *action = BezierBy::create(_duration, r);
  1521. return action;
  1522. }
  1523. //
  1524. // BezierTo
  1525. //
  1526. BezierTo* BezierTo::create(float t, const ccBezierConfig& c)
  1527. {
  1528. BezierTo *bezierTo = new (std::nothrow) BezierTo();
  1529. if (bezierTo && bezierTo->initWithDuration(t, c))
  1530. {
  1531. bezierTo->autorelease();
  1532. return bezierTo;
  1533. }
  1534. delete bezierTo;
  1535. return nullptr;
  1536. }
  1537. bool BezierTo::initWithDuration(float t, const ccBezierConfig &c)
  1538. {
  1539. if (ActionInterval::initWithDuration(t))
  1540. {
  1541. _toConfig = c;
  1542. return true;
  1543. }
  1544. return false;
  1545. }
  1546. BezierTo* BezierTo::clone() const
  1547. {
  1548. // no copy constructor
  1549. return BezierTo::create(_duration, _toConfig);
  1550. }
  1551. void BezierTo::startWithTarget(Node *target)
  1552. {
  1553. BezierBy::startWithTarget(target);
  1554. _config.controlPoint_1 = _toConfig.controlPoint_1 - _startPosition;
  1555. _config.controlPoint_2 = _toConfig.controlPoint_2 - _startPosition;
  1556. _config.endPosition = _toConfig.endPosition - _startPosition;
  1557. }
  1558. BezierTo* BezierTo::reverse() const
  1559. {
  1560. CCASSERT(false, "CCBezierTo doesn't support the 'reverse' method");
  1561. return nullptr;
  1562. }
  1563. //
  1564. // ScaleTo
  1565. //
  1566. ScaleTo* ScaleTo::create(float duration, float s)
  1567. {
  1568. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1569. if (scaleTo && scaleTo->initWithDuration(duration, s))
  1570. {
  1571. scaleTo->autorelease();
  1572. return scaleTo;
  1573. }
  1574. delete scaleTo;
  1575. return nullptr;
  1576. }
  1577. ScaleTo* ScaleTo::create(float duration, float sx, float sy)
  1578. {
  1579. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1580. if (scaleTo && scaleTo->initWithDuration(duration, sx, sy))
  1581. {
  1582. scaleTo->autorelease();
  1583. return scaleTo;
  1584. }
  1585. delete scaleTo;
  1586. return nullptr;
  1587. }
  1588. ScaleTo* ScaleTo::create(float duration, float sx, float sy, float sz)
  1589. {
  1590. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1591. if (scaleTo && scaleTo->initWithDuration(duration, sx, sy, sz))
  1592. {
  1593. scaleTo->autorelease();
  1594. return scaleTo;
  1595. }
  1596. delete scaleTo;
  1597. return nullptr;
  1598. }
  1599. bool ScaleTo::initWithDuration(float duration, float s)
  1600. {
  1601. if (ActionInterval::initWithDuration(duration))
  1602. {
  1603. _endScaleX = s;
  1604. _endScaleY = s;
  1605. _endScaleZ = s;
  1606. return true;
  1607. }
  1608. return false;
  1609. }
  1610. bool ScaleTo::initWithDuration(float duration, float sx, float sy)
  1611. {
  1612. if (ActionInterval::initWithDuration(duration))
  1613. {
  1614. _endScaleX = sx;
  1615. _endScaleY = sy;
  1616. _endScaleZ = 1.f;
  1617. return true;
  1618. }
  1619. return false;
  1620. }
  1621. bool ScaleTo::initWithDuration(float duration, float sx, float sy, float sz)
  1622. {
  1623. if (ActionInterval::initWithDuration(duration))
  1624. {
  1625. _endScaleX = sx;
  1626. _endScaleY = sy;
  1627. _endScaleZ = sz;
  1628. return true;
  1629. }
  1630. return false;
  1631. }
  1632. ScaleTo* ScaleTo::clone() const
  1633. {
  1634. // no copy constructor
  1635. return ScaleTo::create(_duration, _endScaleX, _endScaleY, _endScaleZ);
  1636. }
  1637. ScaleTo* ScaleTo::reverse() const
  1638. {
  1639. CCASSERT(false, "reverse() not supported in ScaleTo");
  1640. return nullptr;
  1641. }
  1642. void ScaleTo::startWithTarget(Node *target)
  1643. {
  1644. ActionInterval::startWithTarget(target);
  1645. _startScaleX = target->getScaleX();
  1646. _startScaleY = target->getScaleY();
  1647. _startScaleZ = target->getScaleZ();
  1648. _deltaX = _endScaleX - _startScaleX;
  1649. _deltaY = _endScaleY - _startScaleY;
  1650. _deltaZ = _endScaleZ - _startScaleZ;
  1651. }
  1652. void ScaleTo::update(float time)
  1653. {
  1654. if (_target)
  1655. {
  1656. _target->setScaleX(_startScaleX + _deltaX * time);
  1657. _target->setScaleY(_startScaleY + _deltaY * time);
  1658. _target->setScaleZ(_startScaleZ + _deltaZ * time);
  1659. }
  1660. }
  1661. //
  1662. // ScaleBy
  1663. //
  1664. ScaleBy* ScaleBy::create(float duration, float s)
  1665. {
  1666. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1667. if (scaleBy && scaleBy->initWithDuration(duration, s))
  1668. {
  1669. scaleBy->autorelease();
  1670. return scaleBy;
  1671. }
  1672. delete scaleBy;
  1673. return nullptr;
  1674. }
  1675. ScaleBy* ScaleBy::create(float duration, float sx, float sy)
  1676. {
  1677. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1678. if (scaleBy && scaleBy->initWithDuration(duration, sx, sy, 1.f))
  1679. {
  1680. scaleBy->autorelease();
  1681. return scaleBy;
  1682. }
  1683. delete scaleBy;
  1684. return nullptr;
  1685. }
  1686. ScaleBy* ScaleBy::create(float duration, float sx, float sy, float sz)
  1687. {
  1688. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1689. if (scaleBy && scaleBy->initWithDuration(duration, sx, sy, sz))
  1690. {
  1691. scaleBy->autorelease();
  1692. return scaleBy;
  1693. }
  1694. delete scaleBy;
  1695. return nullptr;
  1696. }
  1697. ScaleBy* ScaleBy::clone() const
  1698. {
  1699. // no copy constructor
  1700. return ScaleBy::create(_duration, _endScaleX, _endScaleY, _endScaleZ);
  1701. }
  1702. void ScaleBy::startWithTarget(Node *target)
  1703. {
  1704. ScaleTo::startWithTarget(target);
  1705. _deltaX = _startScaleX * _endScaleX - _startScaleX;
  1706. _deltaY = _startScaleY * _endScaleY - _startScaleY;
  1707. _deltaZ = _startScaleZ * _endScaleZ - _startScaleZ;
  1708. }
  1709. ScaleBy* ScaleBy::reverse() const
  1710. {
  1711. return ScaleBy::create(_duration, 1 / _endScaleX, 1 / _endScaleY, 1/ _endScaleZ);
  1712. }
  1713. //
  1714. // Blink
  1715. //
  1716. Blink* Blink::create(float duration, int blinks)
  1717. {
  1718. Blink *blink = new (std::nothrow) Blink();
  1719. if (blink && blink->initWithDuration(duration, blinks))
  1720. {
  1721. blink->autorelease();
  1722. return blink;
  1723. }
  1724. delete blink;
  1725. return nullptr;
  1726. }
  1727. bool Blink::initWithDuration(float duration, int blinks)
  1728. {
  1729. CCASSERT(blinks>=0, "blinks should be >= 0");
  1730. if (blinks < 0)
  1731. {
  1732. log("Blink::initWithDuration error:blinks should be >= 0");
  1733. return false;
  1734. }
  1735. if (ActionInterval::initWithDuration(duration) && blinks>=0)
  1736. {
  1737. _times = blinks;
  1738. return true;
  1739. }
  1740. return false;
  1741. }
  1742. void Blink::stop()
  1743. {
  1744. if (nullptr != _target)
  1745. _target->setVisible(_originalState);
  1746. ActionInterval::stop();
  1747. }
  1748. void Blink::startWithTarget(Node *target)
  1749. {
  1750. ActionInterval::startWithTarget(target);
  1751. _originalState = target->isVisible();
  1752. }
  1753. Blink* Blink::clone(void) const
  1754. {
  1755. // no copy constructor
  1756. return Blink::create(_duration, _times);
  1757. }
  1758. void Blink::update(float time)
  1759. {
  1760. if (_target && ! isDone())
  1761. {
  1762. float slice = 1.0f / _times;
  1763. float m = fmodf(time, slice);
  1764. _target->setVisible(m > slice / 2 ? true : false);
  1765. }
  1766. }
  1767. Blink* Blink::reverse() const
  1768. {
  1769. return Blink::create(_duration, _times);
  1770. }
  1771. //
  1772. // FadeIn
  1773. //
  1774. FadeIn* FadeIn::create(float d)
  1775. {
  1776. FadeIn* action = new (std::nothrow) FadeIn();
  1777. if (action && action->initWithDuration(d,255.0f))
  1778. {
  1779. action->autorelease();
  1780. return action;
  1781. }
  1782. delete action;
  1783. return nullptr;
  1784. }
  1785. FadeIn* FadeIn::clone() const
  1786. {
  1787. // no copy constructor
  1788. return FadeIn::create(_duration);
  1789. }
  1790. void FadeIn::setReverseAction(cocos2d::FadeTo *ac)
  1791. {
  1792. _reverseAction = ac;
  1793. }
  1794. FadeTo* FadeIn::reverse() const
  1795. {
  1796. auto action = FadeOut::create(_duration);
  1797. action->setReverseAction(const_cast<FadeIn*>(this));
  1798. return action;
  1799. }
  1800. void FadeIn::startWithTarget(cocos2d::Node *target)
  1801. {
  1802. ActionInterval::startWithTarget(target);
  1803. if (nullptr != _reverseAction)
  1804. this->_toOpacity = this->_reverseAction->_fromOpacity;
  1805. else
  1806. _toOpacity = 255.0f;
  1807. if (target)
  1808. _fromOpacity = target->getOpacity();
  1809. }
  1810. //
  1811. // FadeOut
  1812. //
  1813. FadeOut* FadeOut::create(float d)
  1814. {
  1815. FadeOut* action = new (std::nothrow) FadeOut();
  1816. if (action && action->initWithDuration(d,0.0f))
  1817. {
  1818. action->autorelease();
  1819. return action;
  1820. }
  1821. delete action;
  1822. return nullptr;
  1823. }
  1824. FadeOut* FadeOut::clone() const
  1825. {
  1826. // no copy constructor
  1827. return FadeOut::create(_duration);
  1828. }
  1829. void FadeOut::startWithTarget(cocos2d::Node *target)
  1830. {
  1831. ActionInterval::startWithTarget(target);
  1832. if (nullptr != _reverseAction)
  1833. _toOpacity = _reverseAction->_fromOpacity;
  1834. else
  1835. _toOpacity = 0.0f;
  1836. if (target)
  1837. _fromOpacity = target->getOpacity();
  1838. }
  1839. void FadeOut::setReverseAction(cocos2d::FadeTo *ac)
  1840. {
  1841. _reverseAction = ac;
  1842. }
  1843. FadeTo* FadeOut::reverse() const
  1844. {
  1845. auto action = FadeIn::create(_duration);
  1846. action->setReverseAction(const_cast<FadeOut*>(this));
  1847. return action;
  1848. }
  1849. //
  1850. // FadeTo
  1851. //
  1852. FadeTo* FadeTo::create(float duration, GLubyte opacity)
  1853. {
  1854. FadeTo *fadeTo = new (std::nothrow) FadeTo();
  1855. if (fadeTo && fadeTo->initWithDuration(duration, opacity))
  1856. {
  1857. fadeTo->autorelease();
  1858. return fadeTo;
  1859. }
  1860. delete fadeTo;
  1861. return nullptr;
  1862. }
  1863. bool FadeTo::initWithDuration(float duration, GLubyte opacity)
  1864. {
  1865. if (ActionInterval::initWithDuration(duration))
  1866. {
  1867. _toOpacity = opacity;
  1868. return true;
  1869. }
  1870. return false;
  1871. }
  1872. FadeTo* FadeTo::clone() const
  1873. {
  1874. // no copy constructor
  1875. return FadeTo::create(_duration, _toOpacity);
  1876. }
  1877. FadeTo* FadeTo::reverse() const
  1878. {
  1879. CCASSERT(false, "reverse() not supported in FadeTo");
  1880. return nullptr;
  1881. }
  1882. void FadeTo::startWithTarget(Node *target)
  1883. {
  1884. ActionInterval::startWithTarget(target);
  1885. if (target)
  1886. {
  1887. _fromOpacity = target->getOpacity();
  1888. }
  1889. }
  1890. void FadeTo::update(float time)
  1891. {
  1892. if (_target)
  1893. {
  1894. _target->setOpacity((GLubyte)(_fromOpacity + (_toOpacity - _fromOpacity) * time));
  1895. }
  1896. }
  1897. //
  1898. // TintTo
  1899. //
  1900. TintTo* TintTo::create(float duration, GLubyte red, GLubyte green, GLubyte blue)
  1901. {
  1902. TintTo *tintTo = new (std::nothrow) TintTo();
  1903. if (tintTo && tintTo->initWithDuration(duration, red, green, blue))
  1904. {
  1905. tintTo->autorelease();
  1906. return tintTo;
  1907. }
  1908. delete tintTo;
  1909. return nullptr;
  1910. }
  1911. TintTo* TintTo::create(float duration, const Color3B& color)
  1912. {
  1913. return create(duration, color.r, color.g, color.b);
  1914. }
  1915. bool TintTo::initWithDuration(float duration, GLubyte red, GLubyte green, GLubyte blue)
  1916. {
  1917. if (ActionInterval::initWithDuration(duration))
  1918. {
  1919. _to = Color3B(red, green, blue);
  1920. return true;
  1921. }
  1922. return false;
  1923. }
  1924. TintTo* TintTo::clone() const
  1925. {
  1926. // no copy constructor
  1927. return TintTo::create(_duration, _to.r, _to.g, _to.b);
  1928. }
  1929. TintTo* TintTo::reverse() const
  1930. {
  1931. CCASSERT(false, "reverse() not supported in TintTo");
  1932. return nullptr;
  1933. }
  1934. void TintTo::startWithTarget(Node *target)
  1935. {
  1936. ActionInterval::startWithTarget(target);
  1937. if (_target)
  1938. {
  1939. _from = _target->getColor();
  1940. }
  1941. }
  1942. void TintTo::update(float time)
  1943. {
  1944. if (_target)
  1945. {
  1946. _target->setColor(Color3B(GLubyte(_from.r + (_to.r - _from.r) * time),
  1947. (GLubyte)(_from.g + (_to.g - _from.g) * time),
  1948. (GLubyte)(_from.b + (_to.b - _from.b) * time)));
  1949. }
  1950. }
  1951. //
  1952. // TintBy
  1953. //
  1954. TintBy* TintBy::create(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue)
  1955. {
  1956. TintBy *tintBy = new (std::nothrow) TintBy();
  1957. if (tintBy && tintBy->initWithDuration(duration, deltaRed, deltaGreen, deltaBlue))
  1958. {
  1959. tintBy->autorelease();
  1960. return tintBy;
  1961. }
  1962. delete tintBy;
  1963. return nullptr;
  1964. }
  1965. bool TintBy::initWithDuration(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue)
  1966. {
  1967. if (ActionInterval::initWithDuration(duration))
  1968. {
  1969. _deltaR = deltaRed;
  1970. _deltaG = deltaGreen;
  1971. _deltaB = deltaBlue;
  1972. return true;
  1973. }
  1974. return false;
  1975. }
  1976. TintBy* TintBy::clone() const
  1977. {
  1978. // no copy constructor
  1979. return TintBy::create(_duration, _deltaR, _deltaG, _deltaB);
  1980. }
  1981. void TintBy::startWithTarget(Node *target)
  1982. {
  1983. ActionInterval::startWithTarget(target);
  1984. if (target)
  1985. {
  1986. Color3B color = target->getColor();
  1987. _fromR = color.r;
  1988. _fromG = color.g;
  1989. _fromB = color.b;
  1990. }
  1991. }
  1992. void TintBy::update(float time)
  1993. {
  1994. if (_target)
  1995. {
  1996. _target->setColor(Color3B((GLubyte)(_fromR + _deltaR * time),
  1997. (GLubyte)(_fromG + _deltaG * time),
  1998. (GLubyte)(_fromB + _deltaB * time)));
  1999. }
  2000. }
  2001. TintBy* TintBy::reverse() const
  2002. {
  2003. return TintBy::create(_duration, -_deltaR, -_deltaG, -_deltaB);
  2004. }
  2005. //
  2006. // DelayTime
  2007. //
  2008. DelayTime* DelayTime::create(float d)
  2009. {
  2010. DelayTime* action = new (std::nothrow) DelayTime();
  2011. if (action && action->initWithDuration(d))
  2012. {
  2013. action->autorelease();
  2014. return action;
  2015. }
  2016. delete action;
  2017. return nullptr;
  2018. }
  2019. DelayTime* DelayTime::clone() const
  2020. {
  2021. // no copy constructor
  2022. return DelayTime::create(_duration);
  2023. }
  2024. void DelayTime::update(float /*time*/)
  2025. {
  2026. return;
  2027. }
  2028. DelayTime* DelayTime::reverse() const
  2029. {
  2030. return DelayTime::create(_duration);
  2031. }
  2032. //
  2033. // ReverseTime
  2034. //
  2035. ReverseTime* ReverseTime::create(FiniteTimeAction *action)
  2036. {
  2037. // casting to prevent warnings
  2038. ReverseTime *reverseTime = new (std::nothrow) ReverseTime();
  2039. if (reverseTime && reverseTime->initWithAction( action->clone() ))
  2040. {
  2041. reverseTime->autorelease();
  2042. return reverseTime;
  2043. }
  2044. delete reverseTime;
  2045. return nullptr;
  2046. }
  2047. bool ReverseTime::initWithAction(FiniteTimeAction *action)
  2048. {
  2049. CCASSERT(action != nullptr, "action can't be nullptr!");
  2050. CCASSERT(action != _other, "action doesn't equal to _other!");
  2051. if (action == nullptr || action == _other)
  2052. {
  2053. log("ReverseTime::initWithAction error: action is null or action equal to _other");
  2054. return false;
  2055. }
  2056. if (ActionInterval::initWithDuration(action->getDuration()))
  2057. {
  2058. // Don't leak if action is reused
  2059. CC_SAFE_RELEASE(_other);
  2060. _other = action;
  2061. action->retain();
  2062. return true;
  2063. }
  2064. return false;
  2065. }
  2066. ReverseTime* ReverseTime::clone() const
  2067. {
  2068. // no copy constructor
  2069. return ReverseTime::create(_other->clone());
  2070. }
  2071. ReverseTime::ReverseTime() : _other(nullptr)
  2072. {
  2073. }
  2074. ReverseTime::~ReverseTime()
  2075. {
  2076. CC_SAFE_RELEASE(_other);
  2077. }
  2078. void ReverseTime::startWithTarget(Node *target)
  2079. {
  2080. ActionInterval::startWithTarget(target);
  2081. _other->startWithTarget(target);
  2082. }
  2083. void ReverseTime::stop(void)
  2084. {
  2085. _other->stop();
  2086. ActionInterval::stop();
  2087. }
  2088. void ReverseTime::update(float time)
  2089. {
  2090. if (_other)
  2091. {
  2092. if (!(sendUpdateEventToScript(1 - time, _other)))
  2093. _other->update(1 - time);
  2094. }
  2095. }
  2096. ReverseTime* ReverseTime::reverse() const
  2097. {
  2098. // FIXME: This looks like a bug
  2099. return (ReverseTime*)_other->clone();
  2100. }
  2101. //
  2102. // Animate
  2103. //
  2104. Animate* Animate::create(Animation *animation)
  2105. {
  2106. Animate *animate = new (std::nothrow) Animate();
  2107. if (animate && animate->initWithAnimation(animation))
  2108. {
  2109. animate->autorelease();
  2110. return animate;
  2111. }
  2112. delete animate;
  2113. return nullptr;
  2114. }
  2115. Animate::Animate()
  2116. : _splitTimes(new std::vector<float>)
  2117. , _nextFrame(0)
  2118. , _origFrame(nullptr)
  2119. , _executedLoops(0)
  2120. , _animation(nullptr)
  2121. , _frameDisplayedEvent(nullptr)
  2122. , _currFrameIndex(0)
  2123. {
  2124. }
  2125. Animate::~Animate()
  2126. {
  2127. CC_SAFE_RELEASE(_animation);
  2128. CC_SAFE_RELEASE(_origFrame);
  2129. CC_SAFE_DELETE(_splitTimes);
  2130. CC_SAFE_RELEASE(_frameDisplayedEvent);
  2131. }
  2132. bool Animate::initWithAnimation(Animation* animation)
  2133. {
  2134. CCASSERT( animation!=nullptr, "Animate: argument Animation must be non-nullptr");
  2135. if (animation == nullptr)
  2136. {
  2137. log("Animate::initWithAnimation: argument Animation must be non-nullptr");
  2138. return false;
  2139. }
  2140. float singleDuration = animation->getDuration();
  2141. if ( ActionInterval::initWithDuration(singleDuration * animation->getLoops() ) )
  2142. {
  2143. _nextFrame = 0;
  2144. setAnimation(animation);
  2145. _origFrame = nullptr;
  2146. _executedLoops = 0;
  2147. _splitTimes->reserve(animation->getFrames().size());
  2148. float accumUnitsOfTime = 0;
  2149. float newUnitOfTimeValue = singleDuration / animation->getTotalDelayUnits();
  2150. auto& frames = animation->getFrames();
  2151. for (auto& frame : frames)
  2152. {
  2153. float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
  2154. accumUnitsOfTime += frame->getDelayUnits();
  2155. _splitTimes->push_back(value);
  2156. }
  2157. return true;
  2158. }
  2159. return false;
  2160. }
  2161. void Animate::setAnimation(cocos2d::Animation *animation)
  2162. {
  2163. if (_animation != animation)
  2164. {
  2165. CC_SAFE_RETAIN(animation);
  2166. CC_SAFE_RELEASE(_animation);
  2167. _animation = animation;
  2168. }
  2169. }
  2170. Animate* Animate::clone() const
  2171. {
  2172. // no copy constructor
  2173. return Animate::create(_animation->clone());
  2174. }
  2175. void Animate::startWithTarget(Node *target)
  2176. {
  2177. ActionInterval::startWithTarget(target);
  2178. Sprite *sprite = static_cast<Sprite*>(target);
  2179. CC_SAFE_RELEASE(_origFrame);
  2180. if (_animation->getRestoreOriginalFrame())
  2181. {
  2182. _origFrame = sprite->getSpriteFrame();
  2183. _origFrame->retain();
  2184. }
  2185. _nextFrame = 0;
  2186. _executedLoops = 0;
  2187. }
  2188. void Animate::stop()
  2189. {
  2190. if (_animation->getRestoreOriginalFrame() && _target)
  2191. {
  2192. auto blend = static_cast<Sprite*>(_target)->getBlendFunc();
  2193. static_cast<Sprite*>(_target)->setSpriteFrame(_origFrame);
  2194. static_cast<Sprite*>(_target)->setBlendFunc(blend);
  2195. }
  2196. ActionInterval::stop();
  2197. }
  2198. void Animate::update(float t)
  2199. {
  2200. // if t==1, ignore. Animation should finish with t==1
  2201. if( t < 1.0f )
  2202. {
  2203. t *= _animation->getLoops();
  2204. // new loop? If so, reset frame counter
  2205. unsigned int loopNumber = (unsigned int)t;
  2206. if( loopNumber > _executedLoops )
  2207. {
  2208. _nextFrame = 0;
  2209. _executedLoops++;
  2210. }
  2211. // new t for animations
  2212. t = fmodf(t, 1.0f);
  2213. }
  2214. auto& frames = _animation->getFrames();
  2215. auto numberOfFrames = frames.size();
  2216. SpriteFrame *frameToDisplay = nullptr;
  2217. for( int i=_nextFrame; i < numberOfFrames; i++ )
  2218. {
  2219. float splitTime = _splitTimes->at(i);
  2220. if( splitTime <= t )
  2221. {
  2222. auto blend = static_cast<Sprite*>(_target)->getBlendFunc();
  2223. _currFrameIndex = i;
  2224. AnimationFrame* frame = frames.at(_currFrameIndex);
  2225. frameToDisplay = frame->getSpriteFrame();
  2226. static_cast<Sprite*>(_target)->setSpriteFrame(frameToDisplay);
  2227. static_cast<Sprite*>(_target)->setBlendFunc(blend);
  2228. const ValueMap& dict = frame->getUserInfo();
  2229. if ( !dict.empty() )
  2230. {
  2231. if (_frameDisplayedEvent == nullptr)
  2232. _frameDisplayedEvent = new (std::nothrow) EventCustom(AnimationFrameDisplayedNotification);
  2233. _frameDisplayedEventInfo.target = _target;
  2234. _frameDisplayedEventInfo.userInfo = &dict;
  2235. _frameDisplayedEvent->setUserData(&_frameDisplayedEventInfo);
  2236. Director::getInstance()->getEventDispatcher()->dispatchEvent(_frameDisplayedEvent);
  2237. }
  2238. _nextFrame = i+1;
  2239. }
  2240. // Issue 1438. Could be more than one frame per tick, due to low frame rate or frame delta < 1/FPS
  2241. else {
  2242. break;
  2243. }
  2244. }
  2245. }
  2246. Animate* Animate::reverse() const
  2247. {
  2248. auto& oldArray = _animation->getFrames();
  2249. Vector<AnimationFrame*> newArray(oldArray.size());
  2250. if (!oldArray.empty())
  2251. {
  2252. for (auto iter = oldArray.crbegin(), iterCrend = oldArray.crend(); iter != iterCrend; ++iter)
  2253. {
  2254. AnimationFrame* animFrame = *iter;
  2255. if (!animFrame)
  2256. {
  2257. break;
  2258. }
  2259. newArray.pushBack(animFrame->clone());
  2260. }
  2261. }
  2262. Animation *newAnim = Animation::create(newArray, _animation->getDelayPerUnit(), _animation->getLoops());
  2263. newAnim->setRestoreOriginalFrame(_animation->getRestoreOriginalFrame());
  2264. return Animate::create(newAnim);
  2265. }
  2266. // TargetedAction
  2267. TargetedAction::TargetedAction()
  2268. : _action(nullptr)
  2269. , _forcedTarget(nullptr)
  2270. {
  2271. }
  2272. TargetedAction::~TargetedAction()
  2273. {
  2274. CC_SAFE_RELEASE(_forcedTarget);
  2275. CC_SAFE_RELEASE(_action);
  2276. }
  2277. TargetedAction* TargetedAction::create(Node* target, FiniteTimeAction* action)
  2278. {
  2279. TargetedAction* p = new (std::nothrow) TargetedAction();
  2280. if (p && p->initWithTarget(target, action))
  2281. {
  2282. p->autorelease();
  2283. return p;
  2284. }
  2285. delete p;
  2286. return nullptr;
  2287. }
  2288. bool TargetedAction::initWithTarget(Node* target, FiniteTimeAction* action)
  2289. {
  2290. if(ActionInterval::initWithDuration(action->getDuration()))
  2291. {
  2292. CC_SAFE_RETAIN(target);
  2293. _forcedTarget = target;
  2294. CC_SAFE_RETAIN(action);
  2295. _action = action;
  2296. return true;
  2297. }
  2298. return false;
  2299. }
  2300. TargetedAction* TargetedAction::clone() const
  2301. {
  2302. // no copy constructor
  2303. // win32 : use the _other's copy object.
  2304. return TargetedAction::create(_forcedTarget, _action->clone());
  2305. }
  2306. TargetedAction* TargetedAction::reverse() const
  2307. {
  2308. // just reverse the internal action
  2309. auto a = new (std::nothrow) TargetedAction();
  2310. a->initWithTarget(_forcedTarget, _action->reverse());
  2311. a->autorelease();
  2312. return a;
  2313. }
  2314. void TargetedAction::startWithTarget(Node *target)
  2315. {
  2316. ActionInterval::startWithTarget(target);
  2317. _action->startWithTarget(_forcedTarget);
  2318. }
  2319. void TargetedAction::stop()
  2320. {
  2321. _action->stop();
  2322. }
  2323. void TargetedAction::update(float time)
  2324. {
  2325. if (!(sendUpdateEventToScript(time, _action)))
  2326. _action->update(time);
  2327. }
  2328. void TargetedAction::setForcedTarget(Node* forcedTarget)
  2329. {
  2330. if( _forcedTarget != forcedTarget )
  2331. {
  2332. CC_SAFE_RETAIN(forcedTarget);
  2333. CC_SAFE_RELEASE(_forcedTarget);
  2334. _forcedTarget = forcedTarget;
  2335. }
  2336. }
  2337. // ActionFloat
  2338. ActionFloat* ActionFloat::create(float duration, float from, float to, ActionFloatCallback callback)
  2339. {
  2340. auto ref = new (std::nothrow) ActionFloat();
  2341. if (ref && ref->initWithDuration(duration, from, to, callback))
  2342. {
  2343. ref->autorelease();
  2344. return ref;
  2345. }
  2346. delete ref;
  2347. return nullptr;
  2348. }
  2349. bool ActionFloat::initWithDuration(float duration, float from, float to, ActionFloatCallback callback)
  2350. {
  2351. if (ActionInterval::initWithDuration(duration))
  2352. {
  2353. _from = from;
  2354. _to = to;
  2355. _callback = callback;
  2356. return true;
  2357. }
  2358. return false;
  2359. }
  2360. ActionFloat* ActionFloat::clone() const
  2361. {
  2362. return ActionFloat::create(_duration, _from, _to, _callback);
  2363. }
  2364. void ActionFloat::startWithTarget(Node *target)
  2365. {
  2366. ActionInterval::startWithTarget(target);
  2367. _delta = _to - _from;
  2368. }
  2369. void ActionFloat::update(float delta)
  2370. {
  2371. float value = _to - _delta * (1 - delta);
  2372. if (_callback)
  2373. {
  2374. // report back value to caller
  2375. _callback(value);
  2376. }
  2377. }
  2378. ActionFloat* ActionFloat::reverse() const
  2379. {
  2380. return ActionFloat::create(_duration, _to, _from, _callback);
  2381. }
  2382. NS_CC_END