CCParticleSystem.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. // ideas taken from:
  25. // . The ocean spray in your face [Jeff Lander]
  26. // http://www.double.co.nz/dust/col0798.pdf
  27. // . Building an Advanced Particle System [John van der Burg]
  28. // http://www.gamasutra.com/features/20000623/vanderburg_01.htm
  29. // . LOVE game engine
  30. // http://love2d.org/
  31. //
  32. //
  33. // Radius mode support, from 71 squared
  34. // http://particledesigner.71squared.com/
  35. //
  36. // IMPORTANT: Particle Designer is supported by cocos2d, but
  37. // 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
  38. // cocos2d uses a another approach, but the results are almost identical.
  39. //
  40. #include "2d/CCParticleSystem.h"
  41. #include <string>
  42. #include "2d/CCParticleBatchNode.h"
  43. #include "renderer/CCTextureAtlas.h"
  44. #include "base/base64.h"
  45. #include "base/ZipUtils.h"
  46. #include "base/CCDirector.h"
  47. #include "base/CCProfiling.h"
  48. #include "base/ccUTF8.h"
  49. #include "renderer/CCTextureCache.h"
  50. #include "platform/CCFileUtils.h"
  51. using namespace std;
  52. NS_CC_BEGIN
  53. // ideas taken from:
  54. // . The ocean spray in your face [Jeff Lander]
  55. // http://www.double.co.nz/dust/col0798.pdf
  56. // . Building an Advanced Particle System [John van der Burg]
  57. // http://www.gamasutra.com/features/20000623/vanderburg_01.htm
  58. // . LOVE game engine
  59. // http://love2d.org/
  60. //
  61. //
  62. // Radius mode support, from 71 squared
  63. // http://particledesigner.71squared.com/
  64. //
  65. // IMPORTANT: Particle Designer is supported by cocos2d, but
  66. // 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
  67. // cocos2d uses a another approach, but the results are almost identical.
  68. //
  69. inline void normalize_point(float x, float y, particle_point* out)
  70. {
  71. float n = x * x + y * y;
  72. // Already normalized.
  73. if (n == 1.0f)
  74. return;
  75. n = sqrt(n);
  76. // Too close to zero.
  77. if (n < MATH_TOLERANCE)
  78. return;
  79. n = 1.0f / n;
  80. out->x = x * n;
  81. out->y = y * n;
  82. }
  83. /**
  84. A more effect random number getter function, get from ejoy2d.
  85. */
  86. inline static float RANDOM_M11(unsigned int *seed) {
  87. *seed = *seed * 134775813 + 1;
  88. union {
  89. uint32_t d;
  90. float f;
  91. } u;
  92. u.d = (((uint32_t)(*seed) & 0x7fff) << 8) | 0x40000000;
  93. return u.f - 3.0f;
  94. }
  95. ParticleData::ParticleData()
  96. {
  97. memset(this, 0, sizeof(ParticleData));
  98. }
  99. bool ParticleData::init(int count)
  100. {
  101. maxCount = count;
  102. posx= (float*)malloc(count * sizeof(float));
  103. posy= (float*)malloc(count * sizeof(float));
  104. startPosX= (float*)malloc(count * sizeof(float));
  105. startPosY= (float*)malloc(count * sizeof(float));
  106. colorR= (float*)malloc(count * sizeof(float));
  107. colorG= (float*)malloc(count * sizeof(float));
  108. colorB= (float*)malloc(count * sizeof(float));
  109. colorA= (float*)malloc(count * sizeof(float));
  110. deltaColorR= (float*)malloc(count * sizeof(float));
  111. deltaColorG= (float*)malloc(count * sizeof(float));
  112. deltaColorB= (float*)malloc(count * sizeof(float));
  113. deltaColorA= (float*)malloc(count * sizeof(float));
  114. size= (float*)malloc(count * sizeof(float));
  115. deltaSize= (float*)malloc(count * sizeof(float));
  116. rotation= (float*)malloc(count * sizeof(float));
  117. deltaRotation= (float*)malloc(count * sizeof(float));
  118. timeToLive= (float*)malloc(count * sizeof(float));
  119. atlasIndex= (unsigned int*)malloc(count * sizeof(unsigned int));
  120. modeA.dirX= (float*)malloc(count * sizeof(float));
  121. modeA.dirY= (float*)malloc(count * sizeof(float));
  122. modeA.radialAccel= (float*)malloc(count * sizeof(float));
  123. modeA.tangentialAccel= (float*)malloc(count * sizeof(float));
  124. modeB.angle= (float*)malloc(count * sizeof(float));
  125. modeB.degreesPerSecond= (float*)malloc(count * sizeof(float));
  126. modeB.deltaRadius= (float*)malloc(count * sizeof(float));
  127. modeB.radius= (float*)malloc(count * sizeof(float));
  128. return posx && posy && startPosY && startPosX && colorR && colorG && colorB && colorA &&
  129. deltaColorR && deltaColorG && deltaColorB && deltaColorA && size && deltaSize &&
  130. rotation && deltaRotation && timeToLive && atlasIndex && modeA.dirX && modeA.dirY &&
  131. modeA.radialAccel && modeA.tangentialAccel && modeB.angle && modeB.degreesPerSecond &&
  132. modeB.deltaRadius && modeB.radius;
  133. }
  134. void ParticleData::release()
  135. {
  136. CC_SAFE_FREE(posx);
  137. CC_SAFE_FREE(posy);
  138. CC_SAFE_FREE(startPosX);
  139. CC_SAFE_FREE(startPosY);
  140. CC_SAFE_FREE(colorR);
  141. CC_SAFE_FREE(colorG);
  142. CC_SAFE_FREE(colorB);
  143. CC_SAFE_FREE(colorA);
  144. CC_SAFE_FREE(deltaColorR);
  145. CC_SAFE_FREE(deltaColorG);
  146. CC_SAFE_FREE(deltaColorB);
  147. CC_SAFE_FREE(deltaColorA);
  148. CC_SAFE_FREE(size);
  149. CC_SAFE_FREE(deltaSize);
  150. CC_SAFE_FREE(rotation);
  151. CC_SAFE_FREE(deltaRotation);
  152. CC_SAFE_FREE(timeToLive);
  153. CC_SAFE_FREE(atlasIndex);
  154. CC_SAFE_FREE(modeA.dirX);
  155. CC_SAFE_FREE(modeA.dirY);
  156. CC_SAFE_FREE(modeA.radialAccel);
  157. CC_SAFE_FREE(modeA.tangentialAccel);
  158. CC_SAFE_FREE(modeB.angle);
  159. CC_SAFE_FREE(modeB.degreesPerSecond);
  160. CC_SAFE_FREE(modeB.deltaRadius);
  161. CC_SAFE_FREE(modeB.radius);
  162. }
  163. Vector<ParticleSystem*> ParticleSystem::__allInstances;
  164. float ParticleSystem::__totalParticleCountFactor = 1.0f;
  165. ParticleSystem::ParticleSystem()
  166. : _isBlendAdditive(false)
  167. , _isAutoRemoveOnFinish(false)
  168. , _plistFile("")
  169. , _elapsed(0)
  170. , _configName("")
  171. , _emitCounter(0)
  172. , _batchNode(nullptr)
  173. , _atlasIndex(0)
  174. , _transformSystemDirty(false)
  175. , _allocatedParticles(0)
  176. , _isActive(true)
  177. , _particleCount(0)
  178. , _duration(0)
  179. , _life(0)
  180. , _lifeVar(0)
  181. , _angle(0)
  182. , _angleVar(0)
  183. , _emitterMode(Mode::GRAVITY)
  184. , _startSize(0)
  185. , _startSizeVar(0)
  186. , _endSize(0)
  187. , _endSizeVar(0)
  188. , _startSpin(0)
  189. , _startSpinVar(0)
  190. , _endSpin(0)
  191. , _endSpinVar(0)
  192. , _emissionRate(0)
  193. , _totalParticles(0)
  194. , _texture(nullptr)
  195. , _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED)
  196. , _opacityModifyRGB(false)
  197. , _yCoordFlipped(1)
  198. , _positionType(PositionType::FREE)
  199. , _paused(false)
  200. , _sourcePositionCompatible(true) // In the furture this member's default value maybe false or be removed.
  201. {
  202. modeA.gravity.setZero();
  203. modeA.speed = 0;
  204. modeA.speedVar = 0;
  205. modeA.tangentialAccel = 0;
  206. modeA.tangentialAccelVar = 0;
  207. modeA.radialAccel = 0;
  208. modeA.radialAccelVar = 0;
  209. modeA.rotationIsDir = false;
  210. modeB.startRadius = 0;
  211. modeB.startRadiusVar = 0;
  212. modeB.endRadius = 0;
  213. modeB.endRadiusVar = 0;
  214. modeB.rotatePerSecond = 0;
  215. modeB.rotatePerSecondVar = 0;
  216. }
  217. // implementation ParticleSystem
  218. ParticleSystem * ParticleSystem::create(const std::string& plistFile)
  219. {
  220. ParticleSystem *ret = new (std::nothrow) ParticleSystem();
  221. if (ret && ret->initWithFile(plistFile))
  222. {
  223. ret->autorelease();
  224. return ret;
  225. }
  226. CC_SAFE_DELETE(ret);
  227. return ret;
  228. }
  229. ParticleSystem* ParticleSystem::createWithTotalParticles(int numberOfParticles)
  230. {
  231. ParticleSystem *ret = new (std::nothrow) ParticleSystem();
  232. if (ret && ret->initWithTotalParticles(numberOfParticles))
  233. {
  234. ret->autorelease();
  235. return ret;
  236. }
  237. CC_SAFE_DELETE(ret);
  238. return ret;
  239. }
  240. // static
  241. Vector<ParticleSystem*>& ParticleSystem::getAllParticleSystems()
  242. {
  243. return __allInstances;
  244. }
  245. void ParticleSystem::setTotalParticleCountFactor(float factor)
  246. {
  247. __totalParticleCountFactor = factor;
  248. }
  249. bool ParticleSystem::init()
  250. {
  251. return initWithTotalParticles(150);
  252. }
  253. bool ParticleSystem::initWithFile(const std::string& plistFile)
  254. {
  255. bool ret = false;
  256. _plistFile = FileUtils::getInstance()->fullPathForFilename(plistFile);
  257. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(_plistFile);
  258. CCASSERT( !dict.empty(), "Particles: file not found");
  259. // FIXME: compute path from a path, should define a function somewhere to do it
  260. string listFilePath = plistFile;
  261. if (listFilePath.find('/') != string::npos)
  262. {
  263. listFilePath = listFilePath.substr(0, listFilePath.rfind('/') + 1);
  264. ret = this->initWithDictionary(dict, listFilePath);
  265. }
  266. else
  267. {
  268. ret = this->initWithDictionary(dict, "");
  269. }
  270. return ret;
  271. }
  272. bool ParticleSystem::initWithDictionary(ValueMap& dictionary)
  273. {
  274. return initWithDictionary(dictionary, "");
  275. }
  276. bool ParticleSystem::initWithDictionary(ValueMap& dictionary, const std::string& dirname)
  277. {
  278. bool ret = false;
  279. unsigned char *buffer = nullptr;
  280. unsigned char *deflated = nullptr;
  281. Image *image = nullptr;
  282. do
  283. {
  284. int maxParticles = dictionary["maxParticles"].asInt();
  285. // self, not super
  286. if(this->initWithTotalParticles(maxParticles))
  287. {
  288. // Emitter name in particle designer 2.0
  289. _configName = dictionary["configName"].asString();
  290. // angle
  291. _angle = dictionary["angle"].asFloat();
  292. _angleVar = dictionary["angleVariance"].asFloat();
  293. // duration
  294. _duration = dictionary["duration"].asFloat();
  295. // blend function
  296. if (!_configName.empty())
  297. {
  298. _blendFunc.src = dictionary["blendFuncSource"].asFloat();
  299. }
  300. else
  301. {
  302. _blendFunc.src = dictionary["blendFuncSource"].asInt();
  303. }
  304. _blendFunc.dst = dictionary["blendFuncDestination"].asInt();
  305. // color
  306. _startColor.r = dictionary["startColorRed"].asFloat();
  307. _startColor.g = dictionary["startColorGreen"].asFloat();
  308. _startColor.b = dictionary["startColorBlue"].asFloat();
  309. _startColor.a = dictionary["startColorAlpha"].asFloat();
  310. _startColorVar.r = dictionary["startColorVarianceRed"].asFloat();
  311. _startColorVar.g = dictionary["startColorVarianceGreen"].asFloat();
  312. _startColorVar.b = dictionary["startColorVarianceBlue"].asFloat();
  313. _startColorVar.a = dictionary["startColorVarianceAlpha"].asFloat();
  314. _endColor.r = dictionary["finishColorRed"].asFloat();
  315. _endColor.g = dictionary["finishColorGreen"].asFloat();
  316. _endColor.b = dictionary["finishColorBlue"].asFloat();
  317. _endColor.a = dictionary["finishColorAlpha"].asFloat();
  318. _endColorVar.r = dictionary["finishColorVarianceRed"].asFloat();
  319. _endColorVar.g = dictionary["finishColorVarianceGreen"].asFloat();
  320. _endColorVar.b = dictionary["finishColorVarianceBlue"].asFloat();
  321. _endColorVar.a = dictionary["finishColorVarianceAlpha"].asFloat();
  322. // particle size
  323. _startSize = dictionary["startParticleSize"].asFloat();
  324. _startSizeVar = dictionary["startParticleSizeVariance"].asFloat();
  325. _endSize = dictionary["finishParticleSize"].asFloat();
  326. _endSizeVar = dictionary["finishParticleSizeVariance"].asFloat();
  327. // position
  328. float x = dictionary["sourcePositionx"].asFloat();
  329. float y = dictionary["sourcePositiony"].asFloat();
  330. if(!_sourcePositionCompatible) {
  331. this->setSourcePosition(Vec2(x, y));
  332. }
  333. else {
  334. this->setPosition(Vec2(x, y));
  335. }
  336. _posVar.x = dictionary["sourcePositionVariancex"].asFloat();
  337. _posVar.y = dictionary["sourcePositionVariancey"].asFloat();
  338. // Spinning
  339. _startSpin = dictionary["rotationStart"].asFloat();
  340. _startSpinVar = dictionary["rotationStartVariance"].asFloat();
  341. _endSpin= dictionary["rotationEnd"].asFloat();
  342. _endSpinVar= dictionary["rotationEndVariance"].asFloat();
  343. _emitterMode = (Mode) dictionary["emitterType"].asInt();
  344. // Mode A: Gravity + tangential accel + radial accel
  345. if (_emitterMode == Mode::GRAVITY)
  346. {
  347. // gravity
  348. modeA.gravity.x = dictionary["gravityx"].asFloat();
  349. modeA.gravity.y = dictionary["gravityy"].asFloat();
  350. // speed
  351. modeA.speed = dictionary["speed"].asFloat();
  352. modeA.speedVar = dictionary["speedVariance"].asFloat();
  353. // radial acceleration
  354. modeA.radialAccel = dictionary["radialAcceleration"].asFloat();
  355. modeA.radialAccelVar = dictionary["radialAccelVariance"].asFloat();
  356. // tangential acceleration
  357. modeA.tangentialAccel = dictionary["tangentialAcceleration"].asFloat();
  358. modeA.tangentialAccelVar = dictionary["tangentialAccelVariance"].asFloat();
  359. // rotation is dir
  360. modeA.rotationIsDir = dictionary["rotationIsDir"].asBool();
  361. }
  362. // or Mode B: radius movement
  363. else if (_emitterMode == Mode::RADIUS)
  364. {
  365. if (!_configName.empty())
  366. {
  367. modeB.startRadius = dictionary["maxRadius"].asInt();
  368. }
  369. else
  370. {
  371. modeB.startRadius = dictionary["maxRadius"].asFloat();
  372. }
  373. modeB.startRadiusVar = dictionary["maxRadiusVariance"].asFloat();
  374. if (!_configName.empty())
  375. {
  376. modeB.endRadius = dictionary["minRadius"].asInt();
  377. }
  378. else
  379. {
  380. modeB.endRadius = dictionary["minRadius"].asFloat();
  381. }
  382. if (dictionary.find("minRadiusVariance") != dictionary.end())
  383. {
  384. modeB.endRadiusVar = dictionary["minRadiusVariance"].asFloat();
  385. }
  386. else
  387. {
  388. modeB.endRadiusVar = 0.0f;
  389. }
  390. if (!_configName.empty())
  391. {
  392. modeB.rotatePerSecond = dictionary["rotatePerSecond"].asInt();
  393. }
  394. else
  395. {
  396. modeB.rotatePerSecond = dictionary["rotatePerSecond"].asFloat();
  397. }
  398. modeB.rotatePerSecondVar = dictionary["rotatePerSecondVariance"].asFloat();
  399. } else {
  400. CCASSERT( false, "Invalid emitterType in config file");
  401. CC_BREAK_IF(true);
  402. }
  403. // life span
  404. _life = dictionary["particleLifespan"].asFloat();
  405. _lifeVar = dictionary["particleLifespanVariance"].asFloat();
  406. // emission Rate
  407. _emissionRate = _totalParticles / _life;
  408. //don't get the internal texture if a batchNode is used
  409. if (!_batchNode)
  410. {
  411. // Set a compatible default for the alpha transfer
  412. _opacityModifyRGB = false;
  413. // texture
  414. // Try to get the texture from the cache
  415. std::string textureName = dictionary["textureFileName"].asString();
  416. size_t rPos = textureName.rfind('/');
  417. if (rPos != string::npos)
  418. {
  419. string textureDir = textureName.substr(0, rPos + 1);
  420. if (!dirname.empty() && textureDir != dirname)
  421. {
  422. textureName = textureName.substr(rPos+1);
  423. textureName = dirname + textureName;
  424. }
  425. }
  426. else if (!dirname.empty() && !textureName.empty())
  427. {
  428. textureName = dirname + textureName;
  429. }
  430. Texture2D *tex = nullptr;
  431. if (!textureName.empty())
  432. {
  433. // set not pop-up message box when load image failed
  434. bool notify = FileUtils::getInstance()->isPopupNotify();
  435. FileUtils::getInstance()->setPopupNotify(false);
  436. tex = Director::getInstance()->getTextureCache()->addImage(textureName);
  437. // reset the value of UIImage notify
  438. FileUtils::getInstance()->setPopupNotify(notify);
  439. }
  440. if (tex)
  441. {
  442. setTexture(tex);
  443. }
  444. else if( dictionary.find("textureImageData") != dictionary.end() )
  445. {
  446. std::string textureData = dictionary.at("textureImageData").asString();
  447. CCASSERT(!textureData.empty(), "textureData can't be empty!");
  448. auto dataLen = textureData.size();
  449. if (dataLen != 0)
  450. {
  451. // if it fails, try to get it from the base64-gzipped data
  452. int decodeLen = base64Decode((unsigned char*)textureData.c_str(), (unsigned int)dataLen, &buffer);
  453. CCASSERT( buffer != nullptr, "CCParticleSystem: error decoding textureImageData");
  454. CC_BREAK_IF(!buffer);
  455. ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
  456. CCASSERT( deflated != nullptr, "CCParticleSystem: error ungzipping textureImageData");
  457. CC_BREAK_IF(!deflated);
  458. // For android, we should retain it in VolatileTexture::addImage which invoked in Director::getInstance()->getTextureCache()->addUIImage()
  459. image = new (std::nothrow) Image();
  460. bool isOK = image->initWithImageData(deflated, deflatedLen);
  461. CCASSERT(isOK, "CCParticleSystem: error init image with Data");
  462. CC_BREAK_IF(!isOK);
  463. setTexture(Director::getInstance()->getTextureCache()->addImage(image, _plistFile + textureName));
  464. image->release();
  465. }
  466. }
  467. _yCoordFlipped = dictionary.find("yCoordFlipped") == dictionary.end() ? 1 : dictionary.at("yCoordFlipped").asInt();
  468. if( !this->_texture)
  469. CCLOGWARN("cocos2d: Warning: ParticleSystemQuad system without a texture");
  470. }
  471. ret = true;
  472. }
  473. } while (0);
  474. free(buffer);
  475. free(deflated);
  476. return ret;
  477. }
  478. bool ParticleSystem::initWithTotalParticles(int numberOfParticles)
  479. {
  480. _totalParticles = numberOfParticles;
  481. _particleData.release();
  482. if( !_particleData.init(_totalParticles) )
  483. {
  484. CCLOG("Particle system: not enough memory");
  485. this->release();
  486. return false;
  487. }
  488. _allocatedParticles = numberOfParticles;
  489. if (_batchNode)
  490. {
  491. for (int i = 0; i < _totalParticles; i++)
  492. {
  493. _particleData.atlasIndex[i] = i;
  494. }
  495. }
  496. // default, active
  497. _isActive = true;
  498. // default blend function
  499. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  500. // default movement type;
  501. _positionType = PositionType::FREE;
  502. // by default be in mode A:
  503. _emitterMode = Mode::GRAVITY;
  504. // default: modulate
  505. // FIXME:: not used
  506. // colorModulate = YES;
  507. _isAutoRemoveOnFinish = false;
  508. // Optimization: compile updateParticle method
  509. //updateParticleSel = @selector(updateQuadWithParticle:newPosition:);
  510. //updateParticleImp = (CC_UPDATE_PARTICLE_IMP) [self methodForSelector:updateParticleSel];
  511. //for batchNode
  512. _transformSystemDirty = false;
  513. return true;
  514. }
  515. ParticleSystem::~ParticleSystem()
  516. {
  517. // Since the scheduler retains the "target (in this case the ParticleSystem)
  518. // it is not needed to call "unscheduleUpdate" here. In fact, it will be called in "cleanup"
  519. //unscheduleUpdate();
  520. _particleData.release();
  521. CC_SAFE_RELEASE(_texture);
  522. }
  523. void ParticleSystem::addParticles(int count)
  524. {
  525. if (_paused)
  526. return;
  527. uint32_t RANDSEED = rand();
  528. int start = _particleCount;
  529. _particleCount += count;
  530. //life
  531. for (int i = start; i < _particleCount ; ++i)
  532. {
  533. float theLife = _life + _lifeVar * RANDOM_M11(&RANDSEED);
  534. _particleData.timeToLive[i] = MAX(0, theLife);
  535. }
  536. //position
  537. for (int i = start; i < _particleCount; ++i)
  538. {
  539. _particleData.posx[i] = _sourcePosition.x + _posVar.x * RANDOM_M11(&RANDSEED);
  540. }
  541. for (int i = start; i < _particleCount; ++i)
  542. {
  543. _particleData.posy[i] = _sourcePosition.y + _posVar.y * RANDOM_M11(&RANDSEED);
  544. }
  545. //color
  546. #define SET_COLOR(c, b, v)\
  547. for (int i = start; i < _particleCount; ++i)\
  548. {\
  549. c[i] = clampf( b + v * RANDOM_M11(&RANDSEED) , 0 , 1 );\
  550. }
  551. SET_COLOR(_particleData.colorR, _startColor.r, _startColorVar.r);
  552. SET_COLOR(_particleData.colorG, _startColor.g, _startColorVar.g);
  553. SET_COLOR(_particleData.colorB, _startColor.b, _startColorVar.b);
  554. SET_COLOR(_particleData.colorA, _startColor.a, _startColorVar.a);
  555. SET_COLOR(_particleData.deltaColorR, _endColor.r, _endColorVar.r);
  556. SET_COLOR(_particleData.deltaColorG, _endColor.g, _endColorVar.g);
  557. SET_COLOR(_particleData.deltaColorB, _endColor.b, _endColorVar.b);
  558. SET_COLOR(_particleData.deltaColorA, _endColor.a, _endColorVar.a);
  559. #define SET_DELTA_COLOR(c, dc)\
  560. for (int i = start; i < _particleCount; ++i)\
  561. {\
  562. dc[i] = (dc[i] - c[i]) / _particleData.timeToLive[i];\
  563. }
  564. SET_DELTA_COLOR(_particleData.colorR, _particleData.deltaColorR);
  565. SET_DELTA_COLOR(_particleData.colorG, _particleData.deltaColorG);
  566. SET_DELTA_COLOR(_particleData.colorB, _particleData.deltaColorB);
  567. SET_DELTA_COLOR(_particleData.colorA, _particleData.deltaColorA);
  568. //size
  569. for (int i = start; i < _particleCount; ++i)
  570. {
  571. _particleData.size[i] = _startSize + _startSizeVar * RANDOM_M11(&RANDSEED);
  572. _particleData.size[i] = MAX(0, _particleData.size[i]);
  573. }
  574. if (_endSize != START_SIZE_EQUAL_TO_END_SIZE)
  575. {
  576. for (int i = start; i < _particleCount; ++i)
  577. {
  578. float endSize = _endSize + _endSizeVar * RANDOM_M11(&RANDSEED);
  579. endSize = MAX(0, endSize);
  580. _particleData.deltaSize[i] = (endSize - _particleData.size[i]) / _particleData.timeToLive[i];
  581. }
  582. }
  583. else
  584. {
  585. for (int i = start; i < _particleCount; ++i)
  586. {
  587. _particleData.deltaSize[i] = 0.0f;
  588. }
  589. }
  590. // rotation
  591. for (int i = start; i < _particleCount; ++i)
  592. {
  593. _particleData.rotation[i] = _startSpin + _startSpinVar * RANDOM_M11(&RANDSEED);
  594. }
  595. for (int i = start; i < _particleCount; ++i)
  596. {
  597. float endA = _endSpin + _endSpinVar * RANDOM_M11(&RANDSEED);
  598. _particleData.deltaRotation[i] = (endA - _particleData.rotation[i]) / _particleData.timeToLive[i];
  599. }
  600. // position
  601. Vec2 pos;
  602. if (_positionType == PositionType::FREE)
  603. {
  604. pos = this->convertToWorldSpace(Vec2::ZERO);
  605. }
  606. else if (_positionType == PositionType::RELATIVE)
  607. {
  608. pos = _position;
  609. }
  610. for (int i = start; i < _particleCount; ++i)
  611. {
  612. _particleData.startPosX[i] = pos.x;
  613. }
  614. for (int i = start; i < _particleCount; ++i)
  615. {
  616. _particleData.startPosY[i] = pos.y;
  617. }
  618. // Mode Gravity: A
  619. if (_emitterMode == Mode::GRAVITY)
  620. {
  621. // radial accel
  622. for (int i = start; i < _particleCount; ++i)
  623. {
  624. _particleData.modeA.radialAccel[i] = modeA.radialAccel + modeA.radialAccelVar * RANDOM_M11(&RANDSEED);
  625. }
  626. // tangential accel
  627. for (int i = start; i < _particleCount; ++i)
  628. {
  629. _particleData.modeA.tangentialAccel[i] = modeA.tangentialAccel + modeA.tangentialAccelVar * RANDOM_M11(&RANDSEED);
  630. }
  631. // rotation is dir
  632. if( modeA.rotationIsDir )
  633. {
  634. for (int i = start; i < _particleCount; ++i)
  635. {
  636. float a = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED) );
  637. Vec2 v(cosf( a ), sinf( a ));
  638. float s = modeA.speed + modeA.speedVar * RANDOM_M11(&RANDSEED);
  639. Vec2 dir = v * s;
  640. _particleData.modeA.dirX[i] = dir.x;//v * s ;
  641. _particleData.modeA.dirY[i] = dir.y;
  642. _particleData.rotation[i] = -CC_RADIANS_TO_DEGREES(dir.getAngle());
  643. }
  644. }
  645. else
  646. {
  647. for (int i = start; i < _particleCount; ++i)
  648. {
  649. float a = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED) );
  650. Vec2 v(cosf( a ), sinf( a ));
  651. float s = modeA.speed + modeA.speedVar * RANDOM_M11(&RANDSEED);
  652. Vec2 dir = v * s;
  653. _particleData.modeA.dirX[i] = dir.x;//v * s ;
  654. _particleData.modeA.dirY[i] = dir.y;
  655. }
  656. }
  657. }
  658. // Mode Radius: B
  659. else
  660. {
  661. //Need to check by Jacky
  662. // Set the default diameter of the particle from the source position
  663. for (int i = start; i < _particleCount; ++i)
  664. {
  665. _particleData.modeB.radius[i] = modeB.startRadius + modeB.startRadiusVar * RANDOM_M11(&RANDSEED);
  666. }
  667. for (int i = start; i < _particleCount; ++i)
  668. {
  669. _particleData.modeB.angle[i] = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED));
  670. }
  671. for (int i = start; i < _particleCount; ++i)
  672. {
  673. _particleData.modeB.degreesPerSecond[i] = CC_DEGREES_TO_RADIANS(modeB.rotatePerSecond + modeB.rotatePerSecondVar * RANDOM_M11(&RANDSEED));
  674. }
  675. if(modeB.endRadius == START_RADIUS_EQUAL_TO_END_RADIUS)
  676. {
  677. for (int i = start; i < _particleCount; ++i)
  678. {
  679. _particleData.modeB.deltaRadius[i] = 0.0f;
  680. }
  681. }
  682. else
  683. {
  684. for (int i = start; i < _particleCount; ++i)
  685. {
  686. float endRadius = modeB.endRadius + modeB.endRadiusVar * RANDOM_M11(&RANDSEED);
  687. _particleData.modeB.deltaRadius[i] = (endRadius - _particleData.modeB.radius[i]) / _particleData.timeToLive[i];
  688. }
  689. }
  690. }
  691. }
  692. void ParticleSystem::onEnter()
  693. {
  694. #if CC_ENABLE_SCRIPT_BINDING
  695. if (_scriptType == kScriptTypeJavascript)
  696. {
  697. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  698. return;
  699. }
  700. #endif
  701. Node::onEnter();
  702. // update after action in run!
  703. this->scheduleUpdateWithPriority(1);
  704. __allInstances.pushBack(this);
  705. }
  706. void ParticleSystem::onExit()
  707. {
  708. #if CC_ENABLE_SCRIPT_BINDING
  709. if (_scriptType == kScriptTypeJavascript)
  710. {
  711. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
  712. return;
  713. }
  714. #endif
  715. this->unscheduleUpdate();
  716. Node::onExit();
  717. auto iter = std::find(std::begin(__allInstances), std::end(__allInstances), this);
  718. if (iter != std::end(__allInstances))
  719. {
  720. __allInstances.erase(iter);
  721. }
  722. }
  723. void ParticleSystem::stopSystem()
  724. {
  725. _isActive = false;
  726. _elapsed = _duration;
  727. _emitCounter = 0;
  728. }
  729. void ParticleSystem::resetSystem()
  730. {
  731. _isActive = true;
  732. _elapsed = 0;
  733. for (int i = 0; i < _particleCount; ++i)
  734. {
  735. _particleData.timeToLive[i] = 0.0f;
  736. }
  737. }
  738. bool ParticleSystem::isFull()
  739. {
  740. return (_particleCount == _totalParticles);
  741. }
  742. // ParticleSystem - MainLoop
  743. void ParticleSystem::update(float dt)
  744. {
  745. CC_PROFILER_START_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
  746. if (_isActive && _emissionRate)
  747. {
  748. float rate = 1.0f / _emissionRate;
  749. int totalParticles = static_cast<int>(_totalParticles * __totalParticleCountFactor);
  750. //issue #1201, prevent bursts of particles, due to too high emitCounter
  751. if (_particleCount < totalParticles)
  752. {
  753. _emitCounter += dt;
  754. if (_emitCounter < 0.f)
  755. _emitCounter = 0.f;
  756. }
  757. int emitCount = MIN(totalParticles - _particleCount, _emitCounter / rate);
  758. addParticles(emitCount);
  759. _emitCounter -= rate * emitCount;
  760. _elapsed += dt;
  761. if (_elapsed < 0.f)
  762. _elapsed = 0.f;
  763. if (_duration != DURATION_INFINITY && _duration < _elapsed)
  764. {
  765. this->stopSystem();
  766. }
  767. }
  768. {
  769. for (int i = 0; i < _particleCount; ++i)
  770. {
  771. _particleData.timeToLive[i] -= dt;
  772. }
  773. for (int i = 0; i < _particleCount; ++i)
  774. {
  775. if (_particleData.timeToLive[i] <= 0.0f)
  776. {
  777. int j = _particleCount - 1;
  778. while (j > 0 && _particleData.timeToLive[j] <= 0)
  779. {
  780. _particleCount--;
  781. j--;
  782. }
  783. _particleData.copyParticle(i, _particleCount - 1);
  784. if (_batchNode)
  785. {
  786. //disable the switched particle
  787. int currentIndex = _particleData.atlasIndex[i];
  788. _batchNode->disableParticle(_atlasIndex + currentIndex);
  789. //switch indexes
  790. _particleData.atlasIndex[_particleCount - 1] = currentIndex;
  791. }
  792. --_particleCount;
  793. if( _particleCount == 0 && _isAutoRemoveOnFinish )
  794. {
  795. this->unscheduleUpdate();
  796. _parent->removeChild(this, true);
  797. return;
  798. }
  799. }
  800. }
  801. if (_emitterMode == Mode::GRAVITY)
  802. {
  803. for (int i = 0 ; i < _particleCount; ++i)
  804. {
  805. particle_point tmp, radial = {0.0f, 0.0f}, tangential;
  806. // radial acceleration
  807. if (_particleData.posx[i] || _particleData.posy[i])
  808. {
  809. normalize_point(_particleData.posx[i], _particleData.posy[i], &radial);
  810. }
  811. tangential = radial;
  812. radial.x *= _particleData.modeA.radialAccel[i];
  813. radial.y *= _particleData.modeA.radialAccel[i];
  814. // tangential acceleration
  815. std::swap(tangential.x, tangential.y);
  816. tangential.x *= - _particleData.modeA.tangentialAccel[i];
  817. tangential.y *= _particleData.modeA.tangentialAccel[i];
  818. // (gravity + radial + tangential) * dt
  819. tmp.x = radial.x + tangential.x + modeA.gravity.x;
  820. tmp.y = radial.y + tangential.y + modeA.gravity.y;
  821. tmp.x *= dt;
  822. tmp.y *= dt;
  823. _particleData.modeA.dirX[i] += tmp.x;
  824. _particleData.modeA.dirY[i] += tmp.y;
  825. // this is cocos2d-x v3.0
  826. // if (_configName.length()>0 && _yCoordFlipped != -1)
  827. // this is cocos2d-x v3.0
  828. tmp.x = _particleData.modeA.dirX[i] * dt * _yCoordFlipped;
  829. tmp.y = _particleData.modeA.dirY[i] * dt * _yCoordFlipped;
  830. _particleData.posx[i] += tmp.x;
  831. _particleData.posy[i] += tmp.y;
  832. }
  833. }
  834. else
  835. {
  836. //Why use so many for-loop separately instead of putting them together?
  837. //When the processor needs to read from or write to a location in memory,
  838. //it first checks whether a copy of that data is in the cache.
  839. //And every property's memory of the particle system is continuous,
  840. //for the purpose of improving cache hit rate, we should process only one property in one for-loop AFAP.
  841. //It was proved to be effective especially for low-end machine.
  842. for (int i = 0; i < _particleCount; ++i)
  843. {
  844. _particleData.modeB.angle[i] += _particleData.modeB.degreesPerSecond[i] * dt;
  845. }
  846. for (int i = 0; i < _particleCount; ++i)
  847. {
  848. _particleData.modeB.radius[i] += _particleData.modeB.deltaRadius[i] * dt;
  849. }
  850. for (int i = 0; i < _particleCount; ++i)
  851. {
  852. _particleData.posx[i] = - cosf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i];
  853. }
  854. for (int i = 0; i < _particleCount; ++i)
  855. {
  856. _particleData.posy[i] = - sinf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i] * _yCoordFlipped;
  857. }
  858. }
  859. //color r,g,b,a
  860. for (int i = 0 ; i < _particleCount; ++i)
  861. {
  862. _particleData.colorR[i] += _particleData.deltaColorR[i] * dt;
  863. }
  864. for (int i = 0 ; i < _particleCount; ++i)
  865. {
  866. _particleData.colorG[i] += _particleData.deltaColorG[i] * dt;
  867. }
  868. for (int i = 0 ; i < _particleCount; ++i)
  869. {
  870. _particleData.colorB[i] += _particleData.deltaColorB[i] * dt;
  871. }
  872. for (int i = 0 ; i < _particleCount; ++i)
  873. {
  874. _particleData.colorA[i] += _particleData.deltaColorA[i] * dt;
  875. }
  876. //size
  877. for (int i = 0 ; i < _particleCount; ++i)
  878. {
  879. _particleData.size[i] += (_particleData.deltaSize[i] * dt);
  880. _particleData.size[i] = MAX(0, _particleData.size[i]);
  881. }
  882. //angle
  883. for (int i = 0 ; i < _particleCount; ++i)
  884. {
  885. _particleData.rotation[i] += _particleData.deltaRotation[i] * dt;
  886. }
  887. updateParticleQuads();
  888. _transformSystemDirty = false;
  889. }
  890. // only update gl buffer when visible
  891. if (_visible && ! _batchNode)
  892. {
  893. postStep();
  894. }
  895. CC_PROFILER_STOP_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
  896. }
  897. void ParticleSystem::updateWithNoTime(void)
  898. {
  899. this->update(0.0f);
  900. }
  901. void ParticleSystem::updateParticleQuads()
  902. {
  903. //should be overridden
  904. }
  905. void ParticleSystem::postStep()
  906. {
  907. // should be overridden
  908. }
  909. // ParticleSystem - Texture protocol
  910. void ParticleSystem::setTexture(Texture2D* var)
  911. {
  912. if (_texture != var)
  913. {
  914. CC_SAFE_RETAIN(var);
  915. CC_SAFE_RELEASE(_texture);
  916. _texture = var;
  917. updateBlendFunc();
  918. }
  919. }
  920. void ParticleSystem::updateBlendFunc()
  921. {
  922. CCASSERT(! _batchNode, "Can't change blending functions when the particle is being batched");
  923. if(_texture)
  924. {
  925. bool premultiplied = _texture->hasPremultipliedAlpha();
  926. _opacityModifyRGB = false;
  927. if( _texture && ( _blendFunc.src == CC_BLEND_SRC && _blendFunc.dst == CC_BLEND_DST ) )
  928. {
  929. if( premultiplied )
  930. {
  931. _opacityModifyRGB = true;
  932. }
  933. else
  934. {
  935. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  936. }
  937. }
  938. }
  939. }
  940. Texture2D * ParticleSystem::getTexture() const
  941. {
  942. return _texture;
  943. }
  944. // ParticleSystem - Additive Blending
  945. void ParticleSystem::setBlendAdditive(bool additive)
  946. {
  947. if( additive )
  948. {
  949. _blendFunc = BlendFunc::ADDITIVE;
  950. }
  951. else
  952. {
  953. if( _texture && ! _texture->hasPremultipliedAlpha() )
  954. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  955. else
  956. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  957. }
  958. }
  959. bool ParticleSystem::isBlendAdditive() const
  960. {
  961. return( _blendFunc.src == GL_SRC_ALPHA && _blendFunc.dst == GL_ONE);
  962. }
  963. // ParticleSystem - Properties of Gravity Mode
  964. void ParticleSystem::setTangentialAccel(float t)
  965. {
  966. CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  967. modeA.tangentialAccel = t;
  968. }
  969. float ParticleSystem::getTangentialAccel() const
  970. {
  971. CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  972. return modeA.tangentialAccel;
  973. }
  974. void ParticleSystem::setTangentialAccelVar(float t)
  975. {
  976. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  977. modeA.tangentialAccelVar = t;
  978. }
  979. float ParticleSystem::getTangentialAccelVar() const
  980. {
  981. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  982. return modeA.tangentialAccelVar;
  983. }
  984. void ParticleSystem::setRadialAccel(float t)
  985. {
  986. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  987. modeA.radialAccel = t;
  988. }
  989. float ParticleSystem::getRadialAccel() const
  990. {
  991. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  992. return modeA.radialAccel;
  993. }
  994. void ParticleSystem::setRadialAccelVar(float t)
  995. {
  996. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  997. modeA.radialAccelVar = t;
  998. }
  999. float ParticleSystem::getRadialAccelVar() const
  1000. {
  1001. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1002. return modeA.radialAccelVar;
  1003. }
  1004. void ParticleSystem::setRotationIsDir(bool t)
  1005. {
  1006. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1007. modeA.rotationIsDir = t;
  1008. }
  1009. bool ParticleSystem::getRotationIsDir() const
  1010. {
  1011. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1012. return modeA.rotationIsDir;
  1013. }
  1014. void ParticleSystem::setGravity(const Vec2& g)
  1015. {
  1016. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1017. modeA.gravity = g;
  1018. }
  1019. const Vec2& ParticleSystem::getGravity()
  1020. {
  1021. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1022. return modeA.gravity;
  1023. }
  1024. void ParticleSystem::setSpeed(float speed)
  1025. {
  1026. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1027. modeA.speed = speed;
  1028. }
  1029. float ParticleSystem::getSpeed() const
  1030. {
  1031. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1032. return modeA.speed;
  1033. }
  1034. void ParticleSystem::setSpeedVar(float speedVar)
  1035. {
  1036. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1037. modeA.speedVar = speedVar;
  1038. }
  1039. float ParticleSystem::getSpeedVar() const
  1040. {
  1041. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1042. return modeA.speedVar;
  1043. }
  1044. // ParticleSystem - Properties of Radius Mode
  1045. void ParticleSystem::setStartRadius(float startRadius)
  1046. {
  1047. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1048. modeB.startRadius = startRadius;
  1049. }
  1050. float ParticleSystem::getStartRadius() const
  1051. {
  1052. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1053. return modeB.startRadius;
  1054. }
  1055. void ParticleSystem::setStartRadiusVar(float startRadiusVar)
  1056. {
  1057. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1058. modeB.startRadiusVar = startRadiusVar;
  1059. }
  1060. float ParticleSystem::getStartRadiusVar() const
  1061. {
  1062. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1063. return modeB.startRadiusVar;
  1064. }
  1065. void ParticleSystem::setEndRadius(float endRadius)
  1066. {
  1067. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1068. modeB.endRadius = endRadius;
  1069. }
  1070. float ParticleSystem::getEndRadius() const
  1071. {
  1072. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1073. return modeB.endRadius;
  1074. }
  1075. void ParticleSystem::setEndRadiusVar(float endRadiusVar)
  1076. {
  1077. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1078. modeB.endRadiusVar = endRadiusVar;
  1079. }
  1080. float ParticleSystem::getEndRadiusVar() const
  1081. {
  1082. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1083. return modeB.endRadiusVar;
  1084. }
  1085. void ParticleSystem::setRotatePerSecond(float degrees)
  1086. {
  1087. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1088. modeB.rotatePerSecond = degrees;
  1089. }
  1090. float ParticleSystem::getRotatePerSecond() const
  1091. {
  1092. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1093. return modeB.rotatePerSecond;
  1094. }
  1095. void ParticleSystem::setRotatePerSecondVar(float degrees)
  1096. {
  1097. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1098. modeB.rotatePerSecondVar = degrees;
  1099. }
  1100. float ParticleSystem::getRotatePerSecondVar() const
  1101. {
  1102. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1103. return modeB.rotatePerSecondVar;
  1104. }
  1105. bool ParticleSystem::isActive() const
  1106. {
  1107. return _isActive;
  1108. }
  1109. int ParticleSystem::getTotalParticles() const
  1110. {
  1111. return _totalParticles;
  1112. }
  1113. void ParticleSystem::setTotalParticles(int var)
  1114. {
  1115. CCASSERT( var <= _allocatedParticles, "Particle: resizing particle array only supported for quads");
  1116. _totalParticles = var;
  1117. }
  1118. const BlendFunc& ParticleSystem::getBlendFunc() const
  1119. {
  1120. return _blendFunc;
  1121. }
  1122. void ParticleSystem::setBlendFunc(const BlendFunc &blendFunc)
  1123. {
  1124. if( _blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst ) {
  1125. _blendFunc = blendFunc;
  1126. this->updateBlendFunc();
  1127. }
  1128. }
  1129. bool ParticleSystem::isAutoRemoveOnFinish() const
  1130. {
  1131. return _isAutoRemoveOnFinish;
  1132. }
  1133. void ParticleSystem::setAutoRemoveOnFinish(bool var)
  1134. {
  1135. _isAutoRemoveOnFinish = var;
  1136. }
  1137. // ParticleSystem - methods for batchNode rendering
  1138. ParticleBatchNode* ParticleSystem::getBatchNode(void) const
  1139. {
  1140. return _batchNode;
  1141. }
  1142. void ParticleSystem::setBatchNode(ParticleBatchNode* batchNode)
  1143. {
  1144. if( _batchNode != batchNode ) {
  1145. _batchNode = batchNode; // weak reference
  1146. if( batchNode ) {
  1147. //each particle needs a unique index
  1148. for (int i = 0; i < _totalParticles; i++)
  1149. {
  1150. _particleData.atlasIndex[i] = i;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. //don't use a transform matrix, this is faster
  1156. void ParticleSystem::setScale(float s)
  1157. {
  1158. _transformSystemDirty = true;
  1159. Node::setScale(s);
  1160. }
  1161. void ParticleSystem::setRotation(float newRotation)
  1162. {
  1163. _transformSystemDirty = true;
  1164. Node::setRotation(newRotation);
  1165. }
  1166. void ParticleSystem::setScaleX(float newScaleX)
  1167. {
  1168. _transformSystemDirty = true;
  1169. Node::setScaleX(newScaleX);
  1170. }
  1171. void ParticleSystem::setScaleY(float newScaleY)
  1172. {
  1173. _transformSystemDirty = true;
  1174. Node::setScaleY(newScaleY);
  1175. }
  1176. void ParticleSystem::start()
  1177. {
  1178. resetSystem();
  1179. }
  1180. void ParticleSystem::stop()
  1181. {
  1182. stopSystem();
  1183. }
  1184. bool ParticleSystem::isPaused() const
  1185. {
  1186. return _paused;
  1187. }
  1188. void ParticleSystem::pauseEmissions()
  1189. {
  1190. _paused = true;
  1191. }
  1192. void ParticleSystem::resumeEmissions()
  1193. {
  1194. _paused = false;
  1195. }
  1196. NS_CC_END