CCParticleSystem.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. #ifndef __CCPARTICLE_SYSTEM_H__
  25. #define __CCPARTICLE_SYSTEM_H__
  26. #include "base/CCProtocols.h"
  27. #include "2d/CCNode.h"
  28. #include "base/CCValue.h"
  29. NS_CC_BEGIN
  30. /**
  31. * @addtogroup _2d
  32. * @{
  33. */
  34. class ParticleBatchNode;
  35. /** @struct sParticle
  36. Structure that contains the values of each particle.
  37. */
  38. struct particle_point
  39. {
  40. float x;
  41. float y;
  42. };
  43. class CC_DLL ParticleData
  44. {
  45. public:
  46. float* posx;
  47. float* posy;
  48. float* startPosX;
  49. float* startPosY;
  50. float* colorR;
  51. float* colorG;
  52. float* colorB;
  53. float* colorA;
  54. float* deltaColorR;
  55. float* deltaColorG;
  56. float* deltaColorB;
  57. float* deltaColorA;
  58. float* size;
  59. float* deltaSize;
  60. float* rotation;
  61. float* deltaRotation;
  62. float* timeToLive;
  63. unsigned int* atlasIndex;
  64. //! Mode A: gravity, direction, radial accel, tangential accel
  65. struct{
  66. float* dirX;
  67. float* dirY;
  68. float* radialAccel;
  69. float* tangentialAccel;
  70. } modeA;
  71. //! Mode B: radius mode
  72. struct{
  73. float* angle;
  74. float* degreesPerSecond;
  75. float* radius;
  76. float* deltaRadius;
  77. } modeB;
  78. unsigned int maxCount;
  79. ParticleData();
  80. bool init(int count);
  81. void release();
  82. unsigned int getMaxCount() { return maxCount; }
  83. void copyParticle(int p1, int p2)
  84. {
  85. posx[p1] = posx[p2];
  86. posy[p1] = posy[p2];
  87. startPosX[p1] = startPosX[p2];
  88. startPosY[p1] = startPosY[p2];
  89. colorR[p1] = colorR[p2];
  90. colorG[p1] = colorG[p2];
  91. colorB[p1] = colorB[p2];
  92. colorA[p1] = colorA[p2];
  93. deltaColorR[p1] = deltaColorR[p2];
  94. deltaColorG[p1] = deltaColorG[p2];
  95. deltaColorB[p1] = deltaColorB[p2];
  96. deltaColorA[p1] = deltaColorA[p2];
  97. size[p1] = size[p2];
  98. deltaSize[p1] = deltaSize[p2];
  99. rotation[p1] = rotation[p2];
  100. deltaRotation[p1] = deltaRotation[p2];
  101. timeToLive[p1] = timeToLive[p2];
  102. atlasIndex[p1] = atlasIndex[p2];
  103. modeA.dirX[p1] = modeA.dirX[p2];
  104. modeA.dirY[p1] = modeA.dirY[p2];
  105. modeA.radialAccel[p1] = modeA.radialAccel[p2];
  106. modeA.tangentialAccel[p1] = modeA.tangentialAccel[p2];
  107. modeB.angle[p1] = modeB.angle[p2];
  108. modeB.degreesPerSecond[p1] = modeB.degreesPerSecond[p2];
  109. modeB.radius[p1] = modeB.radius[p2];
  110. modeB.deltaRadius[p1] = modeB.deltaRadius[p2];
  111. }
  112. };
  113. //typedef void (*CC_UPDATE_PARTICLE_IMP)(id, SEL, tParticle*, Vec2);
  114. class Texture2D;
  115. /** @class ParticleSystem
  116. * @brief Particle System base class.
  117. Attributes of a Particle System:
  118. - emission rate of the particles
  119. - Gravity Mode (Mode A):
  120. - gravity
  121. - direction
  122. - speed +- variance
  123. - tangential acceleration +- variance
  124. - radial acceleration +- variance
  125. - Radius Mode (Mode B):
  126. - startRadius +- variance
  127. - endRadius +- variance
  128. - rotate +- variance
  129. - Properties common to all modes:
  130. - life +- life variance
  131. - start spin +- variance
  132. - end spin +- variance
  133. - start size +- variance
  134. - end size +- variance
  135. - start color +- variance
  136. - end color +- variance
  137. - life +- variance
  138. - blending function
  139. - texture
  140. Cocos2d also supports particles generated by Particle Designer (http://particledesigner.71squared.com/).
  141. 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
  142. cocos2d uses a another approach, but the results are almost identical.
  143. Cocos2d supports all the variables used by Particle Designer plus a bit more:
  144. - spinning particles (supported when using ParticleSystemQuad)
  145. - tangential acceleration (Gravity mode)
  146. - radial acceleration (Gravity mode)
  147. - radius direction (Radius mode) (Particle Designer supports outwards to inwards direction only)
  148. It is possible to customize any of the above mentioned properties in runtime. Example:
  149. @code
  150. emitter.radialAccel = 15;
  151. emitter.startSpin = 0;
  152. @endcode
  153. */
  154. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  155. #ifdef RELATIVE
  156. #undef RELATIVE
  157. #endif
  158. #endif
  159. class CC_DLL ParticleSystem : public Node, public TextureProtocol, public PlayableProtocol
  160. {
  161. public:
  162. /** Mode
  163. * @js cc.ParticleSystem.MODE_GRAVITY;
  164. */
  165. enum class Mode
  166. {
  167. GRAVITY,
  168. RADIUS,
  169. };
  170. /** PositionType
  171. Possible types of particle positions.
  172. * @js cc.ParticleSystem.TYPE_FREE
  173. */
  174. enum class PositionType
  175. {
  176. FREE, /** Living particles are attached to the world and are unaffected by emitter repositioning. */
  177. RELATIVE, /** Living particles are attached to the world but will follow the emitter repositioning.
  178. Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.*/
  179. GROUPED, /** Living particles are attached to the emitter and are translated along with it. */
  180. };
  181. //* @enum
  182. enum {
  183. /** The Particle emitter lives forever. */
  184. DURATION_INFINITY = -1,
  185. /** The starting size of the particle is equal to the ending size. */
  186. START_SIZE_EQUAL_TO_END_SIZE = -1,
  187. /** The starting radius of the particle is equal to the ending radius. */
  188. START_RADIUS_EQUAL_TO_END_RADIUS = -1,
  189. };
  190. /** Creates an initializes a ParticleSystem from a plist file.
  191. This plist files can be created manually or with Particle Designer:
  192. http://particledesigner.71squared.com/
  193. @since v2.0
  194. *
  195. * @param plistFile Particle plist file name.
  196. * @return An autoreleased ParticleSystem object.
  197. */
  198. static ParticleSystem * create(const std::string& plistFile);
  199. /** Create a system with a fixed number of particles.
  200. *
  201. * @param numberOfParticles A given number of particles.
  202. * @return An autoreleased ParticleSystemQuad object.
  203. * @js NA
  204. */
  205. static ParticleSystem* createWithTotalParticles(int numberOfParticles);
  206. /** Gets all ParticleSystem references
  207. */
  208. static Vector<ParticleSystem*>& getAllParticleSystems();
  209. public:
  210. void addParticles(int count);
  211. void stopSystem();
  212. /** Kill all living particles.
  213. */
  214. void resetSystem();
  215. /** Whether or not the system is full.
  216. *
  217. * @return True if the system is full.
  218. */
  219. bool isFull();
  220. /** Update the verts position data of particle,
  221. should be overridden by subclasses.
  222. */
  223. virtual void updateParticleQuads();
  224. /** Update the VBO verts buffer which does not use batch node,
  225. should be overridden by subclasses. */
  226. virtual void postStep();
  227. /** Call the update method with no time..
  228. */
  229. virtual void updateWithNoTime();
  230. /** Whether or not the particle system removed self on finish.
  231. *
  232. * @return True if the particle system removed self on finish.
  233. */
  234. virtual bool isAutoRemoveOnFinish() const;
  235. /** Set the particle system auto removed it self on finish.
  236. *
  237. * @param var True if the particle system removed self on finish.
  238. */
  239. virtual void setAutoRemoveOnFinish(bool var);
  240. // mode A
  241. /** Gets the gravity.
  242. *
  243. * @return The gravity.
  244. */
  245. virtual const Vec2& getGravity();
  246. /** Sets the gravity.
  247. *
  248. * @param g The gravity.
  249. */
  250. virtual void setGravity(const Vec2& g);
  251. /** Gets the speed.
  252. *
  253. * @return The speed.
  254. */
  255. virtual float getSpeed() const;
  256. /** Sets the speed.
  257. *
  258. * @param speed The speed.
  259. */
  260. virtual void setSpeed(float speed);
  261. /** Gets the speed variance.
  262. *
  263. * @return The speed variance.
  264. */
  265. virtual float getSpeedVar() const;
  266. /** Sets the speed variance.
  267. *
  268. * @param speed The speed variance.
  269. */
  270. virtual void setSpeedVar(float speed);
  271. /** Gets the tangential acceleration.
  272. *
  273. * @return The tangential acceleration.
  274. */
  275. virtual float getTangentialAccel() const;
  276. /** Sets the tangential acceleration.
  277. *
  278. * @param t The tangential acceleration.
  279. */
  280. virtual void setTangentialAccel(float t);
  281. /** Gets the tangential acceleration variance.
  282. *
  283. * @return The tangential acceleration variance.
  284. */
  285. virtual float getTangentialAccelVar() const;
  286. /** Sets the tangential acceleration variance.
  287. *
  288. * @param t The tangential acceleration variance.
  289. */
  290. virtual void setTangentialAccelVar(float t);
  291. /** Gets the radial acceleration.
  292. *
  293. * @return The radial acceleration.
  294. */
  295. virtual float getRadialAccel() const;
  296. /** Sets the radial acceleration.
  297. *
  298. * @param t The radial acceleration.
  299. */
  300. virtual void setRadialAccel(float t);
  301. /** Gets the radial acceleration variance.
  302. *
  303. * @return The radial acceleration variance.
  304. */
  305. virtual float getRadialAccelVar() const;
  306. /** Sets the radial acceleration variance.
  307. *
  308. * @param t The radial acceleration variance.
  309. */
  310. virtual void setRadialAccelVar(float t);
  311. /** Whether or not the rotation of each particle to its direction.
  312. *
  313. * @return True if the rotation is the direction.
  314. */
  315. virtual bool getRotationIsDir() const;
  316. /** Sets the rotation of each particle to its direction.
  317. *
  318. * @param t True if the rotation is the direction.
  319. */
  320. virtual void setRotationIsDir(bool t);
  321. // mode B
  322. /** Gets the start radius.
  323. *
  324. * @return The start radius.
  325. */
  326. virtual float getStartRadius() const;
  327. /** Sets the start radius.
  328. *
  329. * @param startRadius The start radius.
  330. */
  331. virtual void setStartRadius(float startRadius);
  332. /** Gets the start radius variance.
  333. *
  334. * @return The start radius variance.
  335. */
  336. virtual float getStartRadiusVar() const;
  337. /** Sets the start radius variance.
  338. *
  339. * @param startRadiusVar The start radius variance.
  340. */
  341. virtual void setStartRadiusVar(float startRadiusVar);
  342. /** Gets the end radius.
  343. *
  344. * @return The end radius.
  345. */
  346. virtual float getEndRadius() const;
  347. /** Sets the end radius.
  348. *
  349. * @param endRadius The end radius.
  350. */
  351. virtual void setEndRadius(float endRadius);
  352. /** Gets the end radius variance.
  353. *
  354. * @return The end radius variance.
  355. */
  356. virtual float getEndRadiusVar() const;
  357. /** Sets the end radius variance.
  358. *
  359. * @param endRadiusVar The end radius variance.
  360. */
  361. virtual void setEndRadiusVar(float endRadiusVar);
  362. /** Gets the number of degrees to rotate a particle around the source pos per second.
  363. *
  364. * @return The number of degrees to rotate a particle around the source pos per second.
  365. */
  366. virtual float getRotatePerSecond() const;
  367. /** Sets the number of degrees to rotate a particle around the source pos per second.
  368. *
  369. * @param degrees The number of degrees to rotate a particle around the source pos per second.
  370. */
  371. virtual void setRotatePerSecond(float degrees);
  372. /** Gets the rotate per second variance.
  373. *
  374. * @return The rotate per second variance.
  375. */
  376. virtual float getRotatePerSecondVar() const;
  377. /** Sets the rotate per second variance.
  378. *
  379. * @param degrees The rotate per second variance.
  380. */
  381. virtual void setRotatePerSecondVar(float degrees);
  382. virtual void setScale(float s) override;
  383. virtual void setRotation(float newRotation) override;
  384. virtual void setScaleX(float newScaleX) override;
  385. virtual void setScaleY(float newScaleY) override;
  386. /** Whether or not the particle system is active.
  387. *
  388. * @return True if the particle system is active.
  389. */
  390. virtual bool isActive() const;
  391. /** Whether or not the particle system is blend additive.
  392. *
  393. * @return True if the particle system is blend additive.
  394. */
  395. virtual bool isBlendAdditive() const;
  396. /** Sets the particle system blend additive.
  397. *
  398. * @param value True if the particle system is blend additive.
  399. */
  400. virtual void setBlendAdditive(bool value);
  401. /** Gets the batch node.
  402. *
  403. * @return The batch node.
  404. */
  405. virtual ParticleBatchNode* getBatchNode() const;
  406. /** Sets the batch node.
  407. *
  408. * @param batchNode The batch node.
  409. */
  410. virtual void setBatchNode(ParticleBatchNode* batchNode);
  411. /** Gets the index of system in batch node array.
  412. *
  413. * @return The index of system in batch node array.
  414. */
  415. int getAtlasIndex() const { return _atlasIndex; }
  416. /** Sets the index of system in batch node array.
  417. *
  418. * @param index The index of system in batch node array.
  419. */
  420. void setAtlasIndex(int index) { _atlasIndex = index; }
  421. /** Gets the Quantity of particles that are being simulated at the moment.
  422. *
  423. * @return The Quantity of particles that are being simulated at the moment.
  424. */
  425. unsigned int getParticleCount() const { return _particleCount; }
  426. /** Gets how many seconds the emitter will run. -1 means 'forever'.
  427. *
  428. * @return The seconds that the emitter will run. -1 means 'forever'.
  429. */
  430. float getDuration() const { return _duration; }
  431. /** Sets how many seconds the emitter will run. -1 means 'forever'.
  432. *
  433. * @param duration The seconds that the emitter will run. -1 means 'forever'.
  434. */
  435. void setDuration(float duration) { _duration = duration; }
  436. /** Gets the source position of the emitter.
  437. *
  438. * @return The source position of the emitter.
  439. */
  440. const Vec2& getSourcePosition() const { return _sourcePosition; }
  441. /** Sets the source position of the emitter.
  442. *
  443. * @param pos The source position of the emitter.
  444. */
  445. void setSourcePosition(const Vec2& pos) { _sourcePosition = pos; }
  446. /** Gets the position variance of the emitter.
  447. *
  448. * @return The position variance of the emitter.
  449. */
  450. const Vec2& getPosVar() const { return _posVar; }
  451. /** Sets the position variance of the emitter.
  452. *
  453. * @param pos The position variance of the emitter.
  454. */
  455. void setPosVar(const Vec2& pos) { _posVar = pos; }
  456. /** Gets the life of each particle.
  457. *
  458. * @return The life of each particle.
  459. */
  460. float getLife() const { return _life; }
  461. /** Sets the life of each particle.
  462. *
  463. * @param life The life of each particle.
  464. */
  465. void setLife(float life) { _life = life; }
  466. /** Gets the life variance of each particle.
  467. *
  468. * @return The life variance of each particle.
  469. */
  470. float getLifeVar() const { return _lifeVar; }
  471. /** Sets the life variance of each particle.
  472. *
  473. * @param lifeVar The life variance of each particle.
  474. */
  475. void setLifeVar(float lifeVar) { _lifeVar = lifeVar; }
  476. /** Gets the angle of each particle.
  477. *
  478. * @return The angle of each particle.
  479. */
  480. float getAngle() const { return _angle; }
  481. /** Sets the angle of each particle.
  482. *
  483. * @param angle The angle of each particle.
  484. */
  485. void setAngle(float angle) { _angle = angle; }
  486. /** Gets the angle variance of each particle.
  487. *
  488. * @return The angle variance of each particle.
  489. */
  490. float getAngleVar() const { return _angleVar; }
  491. /** Sets the angle variance of each particle.
  492. *
  493. * @param angleVar The angle variance of each particle.
  494. */
  495. void setAngleVar(float angleVar) { _angleVar = angleVar; }
  496. /** Switch between different kind of emitter modes:
  497. - kParticleModeGravity: uses gravity, speed, radial and tangential acceleration.
  498. - kParticleModeRadius: uses radius movement + rotation.
  499. *
  500. * @return The mode of the emitter.
  501. */
  502. Mode getEmitterMode() const { return _emitterMode; }
  503. /** Sets the mode of the emitter.
  504. *
  505. * @param mode The mode of the emitter.
  506. */
  507. void setEmitterMode(Mode mode) { _emitterMode = mode; }
  508. /** Gets the start size in pixels of each particle.
  509. *
  510. * @return The start size in pixels of each particle.
  511. */
  512. float getStartSize() const { return _startSize; }
  513. /** Sets the start size in pixels of each particle.
  514. *
  515. * @param startSize The start size in pixels of each particle.
  516. */
  517. void setStartSize(float startSize) { _startSize = startSize; }
  518. /** Gets the start size variance in pixels of each particle.
  519. *
  520. * @return The start size variance in pixels of each particle.
  521. */
  522. float getStartSizeVar() const { return _startSizeVar; }
  523. /** Sets the start size variance in pixels of each particle.
  524. *
  525. * @param sizeVar The start size variance in pixels of each particle.
  526. */
  527. void setStartSizeVar(float sizeVar) { _startSizeVar = sizeVar; }
  528. /** Gets the end size in pixels of each particle.
  529. *
  530. * @return The end size in pixels of each particle.
  531. */
  532. float getEndSize() const { return _endSize; }
  533. /** Sets the end size in pixels of each particle.
  534. *
  535. * @param endSize The end size in pixels of each particle.
  536. */
  537. void setEndSize(float endSize) { _endSize = endSize; }
  538. /** Gets the end size variance in pixels of each particle.
  539. *
  540. * @return The end size variance in pixels of each particle.
  541. */
  542. float getEndSizeVar() const { return _endSizeVar; }
  543. /** Sets the end size variance in pixels of each particle.
  544. *
  545. * @param sizeVar The end size variance in pixels of each particle.
  546. */
  547. void setEndSizeVar(float sizeVar) { _endSizeVar = sizeVar; }
  548. /** Gets the start color of each particle.
  549. *
  550. * @return The start color of each particle.
  551. */
  552. const Color4F& getStartColor() const { return _startColor; }
  553. /** Sets the start color of each particle.
  554. *
  555. * @param color The start color of each particle.
  556. */
  557. void setStartColor(const Color4F& color) { _startColor = color; }
  558. /** Gets the start color variance of each particle.
  559. *
  560. * @return The start color variance of each particle.
  561. */
  562. const Color4F& getStartColorVar() const { return _startColorVar; }
  563. /** Sets the start color variance of each particle.
  564. *
  565. * @param color The start color variance of each particle.
  566. */
  567. void setStartColorVar(const Color4F& color) { _startColorVar = color; }
  568. /** Gets the end color and end color variation of each particle.
  569. *
  570. * @return The end color and end color variation of each particle.
  571. */
  572. const Color4F& getEndColor() const { return _endColor; }
  573. /** Sets the end color and end color variation of each particle.
  574. *
  575. * @param color The end color and end color variation of each particle.
  576. */
  577. void setEndColor(const Color4F& color) { _endColor = color; }
  578. /** Gets the end color variance of each particle.
  579. *
  580. * @return The end color variance of each particle.
  581. */
  582. const Color4F& getEndColorVar() const { return _endColorVar; }
  583. /** Sets the end color variance of each particle.
  584. *
  585. * @param color The end color variance of each particle.
  586. */
  587. void setEndColorVar(const Color4F& color) { _endColorVar = color; }
  588. /** Gets the start spin of each particle.
  589. *
  590. * @return The start spin of each particle.
  591. */
  592. float getStartSpin() const { return _startSpin; }
  593. /** Sets the start spin of each particle.
  594. *
  595. * @param spin The start spin of each particle.
  596. */
  597. void setStartSpin(float spin) { _startSpin = spin; }
  598. /** Gets the start spin variance of each particle.
  599. *
  600. * @return The start spin variance of each particle.
  601. */
  602. float getStartSpinVar() const { return _startSpinVar; }
  603. /** Sets the start spin variance of each particle.
  604. *
  605. * @param pinVar The start spin variance of each particle.
  606. */
  607. void setStartSpinVar(float pinVar) { _startSpinVar = pinVar; }
  608. /** Gets the end spin of each particle.
  609. *
  610. * @return The end spin of each particle.
  611. */
  612. float getEndSpin() const { return _endSpin; }
  613. /** Sets the end spin of each particle.
  614. *
  615. * @param endSpin The end spin of each particle.
  616. */
  617. void setEndSpin(float endSpin) { _endSpin = endSpin; }
  618. /** Gets the end spin variance of each particle.
  619. *
  620. * @return The end spin variance of each particle.
  621. */
  622. float getEndSpinVar() const { return _endSpinVar; }
  623. /** Sets the end spin variance of each particle.
  624. *
  625. * @param endSpinVar The end spin variance of each particle.
  626. */
  627. void setEndSpinVar(float endSpinVar) { _endSpinVar = endSpinVar; }
  628. /** Gets the emission rate of the particles.
  629. *
  630. * @return The emission rate of the particles.
  631. */
  632. float getEmissionRate() const { return _emissionRate; }
  633. /** Sets the emission rate of the particles.
  634. *
  635. * @param rate The emission rate of the particles.
  636. */
  637. void setEmissionRate(float rate) { _emissionRate = rate; }
  638. /** Gets the maximum particles of the system.
  639. *
  640. * @return The maximum particles of the system.
  641. */
  642. virtual int getTotalParticles() const;
  643. /** Sets the maximum particles of the system.
  644. *
  645. * @param totalParticles The maximum particles of the system.
  646. */
  647. virtual void setTotalParticles(int totalParticles);
  648. /** does the alpha value modify color */
  649. void setOpacityModifyRGB(bool opacityModifyRGB) override { _opacityModifyRGB = opacityModifyRGB; }
  650. bool isOpacityModifyRGB() const override { return _opacityModifyRGB; }
  651. CC_DEPRECATED_ATTRIBUTE bool getOpacityModifyRGB() const { return isOpacityModifyRGB(); }
  652. /** Gets the particles movement type: Free or Grouped.
  653. @since v0.8
  654. *
  655. * @return The particles movement type.
  656. */
  657. PositionType getPositionType() const { return _positionType; }
  658. /** Sets the particles movement type: Free or Grouped.
  659. @since v0.8
  660. *
  661. * @param type The particles movement type.
  662. */
  663. void setPositionType(PositionType type) { _positionType = type; }
  664. // Overrides
  665. virtual void onEnter() override;
  666. virtual void onExit() override;
  667. virtual void update(float dt) override;
  668. virtual Texture2D* getTexture() const override;
  669. virtual void setTexture(Texture2D *texture) override;
  670. /**
  671. *@code
  672. *When this function bound into js or lua,the parameter will be changed
  673. *In js: var setBlendFunc(var src, var dst)
  674. *In lua: local setBlendFunc(local src, local dst)
  675. *@endcode
  676. */
  677. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  678. /**
  679. * @js NA
  680. * @lua NA
  681. */
  682. virtual const BlendFunc &getBlendFunc() const override;
  683. const std::string& getResourceFile() const { return _plistFile; }
  684. /// @{
  685. /// @name implement Playable Protocol
  686. virtual void start() override;
  687. virtual void stop() override;
  688. /// @} end of PlayableProtocol
  689. void setSourcePositionCompatible(bool sourcePositionCompatible) { _sourcePositionCompatible = sourcePositionCompatible; }
  690. bool isSourcePositionCompatible() const { return _sourcePositionCompatible; }
  691. CC_CONSTRUCTOR_ACCESS:
  692. /**
  693. * @js ctor
  694. */
  695. ParticleSystem();
  696. /**
  697. * @js NA
  698. * @lua NA
  699. */
  700. virtual ~ParticleSystem();
  701. /** initializes a ParticleSystem*/
  702. bool init() override;
  703. /** initializes a ParticleSystem from a plist file.
  704. This plist files can be created manually or with Particle Designer:
  705. http://particledesigner.71squared.com/
  706. @since v0.99.3
  707. */
  708. bool initWithFile(const std::string& plistFile);
  709. /** initializes a QuadParticleSystem from a Dictionary.
  710. @since v0.99.3
  711. */
  712. bool initWithDictionary(ValueMap& dictionary);
  713. /** initializes a particle system from a NSDictionary and the path from where to load the png
  714. @since v2.1
  715. */
  716. bool initWithDictionary(ValueMap& dictionary, const std::string& dirname);
  717. //! Initializes a system with a fixed number of particles
  718. virtual bool initWithTotalParticles(int numberOfParticles);
  719. /** Are the emissions paused
  720. @return True if the emissions are paused, else false
  721. */
  722. virtual bool isPaused() const;
  723. /* Pause the emissions*/
  724. virtual void pauseEmissions();
  725. /* UnPause the emissions*/
  726. virtual void resumeEmissions();
  727. protected:
  728. virtual void updateBlendFunc();
  729. private:
  730. friend class EngineDataManager;
  731. /** Internal use only, it's used by EngineDataManager class for Android platform */
  732. static void setTotalParticleCountFactor(float factor);
  733. protected:
  734. /** whether or not the particles are using blend additive.
  735. If enabled, the following blending function will be used.
  736. @code
  737. source blend function = GL_SRC_ALPHA;
  738. dest blend function = GL_ONE;
  739. @endcode
  740. */
  741. bool _isBlendAdditive;
  742. /** whether or not the node will be auto-removed when it has no particles left.
  743. By default it is false.
  744. @since v0.8
  745. */
  746. bool _isAutoRemoveOnFinish;
  747. std::string _plistFile;
  748. //! time elapsed since the start of the system (in seconds)
  749. float _elapsed;
  750. // Different modes
  751. //! Mode A:Gravity + Tangential Accel + Radial Accel
  752. struct {
  753. /** Gravity value. Only available in 'Gravity' mode. */
  754. Vec2 gravity;
  755. /** speed of each particle. Only available in 'Gravity' mode. */
  756. float speed;
  757. /** speed variance of each particle. Only available in 'Gravity' mode. */
  758. float speedVar;
  759. /** tangential acceleration of each particle. Only available in 'Gravity' mode. */
  760. float tangentialAccel;
  761. /** tangential acceleration variance of each particle. Only available in 'Gravity' mode. */
  762. float tangentialAccelVar;
  763. /** radial acceleration of each particle. Only available in 'Gravity' mode. */
  764. float radialAccel;
  765. /** radial acceleration variance of each particle. Only available in 'Gravity' mode. */
  766. float radialAccelVar;
  767. /** set the rotation of each particle to its direction Only available in 'Gravity' mode. */
  768. bool rotationIsDir;
  769. } modeA;
  770. //! Mode B: circular movement (gravity, radial accel and tangential accel don't are not used in this mode)
  771. struct {
  772. /** The starting radius of the particles. Only available in 'Radius' mode. */
  773. float startRadius;
  774. /** The starting radius variance of the particles. Only available in 'Radius' mode. */
  775. float startRadiusVar;
  776. /** The ending radius of the particles. Only available in 'Radius' mode. */
  777. float endRadius;
  778. /** The ending radius variance of the particles. Only available in 'Radius' mode. */
  779. float endRadiusVar;
  780. /** Number of degrees to rotate a particle around the source pos per second. Only available in 'Radius' mode. */
  781. float rotatePerSecond;
  782. /** Variance in degrees for rotatePerSecond. Only available in 'Radius' mode. */
  783. float rotatePerSecondVar;
  784. } modeB;
  785. //particle data
  786. ParticleData _particleData;
  787. //Emitter name
  788. std::string _configName;
  789. // color modulate
  790. // BOOL colorModulate;
  791. //! How many particles can be emitted per second
  792. float _emitCounter;
  793. // Optimization
  794. //CC_UPDATE_PARTICLE_IMP updateParticleImp;
  795. //SEL updateParticleSel;
  796. /** weak reference to the SpriteBatchNode that renders the Sprite */
  797. ParticleBatchNode* _batchNode;
  798. // index of system in batch node array
  799. int _atlasIndex;
  800. //true if scaled or rotated
  801. bool _transformSystemDirty;
  802. // Number of allocated particles
  803. int _allocatedParticles;
  804. /** Is the emitter active */
  805. bool _isActive;
  806. /** Quantity of particles that are being simulated at the moment */
  807. int _particleCount;
  808. /** The factor affects the total particle count, its value should be 0.0f ~ 1.0f, default 1.0f*/
  809. static float __totalParticleCountFactor;
  810. /** How many seconds the emitter will run. -1 means 'forever' */
  811. float _duration;
  812. /** sourcePosition of the emitter */
  813. Vec2 _sourcePosition;
  814. /** Position variance of the emitter */
  815. Vec2 _posVar;
  816. /** life, and life variation of each particle */
  817. float _life;
  818. /** life variance of each particle */
  819. float _lifeVar;
  820. /** angle and angle variation of each particle */
  821. float _angle;
  822. /** angle variance of each particle */
  823. float _angleVar;
  824. /** Switch between different kind of emitter modes:
  825. - kParticleModeGravity: uses gravity, speed, radial and tangential acceleration
  826. - kParticleModeRadius: uses radius movement + rotation
  827. */
  828. Mode _emitterMode;
  829. /** start size in pixels of each particle */
  830. float _startSize;
  831. /** size variance in pixels of each particle */
  832. float _startSizeVar;
  833. /** end size in pixels of each particle */
  834. float _endSize;
  835. /** end size variance in pixels of each particle */
  836. float _endSizeVar;
  837. /** start color of each particle */
  838. Color4F _startColor;
  839. /** start color variance of each particle */
  840. Color4F _startColorVar;
  841. /** end color and end color variation of each particle */
  842. Color4F _endColor;
  843. /** end color variance of each particle */
  844. Color4F _endColorVar;
  845. //* initial angle of each particle
  846. float _startSpin;
  847. //* initial angle of each particle
  848. float _startSpinVar;
  849. //* initial angle of each particle
  850. float _endSpin;
  851. //* initial angle of each particle
  852. float _endSpinVar;
  853. /** emission rate of the particles */
  854. float _emissionRate;
  855. /** maximum particles of the system */
  856. int _totalParticles;
  857. /** conforms to CocosNodeTexture protocol */
  858. Texture2D* _texture;
  859. /** conforms to CocosNodeTexture protocol */
  860. BlendFunc _blendFunc;
  861. /** does the alpha value modify color */
  862. bool _opacityModifyRGB;
  863. /** does FlippedY variance of each particle */
  864. int _yCoordFlipped;
  865. /** particles movement type: Free or Grouped
  866. @since v0.8
  867. */
  868. PositionType _positionType;
  869. /** is the emitter paused */
  870. bool _paused;
  871. /** is sourcePosition compatible */
  872. bool _sourcePositionCompatible;
  873. static Vector<ParticleSystem*> __allInstances;
  874. private:
  875. CC_DISALLOW_COPY_AND_ASSIGN(ParticleSystem);
  876. };
  877. // end of _2d group
  878. /// @}
  879. NS_CC_END
  880. #endif //__CCPARTICLE_SYSTEM_H__