CCParticleExamples.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCParticleExamples.h"
  24. #include "base/CCDirector.h"
  25. #include "base/firePngData.h"
  26. #include "renderer/CCTextureCache.h"
  27. NS_CC_BEGIN
  28. //
  29. // ParticleFire
  30. //
  31. static Texture2D* getDefaultTexture()
  32. {
  33. Texture2D* texture = nullptr;
  34. Image* image = nullptr;
  35. do
  36. {
  37. const std::string key = "/__firePngData";
  38. texture = Director::getInstance()->getTextureCache()->getTextureForKey(key);
  39. CC_BREAK_IF(texture != nullptr);
  40. image = new (std::nothrow) Image();
  41. CC_BREAK_IF(nullptr == image);
  42. bool ret = image->initWithImageData(__firePngData, sizeof(__firePngData));
  43. CC_BREAK_IF(!ret);
  44. texture = Director::getInstance()->getTextureCache()->addImage(image, key);
  45. } while (0);
  46. CC_SAFE_RELEASE(image);
  47. return texture;
  48. }
  49. ParticleFire* ParticleFire::create()
  50. {
  51. ParticleFire* ret = new (std::nothrow) ParticleFire();
  52. if (ret && ret->init())
  53. {
  54. ret->autorelease();
  55. }
  56. else
  57. {
  58. CC_SAFE_DELETE(ret);
  59. }
  60. return ret;
  61. }
  62. ParticleFire* ParticleFire::createWithTotalParticles(int numberOfParticles)
  63. {
  64. ParticleFire* ret = new (std::nothrow) ParticleFire();
  65. if (ret && ret->initWithTotalParticles(numberOfParticles))
  66. {
  67. ret->autorelease();
  68. }
  69. else
  70. {
  71. CC_SAFE_DELETE(ret);
  72. }
  73. return ret;
  74. }
  75. bool ParticleFire::initWithTotalParticles(int numberOfParticles)
  76. {
  77. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  78. {
  79. // duration
  80. _duration = DURATION_INFINITY;
  81. // Gravity Mode
  82. this->_emitterMode = Mode::GRAVITY;
  83. // Gravity Mode: gravity
  84. this->modeA.gravity.setZero();
  85. // Gravity Mode: radial acceleration
  86. this->modeA.radialAccel = 0;
  87. this->modeA.radialAccelVar = 0;
  88. // Gravity Mode: speed of particles
  89. this->modeA.speed = 60;
  90. this->modeA.speedVar = 20;
  91. // starting angle
  92. _angle = 90;
  93. _angleVar = 10;
  94. // emitter position
  95. Size winSize = Director::getInstance()->getWinSize();
  96. this->setPosition(winSize.width/2.0f, 60.0f);
  97. this->_posVar.set(40.0f, 20.0f);
  98. // life of particles
  99. _life = 3;
  100. _lifeVar = 0.25f;
  101. // size, in pixels
  102. _startSize = 54.0f;
  103. _startSizeVar = 10.0f;
  104. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  105. // emits per frame
  106. _emissionRate = _totalParticles/_life;
  107. // color of particles
  108. _startColor.r = 0.76f;
  109. _startColor.g = 0.25f;
  110. _startColor.b = 0.12f;
  111. _startColor.a = 1.0f;
  112. _startColorVar.r = 0.0f;
  113. _startColorVar.g = 0.0f;
  114. _startColorVar.b = 0.0f;
  115. _startColorVar.a = 0.0f;
  116. _endColor.r = 0.0f;
  117. _endColor.g = 0.0f;
  118. _endColor.b = 0.0f;
  119. _endColor.a = 1.0f;
  120. _endColorVar.r = 0.0f;
  121. _endColorVar.g = 0.0f;
  122. _endColorVar.b = 0.0f;
  123. _endColorVar.a = 0.0f;
  124. Texture2D* texture = getDefaultTexture();
  125. if (texture != nullptr)
  126. {
  127. setTexture(texture);
  128. }
  129. // additive
  130. this->setBlendAdditive(true);
  131. return true;
  132. }
  133. return false;
  134. }
  135. //
  136. // ParticleFireworks
  137. //
  138. ParticleFireworks* ParticleFireworks::create()
  139. {
  140. ParticleFireworks* ret = new (std::nothrow) ParticleFireworks();
  141. if (ret && ret->init())
  142. {
  143. ret->autorelease();
  144. }
  145. else
  146. {
  147. CC_SAFE_DELETE(ret);
  148. }
  149. return ret;
  150. }
  151. ParticleFireworks* ParticleFireworks::createWithTotalParticles(int numberOfParticles)
  152. {
  153. ParticleFireworks* ret = new (std::nothrow) ParticleFireworks();
  154. if (ret && ret->initWithTotalParticles(numberOfParticles))
  155. {
  156. ret->autorelease();
  157. }
  158. else
  159. {
  160. CC_SAFE_DELETE(ret);
  161. }
  162. return ret;
  163. }
  164. bool ParticleFireworks::initWithTotalParticles(int numberOfParticles)
  165. {
  166. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  167. {
  168. // duration
  169. _duration= DURATION_INFINITY;
  170. // Gravity Mode
  171. this->_emitterMode = Mode::GRAVITY;
  172. // Gravity Mode: gravity
  173. this->modeA.gravity.set(0.0f, -90.0f);
  174. // Gravity Mode: radial
  175. this->modeA.radialAccel = 0.0f;
  176. this->modeA.radialAccelVar = 0.0f;
  177. // Gravity Mode: speed of particles
  178. this->modeA.speed = 180.0f;
  179. this->modeA.speedVar = 50.0f;
  180. // emitter position
  181. Size winSize = Director::getInstance()->getWinSize();
  182. this->setPosition(winSize.width/2, winSize.height/2);
  183. // angle
  184. this->_angle= 90.0f;
  185. this->_angleVar = 20.0f;
  186. // life of particles
  187. this->_life = 3.5f;
  188. this->_lifeVar = 1.0f;
  189. // emits per frame
  190. this->_emissionRate = _totalParticles/_life;
  191. // color of particles
  192. _startColor.r = 0.5f;
  193. _startColor.g = 0.5f;
  194. _startColor.b = 0.5f;
  195. _startColor.a = 1.0f;
  196. _startColorVar.r = 0.5f;
  197. _startColorVar.g = 0.5f;
  198. _startColorVar.b = 0.5f;
  199. _startColorVar.a = 0.1f;
  200. _endColor.r = 0.1f;
  201. _endColor.g = 0.1f;
  202. _endColor.b = 0.1f;
  203. _endColor.a = 0.2f;
  204. _endColorVar.r = 0.1f;
  205. _endColorVar.g = 0.1f;
  206. _endColorVar.b = 0.1f;
  207. _endColorVar.a = 0.2f;
  208. // size, in pixels
  209. _startSize = 8.0f;
  210. _startSizeVar = 2.0f;
  211. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  212. Texture2D* texture = getDefaultTexture();
  213. if (texture != nullptr)
  214. {
  215. setTexture(texture);
  216. }
  217. // additive
  218. this->setBlendAdditive(false);
  219. return true;
  220. }
  221. return false;
  222. }
  223. //
  224. // ParticleSun
  225. //
  226. ParticleSun* ParticleSun::create()
  227. {
  228. ParticleSun* ret = new (std::nothrow) ParticleSun();
  229. if (ret && ret->init())
  230. {
  231. ret->autorelease();
  232. }
  233. else
  234. {
  235. CC_SAFE_DELETE(ret);
  236. }
  237. return ret;
  238. }
  239. ParticleSun* ParticleSun::createWithTotalParticles(int numberOfParticles)
  240. {
  241. ParticleSun* ret = new (std::nothrow) ParticleSun();
  242. if (ret && ret->initWithTotalParticles(numberOfParticles))
  243. {
  244. ret->autorelease();
  245. }
  246. else
  247. {
  248. CC_SAFE_DELETE(ret);
  249. }
  250. return ret;
  251. }
  252. bool ParticleSun::initWithTotalParticles(int numberOfParticles)
  253. {
  254. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  255. {
  256. // additive
  257. this->setBlendAdditive(true);
  258. // duration
  259. _duration = DURATION_INFINITY;
  260. // Gravity Mode
  261. setEmitterMode(Mode::GRAVITY);
  262. // Gravity Mode: gravity
  263. setGravity(Vec2(0,0));
  264. // Gravity mode: radial acceleration
  265. setRadialAccel(0);
  266. setRadialAccelVar(0);
  267. // Gravity mode: speed of particles
  268. setSpeed(20);
  269. setSpeedVar(5);
  270. // angle
  271. _angle = 90;
  272. _angleVar = 360;
  273. // emitter position
  274. Size winSize = Director::getInstance()->getWinSize();
  275. this->setPosition(winSize.width/2, winSize.height/2);
  276. setPosVar(Vec2::ZERO);
  277. // life of particles
  278. _life = 1;
  279. _lifeVar = 0.5f;
  280. // size, in pixels
  281. _startSize = 30.0f;
  282. _startSizeVar = 10.0f;
  283. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  284. // emits per seconds
  285. _emissionRate = _totalParticles/_life;
  286. // color of particles
  287. _startColor.r = 0.76f;
  288. _startColor.g = 0.25f;
  289. _startColor.b = 0.12f;
  290. _startColor.a = 1.0f;
  291. _startColorVar.r = 0.0f;
  292. _startColorVar.g = 0.0f;
  293. _startColorVar.b = 0.0f;
  294. _startColorVar.a = 0.0f;
  295. _endColor.r = 0.0f;
  296. _endColor.g = 0.0f;
  297. _endColor.b = 0.0f;
  298. _endColor.a = 1.0f;
  299. _endColorVar.r = 0.0f;
  300. _endColorVar.g = 0.0f;
  301. _endColorVar.b = 0.0f;
  302. _endColorVar.a = 0.0f;
  303. Texture2D* texture = getDefaultTexture();
  304. if (texture != nullptr)
  305. {
  306. setTexture(texture);
  307. }
  308. return true;
  309. }
  310. return false;
  311. }
  312. //
  313. // ParticleGalaxy
  314. //
  315. ParticleGalaxy* ParticleGalaxy::create()
  316. {
  317. ParticleGalaxy* ret = new (std::nothrow) ParticleGalaxy();
  318. if (ret && ret->init())
  319. {
  320. ret->autorelease();
  321. }
  322. else
  323. {
  324. CC_SAFE_DELETE(ret);
  325. }
  326. return ret;
  327. }
  328. ParticleGalaxy* ParticleGalaxy::createWithTotalParticles(int numberOfParticles)
  329. {
  330. ParticleGalaxy* ret = new (std::nothrow) ParticleGalaxy();
  331. if (ret && ret->initWithTotalParticles(numberOfParticles))
  332. {
  333. ret->autorelease();
  334. }
  335. else
  336. {
  337. CC_SAFE_DELETE(ret);
  338. }
  339. return ret;
  340. }
  341. bool ParticleGalaxy::initWithTotalParticles(int numberOfParticles)
  342. {
  343. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  344. {
  345. // duration
  346. _duration = DURATION_INFINITY;
  347. // Gravity Mode
  348. setEmitterMode(Mode::GRAVITY);
  349. // Gravity Mode: gravity
  350. setGravity(Vec2(0,0));
  351. // Gravity Mode: speed of particles
  352. setSpeed(60);
  353. setSpeedVar(10);
  354. // Gravity Mode: radial
  355. setRadialAccel(-80);
  356. setRadialAccelVar(0);
  357. // Gravity Mode: tangential
  358. setTangentialAccel(80);
  359. setTangentialAccelVar(0);
  360. // angle
  361. _angle = 90;
  362. _angleVar = 360;
  363. // emitter position
  364. Size winSize = Director::getInstance()->getWinSize();
  365. this->setPosition(winSize.width/2, winSize.height/2);
  366. setPosVar(Vec2::ZERO);
  367. // life of particles
  368. _life = 4;
  369. _lifeVar = 1;
  370. // size, in pixels
  371. _startSize = 37.0f;
  372. _startSizeVar = 10.0f;
  373. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  374. // emits per second
  375. _emissionRate = _totalParticles/_life;
  376. // color of particles
  377. _startColor.r = 0.12f;
  378. _startColor.g = 0.25f;
  379. _startColor.b = 0.76f;
  380. _startColor.a = 1.0f;
  381. _startColorVar.r = 0.0f;
  382. _startColorVar.g = 0.0f;
  383. _startColorVar.b = 0.0f;
  384. _startColorVar.a = 0.0f;
  385. _endColor.r = 0.0f;
  386. _endColor.g = 0.0f;
  387. _endColor.b = 0.0f;
  388. _endColor.a = 1.0f;
  389. _endColorVar.r = 0.0f;
  390. _endColorVar.g = 0.0f;
  391. _endColorVar.b = 0.0f;
  392. _endColorVar.a = 0.0f;
  393. Texture2D* texture = getDefaultTexture();
  394. if (texture != nullptr)
  395. {
  396. setTexture(texture);
  397. }
  398. // additive
  399. this->setBlendAdditive(true);
  400. return true;
  401. }
  402. return false;
  403. }
  404. //
  405. // ParticleFlower
  406. //
  407. ParticleFlower* ParticleFlower::create()
  408. {
  409. ParticleFlower* ret = new (std::nothrow) ParticleFlower();
  410. if (ret && ret->init())
  411. {
  412. ret->autorelease();
  413. }
  414. else
  415. {
  416. CC_SAFE_DELETE(ret);
  417. }
  418. return ret;
  419. }
  420. ParticleFlower* ParticleFlower::createWithTotalParticles(int numberOfParticles)
  421. {
  422. ParticleFlower* ret = new (std::nothrow) ParticleFlower();
  423. if (ret && ret->initWithTotalParticles(numberOfParticles))
  424. {
  425. ret->autorelease();
  426. }
  427. else
  428. {
  429. CC_SAFE_DELETE(ret);
  430. }
  431. return ret;
  432. }
  433. bool ParticleFlower::initWithTotalParticles(int numberOfParticles)
  434. {
  435. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  436. {
  437. // duration
  438. _duration = DURATION_INFINITY;
  439. // Gravity Mode
  440. setEmitterMode(Mode::GRAVITY);
  441. // Gravity Mode: gravity
  442. setGravity(Vec2(0,0));
  443. // Gravity Mode: speed of particles
  444. setSpeed(80);
  445. setSpeedVar(10);
  446. // Gravity Mode: radial
  447. setRadialAccel(-60);
  448. setRadialAccelVar(0);
  449. // Gravity Mode: tangential
  450. setTangentialAccel(15);
  451. setTangentialAccelVar(0);
  452. // angle
  453. _angle = 90;
  454. _angleVar = 360;
  455. // emitter position
  456. Size winSize = Director::getInstance()->getWinSize();
  457. this->setPosition(winSize.width/2, winSize.height/2);
  458. setPosVar(Vec2::ZERO);
  459. // life of particles
  460. _life = 4;
  461. _lifeVar = 1;
  462. // size, in pixels
  463. _startSize = 30.0f;
  464. _startSizeVar = 10.0f;
  465. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  466. // emits per second
  467. _emissionRate = _totalParticles/_life;
  468. // color of particles
  469. _startColor.r = 0.50f;
  470. _startColor.g = 0.50f;
  471. _startColor.b = 0.50f;
  472. _startColor.a = 1.0f;
  473. _startColorVar.r = 0.5f;
  474. _startColorVar.g = 0.5f;
  475. _startColorVar.b = 0.5f;
  476. _startColorVar.a = 0.5f;
  477. _endColor.r = 0.0f;
  478. _endColor.g = 0.0f;
  479. _endColor.b = 0.0f;
  480. _endColor.a = 1.0f;
  481. _endColorVar.r = 0.0f;
  482. _endColorVar.g = 0.0f;
  483. _endColorVar.b = 0.0f;
  484. _endColorVar.a = 0.0f;
  485. Texture2D* texture = getDefaultTexture();
  486. if (texture != nullptr)
  487. {
  488. setTexture(texture);
  489. }
  490. // additive
  491. this->setBlendAdditive(true);
  492. return true;
  493. }
  494. return false;
  495. }
  496. //
  497. // ParticleMeteor
  498. //
  499. ParticleMeteor * ParticleMeteor::create()
  500. {
  501. ParticleMeteor *ret = new (std::nothrow) ParticleMeteor();
  502. if (ret && ret->init())
  503. {
  504. ret->autorelease();
  505. }
  506. else
  507. {
  508. CC_SAFE_DELETE(ret);
  509. }
  510. return ret;
  511. }
  512. ParticleMeteor* ParticleMeteor::createWithTotalParticles(int numberOfParticles)
  513. {
  514. ParticleMeteor* ret = new (std::nothrow) ParticleMeteor();
  515. if (ret && ret->initWithTotalParticles(numberOfParticles))
  516. {
  517. ret->autorelease();
  518. }
  519. else
  520. {
  521. CC_SAFE_DELETE(ret);
  522. }
  523. return ret;
  524. }
  525. bool ParticleMeteor::initWithTotalParticles(int numberOfParticles)
  526. {
  527. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  528. {
  529. // duration
  530. _duration = DURATION_INFINITY;
  531. // Gravity Mode
  532. setEmitterMode(Mode::GRAVITY);
  533. // Gravity Mode: gravity
  534. setGravity(Vec2(-200,200));
  535. // Gravity Mode: speed of particles
  536. setSpeed(15);
  537. setSpeedVar(5);
  538. // Gravity Mode: radial
  539. setRadialAccel(0);
  540. setRadialAccelVar(0);
  541. // Gravity Mode: tangential
  542. setTangentialAccel(0);
  543. setTangentialAccelVar(0);
  544. // angle
  545. _angle = 90;
  546. _angleVar = 360;
  547. // emitter position
  548. Size winSize = Director::getInstance()->getWinSize();
  549. this->setPosition(winSize.width/2, winSize.height/2);
  550. setPosVar(Vec2::ZERO);
  551. // life of particles
  552. _life = 2;
  553. _lifeVar = 1;
  554. // size, in pixels
  555. _startSize = 60.0f;
  556. _startSizeVar = 10.0f;
  557. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  558. // emits per second
  559. _emissionRate = _totalParticles/_life;
  560. // color of particles
  561. _startColor.r = 0.2f;
  562. _startColor.g = 0.4f;
  563. _startColor.b = 0.7f;
  564. _startColor.a = 1.0f;
  565. _startColorVar.r = 0.0f;
  566. _startColorVar.g = 0.0f;
  567. _startColorVar.b = 0.2f;
  568. _startColorVar.a = 0.1f;
  569. _endColor.r = 0.0f;
  570. _endColor.g = 0.0f;
  571. _endColor.b = 0.0f;
  572. _endColor.a = 1.0f;
  573. _endColorVar.r = 0.0f;
  574. _endColorVar.g = 0.0f;
  575. _endColorVar.b = 0.0f;
  576. _endColorVar.a = 0.0f;
  577. Texture2D* texture = getDefaultTexture();
  578. if (texture != nullptr)
  579. {
  580. setTexture(texture);
  581. }
  582. // additive
  583. this->setBlendAdditive(true);
  584. return true;
  585. }
  586. return false;
  587. }
  588. //
  589. // ParticleSpiral
  590. //
  591. ParticleSpiral* ParticleSpiral::create()
  592. {
  593. ParticleSpiral* ret = new (std::nothrow) ParticleSpiral();
  594. if (ret && ret->init())
  595. {
  596. ret->autorelease();
  597. }
  598. else
  599. {
  600. CC_SAFE_DELETE(ret);
  601. }
  602. return ret;
  603. }
  604. ParticleSpiral* ParticleSpiral::createWithTotalParticles(int numberOfParticles)
  605. {
  606. ParticleSpiral* ret = new (std::nothrow) ParticleSpiral();
  607. if (ret && ret->initWithTotalParticles(numberOfParticles))
  608. {
  609. ret->autorelease();
  610. }
  611. else
  612. {
  613. CC_SAFE_DELETE(ret);
  614. }
  615. return ret;
  616. }
  617. bool ParticleSpiral::initWithTotalParticles(int numberOfParticles)
  618. {
  619. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  620. {
  621. // duration
  622. _duration = DURATION_INFINITY;
  623. // Gravity Mode
  624. setEmitterMode(Mode::GRAVITY);
  625. // Gravity Mode: gravity
  626. setGravity(Vec2(0,0));
  627. // Gravity Mode: speed of particles
  628. setSpeed(150);
  629. setSpeedVar(0);
  630. // Gravity Mode: radial
  631. setRadialAccel(-380);
  632. setRadialAccelVar(0);
  633. // Gravity Mode: tangential
  634. setTangentialAccel(45);
  635. setTangentialAccelVar(0);
  636. // angle
  637. _angle = 90;
  638. _angleVar = 0;
  639. // emitter position
  640. Size winSize = Director::getInstance()->getWinSize();
  641. this->setPosition(winSize.width/2, winSize.height/2);
  642. setPosVar(Vec2::ZERO);
  643. // life of particles
  644. _life = 12;
  645. _lifeVar = 0;
  646. // size, in pixels
  647. _startSize = 20.0f;
  648. _startSizeVar = 0.0f;
  649. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  650. // emits per second
  651. _emissionRate = _totalParticles/_life;
  652. // color of particles
  653. _startColor.r = 0.5f;
  654. _startColor.g = 0.5f;
  655. _startColor.b = 0.5f;
  656. _startColor.a = 1.0f;
  657. _startColorVar.r = 0.5f;
  658. _startColorVar.g = 0.5f;
  659. _startColorVar.b = 0.5f;
  660. _startColorVar.a = 0.0f;
  661. _endColor.r = 0.5f;
  662. _endColor.g = 0.5f;
  663. _endColor.b = 0.5f;
  664. _endColor.a = 1.0f;
  665. _endColorVar.r = 0.5f;
  666. _endColorVar.g = 0.5f;
  667. _endColorVar.b = 0.5f;
  668. _endColorVar.a = 0.0f;
  669. Texture2D* texture = getDefaultTexture();
  670. if (texture != nullptr)
  671. {
  672. setTexture(texture);
  673. }
  674. // additive
  675. this->setBlendAdditive(false);
  676. return true;
  677. }
  678. return false;
  679. }
  680. //
  681. // ParticleExplosion
  682. //
  683. ParticleExplosion* ParticleExplosion::create()
  684. {
  685. ParticleExplosion* ret = new (std::nothrow) ParticleExplosion();
  686. if (ret && ret->init())
  687. {
  688. ret->autorelease();
  689. }
  690. else
  691. {
  692. CC_SAFE_DELETE(ret);
  693. }
  694. return ret;
  695. }
  696. ParticleExplosion* ParticleExplosion::createWithTotalParticles(int numberOfParticles)
  697. {
  698. ParticleExplosion* ret = new (std::nothrow) ParticleExplosion();
  699. if (ret && ret->initWithTotalParticles(numberOfParticles))
  700. {
  701. ret->autorelease();
  702. }
  703. else
  704. {
  705. CC_SAFE_DELETE(ret);
  706. }
  707. return ret;
  708. }
  709. bool ParticleExplosion::initWithTotalParticles(int numberOfParticles)
  710. {
  711. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  712. {
  713. // duration
  714. _duration = 0.1f;
  715. setEmitterMode(Mode::GRAVITY);
  716. // Gravity Mode: gravity
  717. setGravity(Vec2(0,0));
  718. // Gravity Mode: speed of particles
  719. setSpeed(70);
  720. setSpeedVar(40);
  721. // Gravity Mode: radial
  722. setRadialAccel(0);
  723. setRadialAccelVar(0);
  724. // Gravity Mode: tangential
  725. setTangentialAccel(0);
  726. setTangentialAccelVar(0);
  727. // angle
  728. _angle = 90;
  729. _angleVar = 360;
  730. // emitter position
  731. Size winSize = Director::getInstance()->getWinSize();
  732. this->setPosition(winSize.width/2, winSize.height/2);
  733. setPosVar(Vec2::ZERO);
  734. // life of particles
  735. _life = 5.0f;
  736. _lifeVar = 2;
  737. // size, in pixels
  738. _startSize = 15.0f;
  739. _startSizeVar = 10.0f;
  740. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  741. // emits per second
  742. _emissionRate = _totalParticles/_duration;
  743. // color of particles
  744. _startColor.r = 0.7f;
  745. _startColor.g = 0.1f;
  746. _startColor.b = 0.2f;
  747. _startColor.a = 1.0f;
  748. _startColorVar.r = 0.5f;
  749. _startColorVar.g = 0.5f;
  750. _startColorVar.b = 0.5f;
  751. _startColorVar.a = 0.0f;
  752. _endColor.r = 0.5f;
  753. _endColor.g = 0.5f;
  754. _endColor.b = 0.5f;
  755. _endColor.a = 0.0f;
  756. _endColorVar.r = 0.5f;
  757. _endColorVar.g = 0.5f;
  758. _endColorVar.b = 0.5f;
  759. _endColorVar.a = 0.0f;
  760. Texture2D* texture = getDefaultTexture();
  761. if (texture != nullptr)
  762. {
  763. setTexture(texture);
  764. }
  765. // additive
  766. this->setBlendAdditive(false);
  767. return true;
  768. }
  769. return false;
  770. }
  771. //
  772. // ParticleSmoke
  773. //
  774. ParticleSmoke* ParticleSmoke::create()
  775. {
  776. ParticleSmoke* ret = new (std::nothrow) ParticleSmoke();
  777. if (ret && ret->init())
  778. {
  779. ret->autorelease();
  780. }
  781. else
  782. {
  783. CC_SAFE_DELETE(ret);
  784. }
  785. return ret;
  786. }
  787. ParticleSmoke* ParticleSmoke::createWithTotalParticles(int numberOfParticles)
  788. {
  789. ParticleSmoke* ret = new (std::nothrow) ParticleSmoke();
  790. if (ret && ret->initWithTotalParticles(numberOfParticles))
  791. {
  792. ret->autorelease();
  793. }
  794. else
  795. {
  796. CC_SAFE_DELETE(ret);
  797. }
  798. return ret;
  799. }
  800. bool ParticleSmoke::initWithTotalParticles(int numberOfParticles)
  801. {
  802. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  803. {
  804. // duration
  805. _duration = DURATION_INFINITY;
  806. // Emitter mode: Gravity Mode
  807. setEmitterMode(Mode::GRAVITY);
  808. // Gravity Mode: gravity
  809. setGravity(Vec2(0,0));
  810. // Gravity Mode: radial acceleration
  811. setRadialAccel(0);
  812. setRadialAccelVar(0);
  813. // Gravity Mode: speed of particles
  814. setSpeed(25);
  815. setSpeedVar(10);
  816. // angle
  817. _angle = 90;
  818. _angleVar = 5;
  819. // emitter position
  820. Size winSize = Director::getInstance()->getWinSize();
  821. this->setPosition(winSize.width/2, 0);
  822. setPosVar(Vec2(20, 0));
  823. // life of particles
  824. _life = 4;
  825. _lifeVar = 1;
  826. // size, in pixels
  827. _startSize = 60.0f;
  828. _startSizeVar = 10.0f;
  829. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  830. // emits per frame
  831. _emissionRate = _totalParticles/_life;
  832. // color of particles
  833. _startColor.r = 0.8f;
  834. _startColor.g = 0.8f;
  835. _startColor.b = 0.8f;
  836. _startColor.a = 1.0f;
  837. _startColorVar.r = 0.02f;
  838. _startColorVar.g = 0.02f;
  839. _startColorVar.b = 0.02f;
  840. _startColorVar.a = 0.0f;
  841. _endColor.r = 0.0f;
  842. _endColor.g = 0.0f;
  843. _endColor.b = 0.0f;
  844. _endColor.a = 1.0f;
  845. _endColorVar.r = 0.0f;
  846. _endColorVar.g = 0.0f;
  847. _endColorVar.b = 0.0f;
  848. _endColorVar.a = 0.0f;
  849. Texture2D* texture = getDefaultTexture();
  850. if (texture != nullptr)
  851. {
  852. setTexture(texture);
  853. }
  854. // additive
  855. this->setBlendAdditive(false);
  856. return true;
  857. }
  858. return false;
  859. }
  860. //
  861. // ParticleSnow
  862. //
  863. ParticleSnow* ParticleSnow::create()
  864. {
  865. ParticleSnow* ret = new (std::nothrow) ParticleSnow();
  866. if (ret && ret->init())
  867. {
  868. ret->autorelease();
  869. }
  870. else
  871. {
  872. CC_SAFE_DELETE(ret);
  873. }
  874. return ret;
  875. }
  876. ParticleSnow* ParticleSnow::createWithTotalParticles(int numberOfParticles)
  877. {
  878. ParticleSnow* ret = new (std::nothrow) ParticleSnow();
  879. if (ret && ret->initWithTotalParticles(numberOfParticles))
  880. {
  881. ret->autorelease();
  882. }
  883. else
  884. {
  885. CC_SAFE_DELETE(ret);
  886. }
  887. return ret;
  888. }
  889. bool ParticleSnow::initWithTotalParticles(int numberOfParticles)
  890. {
  891. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  892. {
  893. // duration
  894. _duration = DURATION_INFINITY;
  895. // set gravity mode.
  896. setEmitterMode(Mode::GRAVITY);
  897. // Gravity Mode: gravity
  898. setGravity(Vec2(0,-1));
  899. // Gravity Mode: speed of particles
  900. setSpeed(5);
  901. setSpeedVar(1);
  902. // Gravity Mode: radial
  903. setRadialAccel(0);
  904. setRadialAccelVar(1);
  905. // Gravity mode: tangential
  906. setTangentialAccel(0);
  907. setTangentialAccelVar(1);
  908. // emitter position
  909. Size winSize = Director::getInstance()->getWinSize();
  910. this->setPosition(winSize.width/2, winSize.height + 10);
  911. setPosVar(Vec2(winSize.width/2, 0));
  912. // angle
  913. _angle = -90;
  914. _angleVar = 5;
  915. // life of particles
  916. _life = 45;
  917. _lifeVar = 15;
  918. // size, in pixels
  919. _startSize = 10.0f;
  920. _startSizeVar = 5.0f;
  921. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  922. // emits per second
  923. _emissionRate = 10;
  924. // color of particles
  925. _startColor.r = 1.0f;
  926. _startColor.g = 1.0f;
  927. _startColor.b = 1.0f;
  928. _startColor.a = 1.0f;
  929. _startColorVar.r = 0.0f;
  930. _startColorVar.g = 0.0f;
  931. _startColorVar.b = 0.0f;
  932. _startColorVar.a = 0.0f;
  933. _endColor.r = 1.0f;
  934. _endColor.g = 1.0f;
  935. _endColor.b = 1.0f;
  936. _endColor.a = 0.0f;
  937. _endColorVar.r = 0.0f;
  938. _endColorVar.g = 0.0f;
  939. _endColorVar.b = 0.0f;
  940. _endColorVar.a = 0.0f;
  941. Texture2D* texture = getDefaultTexture();
  942. if (texture != nullptr)
  943. {
  944. setTexture(texture);
  945. }
  946. // additive
  947. this->setBlendAdditive(false);
  948. return true;
  949. }
  950. return false;
  951. }
  952. //
  953. // ParticleRain
  954. //
  955. ParticleRain* ParticleRain::create()
  956. {
  957. ParticleRain* ret = new (std::nothrow) ParticleRain();
  958. if (ret && ret->init())
  959. {
  960. ret->autorelease();
  961. }
  962. else
  963. {
  964. CC_SAFE_DELETE(ret);
  965. }
  966. return ret;
  967. }
  968. ParticleRain* ParticleRain::createWithTotalParticles(int numberOfParticles)
  969. {
  970. ParticleRain* ret = new (std::nothrow) ParticleRain();
  971. if (ret && ret->initWithTotalParticles(numberOfParticles))
  972. {
  973. ret->autorelease();
  974. }
  975. else
  976. {
  977. CC_SAFE_DELETE(ret);
  978. }
  979. return ret;
  980. }
  981. bool ParticleRain::initWithTotalParticles(int numberOfParticles)
  982. {
  983. if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
  984. {
  985. // duration
  986. _duration = DURATION_INFINITY;
  987. setEmitterMode(Mode::GRAVITY);
  988. // Gravity Mode: gravity
  989. setGravity(Vec2(10,-10));
  990. // Gravity Mode: radial
  991. setRadialAccel(0);
  992. setRadialAccelVar(1);
  993. // Gravity Mode: tangential
  994. setTangentialAccel(0);
  995. setTangentialAccelVar(1);
  996. // Gravity Mode: speed of particles
  997. setSpeed(130);
  998. setSpeedVar(30);
  999. // angle
  1000. _angle = -90;
  1001. _angleVar = 5;
  1002. // emitter position
  1003. Size winSize = Director::getInstance()->getWinSize();
  1004. this->setPosition(winSize.width/2, winSize.height);
  1005. setPosVar(Vec2(winSize.width/2, 0));
  1006. // life of particles
  1007. _life = 4.5f;
  1008. _lifeVar = 0;
  1009. // size, in pixels
  1010. _startSize = 4.0f;
  1011. _startSizeVar = 2.0f;
  1012. _endSize = START_SIZE_EQUAL_TO_END_SIZE;
  1013. // emits per second
  1014. _emissionRate = 20;
  1015. // color of particles
  1016. _startColor.r = 0.7f;
  1017. _startColor.g = 0.8f;
  1018. _startColor.b = 1.0f;
  1019. _startColor.a = 1.0f;
  1020. _startColorVar.r = 0.0f;
  1021. _startColorVar.g = 0.0f;
  1022. _startColorVar.b = 0.0f;
  1023. _startColorVar.a = 0.0f;
  1024. _endColor.r = 0.7f;
  1025. _endColor.g = 0.8f;
  1026. _endColor.b = 1.0f;
  1027. _endColor.a = 0.5f;
  1028. _endColorVar.r = 0.0f;
  1029. _endColorVar.g = 0.0f;
  1030. _endColorVar.b = 0.0f;
  1031. _endColorVar.a = 0.0f;
  1032. Texture2D* texture = getDefaultTexture();
  1033. if (texture != nullptr)
  1034. {
  1035. setTexture(texture);
  1036. }
  1037. // additive
  1038. this->setBlendAdditive(false);
  1039. return true;
  1040. }
  1041. return false;
  1042. }
  1043. NS_CC_END