2010-12-28 10:23:14 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:07:16 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-04 19:20:16 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2011-03-19 10:07:16 +08:00
|
|
|
|
|
|
|
// ideas taken from:
|
2012-04-19 14:35:52 +08:00
|
|
|
// . The ocean spray in your face [Jeff Lander]
|
|
|
|
// http://www.double.co.nz/dust/col0798.pdf
|
|
|
|
// . Building an Advanced Particle System [John van der Burg]
|
|
|
|
// http://www.gamasutra.com/features/20000623/vanderburg_01.htm
|
2011-03-19 10:07:16 +08:00
|
|
|
// . LOVE game engine
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://love2d.org/
|
2011-03-19 10:07:16 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Radius mode support, from 71 squared
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://particledesigner.71squared.com/
|
2011-03-19 10:07:16 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT: Particle Designer is supported by cocos2d, but
|
2012-09-17 15:02:24 +08:00
|
|
|
// 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
|
2011-03-19 10:07:16 +08:00
|
|
|
// cocos2d uses a another approach, but the results are almost identical.
|
|
|
|
//
|
|
|
|
|
2010-12-28 10:23:14 +08:00
|
|
|
#include "CCParticleSystem.h"
|
2013-07-25 13:54:32 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "CCParticleBatchNode.h"
|
2010-12-28 10:23:14 +08:00
|
|
|
#include "ccTypes.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCTextureAtlas.h"
|
|
|
|
#include "base64.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCImage.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "ZipUtils.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCProfiling.h"
|
2011-03-19 10:07:16 +08:00
|
|
|
// opengl
|
2012-04-25 16:18:04 +08:00
|
|
|
#include "CCGL.h"
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2010-12-28 10:23:14 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2010-12-27 17:39:15 +08:00
|
|
|
// ideas taken from:
|
2012-04-19 14:35:52 +08:00
|
|
|
// . The ocean spray in your face [Jeff Lander]
|
|
|
|
// http://www.double.co.nz/dust/col0798.pdf
|
|
|
|
// . Building an Advanced Particle System [John van der Burg]
|
|
|
|
// http://www.gamasutra.com/features/20000623/vanderburg_01.htm
|
2010-12-27 17:39:15 +08:00
|
|
|
// . LOVE game engine
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://love2d.org/
|
2010-12-27 17:39:15 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Radius mode support, from 71 squared
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://particledesigner.71squared.com/
|
2010-12-27 17:39:15 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT: Particle Designer is supported by cocos2d, but
|
2012-09-17 15:02:24 +08:00
|
|
|
// 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
|
2010-12-27 17:39:15 +08:00
|
|
|
// cocos2d uses a another approach, but the results are almost identical.
|
|
|
|
//
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::ParticleSystem()
|
2013-11-13 11:22:34 +08:00
|
|
|
: _isBlendAdditive(false)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _isAutoRemoveOnFinish(false)
|
|
|
|
, _plistFile("")
|
2013-06-15 14:03:30 +08:00
|
|
|
, _elapsed(0)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _particles(nullptr)
|
|
|
|
, _configName("")
|
2013-06-15 14:03:30 +08:00
|
|
|
, _emitCounter(0)
|
|
|
|
, _particleIdx(0)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _batchNode(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _atlasIndex(0)
|
|
|
|
, _transformSystemDirty(false)
|
|
|
|
, _allocatedParticles(0)
|
|
|
|
, _isActive(true)
|
|
|
|
, _particleCount(0)
|
|
|
|
, _duration(0)
|
2014-04-15 18:13:57 +08:00
|
|
|
, _sourcePosition(Vector2::ZERO)
|
|
|
|
, _posVar(Vector2::ZERO)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _life(0)
|
|
|
|
, _lifeVar(0)
|
|
|
|
, _angle(0)
|
|
|
|
, _angleVar(0)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _emitterMode(Mode::GRAVITY)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _startSize(0)
|
|
|
|
, _startSizeVar(0)
|
|
|
|
, _endSize(0)
|
|
|
|
, _endSizeVar(0)
|
|
|
|
, _startSpin(0)
|
|
|
|
, _startSpinVar(0)
|
|
|
|
, _endSpin(0)
|
|
|
|
, _endSpinVar(0)
|
|
|
|
, _emissionRate(0)
|
|
|
|
, _totalParticles(0)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _texture(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _opacityModifyRGB(false)
|
2013-09-23 10:12:54 +08:00
|
|
|
, _yCoordFlipped(0)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _positionType(PositionType::FREE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
modeA.gravity = Vector2::ZERO;
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.speed = 0;
|
|
|
|
modeA.speedVar = 0;
|
|
|
|
modeA.tangentialAccel = 0;
|
|
|
|
modeA.tangentialAccelVar = 0;
|
|
|
|
modeA.radialAccel = 0;
|
|
|
|
modeA.radialAccelVar = 0;
|
2013-01-21 17:39:48 +08:00
|
|
|
modeA.rotationIsDir = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.startRadius = 0;
|
|
|
|
modeB.startRadiusVar = 0;
|
|
|
|
modeB.endRadius = 0;
|
|
|
|
modeB.endRadiusVar = 0;
|
|
|
|
modeB.rotatePerSecond = 0;
|
|
|
|
modeB.rotatePerSecondVar = 0;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation ParticleSystem
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
ParticleSystem * ParticleSystem::create(const std::string& plistFile)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ParticleSystem *ret = new ParticleSystem();
|
|
|
|
if (ret && ret->initWithFile(plistFile))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
ParticleSystem* ParticleSystem::createWithTotalParticles(int numberOfParticles)
|
2012-08-29 14:53:01 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ParticleSystem *ret = new ParticleSystem();
|
|
|
|
if (ret && ret->initWithTotalParticles(numberOfParticles))
|
2012-08-29 14:53:01 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-08-29 14:53:01 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return ret;
|
2012-08-29 14:53:01 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystem::init()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithTotalParticles(150);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
bool ParticleSystem::initWithFile(const std::string& plistFile)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2013-07-12 06:24:23 +08:00
|
|
|
_plistFile = FileUtils::getInstance()->fullPathForFilename(plistFile);
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(_plistFile.c_str());
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2013-12-03 15:41:01 +08:00
|
|
|
CCASSERT( !dict.empty(), "Particles: file not found");
|
2012-11-15 17:16:51 +08:00
|
|
|
|
|
|
|
// XXX compute path from a path, should define a function somewhere to do it
|
|
|
|
string listFilePath = plistFile;
|
|
|
|
if (listFilePath.find('/') != string::npos)
|
|
|
|
{
|
|
|
|
listFilePath = listFilePath.substr(0, listFilePath.rfind('/') + 1);
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = this->initWithDictionary(dict, listFilePath.c_str());
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = this->initWithDictionary(dict, "");
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
bool ParticleSystem::initWithDictionary(ValueMap& dictionary)
|
2012-11-15 17:16:51 +08:00
|
|
|
{
|
|
|
|
return initWithDictionary(dictionary, "");
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
bool ParticleSystem::initWithDictionary(ValueMap& dictionary, const std::string& dirname)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2013-12-03 15:31:36 +08:00
|
|
|
unsigned char *buffer = nullptr;
|
|
|
|
unsigned char *deflated = nullptr;
|
|
|
|
Image *image = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
int maxParticles = dictionary["maxParticles"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
// self, not super
|
|
|
|
if(this->initWithTotalParticles(maxParticles))
|
|
|
|
{
|
2013-09-23 10:12:54 +08:00
|
|
|
// Emitter name in particle designer 2.0
|
2013-12-03 15:41:01 +08:00
|
|
|
_configName = dictionary["configName"].asString();
|
2013-09-23 10:12:54 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// angle
|
2013-12-03 15:41:01 +08:00
|
|
|
_angle = dictionary["angle"].asFloat();
|
|
|
|
_angleVar = dictionary["angleVariance"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// duration
|
2013-12-03 15:41:01 +08:00
|
|
|
_duration = dictionary["duration"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// blend function
|
2013-09-23 10:12:54 +08:00
|
|
|
if (_configName.length()>0)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
_blendFunc.src = dictionary["blendFuncSource"].asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
_blendFunc.src = dictionary["blendFuncSource"].asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2013-12-03 15:41:01 +08:00
|
|
|
_blendFunc.dst = dictionary["blendFuncDestination"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// color
|
2013-12-03 15:41:01 +08:00
|
|
|
_startColor.r = dictionary["startColorRed"].asFloat();
|
|
|
|
_startColor.g = dictionary["startColorGreen"].asFloat();
|
|
|
|
_startColor.b = dictionary["startColorBlue"].asFloat();
|
|
|
|
_startColor.a = dictionary["startColorAlpha"].asFloat();
|
|
|
|
|
|
|
|
_startColorVar.r = dictionary["startColorVarianceRed"].asFloat();
|
|
|
|
_startColorVar.g = dictionary["startColorVarianceGreen"].asFloat();
|
|
|
|
_startColorVar.b = dictionary["startColorVarianceBlue"].asFloat();
|
|
|
|
_startColorVar.a = dictionary["startColorVarianceAlpha"].asFloat();
|
|
|
|
|
|
|
|
_endColor.r = dictionary["finishColorRed"].asFloat();
|
|
|
|
_endColor.g = dictionary["finishColorGreen"].asFloat();
|
|
|
|
_endColor.b = dictionary["finishColorBlue"].asFloat();
|
|
|
|
_endColor.a = dictionary["finishColorAlpha"].asFloat();
|
|
|
|
|
|
|
|
_endColorVar.r = dictionary["finishColorVarianceRed"].asFloat();
|
|
|
|
_endColorVar.g = dictionary["finishColorVarianceGreen"].asFloat();
|
|
|
|
_endColorVar.b = dictionary["finishColorVarianceBlue"].asFloat();
|
|
|
|
_endColorVar.a = dictionary["finishColorVarianceAlpha"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// particle size
|
2013-12-03 15:41:01 +08:00
|
|
|
_startSize = dictionary["startParticleSize"].asFloat();
|
|
|
|
_startSizeVar = dictionary["startParticleSizeVariance"].asFloat();
|
|
|
|
_endSize = dictionary["finishParticleSize"].asFloat();
|
|
|
|
_endSizeVar = dictionary["finishParticleSizeVariance"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// position
|
2013-12-03 15:41:01 +08:00
|
|
|
float x = dictionary["sourcePositionx"].asFloat();
|
|
|
|
float y = dictionary["sourcePositiony"].asFloat();
|
2014-04-15 18:13:57 +08:00
|
|
|
this->setPosition( Vector2(x,y) );
|
2013-12-03 15:41:01 +08:00
|
|
|
_posVar.x = dictionary["sourcePositionVariancex"].asFloat();
|
|
|
|
_posVar.y = dictionary["sourcePositionVariancey"].asFloat();
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Spinning
|
2013-12-03 15:41:01 +08:00
|
|
|
_startSpin = dictionary["rotationStart"].asFloat();
|
|
|
|
_startSpinVar = dictionary["rotationStartVariance"].asFloat();
|
|
|
|
_endSpin= dictionary["rotationEnd"].asFloat();
|
|
|
|
_endSpinVar= dictionary["rotationEndVariance"].asFloat();
|
2011-07-04 19:20:16 +08:00
|
|
|
|
2013-12-03 15:41:01 +08:00
|
|
|
_emitterMode = (Mode) dictionary["emitterType"].asInt();
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode A: Gravity + tangential accel + radial accel
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// gravity
|
2013-12-03 15:41:01 +08:00
|
|
|
modeA.gravity.x = dictionary["gravityx"].asFloat();
|
|
|
|
modeA.gravity.y = dictionary["gravityy"].asFloat();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// speed
|
2013-12-03 15:41:01 +08:00
|
|
|
modeA.speed = dictionary["speed"].asFloat();
|
|
|
|
modeA.speedVar = dictionary["speedVariance"].asFloat();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// radial acceleration
|
2013-12-03 15:41:01 +08:00
|
|
|
modeA.radialAccel = dictionary["radialAcceleration"].asFloat();
|
|
|
|
modeA.radialAccelVar = dictionary["radialAccelVariance"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tangential acceleration
|
2013-12-03 15:41:01 +08:00
|
|
|
modeA.tangentialAccel = dictionary["tangentialAcceleration"].asFloat();
|
|
|
|
modeA.tangentialAccelVar = dictionary["tangentialAccelVariance"].asFloat();
|
2013-01-21 17:39:48 +08:00
|
|
|
|
|
|
|
// rotation is dir
|
2013-12-03 15:41:01 +08:00
|
|
|
modeA.rotationIsDir = dictionary["rotationIsDir"].asBool();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// or Mode B: radius movement
|
2013-07-26 13:48:32 +08:00
|
|
|
else if (_emitterMode == Mode::RADIUS)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-23 10:12:54 +08:00
|
|
|
if (_configName.length()>0)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.startRadius = dictionary["maxRadius"].asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.startRadius = dictionary["maxRadius"].asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.startRadiusVar = dictionary["maxRadiusVariance"].asFloat();
|
2013-09-23 10:12:54 +08:00
|
|
|
if (_configName.length()>0)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.endRadius = dictionary["minRadius"].asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.endRadius = dictionary["minRadius"].asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2014-03-21 16:25:22 +08:00
|
|
|
|
|
|
|
if (dictionary.find("minRadiusVariance") != dictionary.end())
|
|
|
|
{
|
|
|
|
modeB.endRadiusVar = dictionary["minRadiusVariance"].asFloat();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
modeB.endRadiusVar = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_configName.length()>0)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.rotatePerSecond = dictionary["rotatePerSecond"].asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.rotatePerSecond = dictionary["rotatePerSecond"].asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2013-12-03 15:41:01 +08:00
|
|
|
modeB.rotatePerSecondVar = dictionary["rotatePerSecondVariance"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
} else {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( false, "Invalid emitterType in config file");
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// life span
|
2013-12-03 15:41:01 +08:00
|
|
|
_life = dictionary["particleLifespan"].asFloat();
|
|
|
|
_lifeVar = dictionary["particleLifespanVariance"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// emission Rate
|
2013-06-15 14:03:30 +08:00
|
|
|
_emissionRate = _totalParticles / _life;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//don't get the internal texture if a batchNode is used
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-12 01:43:07 +08:00
|
|
|
// Set a compatible default for the alpha transfer
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = false;
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
// texture
|
|
|
|
// Try to get the texture from the cache
|
2013-12-03 15:41:01 +08:00
|
|
|
std::string textureName = dictionary["textureFileName"].asString();
|
2012-11-15 17:16:51 +08:00
|
|
|
|
2012-11-21 17:22:26 +08:00
|
|
|
size_t rPos = textureName.rfind('/');
|
|
|
|
|
|
|
|
if (rPos != string::npos)
|
2012-11-15 17:16:51 +08:00
|
|
|
{
|
2012-11-21 17:22:26 +08:00
|
|
|
string textureDir = textureName.substr(0, rPos + 1);
|
|
|
|
|
2014-01-08 10:47:27 +08:00
|
|
|
if (!dirname.empty() && textureDir != dirname)
|
2012-11-21 17:22:26 +08:00
|
|
|
{
|
|
|
|
textureName = textureName.substr(rPos+1);
|
2013-11-06 11:02:03 +08:00
|
|
|
textureName = dirname + textureName;
|
2012-11-21 17:22:26 +08:00
|
|
|
}
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
2014-01-08 10:47:27 +08:00
|
|
|
else if (!dirname.empty() && !textureName.empty())
|
2013-01-17 11:42:41 +08:00
|
|
|
{
|
2014-01-04 04:27:08 +08:00
|
|
|
textureName = dirname + textureName;
|
2013-01-17 11:42:41 +08:00
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D *tex = nullptr;
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2012-11-21 17:22:26 +08:00
|
|
|
if (textureName.length() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
// set not pop-up message box when load image failed
|
2013-12-18 17:47:20 +08:00
|
|
|
bool notify = FileUtils::getInstance()->isPopupNotify();
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils::getInstance()->setPopupNotify(false);
|
2014-01-08 10:47:27 +08:00
|
|
|
tex = Director::getInstance()->getTextureCache()->addImage(textureName);
|
2012-04-24 15:02:18 +08:00
|
|
|
// reset the value of UIImage notify
|
2013-12-18 17:47:20 +08:00
|
|
|
FileUtils::getInstance()->setPopupNotify(notify);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tex)
|
|
|
|
{
|
|
|
|
setTexture(tex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 15:41:01 +08:00
|
|
|
std::string textureData = dictionary["textureImageData"].asString();
|
|
|
|
CCASSERT(!textureData.empty(), "");
|
2012-04-24 15:02:18 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto dataLen = textureData.size();
|
2013-12-03 15:41:01 +08:00
|
|
|
if (dataLen != 0)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
|
|
|
// if it fails, try to get it from the base64-gzipped data
|
2013-12-03 15:41:01 +08:00
|
|
|
int decodeLen = base64Decode((unsigned char*)textureData.c_str(), (unsigned int)dataLen, &buffer);
|
2013-12-03 15:31:36 +08:00
|
|
|
CCASSERT( buffer != nullptr, "CCParticleSystem: error decoding textureImageData");
|
2012-04-24 15:02:18 +08:00
|
|
|
CC_BREAK_IF(!buffer);
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
|
2013-12-03 15:31:36 +08:00
|
|
|
CCASSERT( deflated != nullptr, "CCParticleSystem: error ungzipping textureImageData");
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(!deflated);
|
|
|
|
|
2013-11-07 19:11:09 +08:00
|
|
|
// For android, we should retain it in VolatileTexture::addImage which invoked in Director::getInstance()->getTextureCache()->addUIImage()
|
2013-06-20 14:13:12 +08:00
|
|
|
image = new Image();
|
2012-04-19 14:35:52 +08:00
|
|
|
bool isOK = image->initWithImageData(deflated, deflatedLen);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(isOK, "CCParticleSystem: error init image with Data");
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(!isOK);
|
|
|
|
|
2013-11-07 19:11:09 +08:00
|
|
|
setTexture(Director::getInstance()->getTextureCache()->addImage(image, textureName.c_str()));
|
2012-05-23 11:18:04 +08:00
|
|
|
|
|
|
|
image->release();
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2014-01-08 10:47:27 +08:00
|
|
|
|
|
|
|
if (!_configName.empty())
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2014-01-08 10:47:27 +08:00
|
|
|
_yCoordFlipped = dictionary["yCoordFlipped"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-01-08 10:47:27 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( this->_texture != nullptr, "CCParticleSystem: error loading the texture");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
2013-11-12 10:09:47 +08:00
|
|
|
free(buffer);
|
|
|
|
free(deflated);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
bool ParticleSystem::initWithTotalParticles(int numberOfParticles)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = numberOfParticles;
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_particles);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_particles = (tParticle*)calloc(_totalParticles, sizeof(tParticle));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! _particles )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("Particle system: not enough memory");
|
|
|
|
this->release();
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_allocatedParticles = numberOfParticles;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for (int i = 0; i < _totalParticles; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles[i].atlasIndex=i;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// default, active
|
2013-06-15 14:03:30 +08:00
|
|
|
_isActive = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default blend function
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default movement type;
|
2013-07-26 13:48:32 +08:00
|
|
|
_positionType = PositionType::FREE;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// by default be in mode A:
|
2013-07-26 13:48:32 +08:00
|
|
|
_emitterMode = Mode::GRAVITY;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default: modulate
|
|
|
|
// XXX: not used
|
|
|
|
// colorModulate = YES;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isAutoRemoveOnFinish = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Optimization: compile updateParticle method
|
2012-04-19 14:35:52 +08:00
|
|
|
//updateParticleSel = @selector(updateQuadWithParticle:newPosition:);
|
|
|
|
//updateParticleImp = (CC_UPDATE_PARTICLE_IMP) [self methodForSelector:updateParticleSel];
|
|
|
|
//for batchNode
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::~ParticleSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-02-27 16:26:42 +08:00
|
|
|
// Since the scheduler retains the "target (in this case the ParticleSystem)
|
|
|
|
// it is not needed to call "unscheduleUpdate" here. In fact, it will be called in "cleanup"
|
|
|
|
//unscheduleUpdate();
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_particles);
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystem::addParticle()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (this->isFull())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
tParticle * particle = &_particles[ _particleCount ];
|
2012-04-19 14:35:52 +08:00
|
|
|
this->initParticle(particle);
|
2013-06-15 14:03:30 +08:00
|
|
|
++_particleCount;
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::initParticle(tParticle* particle)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// timeToLive
|
|
|
|
// no negative life. prevent division by 0
|
2013-06-15 14:03:30 +08:00
|
|
|
particle->timeToLive = _life + _lifeVar * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->timeToLive = MAX(0, particle->timeToLive);
|
|
|
|
|
|
|
|
// position
|
2013-06-15 14:03:30 +08:00
|
|
|
particle->pos.x = _sourcePosition.x + _posVar.x * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
particle->pos.y = _sourcePosition.y + _posVar.y * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Color
|
2013-07-05 16:49:22 +08:00
|
|
|
Color4F start;
|
2013-06-15 14:03:30 +08:00
|
|
|
start.r = clampf(_startColor.r + _startColorVar.r * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
start.g = clampf(_startColor.g + _startColorVar.g * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
start.b = clampf(_startColor.b + _startColorVar.b * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
start.a = clampf(_startColor.a + _startColorVar.a * CCRANDOM_MINUS1_1(), 0, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Color4F end;
|
2013-06-15 14:03:30 +08:00
|
|
|
end.r = clampf(_endColor.r + _endColorVar.r * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
end.g = clampf(_endColor.g + _endColorVar.g * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
end.b = clampf(_endColor.b + _endColorVar.b * CCRANDOM_MINUS1_1(), 0, 1);
|
|
|
|
end.a = clampf(_endColor.a + _endColorVar.a * CCRANDOM_MINUS1_1(), 0, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
particle->color = start;
|
|
|
|
particle->deltaColor.r = (end.r - start.r) / particle->timeToLive;
|
|
|
|
particle->deltaColor.g = (end.g - start.g) / particle->timeToLive;
|
|
|
|
particle->deltaColor.b = (end.b - start.b) / particle->timeToLive;
|
|
|
|
particle->deltaColor.a = (end.a - start.a) / particle->timeToLive;
|
|
|
|
|
|
|
|
// size
|
2013-06-15 14:03:30 +08:00
|
|
|
float startS = _startSize + _startSizeVar * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
startS = MAX(0, startS); // No negative value
|
|
|
|
|
|
|
|
particle->size = startS;
|
|
|
|
|
2013-07-25 14:27:46 +08:00
|
|
|
if (_endSize == START_SIZE_EQUAL_TO_END_SIZE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
particle->deltaSize = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float endS = _endSize + _endSizeVar * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
endS = MAX(0, endS); // No negative values
|
|
|
|
particle->deltaSize = (endS - startS) / particle->timeToLive;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotation
|
2013-06-15 14:03:30 +08:00
|
|
|
float startA = _startSpin + _startSpinVar * CCRANDOM_MINUS1_1();
|
|
|
|
float endA = _endSpin + _endSpinVar * CCRANDOM_MINUS1_1();
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->rotation = startA;
|
|
|
|
particle->deltaRotation = (endA - startA) / particle->timeToLive;
|
|
|
|
|
|
|
|
// position
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_positionType == PositionType::FREE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
particle->startPos = this->convertToWorldSpace(Vector2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 13:48:32 +08:00
|
|
|
else if (_positionType == PositionType::RELATIVE)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
particle->startPos = _position;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// direction
|
2013-06-15 14:03:30 +08:00
|
|
|
float a = CC_DEGREES_TO_RADIANS( _angle + _angleVar * CCRANDOM_MINUS1_1() );
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode Gravity: A
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 v(cosf( a ), sinf( a ));
|
2012-04-19 14:35:52 +08:00
|
|
|
float s = modeA.speed + modeA.speedVar * CCRANDOM_MINUS1_1();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// direction
|
2013-07-11 16:38:58 +08:00
|
|
|
particle->modeA.dir = v * s ;
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// radial accel
|
|
|
|
particle->modeA.radialAccel = modeA.radialAccel + modeA.radialAccelVar * CCRANDOM_MINUS1_1();
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// tangential accel
|
|
|
|
particle->modeA.tangentialAccel = modeA.tangentialAccel + modeA.tangentialAccelVar * CCRANDOM_MINUS1_1();
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-01-21 17:39:48 +08:00
|
|
|
// rotation is dir
|
|
|
|
if(modeA.rotationIsDir)
|
2013-07-11 16:38:58 +08:00
|
|
|
particle->rotation = -CC_RADIANS_TO_DEGREES(particle->modeA.dir.getAngle());
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode Radius: B
|
2012-04-24 15:02:18 +08:00
|
|
|
else
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Set the default diameter of the particle from the source position
|
|
|
|
float startRadius = modeB.startRadius + modeB.startRadiusVar * CCRANDOM_MINUS1_1();
|
|
|
|
float endRadius = modeB.endRadius + modeB.endRadiusVar * CCRANDOM_MINUS1_1();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->modeB.radius = startRadius;
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-07-25 14:27:46 +08:00
|
|
|
if (modeB.endRadius == START_RADIUS_EQUAL_TO_END_RADIUS)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->modeB.deltaRadius = 0;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->modeB.deltaRadius = (endRadius - startRadius) / particle->timeToLive;
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
particle->modeB.angle = a;
|
|
|
|
particle->modeB.degreesPerSecond = CC_DEGREES_TO_RADIANS(modeB.rotatePerSecond + modeB.rotatePerSecondVar * CCRANDOM_MINUS1_1());
|
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2014-03-24 18:08:55 +08:00
|
|
|
void ParticleSystem::onEnter()
|
|
|
|
{
|
|
|
|
Node::onEnter();
|
|
|
|
|
|
|
|
// update after action in run!
|
|
|
|
this->scheduleUpdateWithPriority(1);
|
|
|
|
}
|
|
|
|
|
2014-03-28 14:05:38 +08:00
|
|
|
void ParticleSystem::onExit()
|
|
|
|
{
|
|
|
|
this->unscheduleUpdate();
|
|
|
|
Node::onExit();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::stopSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isActive = false;
|
|
|
|
_elapsed = _duration;
|
|
|
|
_emitCounter = 0;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::resetSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isActive = true;
|
|
|
|
_elapsed = 0;
|
|
|
|
for (_particleIdx = 0; _particleIdx < _particleCount; ++_particleIdx)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
tParticle *p = &_particles[_particleIdx];
|
2012-04-19 14:35:52 +08:00
|
|
|
p->timeToLive = 0;
|
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystem::isFull()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_particleCount == _totalParticles);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - MainLoop
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::update(float dt)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROFILER_START_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isActive && _emissionRate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float rate = 1.0f / _emissionRate;
|
2012-04-19 14:35:52 +08:00
|
|
|
//issue #1201, prevent bursts of particles, due to too high emitCounter
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_particleCount < _totalParticles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_emitCounter += dt;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
while (_particleCount < _totalParticles && _emitCounter > rate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->addParticle();
|
2013-06-15 14:03:30 +08:00
|
|
|
_emitCounter -= rate;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed += dt;
|
|
|
|
if (_duration != -1 && _duration < _elapsed)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->stopSystem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_particleIdx = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 currentPosition = Vector2::ZERO;
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_positionType == PositionType::FREE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
currentPosition = this->convertToWorldSpace(Vector2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 13:48:32 +08:00
|
|
|
else if (_positionType == PositionType::RELATIVE)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
currentPosition = _position;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
while (_particleIdx < _particleCount)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
tParticle *p = &_particles[_particleIdx];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// life
|
|
|
|
p->timeToLive -= dt;
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
if (p->timeToLive > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Mode A: gravity, direction, tangential accel & radial accel
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 tmp, radial, tangential;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
radial = Vector2::ZERO;
|
2012-04-19 14:35:52 +08:00
|
|
|
// radial acceleration
|
2012-04-24 15:02:18 +08:00
|
|
|
if (p->pos.x || p->pos.y)
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
radial = p->pos.normalize();
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
tangential = radial;
|
2013-07-11 16:38:58 +08:00
|
|
|
radial = radial * p->modeA.radialAccel;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tangential acceleration
|
|
|
|
float newy = tangential.x;
|
|
|
|
tangential.x = -tangential.y;
|
|
|
|
tangential.y = newy;
|
2013-07-11 16:38:58 +08:00
|
|
|
tangential = tangential * p->modeA.tangentialAccel;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// (gravity + radial + tangential) * dt
|
2013-07-11 16:38:58 +08:00
|
|
|
tmp = radial + tangential + modeA.gravity;
|
|
|
|
tmp = tmp * dt;
|
|
|
|
p->modeA.dir = p->modeA.dir + tmp;
|
2013-09-23 10:12:54 +08:00
|
|
|
if (_configName.length()>0)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2013-09-23 10:12:54 +08:00
|
|
|
if (_yCoordFlipped == -1)
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
|
|
|
tmp = p->modeA.dir * dt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = p->modeA.dir * -dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = p->modeA.dir * dt;
|
|
|
|
}
|
2013-07-11 16:38:58 +08:00
|
|
|
p->pos = p->pos + tmp;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mode B: radius movement
|
2012-04-24 15:02:18 +08:00
|
|
|
else
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Update the angle and radius of the particle.
|
|
|
|
p->modeB.angle += p->modeB.degreesPerSecond * dt;
|
|
|
|
p->modeB.radius += p->modeB.deltaRadius * dt;
|
|
|
|
|
|
|
|
p->pos.x = - cosf(p->modeB.angle) * p->modeB.radius;
|
|
|
|
p->pos.y = - sinf(p->modeB.angle) * p->modeB.radius;
|
2013-09-23 16:24:33 +08:00
|
|
|
if (_yCoordFlipped == 1)
|
|
|
|
{
|
|
|
|
p->pos.y = -p->pos.y;
|
|
|
|
}
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// color
|
|
|
|
p->color.r += (p->deltaColor.r * dt);
|
|
|
|
p->color.g += (p->deltaColor.g * dt);
|
|
|
|
p->color.b += (p->deltaColor.b * dt);
|
|
|
|
p->color.a += (p->deltaColor.a * dt);
|
|
|
|
|
|
|
|
// size
|
|
|
|
p->size += (p->deltaSize * dt);
|
|
|
|
p->size = MAX( 0, p->size );
|
|
|
|
|
|
|
|
// angle
|
|
|
|
p->rotation += (p->deltaRotation * dt);
|
|
|
|
|
|
|
|
//
|
|
|
|
// update values in quad
|
|
|
|
//
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 newPos;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_positionType == PositionType::FREE || _positionType == PositionType::RELATIVE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 diff = currentPosition - p->startPos;
|
2013-07-11 16:38:58 +08:00
|
|
|
newPos = p->pos - diff;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newPos = p->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate newPos to correct position, since matrix transform isn't performed in batchnode
|
|
|
|
// don't update the particle with the new position information, it will interfere with the radius and tangential calculations
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
newPos.x+=_position.x;
|
|
|
|
newPos.y+=_position.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateQuadWithParticle(p, newPos);
|
|
|
|
//updateParticleImp(self, updateParticleSel, p, newPos);
|
|
|
|
|
|
|
|
// update particle counter
|
2013-06-15 14:03:30 +08:00
|
|
|
++_particleIdx;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// life < 0
|
|
|
|
int currentIndex = p->atlasIndex;
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _particleIdx != _particleCount-1 )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles[_particleIdx] = _particles[_particleCount-1];
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//disable the switched particle
|
2013-06-15 14:03:30 +08:00
|
|
|
_batchNode->disableParticle(_atlasIndex+currentIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//switch indexes
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles[_particleCount-1].atlasIndex = currentIndex;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
--_particleCount;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _particleCount == 0 && _isAutoRemoveOnFinish )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->unscheduleUpdate();
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->removeChild(this, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //while
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-03-27 16:47:21 +08:00
|
|
|
|
|
|
|
// only update gl buffer when visible
|
|
|
|
if (_visible && ! _batchNode)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
postStep();
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROFILER_STOP_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::updateWithNoTime(void)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->update(0.0f);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void ParticleSystem::updateQuadWithParticle(tParticle* particle, const Vector2& newPosition)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(particle);
|
|
|
|
CC_UNUSED_PARAM(newPosition);
|
2012-09-17 15:02:24 +08:00
|
|
|
// should be overridden
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::postStep()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// should be overridden
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// ParticleSystem - Texture protocol
|
|
|
|
void ParticleSystem::setTexture(Texture2D* var)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_texture != var)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 14:11:48 +08:00
|
|
|
CC_SAFE_RETAIN(var);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
_texture = var;
|
2012-06-08 14:11:48 +08:00
|
|
|
updateBlendFunc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::updateBlendFunc()
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! _batchNode, "Can't change blending functions when the particle is being batched");
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
bool premultiplied = _texture->hasPremultipliedAlpha();
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = false;
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture && ( _blendFunc.src == CC_BLEND_SRC && _blendFunc.dst == CC_BLEND_DST ) )
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
|
|
|
if( premultiplied )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = true;
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
Texture2D * ParticleSystem::getTexture() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _texture;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - Additive Blending
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setBlendAdditive(bool additive)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( additive )
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ADDITIVE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture && ! _texture->hasPremultipliedAlpha() )
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isBlendAdditive() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return( _blendFunc.src == GL_SRC_ALPHA && _blendFunc.dst == GL_ONE);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - Properties of Gravity Mode
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setTangentialAccel(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.tangentialAccel = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getTangentialAccel() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.tangentialAccel;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setTangentialAccelVar(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.tangentialAccelVar = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getTangentialAccelVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.tangentialAccelVar;
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRadialAccel(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.radialAccel = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRadialAccel() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.radialAccel;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRadialAccelVar(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.radialAccelVar = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRadialAccelVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.radialAccelVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotationIsDir(bool t)
|
2013-01-21 17:39:48 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2013-01-21 17:39:48 +08:00
|
|
|
modeA.rotationIsDir = t;
|
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::getRotationIsDir() const
|
2013-01-21 17:39:48 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2013-01-21 17:39:48 +08:00
|
|
|
return modeA.rotationIsDir;
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
void ParticleSystem::setGravity(const Vector2& g)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.gravity = g;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
const Vector2& ParticleSystem::getGravity()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.gravity;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setSpeed(float speed)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.speed = speed;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getSpeed() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.speed;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setSpeedVar(float speedVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.speedVar = speedVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getSpeedVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.speedVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - Properties of Radius Mode
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setStartRadius(float startRadius)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.startRadius = startRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getStartRadius() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.startRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setStartRadiusVar(float startRadiusVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.startRadiusVar = startRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getStartRadiusVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.startRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setEndRadius(float endRadius)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.endRadius = endRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getEndRadius() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.endRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setEndRadiusVar(float endRadiusVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.endRadiusVar = endRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getEndRadiusVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.endRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotatePerSecond(float degrees)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.rotatePerSecond = degrees;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRotatePerSecond() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.rotatePerSecond;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotatePerSecondVar(float degrees)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.rotatePerSecondVar = degrees;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRotatePerSecondVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-07-26 13:48:32 +08:00
|
|
|
CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.rotatePerSecondVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isActive() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isActive;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
int ParticleSystem::getTotalParticles() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalParticles;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
void ParticleSystem::setTotalParticles(int var)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( var <= _allocatedParticles, "Particle: resizing particle array only supported for quads");
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = var;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& ParticleSystem::getBlendFunc() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void ParticleSystem::setBlendFunc(const BlendFunc &blendFunc)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst ) {
|
|
|
|
_blendFunc = blendFunc;
|
2012-06-12 01:43:07 +08:00
|
|
|
this->updateBlendFunc();
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isAutoRemoveOnFinish() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isAutoRemoveOnFinish;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setAutoRemoveOnFinish(bool var)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isAutoRemoveOnFinish = var;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// ParticleSystem - methods for batchNode rendering
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
ParticleBatchNode* ParticleSystem::getBatchNode(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _batchNode;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setBatchNode(ParticleBatchNode* batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _batchNode != batchNode ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_batchNode = batchNode; // weak reference
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if( batchNode ) {
|
|
|
|
//each particle needs a unique index
|
2013-09-08 11:26:38 +08:00
|
|
|
for (int i = 0; i < _totalParticles; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_particles[i].atlasIndex=i;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//don't use a transform matrix, this is faster
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScale(float s)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScale(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotation(float newRotation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setRotation(newRotation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScaleX(float newScaleX)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScaleX(newScaleX);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScaleY(float newScaleY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScaleY(newScaleY);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2010-12-28 10:23:14 +08:00
|
|
|
|