CCParticleExamples.cpp 29 KB

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