CCActionInterval.cpp 63 KB

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