CCBAnimationManager.cpp 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. #include "editor-support/cocosbuilder/CCBAnimationManager.h"
  2. #include "editor-support/cocosbuilder/CCBReader.h"
  3. #include "editor-support/cocosbuilder/CCNode+CCBRelativePositioning.h"
  4. #include "audio/include/AudioEngine.h"
  5. #include "editor-support/cocosbuilder/CCBSelectorResolver.h"
  6. #include <string>
  7. #include <sstream>
  8. #include <set>
  9. #include "common/CocosConfig.h"
  10. using namespace cocos2d;
  11. using namespace std;
  12. using namespace cocos2d::extension;
  13. using namespace experimental;
  14. namespace cocosbuilder {
  15. // Implementation of CCBAinmationManager
  16. CCBAnimationManager::CCBAnimationManager()
  17. : _jsControlled(false)
  18. , _owner(nullptr)
  19. , _autoPlaySequenceId(0)
  20. , _rootNode(nullptr)
  21. , _rootContainerSize(Size::ZERO)
  22. , _delegate(nullptr)
  23. , _runningSequence(nullptr)
  24. , _speed(1.0f)
  25. {
  26. init();
  27. }
  28. bool CCBAnimationManager::init()
  29. {
  30. _target = nullptr;
  31. _animationCompleteCallbackFunc = nullptr;
  32. return true;
  33. }
  34. CCBAnimationManager::~CCBAnimationManager()
  35. {
  36. // DictElement *pElement = nullptr;
  37. // CCDICT_FOREACH(_nodeSequences, pElement)
  38. // {
  39. // Node *node = (Node*)pElement->getIntKey();
  40. // node->release();
  41. // }
  42. //
  43. // CCDICT_FOREACH(_baseValues, pElement)
  44. // {
  45. // Node *node = (Node*)pElement->getIntKey();
  46. // node->release();
  47. // }
  48. if (_rootNode)
  49. {
  50. _rootNode->stopAllActions();
  51. }
  52. setRootNode(nullptr);
  53. setDelegate(nullptr);
  54. for (auto iter = _objects.begin(); iter != _objects.end(); ++iter)
  55. {
  56. for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
  57. {
  58. iter2->second->release();
  59. }
  60. }
  61. CC_SAFE_RELEASE(_target);
  62. }
  63. Vector<CCBSequence*>& CCBAnimationManager::getSequences()
  64. {
  65. return _sequences;
  66. }
  67. void CCBAnimationManager::setSequences(const Vector<CCBSequence*>& seq)
  68. {
  69. _sequences = seq;
  70. }
  71. int CCBAnimationManager::getAutoPlaySequenceId()
  72. {
  73. return _autoPlaySequenceId;
  74. }
  75. void CCBAnimationManager::setAutoPlaySequenceId(int autoPlaySequenceId)
  76. {
  77. _autoPlaySequenceId = autoPlaySequenceId;
  78. }
  79. Node* CCBAnimationManager::getRootNode()
  80. {
  81. return _rootNode;
  82. }
  83. void CCBAnimationManager::setRootNode(Node *pRootNode)
  84. {
  85. _rootNode = pRootNode;
  86. }
  87. void CCBAnimationManager::setDocumentControllerName(const std::string &name)
  88. {
  89. _documentControllerName = name;
  90. }
  91. std::string CCBAnimationManager::getDocumentControllerName()
  92. {
  93. return _documentControllerName;
  94. }
  95. void CCBAnimationManager::addDocumentCallbackNode(Node *node)
  96. {
  97. _documentCallbackNodes.pushBack(node);
  98. }
  99. void CCBAnimationManager::addDocumentCallbackName(std::string name)
  100. {
  101. _documentCallbackNames.push_back(Value(name));
  102. }
  103. void CCBAnimationManager::addDocumentCallbackControlEvents(Control::EventType eventType)
  104. {
  105. _documentCallbackControlEvents.push_back(Value(static_cast<int>(eventType)));
  106. }
  107. ValueVector& CCBAnimationManager::getDocumentCallbackNames()
  108. {
  109. return _documentCallbackNames;
  110. }
  111. Vector<Node*>& CCBAnimationManager::getDocumentCallbackNodes()
  112. {
  113. return _documentCallbackNodes;
  114. }
  115. ValueVector& CCBAnimationManager::getDocumentCallbackControlEvents()
  116. {
  117. return _documentCallbackControlEvents;
  118. }
  119. void CCBAnimationManager::addDocumentOutletNode(Node *node)
  120. {
  121. _documentOutletNodes.pushBack(node);
  122. }
  123. void CCBAnimationManager::addDocumentOutletName(std::string name)
  124. {
  125. _documentOutletNames.push_back(Value(name));
  126. }
  127. ValueVector& CCBAnimationManager::getDocumentOutletNames()
  128. {
  129. return _documentOutletNames;
  130. }
  131. Vector<Node*>& CCBAnimationManager::getDocumentOutletNodes()
  132. {
  133. return _documentOutletNodes;
  134. }
  135. std::string CCBAnimationManager::getLastCompletedSequenceName()
  136. {
  137. return _lastCompletedSequenceName;
  138. }
  139. ValueVector& CCBAnimationManager::getKeyframeCallbacks()
  140. {
  141. return _keyframeCallbacks;
  142. }
  143. const Size& CCBAnimationManager::getRootContainerSize()
  144. {
  145. return _rootContainerSize;
  146. }
  147. void CCBAnimationManager::setRootContainerSize(const Size &rootContainerSize)
  148. {
  149. _rootContainerSize.setSize(rootContainerSize.width, rootContainerSize.height);
  150. }
  151. CCBAnimationManagerDelegate* CCBAnimationManager::getDelegate()
  152. {
  153. return _delegate;
  154. }
  155. void CCBAnimationManager::setDelegate(CCBAnimationManagerDelegate *pDelegate)
  156. {
  157. CC_SAFE_RELEASE(dynamic_cast<Ref*>(_delegate));
  158. _delegate = pDelegate;
  159. CC_SAFE_RETAIN(dynamic_cast<Ref*>(_delegate));
  160. }
  161. const char* CCBAnimationManager::getRunningSequenceName()
  162. {
  163. if (_runningSequence)
  164. {
  165. return _runningSequence->getName();
  166. }
  167. return nullptr;
  168. }
  169. const Size& CCBAnimationManager::getContainerSize(Node *pNode)
  170. {
  171. if (pNode)
  172. {
  173. return pNode->getContentSize();
  174. }
  175. else
  176. {
  177. return _rootContainerSize;
  178. }
  179. }
  180. // refer to CCBReader::readNodeGraph() for data structure of pSeq
  181. void CCBAnimationManager::addNode(Node *pNode, const std::unordered_map<int, Map<std::string, CCBSequenceProperty*>>& seq)
  182. {
  183. // pNode->retain();
  184. _nodeSequences[pNode] = seq;
  185. }
  186. void CCBAnimationManager::setBaseValue(const Value& value, Node *pNode, const std::string& propName)
  187. {
  188. auto& props = _baseValues[pNode];
  189. props[propName] = value;
  190. }
  191. const Value& CCBAnimationManager::getBaseValue(Node *pNode, const std::string& propName)
  192. {
  193. auto& props = _baseValues[pNode];
  194. return props[propName];
  195. }
  196. void CCBAnimationManager::setObject(Ref* obj, Node *pNode, const std::string& propName)
  197. {
  198. auto& props = _objects[pNode];
  199. auto iter = props.find(propName);
  200. if (iter != props.end())
  201. iter->second->release();
  202. props[propName] = obj;
  203. obj->retain();
  204. }
  205. Ref* CCBAnimationManager::getObject(Node *pNode, const std::string& propName)
  206. {
  207. auto& props = _objects[pNode];
  208. auto iter = props.find(propName);
  209. if (iter != props.end())
  210. return iter->second;
  211. return nullptr;
  212. }
  213. int CCBAnimationManager::getSequenceId(const char* pSequenceName)
  214. {
  215. string seqName(pSequenceName);
  216. for (auto& seq : _sequences)
  217. {
  218. if (seqName.compare(seq->getName()) == 0)
  219. {
  220. return seq->getSequenceId();
  221. }
  222. }
  223. return -1;
  224. }
  225. CCBSequence* CCBAnimationManager::getSequence(int nSequenceId)
  226. {
  227. for (auto& seq : _sequences)
  228. {
  229. if (seq->getSequenceId() == nSequenceId)
  230. {
  231. return seq;
  232. }
  233. }
  234. return nullptr;
  235. }
  236. float CCBAnimationManager::getSequenceDuration(const char *pSequenceName)
  237. {
  238. int id = getSequenceId(pSequenceName);
  239. if (id != -1)
  240. return getSequence(id)->getDuration();
  241. return 0;
  242. }
  243. std::string CCBAnimationManager::getCCBFileName(){
  244. return _ccbFileName;
  245. }
  246. void CCBAnimationManager::setCCBFileName(std::string filename){
  247. _ccbFileName = filename;
  248. }
  249. void CCBAnimationManager::moveAnimationsFromNode(Node* fromNode, Node* toNode)
  250. {
  251. // Move base values
  252. auto baseValueIter = _baseValues.find(fromNode);
  253. if(baseValueIter != _baseValues.end())
  254. {
  255. _baseValues[toNode] = baseValueIter->second;
  256. _baseValues.erase(baseValueIter);
  257. // fromNode->release();
  258. // toNode->retain();
  259. }
  260. auto objIter = _objects.find(fromNode);
  261. if (objIter != _objects.end())
  262. {
  263. _objects[toNode] = objIter->second;
  264. _objects.erase(objIter);
  265. }
  266. // Move seqs
  267. auto seqsIter = _nodeSequences.find(fromNode);
  268. if (seqsIter != _nodeSequences.end())
  269. {
  270. _nodeSequences[toNode] = seqsIter->second;
  271. _nodeSequences.erase(seqsIter);
  272. // fromNode->release();
  273. // toNode->retain();
  274. }
  275. }
  276. // Refer to CCBReader::readKeyframe() for the real type of value
  277. ActionInterval* CCBAnimationManager::getAction(CCBKeyframe *pKeyframe0, CCBKeyframe *pKeyframe1, const std::string& propName, Node *pNode)
  278. {
  279. float duration = pKeyframe1->getTime() - (pKeyframe0 ? pKeyframe0->getTime() : 0);
  280. if (propName == "rotationX")
  281. {
  282. return CCBRotateXTo::create(duration, pKeyframe1->getValue().asFloat());
  283. }
  284. else if (propName == "rotationY")
  285. {
  286. return CCBRotateYTo::create(duration, pKeyframe1->getValue().asFloat());
  287. }
  288. else if (propName == "rotation")
  289. {
  290. return CCBRotateTo::create(duration, pKeyframe1->getValue().asFloat());
  291. }
  292. else if (propName == "opacity")
  293. {
  294. return FadeTo::create(duration, pKeyframe1->getValue().asByte());
  295. }
  296. else if (propName == "color")
  297. {
  298. auto c = pKeyframe1->getValue().asValueMap();
  299. unsigned char r = c["r"].asByte();
  300. unsigned char g = c["g"].asByte();
  301. unsigned char b = c["b"].asByte();
  302. return TintTo::create(duration, r, g, b);
  303. }
  304. else if (propName == "visible")
  305. {
  306. if (pKeyframe1->getValue().asBool())
  307. {
  308. return Sequence::createWithTwoActions(DelayTime::create(duration), Show::create());
  309. }
  310. else
  311. {
  312. return Sequence::createWithTwoActions(DelayTime::create(duration), Hide::create());
  313. }
  314. }
  315. else if (propName == "displayFrame")
  316. {
  317. return Sequence::createWithTwoActions(DelayTime::create(duration),
  318. CCBSetSpriteFrame::create(static_cast<SpriteFrame*>(pKeyframe1->getObject())));
  319. }
  320. else if (propName == "position")
  321. {
  322. // Get position type
  323. auto& array = getBaseValue(pNode, propName).asValueVector();
  324. CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
  325. // Get relative position
  326. auto value = pKeyframe1->getValue().asValueVector();
  327. float x = value[0].asFloat();
  328. float y = value[1].asFloat();
  329. Size containerSize = getContainerSize(pNode->getParent());
  330. Vec2 absPos = getAbsolutePosition(Vec2(x,y), type, containerSize, propName);
  331. return MoveTo::create(duration, absPos);
  332. }
  333. else if (propName == "scale")
  334. {
  335. // Get position type
  336. auto& array = getBaseValue(pNode, propName).asValueVector();
  337. CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
  338. // Get relative scale
  339. auto value = pKeyframe1->getValue().asValueVector();
  340. float x = value[0].asFloat();
  341. float y = value[1].asFloat();
  342. if (type == CCBReader::ScaleType::MULTIPLY_RESOLUTION)
  343. {
  344. float resolutionScale = CCBReader::getResolutionScale();
  345. x *= resolutionScale;
  346. y *= resolutionScale;
  347. }
  348. return ScaleTo::create(duration, x, y);
  349. }
  350. else if (propName == "skew")
  351. {
  352. // Get relative skew
  353. auto& value = pKeyframe1->getValue().asValueVector();
  354. float x = value[0].asFloat();
  355. float y = value[1].asFloat();
  356. return SkewTo::create(duration, x, y);
  357. }
  358. else if (propName == "frameIndex")
  359. {
  360. return CCBFrameIndexTo::create(duration, pKeyframe0->getValue().asByte(), pKeyframe1->getValue().asByte());
  361. }
  362. else if (propName == "posVar"){
  363. //粒子发射器大小
  364. auto& value = pKeyframe1->getValue().asValueVector();
  365. float x = value[0].asFloat();
  366. float y = value[1].asFloat();
  367. return CCBParticlePosVarTo::create(duration, Vec2(x,y));
  368. }
  369. else if (propName == "gravity"){
  370. // 粒子重力
  371. auto& value = pKeyframe1->getValue().asValueVector();
  372. float x = value[0].asFloat();
  373. float y = value[1].asFloat();
  374. return CCBGravityTo::create(duration, Vec2(x,y));
  375. }
  376. else if(propName == "speed"){
  377. // 粒子速度
  378. auto& value = pKeyframe1->getValue().asValueVector();
  379. float x = value[0].asFloat();
  380. float y = value[1].asFloat();
  381. return CCBParticleSpeedTo::create(duration, Vec2(x,y));
  382. }
  383. else if(propName == "life"){
  384. // 粒子生命
  385. auto& value = pKeyframe1->getValue().asValueVector();
  386. float x = value[0].asFloat();
  387. float y = value[1].asFloat();
  388. return CCBParticleLifeTo::create(duration, Vec2(x,y));
  389. }
  390. else if(propName == "startSize"){
  391. // 初始尺寸
  392. auto& value = pKeyframe1->getValue().asValueVector();
  393. float x = value[0].asFloat();
  394. float y = value[1].asFloat();
  395. return CCBParticleStartSizeTo::create(duration, Vec2(x,y));
  396. }
  397. else if(propName == "endSize"){
  398. // 结束尺寸
  399. auto& value = pKeyframe1->getValue().asValueVector();
  400. float x = value[0].asFloat();
  401. float y = value[1].asFloat();
  402. return CCBParticleEndSizeTo::create(duration, Vec2(x,y));
  403. }
  404. else if(propName == "startSpin"){
  405. // 开始旋转角度:
  406. auto& value = pKeyframe1->getValue().asValueVector();
  407. float x = value[0].asFloat();
  408. float y = value[1].asFloat();
  409. return CCBParticleStartSpinTo::create(duration, Vec2(x,y));
  410. }
  411. else if(propName == "endSpin"){
  412. // 结束旋转角度
  413. auto& value = pKeyframe1->getValue().asValueVector();
  414. float x = value[0].asFloat();
  415. float y = value[1].asFloat();
  416. return CCBParticleEndSpinTo::create(duration, Vec2(x,y));
  417. }
  418. else if(propName == "angle"){
  419. // 发射角度
  420. auto& value = pKeyframe1->getValue().asValueVector();
  421. float x = value[0].asFloat();
  422. float y = value[1].asFloat();
  423. return CCBParticleAngleTo::create(duration, Vec2(x,y));
  424. }
  425. else if(propName == "tangentialAccel"){
  426. // 切向加速度
  427. auto& value = pKeyframe1->getValue().asValueVector();
  428. float x = value[0].asFloat();
  429. float y = value[1].asFloat();
  430. return CCBParticleTangentialAccelTo::create(duration, Vec2(x,y));
  431. }
  432. else if(propName == "radialAccel"){
  433. // 径向加速度:
  434. auto& value = pKeyframe1->getValue().asValueVector();
  435. float x = value[0].asFloat();
  436. float y = value[1].asFloat();
  437. return CCBParticleRadialAccelTo::create(duration, Vec2(x,y));
  438. }
  439. else if(propName == "startRadius"){
  440. // 开始半径:
  441. auto& value = pKeyframe1->getValue().asValueVector();
  442. float x = value[0].asFloat();
  443. float y = value[1].asFloat();
  444. return CCBParticleStartRadiusTo::create(duration, Vec2(x,y));
  445. }
  446. else if(propName == "endRadius"){
  447. // 结束半径
  448. auto& value = pKeyframe1->getValue().asValueVector();
  449. float x = value[0].asFloat();
  450. float y = value[1].asFloat();
  451. return CCBParticleEndRadiusTo::create(duration, Vec2(x,y));
  452. }
  453. else if(propName == "rotatePerSecond"){
  454. // 旋转:
  455. auto& value = pKeyframe1->getValue().asValueVector();
  456. float x = value[0].asFloat();
  457. float y = value[1].asFloat();
  458. return CCBParticleRotatePerSecondTo::create(duration, Vec2(x,y));
  459. }
  460. else
  461. {
  462. log("CCBReader: Failed to create animation for property: %s", propName.c_str());
  463. }
  464. return nullptr;
  465. }
  466. void CCBAnimationManager::setAnimatedProperty(const std::string& propName, Node *pNode, const Value& value, Ref* obj, float fTweenDuration)
  467. {
  468. if (fTweenDuration > 0)
  469. {
  470. // Create a fake keyframe to generate the action from
  471. CCBKeyframe *kf1 = new (std::nothrow) CCBKeyframe();
  472. kf1->autorelease();
  473. kf1->setObject(obj);
  474. kf1->setValue(value);
  475. kf1->setTime(fTweenDuration);
  476. kf1->setEasingType(CCBKeyframe::EasingType::LINEAR);
  477. // Animate
  478. ActionInterval *tweenAction = getAction(nullptr, kf1, propName, pNode);
  479. pNode->runAction(tweenAction);
  480. }
  481. else
  482. {
  483. // Just set the value
  484. if (propName == "position")
  485. {
  486. // Get position type
  487. auto& array = getBaseValue(pNode, propName).asValueVector();
  488. CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
  489. // Get relative position
  490. auto& valueVector = value.asValueVector();
  491. float x = valueVector[0].asFloat();
  492. float y = valueVector[1].asFloat();
  493. pNode->setPosition(getAbsolutePosition(Vec2(x,y), type, getContainerSize(pNode->getParent()), propName));
  494. }
  495. else if (propName == "scale")
  496. {
  497. // Get scale type
  498. auto& array = getBaseValue(pNode, propName).asValueVector();
  499. CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
  500. // Get relative scale
  501. auto& valueVector = value.asValueVector();
  502. float x = valueVector[0].asFloat();
  503. float y = valueVector[1].asFloat();
  504. setRelativeScale(pNode, x, y, type, propName);
  505. }
  506. else if(propName == "skew")
  507. {
  508. // Get relative scale
  509. auto& valueVector = value.asValueVector();
  510. float x = valueVector[0].asFloat();
  511. float y = valueVector[1].asFloat();
  512. pNode->setSkewX(x);
  513. pNode->setSkewY(y);
  514. }
  515. else
  516. {
  517. // [node setValue:value forKey:name];
  518. // TODO: only handle rotation, opacity, displayFrame, color
  519. if (propName == "rotation")
  520. {
  521. float rotate = value.asFloat();
  522. pNode->setRotation(rotate);
  523. } else if(propName == "rotationX")
  524. {
  525. float rotate = value.asFloat();
  526. pNode->setRotationSkewX(rotate);
  527. }else if(propName == "rotationY")
  528. {
  529. float rotate = value.asFloat();
  530. pNode->setRotationSkewY(rotate);
  531. }
  532. else if (propName == "opacity")
  533. {
  534. unsigned char opacity = value.asByte();
  535. pNode->setOpacity(opacity);
  536. }
  537. else if (propName == "displayFrame")
  538. {
  539. static_cast<Sprite*>(pNode)->setSpriteFrame(static_cast<SpriteFrame*>(obj));
  540. }else if(propName == "progressFrame"){
  541. static_cast<ProgressTimer*>(pNode)->setSpriteFrame(static_cast<SpriteFrame*>(obj));
  542. }else if (propName == "color")
  543. {
  544. auto c = value.asValueMap();
  545. unsigned char r = c["r"].asByte();
  546. unsigned char g = c["g"].asByte();
  547. unsigned char b = c["b"].asByte();
  548. pNode->setColor(Color3B(r, g, b));
  549. }
  550. else if (propName == "visible")
  551. {
  552. bool visible = value.asBool();
  553. pNode->setVisible(visible);
  554. }
  555. else if (propName == "frameIndex") {
  556. unsigned char frameIndex = value.asByte();
  557. static_cast<ZGFrameActionSprite*>(pNode)->setFrameIndex(frameIndex);
  558. }
  559. else if (propName == "posVar"){
  560. // 粒子发射器尺寸
  561. auto& valueVector = value.asValueVector();
  562. float gx = valueVector[0].asFloat();
  563. float gy = valueVector[1].asFloat();
  564. static_cast<ParticleSystem*>(pNode)->setPosVar(Vec2(gx, gy));
  565. }
  566. else if (propName == "gravity") {
  567. // 粒子重力
  568. auto& valueVector = value.asValueVector();
  569. float gx = valueVector[0].asFloat();
  570. float gy = valueVector[1].asFloat();
  571. static_cast<ParticleSystem*>(pNode)->setGravity(Vec2(gx, gy));
  572. }
  573. else if (propName == "speed") {
  574. // 粒子速度
  575. auto& valueVector = value.asValueVector();
  576. float speed = valueVector[0].asFloat();
  577. float speedVar = valueVector[1].asFloat();
  578. static_cast<ParticleSystem*>(pNode)->setSpeed(speed);
  579. static_cast<ParticleSystem*>(pNode)->setSpeedVar(speedVar);
  580. }
  581. else if(propName == "life"){
  582. // 粒子生命
  583. auto& valueVector = value.asValueVector();
  584. float speed = valueVector[0].asFloat();
  585. float speedVar = valueVector[1].asFloat();
  586. static_cast<ParticleSystem*>(pNode)->setLife(speed);
  587. static_cast<ParticleSystem*>(pNode)->setLifeVar(speedVar);
  588. }
  589. else if(propName == "startSize"){
  590. // 初始尺寸
  591. auto& valueVector = value.asValueVector();
  592. float speed = valueVector[0].asFloat();
  593. float speedVar = valueVector[1].asFloat();
  594. static_cast<ParticleSystem*>(pNode)->setStartSize(speed);
  595. static_cast<ParticleSystem*>(pNode)->setStartSizeVar(speedVar);
  596. }
  597. else if(propName == "endSize"){
  598. // 结束尺寸
  599. auto& valueVector = value.asValueVector();
  600. float speed = valueVector[0].asFloat();
  601. float speedVar = valueVector[1].asFloat();
  602. static_cast<ParticleSystem*>(pNode)->setEndSize(speed);
  603. static_cast<ParticleSystem*>(pNode)->setEndSizeVar(speedVar);
  604. }
  605. else if(propName == "startSpin"){
  606. // 开始旋转角度:
  607. auto& valueVector = value.asValueVector();
  608. float speed = valueVector[0].asFloat();
  609. float speedVar = valueVector[1].asFloat();
  610. static_cast<ParticleSystem*>(pNode)->setStartSpin(speed);
  611. static_cast<ParticleSystem*>(pNode)->setStartSpinVar(speedVar);
  612. }
  613. else if(propName == "endSpin"){
  614. // 结束旋转角度
  615. auto& valueVector = value.asValueVector();
  616. float speed = valueVector[0].asFloat();
  617. float speedVar = valueVector[1].asFloat();
  618. static_cast<ParticleSystem*>(pNode)->setEndSpin(speed);
  619. static_cast<ParticleSystem*>(pNode)->setEndSpinVar(speedVar);
  620. }
  621. else if(propName == "angle"){
  622. // 发射角度
  623. auto& valueVector = value.asValueVector();
  624. float speed = valueVector[0].asFloat();
  625. float speedVar = valueVector[1].asFloat();
  626. static_cast<ParticleSystem*>(pNode)->setAngle(speed);
  627. static_cast<ParticleSystem*>(pNode)->setAngleVar(speedVar);
  628. }
  629. else if(propName == "tangentialAccel"){
  630. // 切向加速度
  631. auto& valueVector = value.asValueVector();
  632. float speed = valueVector[0].asFloat();
  633. float speedVar = valueVector[1].asFloat();
  634. static_cast<ParticleSystem*>(pNode)->setTangentialAccel(speed);
  635. static_cast<ParticleSystem*>(pNode)->setTangentialAccelVar(speedVar);
  636. }
  637. else if(propName == "radialAccel"){
  638. // 径向加速度:
  639. auto& valueVector = value.asValueVector();
  640. float speed = valueVector[0].asFloat();
  641. float speedVar = valueVector[1].asFloat();
  642. static_cast<ParticleSystem*>(pNode)->setRadialAccel(speed);
  643. static_cast<ParticleSystem*>(pNode)->setRadialAccelVar(speedVar);
  644. }
  645. else if(propName == "startRadius"){
  646. // 开始半径:
  647. auto& valueVector = value.asValueVector();
  648. float speed = valueVector[0].asFloat();
  649. float speedVar = valueVector[1].asFloat();
  650. static_cast<ParticleSystem*>(pNode)->setStartSpin(speed);
  651. static_cast<ParticleSystem*>(pNode)->setStartSpinVar(speedVar);
  652. }
  653. else if(propName == "endRadius"){
  654. // 结束半径
  655. auto& valueVector = value.asValueVector();
  656. float speed = valueVector[0].asFloat();
  657. float speedVar = valueVector[1].asFloat();
  658. static_cast<ParticleSystem*>(pNode)->setEndRadius(speed);
  659. static_cast<ParticleSystem*>(pNode)->setEndRadiusVar(speedVar);
  660. }
  661. else if(propName == "rotatePerSecond"){
  662. // 旋转:
  663. auto& valueVector = value.asValueVector();
  664. float speed = valueVector[0].asFloat();
  665. float speedVar = valueVector[1].asFloat();
  666. static_cast<ParticleSystem*>(pNode)->setRotatePerSecond(speed);
  667. static_cast<ParticleSystem*>(pNode)->setRotatePerSecondVar(speedVar);
  668. }
  669. else
  670. {
  671. log("unsupported property name is %s", propName.c_str());
  672. CCASSERT(false, "unsupported property now");
  673. }
  674. }
  675. }
  676. }
  677. void CCBAnimationManager::setFirstFrame(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
  678. {
  679. auto& keyframes = pSeqProp->getKeyframes();
  680. if (keyframes.empty())
  681. {
  682. // Use base value (no animation)
  683. auto& baseValue = getBaseValue(pNode, pSeqProp->getName());
  684. auto obj = getObject(pNode, pSeqProp->getName());
  685. CCASSERT(!baseValue.isNull(), "No baseValue found for property");
  686. setAnimatedProperty(pSeqProp->getName(), pNode, baseValue, obj, fTweenDuration);
  687. }
  688. else
  689. {
  690. // Use first keyframe
  691. CCBKeyframe *keyframe = keyframes.at(0);
  692. setAnimatedProperty(pSeqProp->getName(), pNode, keyframe->getValue(), keyframe->getObject(), fTweenDuration);
  693. }
  694. }
  695. ActionInterval* CCBAnimationManager::getEaseAction(ActionInterval *pAction, CCBKeyframe::EasingType easingType, float fEasingOpt)
  696. {
  697. if (dynamic_cast<Sequence*>(pAction))
  698. {
  699. return pAction;
  700. }
  701. if (easingType == CCBKeyframe::EasingType::LINEAR)
  702. {
  703. return pAction;
  704. }
  705. else if (easingType == CCBKeyframe::EasingType::INSTANT)
  706. {
  707. return CCBEaseInstant::create(pAction);
  708. }
  709. else if (easingType == CCBKeyframe::EasingType::CUBIC_IN)
  710. {
  711. return EaseIn::create(pAction, fEasingOpt);
  712. }
  713. else if (easingType == CCBKeyframe::EasingType::CUBIC_OUT)
  714. {
  715. return EaseOut::create(pAction, fEasingOpt);
  716. }
  717. else if (easingType == CCBKeyframe::EasingType::CUBIC_INOUT)
  718. {
  719. return EaseInOut::create(pAction, fEasingOpt);
  720. }
  721. else if (easingType == CCBKeyframe::EasingType::BACK_IN)
  722. {
  723. return EaseBackIn::create(pAction);
  724. }
  725. else if (easingType == CCBKeyframe::EasingType::BACK_OUT)
  726. {
  727. return EaseBackOut::create(pAction);
  728. }
  729. else if (easingType == CCBKeyframe::EasingType::BACK_INOUT)
  730. {
  731. return EaseBackInOut::create(pAction);
  732. }
  733. else if (easingType == CCBKeyframe::EasingType::BOUNCE_IN)
  734. {
  735. return EaseBounceIn::create(pAction);
  736. }
  737. else if (easingType == CCBKeyframe::EasingType::BOUNCE_OUT)
  738. {
  739. return EaseBounceOut::create(pAction);
  740. }
  741. else if (easingType == CCBKeyframe::EasingType::BOUNCE_INOUT)
  742. {
  743. return EaseBounceInOut::create(pAction);
  744. }
  745. else if (easingType == CCBKeyframe::EasingType::ELASTIC_IN)
  746. {
  747. return EaseElasticIn::create(pAction, fEasingOpt);
  748. }
  749. else if (easingType == CCBKeyframe::EasingType::ELASTIC_OUT)
  750. {
  751. return EaseElasticOut::create(pAction, fEasingOpt);
  752. }
  753. else if (easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
  754. {
  755. return EaseElasticInOut::create(pAction, fEasingOpt);
  756. }
  757. else
  758. {
  759. log("CCBReader: Unknown easing type %d", static_cast<int>(easingType));
  760. return pAction;
  761. }
  762. }
  763. Sequence* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* channel) {
  764. float lastKeyframeTime = 0;
  765. Vector<FiniteTimeAction*> actions;
  766. auto& keyframes = channel->getKeyframes();
  767. ssize_t numKeyframes = keyframes.size();
  768. for (long i = 0; i < numKeyframes; ++i)
  769. {
  770. CCBKeyframe *keyframe = keyframes.at(i);
  771. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  772. lastKeyframeTime = keyframe->getTime();
  773. if(timeSinceLastKeyframe > 0) {
  774. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  775. }
  776. auto& keyVal = keyframe->getValue().asValueVector();
  777. std::string selectorName = keyVal[0].asString();
  778. CCBReader::TargetType selectorTarget = (CCBReader::TargetType)keyVal[1].asInt();
  779. if(_jsControlled) {
  780. std::stringstream callbackName;
  781. callbackName << static_cast<int>(selectorTarget);
  782. callbackName << ":" + selectorName;
  783. auto callback = _keyframeCallFuncs.at(callbackName.str());
  784. if (nullptr != callback)
  785. {
  786. CallFunc* callbackClone = callback->clone();
  787. if (callbackClone != nullptr)
  788. {
  789. actions.pushBack(callbackClone);
  790. }
  791. }
  792. }
  793. else
  794. {
  795. Ref* target = nullptr;
  796. if(selectorTarget == CCBReader::TargetType::DOCUMENT_ROOT)
  797. target = _rootNode;
  798. else if (selectorTarget == CCBReader::TargetType::OWNER)
  799. target = _owner;
  800. if(target != nullptr)
  801. {
  802. if(!selectorName.empty())
  803. {
  804. SEL_CallFuncN selCallFunc = 0;
  805. CCBSelectorResolver* targetAsCCBSelectorResolver = dynamic_cast<CCBSelectorResolver *>(target);
  806. if(targetAsCCBSelectorResolver != nullptr)
  807. {
  808. selCallFunc = targetAsCCBSelectorResolver->onResolveCCBCCCallFuncSelector(target, selectorName.c_str ());
  809. }
  810. if(selCallFunc == 0)
  811. {
  812. CCLOG("Skipping selector '%s' since no CCBSelectorResolver is present.", selectorName.c_str());
  813. }
  814. else
  815. {
  816. auto savedTarget = std::make_shared<Vector<Ref*>>();
  817. savedTarget->pushBack(target);
  818. auto callback = CallFuncN::create([savedTarget, selCallFunc](Node* sender){
  819. auto t = savedTarget->at(0);
  820. (t->*selCallFunc)(sender);
  821. });
  822. actions.pushBack(callback);
  823. }
  824. }
  825. else
  826. {
  827. CCLOG("Unexpected empty selector.");
  828. }
  829. }
  830. }
  831. }
  832. if(actions.size() < 1) return nullptr;
  833. return Sequence::create(actions);
  834. }
  835. Sequence* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) {
  836. float lastKeyframeTime = 0;
  837. Vector<FiniteTimeAction*> actions;
  838. auto& keyframes = channel->getKeyframes();
  839. ssize_t numKeyframes = keyframes.size();
  840. for (int i = 0; i < numKeyframes; ++i)
  841. {
  842. CCBKeyframe *keyframe = keyframes.at(i);
  843. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  844. lastKeyframeTime = keyframe->getTime();
  845. if(timeSinceLastKeyframe > 0) {
  846. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  847. }
  848. stringstream ss (stringstream::in | stringstream::out);
  849. auto& keyVal = keyframe->getValue().asValueVector();
  850. std::string soundFile = keyVal[0].asString();
  851. float pitch = 0.0f, pan = 0.0f, gain = 0.0f;
  852. ss << keyVal[1].asString();
  853. ss >> pitch;
  854. ss.flush();
  855. ss << keyVal[2].asString();
  856. ss >> pan;
  857. ss.flush();
  858. ss << keyVal[3].asString();
  859. ss >> gain;
  860. ss.flush();
  861. actions.pushBack(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
  862. }
  863. if(actions.size() < 1) return nullptr;
  864. return Sequence::create(actions);
  865. }
  866. void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
  867. {
  868. auto& keyframes = pSeqProp->getKeyframes();
  869. ssize_t numKeyframes = keyframes.size();
  870. if (numKeyframes > 1)
  871. {
  872. // Make an animation!
  873. Vector<FiniteTimeAction*> actions;
  874. CCBKeyframe *keyframeFirst = keyframes.at(0);
  875. float timeFirst = keyframeFirst->getTime() + fTweenDuration;
  876. if (timeFirst > 0)
  877. {
  878. actions.pushBack(DelayTime::create(timeFirst));
  879. }
  880. for (ssize_t i = 0; i < numKeyframes - 1; ++i)
  881. {
  882. CCBKeyframe *kf0 = keyframes.at(i);
  883. CCBKeyframe *kf1 = keyframes.at(i+1);
  884. ActionInterval *action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
  885. if (action)
  886. {
  887. // Apply easing
  888. action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
  889. actions.pushBack(action);
  890. }
  891. }
  892. //add song
  893. auto seq = Sequence::create(actions);
  894. auto speed = Speed::create(seq, _speed);
  895. speed->setTag(101);
  896. pNode->runAction(speed);
  897. }
  898. }
  899. void CCBAnimationManager::updateSpeed(float speed, Node* node) {
  900. std::vector<Action*> actions;
  901. _rootNode->getActionManager()->getActionsByTag(101, node, actions);
  902. std::vector<Action*>::iterator iter = actions.begin();
  903. while (iter != actions.end()) {
  904. Speed* sp = dynamic_cast<Speed*>(*iter);
  905. if (sp != nullptr) {
  906. sp->setSpeed(_speed);
  907. }
  908. iter ++;
  909. }
  910. Vector<Node*> children = node->getChildren();
  911. Vector<Node*>::iterator iter2 = children.begin();
  912. while (iter2 != children.end()) {
  913. updateSpeed(speed, *iter2);
  914. iter2 ++;
  915. }
  916. }
  917. void CCBAnimationManager::updateSpeed(float speed) {
  918. _speed = speed;
  919. updateSpeed(speed, _rootNode);
  920. }
  921. const std::unordered_map<cocos2d::Node*, std::unordered_map<int, cocos2d::Map<std::string, CCBSequenceProperty*>>> & CCBAnimationManager::getNodeSequences(){
  922. return _nodeSequences;
  923. }
  924. void CCBAnimationManager::runAnimations(const char *pName, float fTweenDuration)
  925. {
  926. runAnimationsForSequenceNamedTweenDuration(pName, fTweenDuration);
  927. }
  928. void CCBAnimationManager::runAnimations(const char *pName)
  929. {
  930. runAnimationsForSequenceNamed(pName);
  931. }
  932. void CCBAnimationManager::runAnimations(int nSeqId, float fTweenDuraiton)
  933. {
  934. runAnimationsForSequenceIdTweenDuration(nSeqId, fTweenDuraiton);
  935. }
  936. void CCBAnimationManager::runAnimationsForSequenceIdTweenDuration(int nSeqId, float fTweenDuration)
  937. {
  938. CCASSERT(nSeqId != -1, "Sequence id couldn't be found");
  939. _rootNode->stopAllActions();
  940. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  941. {
  942. Node *node = nodeSeqIter->first;
  943. node->stopAllActions();
  944. // Refer to CCBReader::readKeyframe() for the real type of value
  945. auto seqs = nodeSeqIter->second;
  946. auto seqNodeProps = seqs[nSeqId];
  947. std::set<std::string> seqNodePropNames;
  948. if (!seqNodeProps.empty())
  949. {
  950. // Reset nodes that have sequence node properties, and run actions on them
  951. for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
  952. {
  953. const std::string propName = iter->first;
  954. CCBSequenceProperty *seqProp = iter->second;
  955. seqNodePropNames.insert(propName);
  956. setFirstFrame(node, seqProp, fTweenDuration);
  957. runAction(node, seqProp, fTweenDuration);
  958. }
  959. }
  960. // Reset the nodes that may have been changed by other timelines
  961. auto& nodeBaseValues = _baseValues[node];
  962. if (!nodeBaseValues.empty())
  963. {
  964. for (auto iter = nodeBaseValues.begin(); iter != nodeBaseValues.end(); ++iter)
  965. {
  966. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  967. {
  968. setAnimatedProperty(iter->first, node, iter->second, nullptr, fTweenDuration);
  969. }
  970. }
  971. }
  972. auto& nodeObject = _objects[node];
  973. if (!nodeObject.empty())
  974. {
  975. for (auto iter = nodeObject.begin(); iter != nodeObject.end(); ++iter)
  976. {
  977. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  978. {
  979. setAnimatedProperty(iter->first, node, Value(), iter->second, fTweenDuration);
  980. }
  981. }
  982. }
  983. }
  984. // Make callback at end of sequence
  985. CCBSequence *seq = getSequence(nSeqId);
  986. if (seq == nullptr) {
  987. CCASSERT(false, "Sequence id couldn't be found");
  988. return;
  989. }
  990. ActionInterval *completeAction = Sequence::createWithTwoActions(DelayTime::create((seq->getDuration() + fTweenDuration)),
  991. CallFunc::create( CC_CALLBACK_0(CCBAnimationManager::sequenceCompleted,this)));
  992. auto speed = Speed::create(completeAction, _speed);
  993. speed->setTag(101);
  994. _rootNode->runAction(speed);
  995. // Set the running scene
  996. if(seq->getCallbackChannel() != nullptr) {
  997. Action* action = (Action *)actionForCallbackChannel(seq->getCallbackChannel());
  998. if(action != nullptr) {
  999. _rootNode->runAction(action);
  1000. }
  1001. }
  1002. if(seq->getSoundChannel() != nullptr) {
  1003. Action* action = (Action *)actionForSoundChannel(seq->getSoundChannel());
  1004. if(action != nullptr) {
  1005. _rootNode->runAction(action);
  1006. }
  1007. }
  1008. _runningSequence = getSequence(nSeqId);
  1009. }
  1010. //add by djd
  1011. Spawn* CCBAnimationManager::getNodeAniByName(const char *pName, Node* pNode,Vec2 orginPos){
  1012. Spawn *tAction = nullptr;
  1013. int nSeqId = getSequenceId(pName);
  1014. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  1015. {
  1016. Node *node = nodeSeqIter->first;
  1017. if (node != pNode) {
  1018. continue;
  1019. }
  1020. // Refer to CCBReader::readKeyframe() for the real type of value
  1021. auto seqs = nodeSeqIter->second;
  1022. auto seqNodeProps = seqs[nSeqId];
  1023. if (!seqNodeProps.empty())
  1024. {
  1025. // Reset nodes that have sequence node properties, and run actions on them
  1026. Vector<FiniteTimeAction*> seqList;
  1027. for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
  1028. {
  1029. const std::string propName = iter->first;
  1030. CCBSequenceProperty *pSeqProp = iter->second;
  1031. auto& keyframes = pSeqProp->getKeyframes();
  1032. ssize_t numKeyframes = keyframes.size();
  1033. if (numKeyframes > 1)
  1034. {
  1035. // Make an animation!
  1036. Vector<FiniteTimeAction*> actions;
  1037. CCBKeyframe *keyframeFirst = keyframes.at(0);
  1038. float timeFirst = keyframeFirst->getTime();
  1039. if (timeFirst > 0)
  1040. {
  1041. actions.pushBack(DelayTime::create(timeFirst));
  1042. }
  1043. for (ssize_t i = 0; i < numKeyframes - 1; ++i)
  1044. {
  1045. CCBKeyframe *kf0 = keyframes.at(i);
  1046. CCBKeyframe *kf1 = keyframes.at(i+1);
  1047. ActionInterval *action = nullptr;
  1048. //重写position
  1049. if (strcmp(pSeqProp->getName(), "position") == 0)
  1050. {
  1051. float duration = kf1->getTime() - (kf0 ? kf0->getTime() : 0);
  1052. // Get position type
  1053. auto& array = getBaseValue(pNode, propName).asValueVector();
  1054. CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
  1055. // Get relative position
  1056. auto value = kf1->getValue().asValueVector();
  1057. float x = value[0].asFloat();
  1058. float y = value[1].asFloat();
  1059. Size containerSize = getContainerSize(pNode->getParent());
  1060. Vec2 absPos = getAbsolutePosition(Vec2(x,y), type, containerSize, propName);
  1061. action = MoveTo::create(duration, Vec2(absPos.x + orginPos.x, absPos.y + orginPos.y));
  1062. }else{
  1063. action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
  1064. }
  1065. if (action)
  1066. {
  1067. // Apply easing
  1068. action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
  1069. actions.pushBack(action);
  1070. }
  1071. }
  1072. auto seq = Sequence::create(actions);
  1073. seqList.pushBack(seq);
  1074. }
  1075. }
  1076. tAction = Spawn::create(seqList);
  1077. }
  1078. break;
  1079. }
  1080. return tAction;
  1081. }
  1082. int CCBAnimationManager::runAnimationsForSequenceNamedTweenDuration(const char *pName, float fTweenDuration)
  1083. {
  1084. int seqId = getSequenceId(pName);
  1085. if (seqId != -1) {
  1086. runAnimationsForSequenceIdTweenDuration(seqId, fTweenDuration);
  1087. } else {
  1088. // logCppErrMsg("ACT:%s", pName);
  1089. return -1;
  1090. }
  1091. return 0;
  1092. }
  1093. int CCBAnimationManager::runAnimationsForSequenceNamed(const char *pName)
  1094. {
  1095. return runAnimationsForSequenceNamedTweenDuration(pName, 0);
  1096. }
  1097. void CCBAnimationManager::debug()
  1098. {
  1099. }
  1100. void CCBAnimationManager::setAnimationCompletedCallback(Ref *target, SEL_CallFunc callbackFunc) {
  1101. if (target)
  1102. {
  1103. target->retain();
  1104. }
  1105. if (_target)
  1106. {
  1107. _target->release();
  1108. }
  1109. _target = target;
  1110. _animationCompleteCallbackFunc = callbackFunc;
  1111. }
  1112. void CCBAnimationManager::setCallFunc(CallFunc* callFunc, const std::string &callbackNamed)
  1113. {
  1114. _keyframeCallFuncs.insert(callbackNamed, callFunc);
  1115. }
  1116. void CCBAnimationManager::sequenceCompleted()
  1117. {
  1118. const char *runningSequenceName = _runningSequence->getName();
  1119. int nextSeqId = _runningSequence->getChainedSequenceId();
  1120. _runningSequence = nullptr;
  1121. if(_lastCompletedSequenceName != runningSequenceName) {
  1122. _lastCompletedSequenceName = runningSequenceName;
  1123. }
  1124. if (nextSeqId != -1)
  1125. {
  1126. runAnimationsForSequenceIdTweenDuration(nextSeqId, 0);
  1127. }
  1128. if (_delegate)
  1129. {
  1130. // There may be another runAnimation() call in this delegate method
  1131. // which will assign _runningSequence
  1132. _delegate->completedAnimationSequenceNamed(runningSequenceName);
  1133. }
  1134. if (_target && _animationCompleteCallbackFunc) {
  1135. (_target->*_animationCompleteCallbackFunc)();
  1136. }
  1137. }
  1138. // Custom actions
  1139. /************************************************************
  1140. CCBSetSpriteFrame
  1141. ************************************************************/
  1142. CCBSetSpriteFrame* CCBSetSpriteFrame::create(SpriteFrame *pSpriteFrame)
  1143. {
  1144. CCBSetSpriteFrame *ret = new (std::nothrow) CCBSetSpriteFrame();
  1145. if (ret)
  1146. {
  1147. if (ret->initWithSpriteFrame(pSpriteFrame))
  1148. {
  1149. ret->autorelease();
  1150. }
  1151. else
  1152. {
  1153. CC_SAFE_DELETE(ret);
  1154. }
  1155. }
  1156. return ret;
  1157. }
  1158. bool CCBSetSpriteFrame::initWithSpriteFrame(SpriteFrame *pSpriteFrame)
  1159. {
  1160. _spriteFrame = pSpriteFrame;
  1161. CC_SAFE_RETAIN(_spriteFrame);
  1162. return true;
  1163. }
  1164. CCBSetSpriteFrame::~CCBSetSpriteFrame()
  1165. {
  1166. CC_SAFE_RELEASE_NULL(_spriteFrame);
  1167. }
  1168. CCBSetSpriteFrame* CCBSetSpriteFrame::clone() const
  1169. {
  1170. // no copy constructor
  1171. auto a = new (std::nothrow) CCBSetSpriteFrame();
  1172. a->initWithSpriteFrame(_spriteFrame);
  1173. a->autorelease();
  1174. return a;
  1175. }
  1176. CCBSetSpriteFrame* CCBSetSpriteFrame::reverse() const
  1177. {
  1178. // returns a copy of itself
  1179. return this->clone();
  1180. }
  1181. void CCBSetSpriteFrame::update(float time)
  1182. {
  1183. ActionInstant::update(time);
  1184. static_cast<Sprite*>(_target)->setSpriteFrame(_spriteFrame);
  1185. }
  1186. /************************************************************
  1187. CCBSoundEffect
  1188. ************************************************************/
  1189. CCBSoundEffect* CCBSoundEffect::actionWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  1190. CCBSoundEffect* pRet = new (std::nothrow) CCBSoundEffect();
  1191. if (pRet != nullptr && pRet->initWithSoundFile(filename, pitch, pan, gain))
  1192. {
  1193. pRet->autorelease();
  1194. }
  1195. else
  1196. {
  1197. CC_SAFE_DELETE(pRet);
  1198. }
  1199. return pRet;
  1200. }
  1201. CCBSoundEffect::~CCBSoundEffect()
  1202. {
  1203. }
  1204. bool CCBSoundEffect::initWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  1205. _soundFile = filename;
  1206. _pitch = pitch;
  1207. _pan = pan;
  1208. _gain = gain;
  1209. return true;
  1210. }
  1211. CCBSoundEffect* CCBSoundEffect::clone() const
  1212. {
  1213. // no copy constructor
  1214. auto a = new (std::nothrow) CCBSoundEffect();
  1215. a->initWithSoundFile(_soundFile, _pitch, _pan, _gain);
  1216. a->autorelease();
  1217. return a;
  1218. }
  1219. CCBSoundEffect* CCBSoundEffect::reverse() const
  1220. {
  1221. // returns a copy of itself
  1222. return this->clone();
  1223. }
  1224. void CCBSoundEffect::update(float time)
  1225. {
  1226. ActionInstant::update(time);
  1227. if (!CocosConfig::getCCBAudioEnable()) return;
  1228. string efx = CocosConfig::getCcbEfxPrefix() + _soundFile;
  1229. CCLOG("playEffect:%s",efx.c_str());
  1230. int soundId = AudioEngine::play2d(efx);
  1231. if (CocosConfig::pushNotificationWhenCCBSoundPlayed()) {
  1232. __Integer *i = __Integer::create(soundId);
  1233. __NotificationCenter::getInstance()->postNotification("CCB_PLAY_SOUND", i);
  1234. }
  1235. }
  1236. /************************************************************
  1237. CCBRotateTo
  1238. ************************************************************/
  1239. CCBRotateTo* CCBRotateTo::create(float fDuration, float fAngle)
  1240. {
  1241. CCBRotateTo *ret = new (std::nothrow) CCBRotateTo();
  1242. if (ret)
  1243. {
  1244. if (ret->initWithDuration(fDuration, fAngle))
  1245. {
  1246. ret->autorelease();
  1247. }
  1248. else
  1249. {
  1250. CC_SAFE_DELETE(ret);
  1251. }
  1252. }
  1253. return ret;
  1254. }
  1255. bool CCBRotateTo::initWithDuration(float fDuration, float fAngle)
  1256. {
  1257. if (ActionInterval::initWithDuration(fDuration))
  1258. {
  1259. _dstAngle = fAngle;
  1260. return true;
  1261. }
  1262. else
  1263. {
  1264. return false;
  1265. }
  1266. }
  1267. CCBRotateTo* CCBRotateTo::clone() const
  1268. {
  1269. // no copy constructor
  1270. auto a = new (std::nothrow) CCBRotateTo();
  1271. a->initWithDuration(_duration, _dstAngle);
  1272. a->autorelease();
  1273. return a;
  1274. }
  1275. CCBRotateTo* CCBRotateTo::reverse() const
  1276. {
  1277. CCASSERT(false, "reverse() is not supported in CCBRotateTo");
  1278. return nullptr;
  1279. }
  1280. void CCBRotateTo::startWithTarget(Node *pNode)
  1281. {
  1282. ActionInterval::startWithTarget(pNode);
  1283. _startAngle = _target->getRotation();
  1284. _diffAngle = _dstAngle - _startAngle;
  1285. }
  1286. void CCBRotateTo::update(float time)
  1287. {
  1288. _target->setRotation(_startAngle + (_diffAngle * time))
  1289. ;
  1290. }
  1291. /************************************************************
  1292. CCBRotateXTO
  1293. ************************************************************/
  1294. CCBRotateXTo* CCBRotateXTo::create(float fDuration, float fAngle)
  1295. {
  1296. CCBRotateXTo *ret = new (std::nothrow) CCBRotateXTo();
  1297. if (ret)
  1298. {
  1299. if (ret->initWithDuration(fDuration, fAngle))
  1300. {
  1301. ret->autorelease();
  1302. }
  1303. else
  1304. {
  1305. CC_SAFE_DELETE(ret);
  1306. }
  1307. }
  1308. return ret;
  1309. }
  1310. bool CCBRotateXTo::initWithDuration(float fDuration, float fAngle)
  1311. {
  1312. if (ActionInterval::initWithDuration(fDuration))
  1313. {
  1314. _dstAngle = fAngle;
  1315. return true;
  1316. }
  1317. else
  1318. {
  1319. return false;
  1320. }
  1321. }
  1322. void CCBRotateXTo::startWithTarget(Node *pNode)
  1323. {
  1324. //CCActionInterval::startWithTarget(pNode);
  1325. _originalTarget = pNode;
  1326. _target = pNode;
  1327. _elapsed = 0.0f;
  1328. _firstTick = true;
  1329. _startAngle = _target->getRotationSkewX();
  1330. _diffAngle = _dstAngle - _startAngle;
  1331. }
  1332. CCBRotateXTo* CCBRotateXTo::clone() const
  1333. {
  1334. // no copy constructor
  1335. auto a = new (std::nothrow) CCBRotateXTo();
  1336. a->initWithDuration(_duration, _dstAngle);
  1337. a->autorelease();
  1338. return a;
  1339. }
  1340. CCBRotateXTo* CCBRotateXTo::reverse() const
  1341. {
  1342. CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
  1343. return nullptr;
  1344. }
  1345. void CCBRotateXTo::update(float time)
  1346. {
  1347. _target->setRotationSkewX(_startAngle + (_diffAngle * time));
  1348. }
  1349. /************************************************************
  1350. CCBRotateYTO
  1351. ************************************************************/
  1352. CCBRotateYTo* CCBRotateYTo::create(float fDuration, float fAngle)
  1353. {
  1354. CCBRotateYTo *ret = new (std::nothrow) CCBRotateYTo();
  1355. if (ret)
  1356. {
  1357. if (ret->initWithDuration(fDuration, fAngle))
  1358. {
  1359. ret->autorelease();
  1360. }
  1361. else
  1362. {
  1363. CC_SAFE_DELETE(ret);
  1364. }
  1365. }
  1366. return ret;
  1367. }
  1368. bool CCBRotateYTo::initWithDuration(float fDuration, float fAngle)
  1369. {
  1370. if (ActionInterval::initWithDuration(fDuration))
  1371. {
  1372. _dstAngle = fAngle;
  1373. return true;
  1374. }
  1375. else
  1376. {
  1377. return false;
  1378. }
  1379. }
  1380. CCBRotateYTo* CCBRotateYTo::clone() const
  1381. {
  1382. // no copy constructor
  1383. auto a = new (std::nothrow) CCBRotateYTo();
  1384. a->initWithDuration(_duration, _dstAngle);
  1385. a->autorelease();
  1386. return a;
  1387. }
  1388. CCBRotateYTo* CCBRotateYTo::reverse() const
  1389. {
  1390. CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
  1391. return nullptr;
  1392. }
  1393. void CCBRotateYTo::startWithTarget(Node *pNode)
  1394. {
  1395. // ActionInterval::startWithTarget(pNode);
  1396. _originalTarget = pNode;
  1397. _target = pNode;
  1398. _elapsed = 0.0f;
  1399. _firstTick = true;
  1400. _startAngle = _target->getRotationSkewY();
  1401. _diffAngle = _dstAngle - _startAngle;
  1402. }
  1403. void CCBRotateYTo::update(float time)
  1404. {
  1405. _target->setRotationSkewY(_startAngle + (_diffAngle * time));
  1406. }
  1407. /************************************************************
  1408. CCBFrameIndexTo
  1409. ************************************************************/
  1410. CCBFrameIndexTo* CCBFrameIndexTo::create(float fDuration, int startIndex, int endIndex) {
  1411. CCBFrameIndexTo *ret = new (std::nothrow) CCBFrameIndexTo();
  1412. if (ret) {
  1413. if (ret->initWithDuration(fDuration, startIndex, endIndex)) {
  1414. ret->autorelease();
  1415. } else {
  1416. CC_SAFE_DELETE(ret);
  1417. }
  1418. }
  1419. return ret;
  1420. }
  1421. bool CCBFrameIndexTo::initWithDuration(float fDuration, int startIndex, int endIndex) {
  1422. if (ActionInterval::initWithDuration(fDuration)) {
  1423. _startIndex = startIndex;
  1424. _endIndex = endIndex;
  1425. return true;
  1426. } else {
  1427. return false;
  1428. }
  1429. }
  1430. CCBFrameIndexTo* CCBFrameIndexTo::clone() const {
  1431. auto a = new (std::nothrow) CCBFrameIndexTo();
  1432. a->initWithDuration(_duration, _startIndex, _endIndex);
  1433. a->autorelease();
  1434. return a;
  1435. }
  1436. CCBFrameIndexTo* CCBFrameIndexTo::reverse() const {
  1437. CCASSERT(false, "reverse() is not supported in CCBFrameIndexTo");
  1438. return nullptr;
  1439. }
  1440. void CCBFrameIndexTo::startWithTarget(cocos2d::Node *pNode) {
  1441. ActionInterval::startWithTarget(pNode);
  1442. _diffIndex = _endIndex - _startIndex;
  1443. }
  1444. void CCBFrameIndexTo::update(float time) {
  1445. auto fasp = dynamic_cast<ZGFrameActionSprite*>(_target);
  1446. CCASSERT(fasp != nullptr, "CCBFrameIndexTo 只支持 ZGFrameActionSprite类型的图片");
  1447. auto frameIndex = (int)(_startIndex + (_diffIndex * time));
  1448. fasp->setFrameIndex(frameIndex);
  1449. }
  1450. /************************************************************
  1451. CCBParticlePosVarTo
  1452. ************************************************************/
  1453. CCBParticlePosVarTo* CCBParticlePosVarTo::create(float fDuration, cocos2d::Vec2 pos){
  1454. CCBParticlePosVarTo *ret = new (std::nothrow) CCBParticlePosVarTo();
  1455. if (ret)
  1456. {
  1457. if (ret->initWithDuration(fDuration, pos))
  1458. {
  1459. ret->autorelease();
  1460. }
  1461. else
  1462. {
  1463. CC_SAFE_DELETE(ret);
  1464. }
  1465. }
  1466. return ret;
  1467. }
  1468. bool CCBParticlePosVarTo::initWithDuration(float fDuration, cocos2d::Vec2 pos){
  1469. if (ActionInterval::initWithDuration(fDuration))
  1470. {
  1471. _endGravity = pos;
  1472. return true;
  1473. }
  1474. else
  1475. {
  1476. return false;
  1477. }
  1478. }
  1479. CCBParticlePosVarTo* CCBParticlePosVarTo::clone() const {
  1480. // no copy constructor
  1481. auto a = new (std::nothrow) CCBParticlePosVarTo();
  1482. a->initWithDuration(_duration, _endGravity);
  1483. a->autorelease();
  1484. return a;
  1485. }
  1486. CCBParticlePosVarTo* CCBParticlePosVarTo::reverse() const {
  1487. CCASSERT(false, "reverse() is not supported in CCBParticlePosVarTo");
  1488. return nullptr;
  1489. }
  1490. void CCBParticlePosVarTo::startWithTarget(cocos2d::Node *pNode) {
  1491. ActionInterval::startWithTarget(pNode);
  1492. _startGravity = dynamic_cast<ParticleSystem*>(pNode)->getPosVar();
  1493. _diffGravity = _endGravity - _startGravity;
  1494. }
  1495. void CCBParticlePosVarTo::update(float time){
  1496. auto fasp = dynamic_cast<ParticleSystem*>(_target);
  1497. CCASSERT(fasp != nullptr, "CCBGravityTo 只支持 ParticleSystem");
  1498. auto gravity = _startGravity + (_diffGravity * time);
  1499. fasp->setPosVar(gravity);
  1500. }
  1501. /************************************************************
  1502. CCBGravityTo
  1503. ************************************************************/
  1504. CCBGravityTo* CCBGravityTo::create(float fDuration, cocos2d::Vec2 gravity){
  1505. CCBGravityTo *ret = new (std::nothrow) CCBGravityTo();
  1506. if (ret)
  1507. {
  1508. if (ret->initWithDuration(fDuration, gravity))
  1509. {
  1510. ret->autorelease();
  1511. }
  1512. else
  1513. {
  1514. CC_SAFE_DELETE(ret);
  1515. }
  1516. }
  1517. return ret;
  1518. }
  1519. bool CCBGravityTo::initWithDuration(float fDuration, cocos2d::Vec2 gravity){
  1520. if (ActionInterval::initWithDuration(fDuration))
  1521. {
  1522. _endGravity = gravity;
  1523. return true;
  1524. }
  1525. else
  1526. {
  1527. return false;
  1528. }
  1529. }
  1530. CCBGravityTo* CCBGravityTo::clone() const {
  1531. // no copy constructor
  1532. auto a = new (std::nothrow) CCBGravityTo();
  1533. a->initWithDuration(_duration, _endGravity);
  1534. a->autorelease();
  1535. return a;
  1536. }
  1537. CCBGravityTo* CCBGravityTo::reverse() const {
  1538. CCASSERT(false, "reverse() is not supported in CCBGravityTo");
  1539. return nullptr;
  1540. }
  1541. void CCBGravityTo::startWithTarget(cocos2d::Node *pNode) {
  1542. ActionInterval::startWithTarget(pNode);
  1543. _startGravity = dynamic_cast<ParticleSystem*>(pNode)->getGravity();
  1544. _diffGravity = _endGravity - _startGravity;
  1545. }
  1546. void CCBGravityTo::update(float time){
  1547. auto fasp = dynamic_cast<ParticleSystem*>(_target);
  1548. CCASSERT(fasp != nullptr, "CCBGravityTo 只支持 ParticleSystem");
  1549. auto gravity = _startGravity + (_diffGravity * time);
  1550. fasp->setGravity(gravity);
  1551. }
  1552. /************************************************************
  1553. CCBParticleSpeedTo
  1554. ************************************************************/
  1555. //粒子速度
  1556. CCBParticleSpeedTo* CCBParticleSpeedTo::create(float fDuration, cocos2d::Vec2 gravity){
  1557. CCBParticleSpeedTo *ret = new (std::nothrow) CCBParticleSpeedTo();
  1558. if (ret)
  1559. {
  1560. if (ret->initWithDuration(fDuration, gravity))
  1561. {
  1562. ret->autorelease();
  1563. }
  1564. else
  1565. {
  1566. CC_SAFE_DELETE(ret);
  1567. }
  1568. }
  1569. return ret;
  1570. }
  1571. bool CCBParticleSpeedTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1572. if (ActionInterval::initWithDuration(fDuration))
  1573. {
  1574. _endFloatVar = folatVar;
  1575. return true;
  1576. }
  1577. else
  1578. {
  1579. return false;
  1580. }
  1581. }
  1582. CCBParticleSpeedTo* CCBParticleSpeedTo::clone() const {
  1583. // no copy constructor
  1584. auto a = new (std::nothrow) CCBParticleSpeedTo();
  1585. a->initWithDuration(_duration, _endFloatVar);
  1586. a->autorelease();
  1587. return a;
  1588. }
  1589. CCBParticleSpeedTo* CCBParticleSpeedTo::reverse() const {
  1590. CCASSERT(false, "reverse() is not supported in CCBParticleSpeedTo");
  1591. return nullptr;
  1592. }
  1593. void CCBParticleSpeedTo::startWithTarget(cocos2d::Node *pNode) {
  1594. ActionInterval::startWithTarget(pNode);
  1595. float a = dynamic_cast<ParticleSystem*>(pNode)->getSpeed();
  1596. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getSpeedVar();
  1597. _startFloatVar = Vec2(a, aVar);
  1598. _diffFloatVar = _endFloatVar - _startFloatVar;
  1599. }
  1600. void CCBParticleSpeedTo::update(float time){
  1601. auto par = dynamic_cast<ParticleSystem*>(_target);
  1602. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1603. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1604. par->setSpeed(floatVar.x);
  1605. par->setSpeedVar(floatVar.y);
  1606. }
  1607. /************************************************************
  1608. CCBParticleLifeTo
  1609. ************************************************************/
  1610. //粒子生命
  1611. CCBParticleLifeTo* CCBParticleLifeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  1612. CCBParticleLifeTo *ret = new (std::nothrow) CCBParticleLifeTo();
  1613. if (ret)
  1614. {
  1615. if (ret->initWithDuration(fDuration, folatVar))
  1616. {
  1617. ret->autorelease();
  1618. }
  1619. else
  1620. {
  1621. CC_SAFE_DELETE(ret);
  1622. }
  1623. }
  1624. return ret;
  1625. }
  1626. bool CCBParticleLifeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1627. if (ActionInterval::initWithDuration(fDuration))
  1628. {
  1629. _endFloatVar = folatVar;
  1630. return true;
  1631. }
  1632. else
  1633. {
  1634. return false;
  1635. }
  1636. }
  1637. CCBParticleLifeTo* CCBParticleLifeTo::clone() const {
  1638. // no copy constructor
  1639. auto a = new (std::nothrow) CCBParticleLifeTo();
  1640. a->initWithDuration(_duration, _endFloatVar);
  1641. a->autorelease();
  1642. return a;
  1643. }
  1644. CCBParticleLifeTo* CCBParticleLifeTo::reverse() const {
  1645. CCASSERT(false, "reverse() is not supported in CCBParticleLifeTo");
  1646. return nullptr;
  1647. }
  1648. void CCBParticleLifeTo::startWithTarget(cocos2d::Node *pNode) {
  1649. ActionInterval::startWithTarget(pNode);
  1650. float a = dynamic_cast<ParticleSystem*>(pNode)->getLife();
  1651. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getLifeVar();
  1652. _startFloatVar = Vec2(a, aVar);
  1653. _diffFloatVar = _endFloatVar - _startFloatVar;
  1654. }
  1655. void CCBParticleLifeTo::update(float time){
  1656. auto par = dynamic_cast<ParticleSystem*>(_target);
  1657. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1658. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1659. par->setLife(floatVar.x);
  1660. par->setLifeVar(floatVar.y);
  1661. }
  1662. /************************************************************
  1663. CCBParticleStartSizeTo
  1664. ************************************************************/
  1665. //粒子初始尺寸
  1666. CCBParticleStartSizeTo* CCBParticleStartSizeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  1667. CCBParticleStartSizeTo *ret = new (std::nothrow) CCBParticleStartSizeTo();
  1668. if (ret)
  1669. {
  1670. if (ret->initWithDuration(fDuration, folatVar))
  1671. {
  1672. ret->autorelease();
  1673. }
  1674. else
  1675. {
  1676. CC_SAFE_DELETE(ret);
  1677. }
  1678. }
  1679. return ret;
  1680. }
  1681. bool CCBParticleStartSizeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1682. if (ActionInterval::initWithDuration(fDuration))
  1683. {
  1684. _endFloatVar = folatVar;
  1685. return true;
  1686. }
  1687. else
  1688. {
  1689. return false;
  1690. }
  1691. }
  1692. CCBParticleStartSizeTo* CCBParticleStartSizeTo::clone() const {
  1693. // no copy constructor
  1694. auto a = new (std::nothrow) CCBParticleStartSizeTo();
  1695. a->initWithDuration(_duration, _endFloatVar);
  1696. a->autorelease();
  1697. return a;
  1698. }
  1699. CCBParticleStartSizeTo* CCBParticleStartSizeTo::reverse() const {
  1700. CCASSERT(false, "reverse() is not supported in CCBParticleStartSizeTo");
  1701. return nullptr;
  1702. }
  1703. void CCBParticleStartSizeTo::startWithTarget(cocos2d::Node *pNode) {
  1704. ActionInterval::startWithTarget(pNode);
  1705. float a = dynamic_cast<ParticleSystem*>(pNode)->getStartSize();
  1706. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getStartSizeVar();
  1707. _startFloatVar = Vec2(a, aVar);
  1708. _diffFloatVar = _endFloatVar - _startFloatVar;
  1709. }
  1710. void CCBParticleStartSizeTo::update(float time){
  1711. auto par = dynamic_cast<ParticleSystem*>(_target);
  1712. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1713. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1714. par->setStartSize(floatVar.x);
  1715. par->setStartSizeVar(floatVar.y);
  1716. }
  1717. /************************************************************
  1718. CCBParticleEndSizeTo
  1719. ************************************************************/
  1720. //粒子结束时的尺寸大小
  1721. CCBParticleEndSizeTo* CCBParticleEndSizeTo::create(float fDuration, cocos2d::Vec2 folatVar){
  1722. CCBParticleEndSizeTo *ret = new (std::nothrow) CCBParticleEndSizeTo();
  1723. if (ret)
  1724. {
  1725. if (ret->initWithDuration(fDuration, folatVar))
  1726. {
  1727. ret->autorelease();
  1728. }
  1729. else
  1730. {
  1731. CC_SAFE_DELETE(ret);
  1732. }
  1733. }
  1734. return ret;
  1735. }
  1736. bool CCBParticleEndSizeTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1737. if (ActionInterval::initWithDuration(fDuration))
  1738. {
  1739. _endFloatVar = folatVar;
  1740. return true;
  1741. }
  1742. else
  1743. {
  1744. return false;
  1745. }
  1746. }
  1747. CCBParticleEndSizeTo* CCBParticleEndSizeTo::clone() const {
  1748. // no copy constructor
  1749. auto a = new (std::nothrow) CCBParticleEndSizeTo();
  1750. a->initWithDuration(_duration, _endFloatVar);
  1751. a->autorelease();
  1752. return a;
  1753. }
  1754. CCBParticleEndSizeTo* CCBParticleEndSizeTo::reverse() const {
  1755. CCASSERT(false, "reverse() is not supported in CCBParticleEndSizeTo");
  1756. return nullptr;
  1757. }
  1758. void CCBParticleEndSizeTo::startWithTarget(cocos2d::Node *pNode) {
  1759. ActionInterval::startWithTarget(pNode);
  1760. float a = dynamic_cast<ParticleSystem*>(pNode)->getEndSize();
  1761. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getEndSizeVar();
  1762. _startFloatVar = Vec2(a, aVar);
  1763. _diffFloatVar = _endFloatVar - _startFloatVar;
  1764. }
  1765. void CCBParticleEndSizeTo::update(float time){
  1766. auto par = dynamic_cast<ParticleSystem*>(_target);
  1767. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1768. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1769. par->setEndSize(floatVar.x);
  1770. par->setEndSizeVar(floatVar.y);
  1771. }
  1772. /************************************************************
  1773. CCBParticleStartSpinTo
  1774. ************************************************************/
  1775. //粒子速度
  1776. CCBParticleStartSpinTo* CCBParticleStartSpinTo::create(float fDuration, cocos2d::Vec2 folatVar){
  1777. CCBParticleStartSpinTo *ret = new (std::nothrow) CCBParticleStartSpinTo();
  1778. if (ret)
  1779. {
  1780. if (ret->initWithDuration(fDuration, folatVar))
  1781. {
  1782. ret->autorelease();
  1783. }
  1784. else
  1785. {
  1786. CC_SAFE_DELETE(ret);
  1787. }
  1788. }
  1789. return ret;
  1790. }
  1791. bool CCBParticleStartSpinTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1792. if (ActionInterval::initWithDuration(fDuration))
  1793. {
  1794. _endFloatVar = folatVar;
  1795. return true;
  1796. }
  1797. else
  1798. {
  1799. return false;
  1800. }
  1801. }
  1802. CCBParticleStartSpinTo* CCBParticleStartSpinTo::clone() const {
  1803. // no copy constructor
  1804. auto a = new (std::nothrow) CCBParticleStartSpinTo();
  1805. a->initWithDuration(_duration, _endFloatVar);
  1806. a->autorelease();
  1807. return a;
  1808. }
  1809. CCBParticleStartSpinTo* CCBParticleStartSpinTo::reverse() const {
  1810. CCASSERT(false, "reverse() is not supported in CCBParticleStartSpinTo");
  1811. return nullptr;
  1812. }
  1813. void CCBParticleStartSpinTo::startWithTarget(cocos2d::Node *pNode) {
  1814. ActionInterval::startWithTarget(pNode);
  1815. float a = dynamic_cast<ParticleSystem*>(pNode)->getStartSpin();
  1816. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getStartSpinVar();
  1817. _startFloatVar = Vec2(a, aVar);
  1818. _diffFloatVar = _endFloatVar - _startFloatVar;
  1819. }
  1820. void CCBParticleStartSpinTo::update(float time){
  1821. auto par = dynamic_cast<ParticleSystem*>(_target);
  1822. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1823. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1824. par->setStartSpin(floatVar.x);
  1825. par->setStartSpinVar(floatVar.y);
  1826. }
  1827. /************************************************************
  1828. CCBParticleEndSpinTo
  1829. ************************************************************/
  1830. //粒子结束角度
  1831. CCBParticleEndSpinTo* CCBParticleEndSpinTo::create(float fDuration, cocos2d::Vec2 folatVar){
  1832. CCBParticleEndSpinTo *ret = new (std::nothrow) CCBParticleEndSpinTo();
  1833. if (ret)
  1834. {
  1835. if (ret->initWithDuration(fDuration, folatVar))
  1836. {
  1837. ret->autorelease();
  1838. }
  1839. else
  1840. {
  1841. CC_SAFE_DELETE(ret);
  1842. }
  1843. }
  1844. return ret;
  1845. }
  1846. bool CCBParticleEndSpinTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1847. if (ActionInterval::initWithDuration(fDuration))
  1848. {
  1849. _endFloatVar = folatVar;
  1850. return true;
  1851. }
  1852. else
  1853. {
  1854. return false;
  1855. }
  1856. }
  1857. CCBParticleEndSpinTo* CCBParticleEndSpinTo::clone() const {
  1858. // no copy constructor
  1859. auto a = new (std::nothrow) CCBParticleEndSpinTo();
  1860. a->initWithDuration(_duration, _endFloatVar);
  1861. a->autorelease();
  1862. return a;
  1863. }
  1864. CCBParticleEndSpinTo* CCBParticleEndSpinTo::reverse() const {
  1865. CCASSERT(false, "reverse() is not supported in CCBParticleEndSpinTo");
  1866. return nullptr;
  1867. }
  1868. void CCBParticleEndSpinTo::startWithTarget(cocos2d::Node *pNode) {
  1869. ActionInterval::startWithTarget(pNode);
  1870. float a = dynamic_cast<ParticleSystem*>(pNode)->getEndSpin();
  1871. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getEndSpinVar();
  1872. _startFloatVar = Vec2(a, aVar);
  1873. _diffFloatVar = _endFloatVar - _startFloatVar;
  1874. }
  1875. void CCBParticleEndSpinTo::update(float time){
  1876. auto par = dynamic_cast<ParticleSystem*>(_target);
  1877. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1878. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1879. par->setEndSpin(floatVar.x);
  1880. par->setEndSpinVar(floatVar.y);
  1881. }
  1882. /************************************************************
  1883. CCBParticleAngleTo
  1884. ************************************************************/
  1885. //粒子发射角度
  1886. CCBParticleAngleTo* CCBParticleAngleTo::create(float fDuration, cocos2d::Vec2 gravity){
  1887. CCBParticleAngleTo *ret = new (std::nothrow) CCBParticleAngleTo();
  1888. if (ret)
  1889. {
  1890. if (ret->initWithDuration(fDuration, gravity))
  1891. {
  1892. ret->autorelease();
  1893. }
  1894. else
  1895. {
  1896. CC_SAFE_DELETE(ret);
  1897. }
  1898. }
  1899. return ret;
  1900. }
  1901. bool CCBParticleAngleTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1902. if (ActionInterval::initWithDuration(fDuration))
  1903. {
  1904. _endFloatVar = folatVar;
  1905. return true;
  1906. }
  1907. else
  1908. {
  1909. return false;
  1910. }
  1911. }
  1912. CCBParticleAngleTo* CCBParticleAngleTo::clone() const {
  1913. // no copy constructor
  1914. auto a = new (std::nothrow) CCBParticleAngleTo();
  1915. a->initWithDuration(_duration, _endFloatVar);
  1916. a->autorelease();
  1917. return a;
  1918. }
  1919. CCBParticleAngleTo* CCBParticleAngleTo::reverse() const {
  1920. CCASSERT(false, "reverse() is not supported in CCBParticleAngleTo");
  1921. return nullptr;
  1922. }
  1923. void CCBParticleAngleTo::startWithTarget(cocos2d::Node *pNode) {
  1924. ActionInterval::startWithTarget(pNode);
  1925. float a = dynamic_cast<ParticleSystem*>(pNode)->getAngle();
  1926. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getAngleVar();
  1927. _startFloatVar = Vec2(a, aVar);
  1928. _diffFloatVar = _endFloatVar - _startFloatVar;
  1929. }
  1930. void CCBParticleAngleTo::update(float time){
  1931. auto par = dynamic_cast<ParticleSystem*>(_target);
  1932. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1933. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1934. par->setAngle(floatVar.x);
  1935. par->setAngleVar(floatVar.y);
  1936. }
  1937. /************************************************************
  1938. CCBParticleTangentialAccelTo
  1939. ************************************************************/
  1940. //粒子重力模式切向加速度
  1941. CCBParticleTangentialAccelTo* CCBParticleTangentialAccelTo::create(float fDuration, cocos2d::Vec2 gravity){
  1942. CCBParticleTangentialAccelTo *ret = new (std::nothrow) CCBParticleTangentialAccelTo();
  1943. if (ret)
  1944. {
  1945. if (ret->initWithDuration(fDuration, gravity))
  1946. {
  1947. ret->autorelease();
  1948. }
  1949. else
  1950. {
  1951. CC_SAFE_DELETE(ret);
  1952. }
  1953. }
  1954. return ret;
  1955. }
  1956. bool CCBParticleTangentialAccelTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  1957. if (ActionInterval::initWithDuration(fDuration))
  1958. {
  1959. _endFloatVar = folatVar;
  1960. return true;
  1961. }
  1962. else
  1963. {
  1964. return false;
  1965. }
  1966. }
  1967. CCBParticleTangentialAccelTo* CCBParticleTangentialAccelTo::clone() const {
  1968. // no copy constructor
  1969. auto a = new (std::nothrow) CCBParticleTangentialAccelTo();
  1970. a->initWithDuration(_duration, _endFloatVar);
  1971. a->autorelease();
  1972. return a;
  1973. }
  1974. CCBParticleTangentialAccelTo* CCBParticleTangentialAccelTo::reverse() const {
  1975. CCASSERT(false, "reverse() is not supported in CCBParticleTangentialAccelTo");
  1976. return nullptr;
  1977. }
  1978. void CCBParticleTangentialAccelTo::startWithTarget(cocos2d::Node *pNode) {
  1979. ActionInterval::startWithTarget(pNode);
  1980. float a = dynamic_cast<ParticleSystem*>(pNode)->getTangentialAccel();
  1981. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getTangentialAccelVar();
  1982. _startFloatVar = Vec2(a, aVar);
  1983. _diffFloatVar = _endFloatVar - _startFloatVar;
  1984. }
  1985. void CCBParticleTangentialAccelTo::update(float time){
  1986. auto par = dynamic_cast<ParticleSystem*>(_target);
  1987. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  1988. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  1989. par->setTangentialAccel(floatVar.x);
  1990. par->setTangentialAccelVar(floatVar.y);
  1991. }
  1992. /************************************************************
  1993. CCBParticleRadialAccelTo
  1994. ************************************************************/
  1995. //粒子径向加速度
  1996. CCBParticleRadialAccelTo* CCBParticleRadialAccelTo::create(float fDuration, cocos2d::Vec2 gravity){
  1997. CCBParticleRadialAccelTo *ret = new (std::nothrow) CCBParticleRadialAccelTo();
  1998. if (ret)
  1999. {
  2000. if (ret->initWithDuration(fDuration, gravity))
  2001. {
  2002. ret->autorelease();
  2003. }
  2004. else
  2005. {
  2006. CC_SAFE_DELETE(ret);
  2007. }
  2008. }
  2009. return ret;
  2010. }
  2011. bool CCBParticleRadialAccelTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2012. if (ActionInterval::initWithDuration(fDuration))
  2013. {
  2014. _endFloatVar = folatVar;
  2015. return true;
  2016. }
  2017. else
  2018. {
  2019. return false;
  2020. }
  2021. }
  2022. CCBParticleRadialAccelTo* CCBParticleRadialAccelTo::clone() const {
  2023. // no copy constructor
  2024. auto a = new (std::nothrow) CCBParticleRadialAccelTo();
  2025. a->initWithDuration(_duration, _endFloatVar);
  2026. a->autorelease();
  2027. return a;
  2028. }
  2029. CCBParticleRadialAccelTo* CCBParticleRadialAccelTo::reverse() const {
  2030. CCASSERT(false, "reverse() is not supported in CCBParticleRadialAccelTo");
  2031. return nullptr;
  2032. }
  2033. void CCBParticleRadialAccelTo::startWithTarget(cocos2d::Node *pNode) {
  2034. ActionInterval::startWithTarget(pNode);
  2035. float a = dynamic_cast<ParticleSystem*>(pNode)->getRadialAccel();
  2036. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getRadialAccelVar();
  2037. _startFloatVar = Vec2(a, aVar);
  2038. _diffFloatVar = _endFloatVar - _startFloatVar;
  2039. }
  2040. void CCBParticleRadialAccelTo::update(float time){
  2041. auto par = dynamic_cast<ParticleSystem*>(_target);
  2042. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  2043. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2044. par->setRadialAccel(floatVar.x);
  2045. par->setRadialAccelVar(floatVar.y);
  2046. }
  2047. /************************************************************
  2048. CCBParticleStartRadiusTo
  2049. ************************************************************/
  2050. //粒子半径模式开始半径
  2051. CCBParticleStartRadiusTo* CCBParticleStartRadiusTo::create(float fDuration, cocos2d::Vec2 gravity){
  2052. CCBParticleStartRadiusTo *ret = new (std::nothrow) CCBParticleStartRadiusTo();
  2053. if (ret)
  2054. {
  2055. if (ret->initWithDuration(fDuration, gravity))
  2056. {
  2057. ret->autorelease();
  2058. }
  2059. else
  2060. {
  2061. CC_SAFE_DELETE(ret);
  2062. }
  2063. }
  2064. return ret;
  2065. }
  2066. bool CCBParticleStartRadiusTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2067. if (ActionInterval::initWithDuration(fDuration))
  2068. {
  2069. _endFloatVar = folatVar;
  2070. return true;
  2071. }
  2072. else
  2073. {
  2074. return false;
  2075. }
  2076. }
  2077. CCBParticleStartRadiusTo* CCBParticleStartRadiusTo::clone() const {
  2078. // no copy constructor
  2079. auto a = new (std::nothrow) CCBParticleStartRadiusTo();
  2080. a->initWithDuration(_duration, _endFloatVar);
  2081. a->autorelease();
  2082. return a;
  2083. }
  2084. CCBParticleStartRadiusTo* CCBParticleStartRadiusTo::reverse() const {
  2085. CCASSERT(false, "reverse() is not supported in CCBParticleStartRadiusTo");
  2086. return nullptr;
  2087. }
  2088. void CCBParticleStartRadiusTo::startWithTarget(cocos2d::Node *pNode) {
  2089. ActionInterval::startWithTarget(pNode);
  2090. float a = dynamic_cast<ParticleSystem*>(pNode)->getStartRadius();
  2091. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getStartRadiusVar();
  2092. _startFloatVar = Vec2(a, aVar);
  2093. _diffFloatVar = _endFloatVar - _startFloatVar;
  2094. }
  2095. void CCBParticleStartRadiusTo::update(float time){
  2096. auto par = dynamic_cast<ParticleSystem*>(_target);
  2097. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  2098. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2099. par->setStartRadius(floatVar.x);
  2100. par->setStartRadiusVar(floatVar.y);
  2101. }
  2102. /************************************************************
  2103. CCBParticleEndRadiusTo
  2104. ************************************************************/
  2105. //粒子半径模式结束半径
  2106. CCBParticleEndRadiusTo* CCBParticleEndRadiusTo::create(float fDuration, cocos2d::Vec2 gravity){
  2107. CCBParticleEndRadiusTo *ret = new (std::nothrow) CCBParticleEndRadiusTo();
  2108. if (ret)
  2109. {
  2110. if (ret->initWithDuration(fDuration, gravity))
  2111. {
  2112. ret->autorelease();
  2113. }
  2114. else
  2115. {
  2116. CC_SAFE_DELETE(ret);
  2117. }
  2118. }
  2119. return ret;
  2120. }
  2121. bool CCBParticleEndRadiusTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2122. if (ActionInterval::initWithDuration(fDuration))
  2123. {
  2124. _endFloatVar = folatVar;
  2125. return true;
  2126. }
  2127. else
  2128. {
  2129. return false;
  2130. }
  2131. }
  2132. CCBParticleEndRadiusTo* CCBParticleEndRadiusTo::clone() const {
  2133. // no copy constructor
  2134. auto a = new (std::nothrow) CCBParticleEndRadiusTo();
  2135. a->initWithDuration(_duration, _endFloatVar);
  2136. a->autorelease();
  2137. return a;
  2138. }
  2139. CCBParticleEndRadiusTo* CCBParticleEndRadiusTo::reverse() const {
  2140. CCASSERT(false, "reverse() is not supported in CCBParticleEndRadiusTo");
  2141. return nullptr;
  2142. }
  2143. void CCBParticleEndRadiusTo::startWithTarget(cocos2d::Node *pNode) {
  2144. ActionInterval::startWithTarget(pNode);
  2145. float a = dynamic_cast<ParticleSystem*>(pNode)->getEndRadius();
  2146. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getEndRadiusVar();
  2147. _startFloatVar = Vec2(a, aVar);
  2148. _diffFloatVar = _endFloatVar - _startFloatVar;
  2149. }
  2150. void CCBParticleEndRadiusTo::update(float time){
  2151. auto par = dynamic_cast<ParticleSystem*>(_target);
  2152. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  2153. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2154. par->setEndRadius(floatVar.x);
  2155. par->setEndRadiusVar(floatVar.y);
  2156. }
  2157. /************************************************************
  2158. CCBParticleRotatePerSecondTo
  2159. ************************************************************/
  2160. //粒子速度
  2161. CCBParticleRotatePerSecondTo* CCBParticleRotatePerSecondTo::create(float fDuration, cocos2d::Vec2 gravity){
  2162. CCBParticleRotatePerSecondTo *ret = new (std::nothrow) CCBParticleRotatePerSecondTo();
  2163. if (ret)
  2164. {
  2165. if (ret->initWithDuration(fDuration, gravity))
  2166. {
  2167. ret->autorelease();
  2168. }
  2169. else
  2170. {
  2171. CC_SAFE_DELETE(ret);
  2172. }
  2173. }
  2174. return ret;
  2175. }
  2176. bool CCBParticleRotatePerSecondTo::initWithDuration(float fDuration, cocos2d::Vec2 folatVar){
  2177. if (ActionInterval::initWithDuration(fDuration))
  2178. {
  2179. _endFloatVar = folatVar;
  2180. return true;
  2181. }
  2182. else
  2183. {
  2184. return false;
  2185. }
  2186. }
  2187. CCBParticleRotatePerSecondTo* CCBParticleRotatePerSecondTo::clone() const {
  2188. // no copy constructor
  2189. auto a = new (std::nothrow) CCBParticleRotatePerSecondTo();
  2190. a->initWithDuration(_duration, _endFloatVar);
  2191. a->autorelease();
  2192. return a;
  2193. }
  2194. CCBParticleRotatePerSecondTo* CCBParticleRotatePerSecondTo::reverse() const {
  2195. CCASSERT(false, "reverse() is not supported in CCBParticleSpeedTo");
  2196. return nullptr;
  2197. }
  2198. void CCBParticleRotatePerSecondTo::startWithTarget(cocos2d::Node *pNode) {
  2199. ActionInterval::startWithTarget(pNode);
  2200. float a = dynamic_cast<ParticleSystem*>(pNode)->getRotatePerSecond();
  2201. float aVar = dynamic_cast<ParticleSystem*>(pNode)->getRotatePerSecondVar();
  2202. _startFloatVar = Vec2(a, aVar);
  2203. _diffFloatVar = _endFloatVar - _startFloatVar;
  2204. }
  2205. void CCBParticleRotatePerSecondTo::update(float time){
  2206. auto par = dynamic_cast<ParticleSystem*>(_target);
  2207. CCASSERT(par != nullptr, "CCBParticleSpeedTo 只支持 ParticleSystem");
  2208. auto floatVar = _startFloatVar + (_diffFloatVar * time);
  2209. par->setRotatePerSecond(floatVar.x);
  2210. par->setRotatePerSecondVar(floatVar.y);
  2211. }
  2212. /************************************************************
  2213. CCBEaseInstant
  2214. ************************************************************/
  2215. CCBEaseInstant* CCBEaseInstant::create(ActionInterval *pAction)
  2216. {
  2217. CCBEaseInstant *pRet = new (std::nothrow) CCBEaseInstant();
  2218. if (pRet && pRet->initWithAction(pAction))
  2219. {
  2220. pRet->autorelease();
  2221. }
  2222. else
  2223. {
  2224. CC_SAFE_RELEASE_NULL(pRet);
  2225. }
  2226. return pRet;
  2227. }
  2228. CCBEaseInstant* CCBEaseInstant::clone() const
  2229. {
  2230. // no copy constructor
  2231. auto a = new (std::nothrow) CCBEaseInstant();
  2232. a->initWithAction(_inner);
  2233. a->autorelease();
  2234. return a;
  2235. }
  2236. CCBEaseInstant* CCBEaseInstant::reverse() const
  2237. {
  2238. return CCBEaseInstant::create(_inner->reverse());
  2239. }
  2240. void CCBEaseInstant::update(float dt)
  2241. {
  2242. if (dt < 0)
  2243. {
  2244. _inner->update(0);
  2245. }
  2246. else
  2247. {
  2248. _inner->update(1);
  2249. }
  2250. }
  2251. }