CCParticleSystem.h 33 KB

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