CCActionInterval.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCActionInterval.h"
  25. #include <stdarg.h>
  26. #include "2d/CCSprite.h"
  27. #include "2d/CCNode.h"
  28. #include "2d/CCSpriteFrame.h"
  29. #include "2d/CCActionInstant.h"
  30. #include "base/CCDirector.h"
  31. #include "base/CCEventCustom.h"
  32. #include "base/CCEventDispatcher.h"
  33. #include "platform/CCStdC.h"
  34. #include "base/CCScriptSupport.h"
  35. NS_CC_BEGIN
  36. // Extra action for making a Sequence or Spawn when only adding one action to it.
  37. class ExtraAction : public FiniteTimeAction
  38. {
  39. public:
  40. static ExtraAction* create();
  41. virtual ExtraAction* clone() const;
  42. virtual ExtraAction* reverse() const;
  43. virtual void update(float time);
  44. virtual void step(float dt);
  45. };
  46. ExtraAction* ExtraAction::create()
  47. {
  48. ExtraAction* ret = new (std::nothrow) ExtraAction();
  49. if (ret)
  50. {
  51. ret->autorelease();
  52. }
  53. return ret;
  54. }
  55. ExtraAction* ExtraAction::clone() const
  56. {
  57. // no copy constructor
  58. return ExtraAction::create();
  59. }
  60. ExtraAction* ExtraAction::reverse() const
  61. {
  62. return ExtraAction::create();
  63. }
  64. void ExtraAction::update(float /*time*/)
  65. {
  66. }
  67. void ExtraAction::step(float /*dt*/)
  68. {
  69. }
  70. //
  71. // IntervalAction
  72. //
  73. bool ActionInterval::initWithDuration(float d)
  74. {
  75. _duration = d;
  76. _elapsed = 0;
  77. _firstTick = true;
  78. _done = false;
  79. return true;
  80. }
  81. bool ActionInterval::sendUpdateEventToScript(float dt, Action *actionObject)
  82. {
  83. #if CC_ENABLE_SCRIPT_BINDING
  84. if (_scriptType == kScriptTypeJavascript)
  85. {
  86. if (ScriptEngineManager::sendActionEventToJS(actionObject, kActionUpdate, (void *)&dt))
  87. return true;
  88. }
  89. #endif
  90. return false;
  91. }
  92. bool ActionInterval::isDone() const
  93. {
  94. return _done;
  95. }
  96. void ActionInterval::step(float dt)
  97. {
  98. if (_firstTick)
  99. {
  100. _firstTick = false;
  101. _elapsed = 0;
  102. }
  103. else
  104. {
  105. _elapsed += dt;
  106. }
  107. float updateDt = std::max(0.0f, // needed for rewind. elapsed could be negative
  108. std::min(1.0f, _elapsed / _duration)
  109. );
  110. if (sendUpdateEventToScript(updateDt, this)) return;
  111. this->update(updateDt);
  112. _done = _elapsed >= _duration;
  113. }
  114. void ActionInterval::setAmplitudeRate(float /*amp*/)
  115. {
  116. // Abstract class needs implementation
  117. CCASSERT(0, "Subclass should implement this method!");
  118. }
  119. float ActionInterval::getAmplitudeRate()
  120. {
  121. // Abstract class needs implementation
  122. CCASSERT(0, "Subclass should implement this method!");
  123. return 0;
  124. }
  125. void ActionInterval::startWithTarget(Node *target)
  126. {
  127. FiniteTimeAction::startWithTarget(target);
  128. _elapsed = 0.0f;
  129. _firstTick = true;
  130. _done = false;
  131. }
  132. //
  133. // Sequence
  134. //
  135. Sequence* Sequence::createWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo)
  136. {
  137. Sequence *sequence = new (std::nothrow) Sequence();
  138. if (sequence && sequence->initWithTwoActions(actionOne, actionTwo))
  139. {
  140. sequence->autorelease();
  141. return sequence;
  142. }
  143. delete sequence;
  144. return nullptr;
  145. }
  146. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  147. Sequence* Sequence::variadicCreate(FiniteTimeAction *action1, ...)
  148. {
  149. va_list params;
  150. va_start(params, action1);
  151. Sequence *ret = Sequence::createWithVariableList(action1, params);
  152. va_end(params);
  153. return ret;
  154. }
  155. #else
  156. Sequence* Sequence::create(FiniteTimeAction *action1, ...)
  157. {
  158. va_list params;
  159. va_start(params, action1);
  160. Sequence *ret = Sequence::createWithVariableList(action1, params);
  161. va_end(params);
  162. return ret;
  163. }
  164. #endif
  165. Sequence* Sequence::createWithVariableList(FiniteTimeAction *action1, va_list args)
  166. {
  167. FiniteTimeAction *now;
  168. FiniteTimeAction *prev = action1;
  169. bool bOneAction = true;
  170. while (action1)
  171. {
  172. now = va_arg(args, FiniteTimeAction*);
  173. if (now)
  174. {
  175. prev = createWithTwoActions(prev, now);
  176. bOneAction = false;
  177. }
  178. else
  179. {
  180. // If only one action is added to Sequence, make up a Sequence by adding a simplest finite time action.
  181. if (bOneAction)
  182. {
  183. prev = createWithTwoActions(prev, ExtraAction::create());
  184. }
  185. break;
  186. }
  187. }
  188. return ((Sequence*)prev);
  189. }
  190. Sequence* Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions)
  191. {
  192. Sequence* seq = new (std::nothrow) Sequence;
  193. if (seq && seq->init(arrayOfActions))
  194. {
  195. seq->autorelease();
  196. return seq;
  197. }
  198. delete seq;
  199. return nullptr;
  200. }
  201. bool Sequence::init(const Vector<FiniteTimeAction*>& arrayOfActions)
  202. {
  203. auto count = arrayOfActions.size();
  204. if (count == 0)
  205. return false;
  206. if (count == 1)
  207. return initWithTwoActions(arrayOfActions.at(0), ExtraAction::create());
  208. // else size > 1
  209. auto prev = arrayOfActions.at(0);
  210. for (int i = 1; i < count-1; ++i)
  211. {
  212. prev = createWithTwoActions(prev, arrayOfActions.at(i));
  213. }
  214. return initWithTwoActions(prev, arrayOfActions.at(count-1));
  215. }
  216. bool Sequence::initWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo)
  217. {
  218. CCASSERT(actionOne != nullptr, "actionOne can't be nullptr!");
  219. CCASSERT(actionTwo != nullptr, "actionTwo can't be nullptr!");
  220. if (actionOne == nullptr || actionTwo == nullptr)
  221. {
  222. log("Sequence::initWithTwoActions error: action is nullptr!!");
  223. return false;
  224. }
  225. float d = actionOne->getDuration() + actionTwo->getDuration();
  226. ActionInterval::initWithDuration(d);
  227. _actions[0] = actionOne;
  228. actionOne->retain();
  229. _actions[1] = actionTwo;
  230. actionTwo->retain();
  231. return true;
  232. }
  233. bool Sequence::isDone() const
  234. {
  235. // fix issue #17884
  236. if (dynamic_cast<ActionInstant*>(_actions[1]))
  237. return (_done && _actions[1]->isDone());
  238. else
  239. return _done;
  240. }
  241. Sequence* Sequence::clone() const
  242. {
  243. // no copy constructor
  244. if (_actions[0] && _actions[1])
  245. {
  246. return Sequence::create(_actions[0]->clone(), _actions[1]->clone(), nullptr);
  247. } else {
  248. return nullptr;
  249. }
  250. }
  251. Sequence::Sequence()
  252. : _split(0)
  253. {
  254. _actions[0] = nullptr;
  255. _actions[1] = nullptr;
  256. }
  257. Sequence::~Sequence()
  258. {
  259. CC_SAFE_RELEASE(_actions[0]);
  260. CC_SAFE_RELEASE(_actions[1]);
  261. }
  262. void Sequence::startWithTarget(Node *target)
  263. {
  264. if (target == nullptr)
  265. {
  266. log("Sequence::startWithTarget error: target is nullptr!");
  267. return;
  268. }
  269. if (_actions[0] == nullptr || _actions[1] == nullptr)
  270. {
  271. log("Sequence::startWithTarget error: _actions[0] or _actions[1] is nullptr!");
  272. return;
  273. }
  274. if (_duration > FLT_EPSILON)
  275. // fix #14936 - FLT_EPSILON (instant action) / very fast duration (0.001) leads to worng split, that leads to call instant action few times
  276. _split = _actions[0]->getDuration() > FLT_EPSILON ? _actions[0]->getDuration() / _duration : 0;
  277. ActionInterval::startWithTarget(target);
  278. _last = -1;
  279. }
  280. void Sequence::stop()
  281. {
  282. // Issue #1305
  283. if( _last != - 1 && _actions[_last])
  284. {
  285. _actions[_last]->stop();
  286. }
  287. ActionInterval::stop();
  288. }
  289. void Sequence::update(float t)
  290. {
  291. int found = 0;
  292. float new_t = 0.0f;
  293. if( t < _split )
  294. {
  295. // action[0]
  296. found = 0;
  297. if( _split != 0 )
  298. new_t = t / _split;
  299. else
  300. new_t = 1;
  301. }
  302. else
  303. {
  304. // action[1]
  305. found = 1;
  306. if ( _split == 1 )
  307. new_t = 1;
  308. else
  309. new_t = (t-_split) / (1 - _split );
  310. }
  311. if ( found==1 )
  312. {
  313. if( _last == -1 )
  314. {
  315. // action[0] was skipped, execute it.
  316. _actions[0]->startWithTarget(_target);
  317. if (!(sendUpdateEventToScript(1.0f, _actions[0])))
  318. _actions[0]->update(1.0f);
  319. _actions[0]->stop();
  320. }
  321. else if( _last == 0 )
  322. {
  323. // switching to action 1. stop action 0.
  324. if (!(sendUpdateEventToScript(1.0f, _actions[0])))
  325. _actions[0]->update(1.0f);
  326. _actions[0]->stop();
  327. }
  328. }
  329. else if(found==0 && _last==1 )
  330. {
  331. // Reverse mode ?
  332. // FIXME: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode"
  333. // since it will require a hack to know if an action is on reverse mode or not.
  334. // "step" should be overridden, and the "reverseMode" value propagated to inner Sequences.
  335. if (!(sendUpdateEventToScript(0, _actions[1])))
  336. _actions[1]->update(0);
  337. _actions[1]->stop();
  338. }
  339. // Last action found and it is done.
  340. if( found == _last && _actions[found]->isDone() )
  341. {
  342. return;
  343. }
  344. // Last action found and it is done
  345. if( found != _last )
  346. {
  347. _actions[found]->startWithTarget(_target);
  348. }
  349. if (!(sendUpdateEventToScript(new_t, _actions[found])))
  350. _actions[found]->update(new_t);
  351. _last = found;
  352. }
  353. Sequence* Sequence::reverse() const
  354. {
  355. if (_actions[0] && _actions[1])
  356. return Sequence::createWithTwoActions(_actions[1]->reverse(), _actions[0]->reverse());
  357. else
  358. return nullptr;
  359. }
  360. //
  361. // Repeat
  362. //
  363. Repeat* Repeat::create(FiniteTimeAction *action, unsigned int times)
  364. {
  365. Repeat* repeat = new (std::nothrow) Repeat();
  366. if (repeat && repeat->initWithAction(action, times))
  367. {
  368. repeat->autorelease();
  369. return repeat;
  370. }
  371. delete repeat;
  372. return nullptr;
  373. }
  374. bool Repeat::initWithAction(FiniteTimeAction *action, unsigned int times)
  375. {
  376. if (action && ActionInterval::initWithDuration(action->getDuration() * times))
  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. //
  1460. // BezierBy
  1461. //
  1462. BezierBy* BezierBy::create(float t, const ccBezierConfig& c)
  1463. {
  1464. BezierBy *bezierBy = new (std::nothrow) BezierBy();
  1465. if (bezierBy && bezierBy->initWithDuration(t, c))
  1466. {
  1467. bezierBy->autorelease();
  1468. return bezierBy;
  1469. }
  1470. delete bezierBy;
  1471. return nullptr;
  1472. }
  1473. bool BezierBy::initWithDuration(float t, const ccBezierConfig& c)
  1474. {
  1475. if (ActionInterval::initWithDuration(t))
  1476. {
  1477. _config = c;
  1478. return true;
  1479. }
  1480. return false;
  1481. }
  1482. void BezierBy::startWithTarget(Node *target)
  1483. {
  1484. ActionInterval::startWithTarget(target);
  1485. _previousPosition = _startPosition = target->getPosition();
  1486. }
  1487. BezierBy* BezierBy::clone() const
  1488. {
  1489. // no copy constructor
  1490. return BezierBy::create(_duration, _config);
  1491. }
  1492. void BezierBy::update(float time)
  1493. {
  1494. if (_target)
  1495. {
  1496. float xa = 0;
  1497. float xb = _config.controlPoint_1.x;
  1498. float xc = _config.controlPoint_2.x;
  1499. float xd = _config.endPosition.x;
  1500. float ya = 0;
  1501. float yb = _config.controlPoint_1.y;
  1502. float yc = _config.controlPoint_2.y;
  1503. float yd = _config.endPosition.y;
  1504. float x = bezierat(xa, xb, xc, xd, time);
  1505. float y = bezierat(ya, yb, yc, yd, time);
  1506. #if CC_ENABLE_STACKABLE_ACTIONS
  1507. Vec2 currentPos = _target->getPosition();
  1508. Vec2 diff = currentPos - _previousPosition;
  1509. _startPosition = _startPosition + diff;
  1510. Vec2 newPos = _startPosition + Vec2(x,y);
  1511. _target->setPosition(newPos);
  1512. _previousPosition = newPos;
  1513. #else
  1514. _target->setPosition( _startPosition + Vec2(x,y));
  1515. #endif // !CC_ENABLE_STACKABLE_ACTIONS
  1516. }
  1517. }
  1518. BezierBy* BezierBy::reverse() const
  1519. {
  1520. ccBezierConfig r;
  1521. r.endPosition = -_config.endPosition;
  1522. r.controlPoint_1 = _config.controlPoint_2 + (-_config.endPosition);
  1523. r.controlPoint_2 = _config.controlPoint_1 + (-_config.endPosition);
  1524. BezierBy *action = BezierBy::create(_duration, r);
  1525. return action;
  1526. }
  1527. //
  1528. // BezierTo
  1529. //
  1530. BezierTo* BezierTo::create(float t, const ccBezierConfig& c)
  1531. {
  1532. BezierTo *bezierTo = new (std::nothrow) BezierTo();
  1533. if (bezierTo && bezierTo->initWithDuration(t, c))
  1534. {
  1535. bezierTo->autorelease();
  1536. return bezierTo;
  1537. }
  1538. delete bezierTo;
  1539. return nullptr;
  1540. }
  1541. bool BezierTo::initWithDuration(float t, const ccBezierConfig &c)
  1542. {
  1543. if (ActionInterval::initWithDuration(t))
  1544. {
  1545. _toConfig = c;
  1546. return true;
  1547. }
  1548. return false;
  1549. }
  1550. BezierTo* BezierTo::clone() const
  1551. {
  1552. // no copy constructor
  1553. return BezierTo::create(_duration, _toConfig);
  1554. }
  1555. void BezierTo::startWithTarget(Node *target)
  1556. {
  1557. BezierBy::startWithTarget(target);
  1558. _config.controlPoint_1 = _toConfig.controlPoint_1 - _startPosition;
  1559. _config.controlPoint_2 = _toConfig.controlPoint_2 - _startPosition;
  1560. _config.endPosition = _toConfig.endPosition - _startPosition;
  1561. }
  1562. BezierTo* BezierTo::reverse() const
  1563. {
  1564. CCASSERT(false, "CCBezierTo doesn't support the 'reverse' method");
  1565. return nullptr;
  1566. }
  1567. //
  1568. // ScaleTo
  1569. //
  1570. ScaleTo* ScaleTo::create(float duration, float s)
  1571. {
  1572. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1573. if (scaleTo && scaleTo->initWithDuration(duration, s))
  1574. {
  1575. scaleTo->autorelease();
  1576. return scaleTo;
  1577. }
  1578. delete scaleTo;
  1579. return nullptr;
  1580. }
  1581. ScaleTo* ScaleTo::create(float duration, float sx, float sy)
  1582. {
  1583. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1584. if (scaleTo && scaleTo->initWithDuration(duration, sx, sy))
  1585. {
  1586. scaleTo->autorelease();
  1587. return scaleTo;
  1588. }
  1589. delete scaleTo;
  1590. return nullptr;
  1591. }
  1592. ScaleTo* ScaleTo::create(float duration, float sx, float sy, float sz)
  1593. {
  1594. ScaleTo *scaleTo = new (std::nothrow) ScaleTo();
  1595. if (scaleTo && scaleTo->initWithDuration(duration, sx, sy, sz))
  1596. {
  1597. scaleTo->autorelease();
  1598. return scaleTo;
  1599. }
  1600. delete scaleTo;
  1601. return nullptr;
  1602. }
  1603. bool ScaleTo::initWithDuration(float duration, float s)
  1604. {
  1605. if (ActionInterval::initWithDuration(duration))
  1606. {
  1607. _endScaleX = s;
  1608. _endScaleY = s;
  1609. _endScaleZ = s;
  1610. return true;
  1611. }
  1612. return false;
  1613. }
  1614. bool ScaleTo::initWithDuration(float duration, float sx, float sy)
  1615. {
  1616. if (ActionInterval::initWithDuration(duration))
  1617. {
  1618. _endScaleX = sx;
  1619. _endScaleY = sy;
  1620. _endScaleZ = 1.f;
  1621. return true;
  1622. }
  1623. return false;
  1624. }
  1625. bool ScaleTo::initWithDuration(float duration, float sx, float sy, float sz)
  1626. {
  1627. if (ActionInterval::initWithDuration(duration))
  1628. {
  1629. _endScaleX = sx;
  1630. _endScaleY = sy;
  1631. _endScaleZ = sz;
  1632. return true;
  1633. }
  1634. return false;
  1635. }
  1636. ScaleTo* ScaleTo::clone() const
  1637. {
  1638. // no copy constructor
  1639. return ScaleTo::create(_duration, _endScaleX, _endScaleY, _endScaleZ);
  1640. }
  1641. ScaleTo* ScaleTo::reverse() const
  1642. {
  1643. CCASSERT(false, "reverse() not supported in ScaleTo");
  1644. return nullptr;
  1645. }
  1646. void ScaleTo::startWithTarget(Node *target)
  1647. {
  1648. ActionInterval::startWithTarget(target);
  1649. _startScaleX = target->getScaleX();
  1650. _startScaleY = target->getScaleY();
  1651. _startScaleZ = target->getScaleZ();
  1652. _deltaX = _endScaleX - _startScaleX;
  1653. _deltaY = _endScaleY - _startScaleY;
  1654. _deltaZ = _endScaleZ - _startScaleZ;
  1655. }
  1656. void ScaleTo::update(float time)
  1657. {
  1658. if (_target)
  1659. {
  1660. _target->setScaleX(_startScaleX + _deltaX * time);
  1661. _target->setScaleY(_startScaleY + _deltaY * time);
  1662. _target->setScaleZ(_startScaleZ + _deltaZ * time);
  1663. }
  1664. }
  1665. //
  1666. // ScaleBy
  1667. //
  1668. ScaleBy* ScaleBy::create(float duration, float s)
  1669. {
  1670. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1671. if (scaleBy && scaleBy->initWithDuration(duration, s))
  1672. {
  1673. scaleBy->autorelease();
  1674. return scaleBy;
  1675. }
  1676. delete scaleBy;
  1677. return nullptr;
  1678. }
  1679. ScaleBy* ScaleBy::create(float duration, float sx, float sy)
  1680. {
  1681. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1682. if (scaleBy && scaleBy->initWithDuration(duration, sx, sy, 1.f))
  1683. {
  1684. scaleBy->autorelease();
  1685. return scaleBy;
  1686. }
  1687. delete scaleBy;
  1688. return nullptr;
  1689. }
  1690. ScaleBy* ScaleBy::create(float duration, float sx, float sy, float sz)
  1691. {
  1692. ScaleBy *scaleBy = new (std::nothrow) ScaleBy();
  1693. if (scaleBy && scaleBy->initWithDuration(duration, sx, sy, sz))
  1694. {
  1695. scaleBy->autorelease();
  1696. return scaleBy;
  1697. }
  1698. delete scaleBy;
  1699. return nullptr;
  1700. }
  1701. ScaleBy* ScaleBy::clone() const
  1702. {
  1703. // no copy constructor
  1704. return ScaleBy::create(_duration, _endScaleX, _endScaleY, _endScaleZ);
  1705. }
  1706. void ScaleBy::startWithTarget(Node *target)
  1707. {
  1708. ScaleTo::startWithTarget(target);
  1709. _deltaX = _startScaleX * _endScaleX - _startScaleX;
  1710. _deltaY = _startScaleY * _endScaleY - _startScaleY;
  1711. _deltaZ = _startScaleZ * _endScaleZ - _startScaleZ;
  1712. }
  1713. ScaleBy* ScaleBy::reverse() const
  1714. {
  1715. return ScaleBy::create(_duration, 1 / _endScaleX, 1 / _endScaleY, 1/ _endScaleZ);
  1716. }
  1717. //
  1718. // Blink
  1719. //
  1720. Blink* Blink::create(float duration, int blinks)
  1721. {
  1722. Blink *blink = new (std::nothrow) Blink();
  1723. if (blink && blink->initWithDuration(duration, blinks))
  1724. {
  1725. blink->autorelease();
  1726. return blink;
  1727. }
  1728. delete blink;
  1729. return nullptr;
  1730. }
  1731. bool Blink::initWithDuration(float duration, int blinks)
  1732. {
  1733. CCASSERT(blinks>=0, "blinks should be >= 0");
  1734. if (blinks < 0)
  1735. {
  1736. log("Blink::initWithDuration error:blinks should be >= 0");
  1737. return false;
  1738. }
  1739. if (ActionInterval::initWithDuration(duration) && blinks>=0)
  1740. {
  1741. _times = blinks;
  1742. return true;
  1743. }
  1744. return false;
  1745. }
  1746. void Blink::stop()
  1747. {
  1748. if (nullptr != _target)
  1749. _target->setVisible(_originalState);
  1750. ActionInterval::stop();
  1751. }
  1752. void Blink::startWithTarget(Node *target)
  1753. {
  1754. ActionInterval::startWithTarget(target);
  1755. _originalState = target->isVisible();
  1756. }
  1757. Blink* Blink::clone() const
  1758. {
  1759. // no copy constructor
  1760. return Blink::create(_duration, _times);
  1761. }
  1762. void Blink::update(float time)
  1763. {
  1764. if (_target && ! isDone())
  1765. {
  1766. float slice = 1.0f / _times;
  1767. float m = fmodf(time, slice);
  1768. _target->setVisible(m > slice / 2 ? true : false);
  1769. }
  1770. }
  1771. Blink* Blink::reverse() const
  1772. {
  1773. return Blink::create(_duration, _times);
  1774. }
  1775. //
  1776. // FadeIn
  1777. //
  1778. FadeIn* FadeIn::create(float d)
  1779. {
  1780. FadeIn* action = new (std::nothrow) FadeIn();
  1781. if (action && action->initWithDuration(d,255.0f))
  1782. {
  1783. action->autorelease();
  1784. return action;
  1785. }
  1786. delete action;
  1787. return nullptr;
  1788. }
  1789. FadeIn* FadeIn::clone() const
  1790. {
  1791. // no copy constructor
  1792. return FadeIn::create(_duration);
  1793. }
  1794. void FadeIn::setReverseAction(cocos2d::FadeTo *ac)
  1795. {
  1796. _reverseAction = ac;
  1797. }
  1798. FadeTo* FadeIn::reverse() const
  1799. {
  1800. auto action = FadeOut::create(_duration);
  1801. action->setReverseAction(const_cast<FadeIn*>(this));
  1802. return action;
  1803. }
  1804. void FadeIn::startWithTarget(cocos2d::Node *target)
  1805. {
  1806. ActionInterval::startWithTarget(target);
  1807. if (nullptr != _reverseAction)
  1808. this->_toOpacity = this->_reverseAction->_fromOpacity;
  1809. else
  1810. _toOpacity = 255.0f;
  1811. if (target)
  1812. _fromOpacity = target->getOpacity();
  1813. }
  1814. //
  1815. // FadeOut
  1816. //
  1817. FadeOut* FadeOut::create(float d)
  1818. {
  1819. FadeOut* action = new (std::nothrow) FadeOut();
  1820. if (action && action->initWithDuration(d,0.0f))
  1821. {
  1822. action->autorelease();
  1823. return action;
  1824. }
  1825. delete action;
  1826. return nullptr;
  1827. }
  1828. FadeOut* FadeOut::clone() const
  1829. {
  1830. // no copy constructor
  1831. return FadeOut::create(_duration);
  1832. }
  1833. void FadeOut::startWithTarget(cocos2d::Node *target)
  1834. {
  1835. ActionInterval::startWithTarget(target);
  1836. if (nullptr != _reverseAction)
  1837. _toOpacity = _reverseAction->_fromOpacity;
  1838. else
  1839. _toOpacity = 0.0f;
  1840. if (target)
  1841. _fromOpacity = target->getOpacity();
  1842. }
  1843. void FadeOut::setReverseAction(cocos2d::FadeTo *ac)
  1844. {
  1845. _reverseAction = ac;
  1846. }
  1847. FadeTo* FadeOut::reverse() const
  1848. {
  1849. auto action = FadeIn::create(_duration);
  1850. action->setReverseAction(const_cast<FadeOut*>(this));
  1851. return action;
  1852. }
  1853. //
  1854. // FadeTo
  1855. //
  1856. FadeTo* FadeTo::create(float duration, GLubyte opacity)
  1857. {
  1858. FadeTo *fadeTo = new (std::nothrow) FadeTo();
  1859. if (fadeTo && fadeTo->initWithDuration(duration, opacity))
  1860. {
  1861. fadeTo->autorelease();
  1862. return fadeTo;
  1863. }
  1864. delete fadeTo;
  1865. return nullptr;
  1866. }
  1867. bool FadeTo::initWithDuration(float duration, GLubyte opacity)
  1868. {
  1869. if (ActionInterval::initWithDuration(duration))
  1870. {
  1871. _toOpacity = opacity;
  1872. return true;
  1873. }
  1874. return false;
  1875. }
  1876. FadeTo* FadeTo::clone() const
  1877. {
  1878. // no copy constructor
  1879. return FadeTo::create(_duration, _toOpacity);
  1880. }
  1881. FadeTo* FadeTo::reverse() const
  1882. {
  1883. CCASSERT(false, "reverse() not supported in FadeTo");
  1884. return nullptr;
  1885. }
  1886. void FadeTo::startWithTarget(Node *target)
  1887. {
  1888. ActionInterval::startWithTarget(target);
  1889. if (target)
  1890. {
  1891. _fromOpacity = target->getOpacity();
  1892. }
  1893. }
  1894. void FadeTo::update(float time)
  1895. {
  1896. if (_target)
  1897. {
  1898. _target->setOpacity((GLubyte)(_fromOpacity + (_toOpacity - _fromOpacity) * time));
  1899. }
  1900. }
  1901. //
  1902. // TintTo
  1903. //
  1904. TintTo* TintTo::create(float duration, GLubyte red, GLubyte green, GLubyte blue)
  1905. {
  1906. TintTo *tintTo = new (std::nothrow) TintTo();
  1907. if (tintTo && tintTo->initWithDuration(duration, red, green, blue))
  1908. {
  1909. tintTo->autorelease();
  1910. return tintTo;
  1911. }
  1912. delete tintTo;
  1913. return nullptr;
  1914. }
  1915. TintTo* TintTo::create(float duration, const Color3B& color)
  1916. {
  1917. return create(duration, color.r, color.g, color.b);
  1918. }
  1919. bool TintTo::initWithDuration(float duration, GLubyte red, GLubyte green, GLubyte blue)
  1920. {
  1921. if (ActionInterval::initWithDuration(duration))
  1922. {
  1923. _to = Color3B(red, green, blue);
  1924. return true;
  1925. }
  1926. return false;
  1927. }
  1928. TintTo* TintTo::clone() const
  1929. {
  1930. // no copy constructor
  1931. return TintTo::create(_duration, _to.r, _to.g, _to.b);
  1932. }
  1933. TintTo* TintTo::reverse() const
  1934. {
  1935. CCASSERT(false, "reverse() not supported in TintTo");
  1936. return nullptr;
  1937. }
  1938. void TintTo::startWithTarget(Node *target)
  1939. {
  1940. ActionInterval::startWithTarget(target);
  1941. if (_target)
  1942. {
  1943. _from = _target->getColor();
  1944. }
  1945. }
  1946. void TintTo::update(float time)
  1947. {
  1948. if (_target)
  1949. {
  1950. _target->setColor(Color3B(GLubyte(_from.r + (_to.r - _from.r) * time),
  1951. (GLubyte)(_from.g + (_to.g - _from.g) * time),
  1952. (GLubyte)(_from.b + (_to.b - _from.b) * time)));
  1953. }
  1954. }
  1955. //
  1956. // TintBy
  1957. //
  1958. TintBy* TintBy::create(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue)
  1959. {
  1960. TintBy *tintBy = new (std::nothrow) TintBy();
  1961. if (tintBy && tintBy->initWithDuration(duration, deltaRed, deltaGreen, deltaBlue))
  1962. {
  1963. tintBy->autorelease();
  1964. return tintBy;
  1965. }
  1966. delete tintBy;
  1967. return nullptr;
  1968. }
  1969. bool TintBy::initWithDuration(float duration, GLshort deltaRed, GLshort deltaGreen, GLshort deltaBlue)
  1970. {
  1971. if (ActionInterval::initWithDuration(duration))
  1972. {
  1973. _deltaR = deltaRed;
  1974. _deltaG = deltaGreen;
  1975. _deltaB = deltaBlue;
  1976. return true;
  1977. }
  1978. return false;
  1979. }
  1980. TintBy* TintBy::clone() const
  1981. {
  1982. // no copy constructor
  1983. return TintBy::create(_duration, _deltaR, _deltaG, _deltaB);
  1984. }
  1985. void TintBy::startWithTarget(Node *target)
  1986. {
  1987. ActionInterval::startWithTarget(target);
  1988. if (target)
  1989. {
  1990. Color3B color = target->getColor();
  1991. _fromR = color.r;
  1992. _fromG = color.g;
  1993. _fromB = color.b;
  1994. }
  1995. }
  1996. void TintBy::update(float time)
  1997. {
  1998. if (_target)
  1999. {
  2000. _target->setColor(Color3B((GLubyte)(_fromR + _deltaR * time),
  2001. (GLubyte)(_fromG + _deltaG * time),
  2002. (GLubyte)(_fromB + _deltaB * time)));
  2003. }
  2004. }
  2005. TintBy* TintBy::reverse() const
  2006. {
  2007. return TintBy::create(_duration, -_deltaR, -_deltaG, -_deltaB);
  2008. }
  2009. //
  2010. // DelayTime
  2011. //
  2012. DelayTime* DelayTime::create(float d)
  2013. {
  2014. DelayTime* action = new (std::nothrow) DelayTime();
  2015. if (action && action->initWithDuration(d))
  2016. {
  2017. action->autorelease();
  2018. return action;
  2019. }
  2020. delete action;
  2021. return nullptr;
  2022. }
  2023. DelayTime* DelayTime::clone() const
  2024. {
  2025. // no copy constructor
  2026. return DelayTime::create(_duration);
  2027. }
  2028. void DelayTime::update(float /*time*/)
  2029. {
  2030. return;
  2031. }
  2032. DelayTime* DelayTime::reverse() const
  2033. {
  2034. return DelayTime::create(_duration);
  2035. }
  2036. //
  2037. // ReverseTime
  2038. //
  2039. ReverseTime* ReverseTime::create(FiniteTimeAction *action)
  2040. {
  2041. // casting to prevent warnings
  2042. ReverseTime *reverseTime = new (std::nothrow) ReverseTime();
  2043. if (reverseTime && reverseTime->initWithAction( action->clone() ))
  2044. {
  2045. reverseTime->autorelease();
  2046. return reverseTime;
  2047. }
  2048. delete reverseTime;
  2049. return nullptr;
  2050. }
  2051. bool ReverseTime::initWithAction(FiniteTimeAction *action)
  2052. {
  2053. CCASSERT(action != nullptr, "action can't be nullptr!");
  2054. CCASSERT(action != _other, "action doesn't equal to _other!");
  2055. if (action == nullptr || action == _other)
  2056. {
  2057. log("ReverseTime::initWithAction error: action is null or action equal to _other");
  2058. return false;
  2059. }
  2060. if (ActionInterval::initWithDuration(action->getDuration()))
  2061. {
  2062. // Don't leak if action is reused
  2063. CC_SAFE_RELEASE(_other);
  2064. _other = action;
  2065. action->retain();
  2066. return true;
  2067. }
  2068. return false;
  2069. }
  2070. ReverseTime* ReverseTime::clone() const
  2071. {
  2072. // no copy constructor
  2073. return ReverseTime::create(_other->clone());
  2074. }
  2075. ReverseTime::ReverseTime() : _other(nullptr)
  2076. {
  2077. }
  2078. ReverseTime::~ReverseTime()
  2079. {
  2080. CC_SAFE_RELEASE(_other);
  2081. }
  2082. void ReverseTime::startWithTarget(Node *target)
  2083. {
  2084. ActionInterval::startWithTarget(target);
  2085. _other->startWithTarget(target);
  2086. }
  2087. void ReverseTime::stop()
  2088. {
  2089. _other->stop();
  2090. ActionInterval::stop();
  2091. }
  2092. void ReverseTime::update(float time)
  2093. {
  2094. if (_other)
  2095. {
  2096. if (!(sendUpdateEventToScript(1 - time, _other)))
  2097. _other->update(1 - time);
  2098. }
  2099. }
  2100. ReverseTime* ReverseTime::reverse() const
  2101. {
  2102. // FIXME: This looks like a bug
  2103. return (ReverseTime*)_other->clone();
  2104. }
  2105. //
  2106. // Animate
  2107. //
  2108. Animate* Animate::create(Animation *animation)
  2109. {
  2110. Animate *animate = new (std::nothrow) Animate();
  2111. if (animate && animate->initWithAnimation(animation))
  2112. {
  2113. animate->autorelease();
  2114. return animate;
  2115. }
  2116. delete animate;
  2117. return nullptr;
  2118. }
  2119. Animate::Animate()
  2120. : _splitTimes(new std::vector<float>)
  2121. , _nextFrame(0)
  2122. , _origFrame(nullptr)
  2123. , _executedLoops(0)
  2124. , _animation(nullptr)
  2125. , _frameDisplayedEvent(nullptr)
  2126. , _currFrameIndex(0)
  2127. {
  2128. }
  2129. Animate::~Animate()
  2130. {
  2131. CC_SAFE_RELEASE(_animation);
  2132. CC_SAFE_RELEASE(_origFrame);
  2133. CC_SAFE_DELETE(_splitTimes);
  2134. CC_SAFE_RELEASE(_frameDisplayedEvent);
  2135. }
  2136. bool Animate::initWithAnimation(Animation* animation)
  2137. {
  2138. CCASSERT( animation!=nullptr, "Animate: argument Animation must be non-nullptr");
  2139. if (animation == nullptr)
  2140. {
  2141. log("Animate::initWithAnimation: argument Animation must be non-nullptr");
  2142. return false;
  2143. }
  2144. float singleDuration = animation->getDuration();
  2145. if ( ActionInterval::initWithDuration(singleDuration * animation->getLoops() ) )
  2146. {
  2147. _nextFrame = 0;
  2148. setAnimation(animation);
  2149. _origFrame = nullptr;
  2150. _executedLoops = 0;
  2151. _splitTimes->reserve(animation->getFrames().size());
  2152. float accumUnitsOfTime = 0;
  2153. float newUnitOfTimeValue = singleDuration / animation->getTotalDelayUnits();
  2154. auto& frames = animation->getFrames();
  2155. for (auto& frame : frames)
  2156. {
  2157. float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration;
  2158. accumUnitsOfTime += frame->getDelayUnits();
  2159. _splitTimes->push_back(value);
  2160. }
  2161. return true;
  2162. }
  2163. return false;
  2164. }
  2165. void Animate::setAnimation(cocos2d::Animation *animation)
  2166. {
  2167. if (_animation != animation)
  2168. {
  2169. CC_SAFE_RETAIN(animation);
  2170. CC_SAFE_RELEASE(_animation);
  2171. _animation = animation;
  2172. }
  2173. }
  2174. Animate* Animate::clone() const
  2175. {
  2176. // no copy constructor
  2177. return Animate::create(_animation->clone());
  2178. }
  2179. void Animate::startWithTarget(Node *target)
  2180. {
  2181. ActionInterval::startWithTarget(target);
  2182. Sprite *sprite = static_cast<Sprite*>(target);
  2183. CC_SAFE_RELEASE(_origFrame);
  2184. if (_animation->getRestoreOriginalFrame())
  2185. {
  2186. _origFrame = sprite->getSpriteFrame();
  2187. _origFrame->retain();
  2188. }
  2189. _nextFrame = 0;
  2190. _executedLoops = 0;
  2191. }
  2192. void Animate::stop()
  2193. {
  2194. if (_animation->getRestoreOriginalFrame() && _target)
  2195. {
  2196. auto blend = static_cast<Sprite*>(_target)->getBlendFunc();
  2197. static_cast<Sprite*>(_target)->setSpriteFrame(_origFrame);
  2198. static_cast<Sprite*>(_target)->setBlendFunc(blend);
  2199. }
  2200. ActionInterval::stop();
  2201. }
  2202. void Animate::update(float t)
  2203. {
  2204. // if t==1, ignore. Animation should finish with t==1
  2205. if( t < 1.0f )
  2206. {
  2207. t *= _animation->getLoops();
  2208. // new loop? If so, reset frame counter
  2209. unsigned int loopNumber = (unsigned int)t;
  2210. if( loopNumber > _executedLoops )
  2211. {
  2212. _nextFrame = 0;
  2213. _executedLoops++;
  2214. }
  2215. // new t for animations
  2216. t = fmodf(t, 1.0f);
  2217. }
  2218. auto& frames = _animation->getFrames();
  2219. auto numberOfFrames = frames.size();
  2220. SpriteFrame *frameToDisplay = nullptr;
  2221. for( int i=_nextFrame; i < numberOfFrames; i++ )
  2222. {
  2223. float splitTime = _splitTimes->at(i);
  2224. if( splitTime <= t )
  2225. {
  2226. auto blend = static_cast<Sprite*>(_target)->getBlendFunc();
  2227. _currFrameIndex = i;
  2228. AnimationFrame* frame = frames.at(_currFrameIndex);
  2229. frameToDisplay = frame->getSpriteFrame();
  2230. static_cast<Sprite*>(_target)->setSpriteFrame(frameToDisplay);
  2231. static_cast<Sprite*>(_target)->setBlendFunc(blend);
  2232. const ValueMap& dict = frame->getUserInfo();
  2233. if ( !dict.empty() )
  2234. {
  2235. if (_frameDisplayedEvent == nullptr)
  2236. _frameDisplayedEvent = new (std::nothrow) EventCustom(AnimationFrameDisplayedNotification);
  2237. _frameDisplayedEventInfo.target = _target;
  2238. _frameDisplayedEventInfo.userInfo = &dict;
  2239. _frameDisplayedEvent->setUserData(&_frameDisplayedEventInfo);
  2240. Director::getInstance()->getEventDispatcher()->dispatchEvent(_frameDisplayedEvent);
  2241. }
  2242. _nextFrame = i+1;
  2243. }
  2244. // Issue 1438. Could be more than one frame per tick, due to low frame rate or frame delta < 1/FPS
  2245. else {
  2246. break;
  2247. }
  2248. }
  2249. }
  2250. Animate* Animate::reverse() const
  2251. {
  2252. auto& oldArray = _animation->getFrames();
  2253. Vector<AnimationFrame*> newArray(oldArray.size());
  2254. if (!oldArray.empty())
  2255. {
  2256. for (auto iter = oldArray.crbegin(), iterCrend = oldArray.crend(); iter != iterCrend; ++iter)
  2257. {
  2258. AnimationFrame* animFrame = *iter;
  2259. if (!animFrame)
  2260. {
  2261. break;
  2262. }
  2263. newArray.pushBack(animFrame->clone());
  2264. }
  2265. }
  2266. Animation *newAnim = Animation::create(newArray, _animation->getDelayPerUnit(), _animation->getLoops());
  2267. newAnim->setRestoreOriginalFrame(_animation->getRestoreOriginalFrame());
  2268. return Animate::create(newAnim);
  2269. }
  2270. // TargetedAction
  2271. TargetedAction::TargetedAction()
  2272. : _action(nullptr)
  2273. , _forcedTarget(nullptr)
  2274. {
  2275. }
  2276. TargetedAction::~TargetedAction()
  2277. {
  2278. CC_SAFE_RELEASE(_forcedTarget);
  2279. CC_SAFE_RELEASE(_action);
  2280. }
  2281. TargetedAction* TargetedAction::create(Node* target, FiniteTimeAction* action)
  2282. {
  2283. TargetedAction* p = new (std::nothrow) TargetedAction();
  2284. if (p && p->initWithTarget(target, action))
  2285. {
  2286. p->autorelease();
  2287. return p;
  2288. }
  2289. delete p;
  2290. return nullptr;
  2291. }
  2292. bool TargetedAction::initWithTarget(Node* target, FiniteTimeAction* action)
  2293. {
  2294. if(ActionInterval::initWithDuration(action->getDuration()))
  2295. {
  2296. CC_SAFE_RETAIN(target);
  2297. _forcedTarget = target;
  2298. CC_SAFE_RETAIN(action);
  2299. _action = action;
  2300. return true;
  2301. }
  2302. return false;
  2303. }
  2304. TargetedAction* TargetedAction::clone() const
  2305. {
  2306. // no copy constructor
  2307. // win32 : use the _other's copy object.
  2308. return TargetedAction::create(_forcedTarget, _action->clone());
  2309. }
  2310. TargetedAction* TargetedAction::reverse() const
  2311. {
  2312. // just reverse the internal action
  2313. auto a = new (std::nothrow) TargetedAction();
  2314. a->initWithTarget(_forcedTarget, _action->reverse());
  2315. a->autorelease();
  2316. return a;
  2317. }
  2318. void TargetedAction::startWithTarget(Node *target)
  2319. {
  2320. ActionInterval::startWithTarget(target);
  2321. _action->startWithTarget(_forcedTarget);
  2322. }
  2323. void TargetedAction::stop()
  2324. {
  2325. _action->stop();
  2326. }
  2327. void TargetedAction::update(float time)
  2328. {
  2329. if (!(sendUpdateEventToScript(time, _action)))
  2330. _action->update(time);
  2331. }
  2332. void TargetedAction::setForcedTarget(Node* forcedTarget)
  2333. {
  2334. if( _forcedTarget != forcedTarget )
  2335. {
  2336. CC_SAFE_RETAIN(forcedTarget);
  2337. CC_SAFE_RELEASE(_forcedTarget);
  2338. _forcedTarget = forcedTarget;
  2339. }
  2340. }
  2341. // ActionFloat
  2342. ActionFloat* ActionFloat::create(float duration, float from, float to, ActionFloatCallback callback)
  2343. {
  2344. auto ref = new (std::nothrow) ActionFloat();
  2345. if (ref && ref->initWithDuration(duration, from, to, callback))
  2346. {
  2347. ref->autorelease();
  2348. return ref;
  2349. }
  2350. delete ref;
  2351. return nullptr;
  2352. }
  2353. bool ActionFloat::initWithDuration(float duration, float from, float to, ActionFloatCallback callback)
  2354. {
  2355. if (ActionInterval::initWithDuration(duration))
  2356. {
  2357. _from = from;
  2358. _to = to;
  2359. _callback = callback;
  2360. return true;
  2361. }
  2362. return false;
  2363. }
  2364. ActionFloat* ActionFloat::clone() const
  2365. {
  2366. return ActionFloat::create(_duration, _from, _to, _callback);
  2367. }
  2368. void ActionFloat::startWithTarget(Node *target)
  2369. {
  2370. ActionInterval::startWithTarget(target);
  2371. _delta = _to - _from;
  2372. }
  2373. void ActionFloat::update(float delta)
  2374. {
  2375. float value = _to - _delta * (1 - delta);
  2376. if (_callback)
  2377. {
  2378. // report back value to caller
  2379. _callback(value);
  2380. }
  2381. }
  2382. ActionFloat* ActionFloat::reverse() const
  2383. {
  2384. return ActionFloat::create(_duration, _to, _from, _callback);
  2385. }
  2386. NS_CC_END