axmol/cocos2dx/include/CCParticleSystem.h

413 lines
14 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
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.
****************************************************************************/
#ifndef __CCPARTICLE_SYSTEM_H__
#define __CCPARTICLE_SYSTEM_H__
2010-08-23 14:57:37 +08:00
#include "CCProtocols.h"
#include "CCNode.h"
#include "CCMutableDictionary.h"
#include "CCString.h"
namespace cocos2d {
2010-08-23 14:57:37 +08:00
class CCParticleBatchNode;
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
//* @enum
enum {
/** The Particle emitter lives forever */
kCCParticleDurationInfinity = -1,
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
/** The starting size of the particle is equal to the ending size */
kCCParticleStartSizeEqualToEndSize = -1,
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
/** The starting radius of the particle is equal to the ending radius */
kCCParticleStartRadiusEqualToEndRadius = -1,
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
// backward compatible
kParticleStartSizeEqualToEndSize = kCCParticleStartSizeEqualToEndSize,
kParticleDurationInfinity = kCCParticleDurationInfinity,
};
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
//* @enum
enum {
/** Gravity mode (A mode) */
kCCParticleModeGravity,
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
/** Radius mode (B mode) */
kCCParticleModeRadius,
};
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
/** @typedef tCCPositionType
possible types of particle positions
*/
typedef enum {
/** Living particles are attached to the world and are unaffected by emitter repositioning. */
2010-12-27 17:39:15 +08:00
kCCPositionTypeFree,
2010-08-23 14:57:37 +08:00
/** Living particles are attached to the world but will follow the emitter repositioning.
Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.
*/
kCCPositionTypeRelative,
/** Living particles are attached to the emitter and are translated along with it. */
2010-12-27 17:39:15 +08:00
kCCPositionTypeGrouped,
}tCCPositionType;
// backward compatible
enum {
kPositionTypeFree = kCCPositionTypeFree,
kPositionTypeGrouped = kCCPositionTypeGrouped,
};
/**
Structure that contains the values of each particle
*/
typedef struct sCCParticle {
CCPoint pos;
CCPoint startPos;
2010-12-27 17:39:15 +08:00
ccColor4F color;
ccColor4F deltaColor;
float size;
float deltaSize;
float rotation;
float deltaRotation;
ccTime timeToLive;
unsigned int atlasIndex;
2010-12-27 17:39:15 +08:00
//! Mode A: gravity, direction, radial accel, tangential accel
struct {
CCPoint dir;
2010-12-27 17:39:15 +08:00
float radialAccel;
float tangentialAccel;
} modeA;
//! Mode B: radius mode
struct {
float angle;
float degreesPerSecond;
float radius;
float deltaRadius;
} modeB;
}tCCParticle;
//typedef void (*CC_UPDATE_PARTICLE_IMP)(id, SEL, tCCParticle*, CCPoint);
2010-12-27 17:39:15 +08:00
class CCTexture2D;
/** @brief Particle System base class.
Attributes of a Particle System:
- emmision rate of the particles
- Gravity Mode (Mode A):
- gravity
- direction
- speed +- variance
- tangential acceleration +- variance
- radial acceleration +- variance
- Radius Mode (Mode B):
- startRadius +- variance
- endRadius +- variance
- rotate +- variance
- Properties common to all modes:
- life +- life variance
- start spin +- variance
- end spin +- variance
- start size +- variance
- end size +- variance
- start color +- variance
- end color +- variance
- life +- variance
- blending function
- texture
cocos2d also supports particles generated by Particle Designer (http://particledesigner.71squared.com/).
'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guarateed in cocos2d,
cocos2d uses a another approach, but the results are almost identical.
cocos2d supports all the variables used by Particle Designer plus a bit more:
- spinning particles (supported when using CCParticleSystemQuad)
- tangential acceleration (Gravity mode)
- radial acceleration (Gravity mode)
- radius direction (Radius mode) (Particle Designer supports outwards to inwards direction only)
It is possible to customize any of the above mentioned properties in runtime. Example:
@code
emitter.radialAccel = 15;
emitter.startSpin = 0;
@endcode
*/
class CC_DLL CCParticleSystem : public CCNode, public CCTextureProtocol
2010-12-27 17:39:15 +08:00
{
protected:
std::string m_sPlistFile;
//! time elapsed since the start of the system (in seconds)
float m_fElapsed;
// Different modes
//! Mode A:Gravity + Tangential Accel + Radial Accel
struct {
/** Gravity value. Only available in 'Gravity' mode. */
CCPoint gravity;
2010-12-27 17:39:15 +08:00
/** speed of each particle. Only available in 'Gravity' mode. */
float speed;
/** speed variance of each particle. Only available in 'Gravity' mode. */
float speedVar;
/** tangential acceleration of each particle. Only available in 'Gravity' mode. */
float tangentialAccel;
/** tangential acceleration variance of each particle. Only available in 'Gravity' mode. */
float tangentialAccelVar;
/** radial acceleration of each particle. Only available in 'Gravity' mode. */
float radialAccel;
/** radial acceleration variance of each particle. Only available in 'Gravity' mode. */
float radialAccelVar;
} modeA;
//! Mode B: circular movement (gravity, radial accel and tangential accel don't are not used in this mode)
struct {
/** The starting radius of the particles. Only available in 'Radius' mode. */
float startRadius;
/** The starting radius variance of the particles. Only available in 'Radius' mode. */
float startRadiusVar;
/** The ending radius of the particles. Only available in 'Radius' mode. */
float endRadius;
/** The ending radius variance of the particles. Only available in 'Radius' mode. */
float endRadiusVar;
/** Number of degress to rotate a particle around the source pos per second. Only available in 'Radius' mode. */
float rotatePerSecond;
/** Variance in degrees for rotatePerSecond. Only available in 'Radius' mode. */
float rotatePerSecondVar;
} modeB;
//! Array of particles
tCCParticle *m_pParticles;
// color modulate
// BOOL colorModulate;
//! How many particles can be emitted per second
float m_fEmitCounter;
//! particle idx
2011-07-07 11:27:45 +08:00
unsigned int m_uParticleIdx;
2010-12-27 17:39:15 +08:00
// Optimization
//CC_UPDATE_PARTICLE_IMP updateParticleImp;
//SEL updateParticleSel;
/** weak reference to the CCSpriteBatchNode that renders the CCSprite */
CC_PROPERTY(CCParticleBatchNode*, m_pBatchNode, BatchNode);
// index of system in batch node array
CC_SYNTHESIZE(unsigned int, m_uAtlasIndex, AtlasIndex);
//true if scaled or rotated
bool m_bTransformSystemDirty;
// Number of allocated particles
unsigned int m_uAllocatedParticles;
2010-08-23 14:57:37 +08:00
2010-12-27 17:39:15 +08:00
/** Is the emitter active */
CC_PROPERTY_READONLY(bool, m_bIsActive, IsActive)
2010-12-27 17:39:15 +08:00
/** Quantity of particles that are being simulated at the moment */
CC_PROPERTY_READONLY(unsigned int, m_uParticleCount, ParticleCount)
2010-12-27 17:39:15 +08:00
/** How many seconds the emitter wil run. -1 means 'forever' */
CC_PROPERTY(float, m_fDuration, Duration)
2010-12-27 17:39:15 +08:00
/** sourcePosition of the emitter */
CC_PROPERTY_PASS_BY_REF(CCPoint, m_tSourcePosition, SourcePosition)
2010-12-27 17:39:15 +08:00
/** Position variance of the emitter */
CC_PROPERTY_PASS_BY_REF(CCPoint, m_tPosVar, PosVar)
2010-12-27 17:39:15 +08:00
/** life, and life variation of each particle */
CC_PROPERTY(float, m_fLife, Life)
2010-12-27 17:39:15 +08:00
/** life variance of each particle */
CC_PROPERTY(float, m_fLifeVar, LifeVar)
2010-12-27 17:39:15 +08:00
/** angle and angle variation of each particle */
CC_PROPERTY(float, m_fAngle, Angle)
2010-12-27 17:39:15 +08:00
/** angle variance of each particle */
CC_PROPERTY(float, m_fAngleVar, AngleVar)
2010-12-27 17:39:15 +08:00
//////////////////////////////////////////////////////////////////////////
2010-12-27 17:39:15 +08:00
public:
// mode A
virtual const CCPoint& getGravity();
virtual void setGravity(const CCPoint& g);
2010-12-27 17:39:15 +08:00
virtual float getSpeed();
virtual void setSpeed(float speed);
virtual float getSpeedVar();
virtual void setSpeedVar(float speed);
virtual float getTangentialAccel();
virtual void setTangentialAccel(float t);
virtual float getTangentialAccelVar();
virtual void setTangentialAccelVar(float t);
virtual float getRadialAccel();
virtual void setRadialAccel(float t);
virtual float getRadialAccelVar();
virtual void setRadialAccelVar(float t);
// mode B
virtual float getStartRadius();
virtual void setStartRadius(float startRadius);
virtual float getStartRadiusVar();
virtual void setStartRadiusVar(float startRadiusVar);
virtual float getEndRadius();
virtual void setEndRadius(float endRadius);
virtual float getEndRadiusVar();
virtual void setEndRadiusVar(float endRadiusVar);
virtual float getRotatePerSecond();
virtual void setRotatePerSecond(float degrees);
virtual float getRotatePerSecondVar();
virtual void setRotatePerSecondVar(float degrees);
void setZOrder(int z);
virtual void setScale(float s);
virtual void setRotation(float newRotation);
virtual void setScaleX(float newScaleX);
virtual void setScaleY(float newScaleY);
//////////////////////////////////////////////////////////////////////////
2010-12-27 17:39:15 +08:00
/** start size in pixels of each particle */
CC_PROPERTY(float, m_fStartSize, StartSize)
2010-12-27 17:39:15 +08:00
/** size variance in pixels of each particle */
CC_PROPERTY(float, m_fStartSizeVar, StartSizeVar)
2010-12-27 17:39:15 +08:00
/** end size in pixels of each particle */
CC_PROPERTY(float, m_fEndSize, EndSize)
2010-12-27 17:39:15 +08:00
/** end size variance in pixels of each particle */
CC_PROPERTY(float, m_fEndSizeVar, EndSizeVar)
2010-12-27 17:39:15 +08:00
/** start color of each particle */
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tStartColor, StartColor)
2010-12-27 17:39:15 +08:00
/** start color variance of each particle */
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tStartColorVar, StartColorVar)
2010-12-27 17:39:15 +08:00
/** end color and end color variation of each particle */
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tEndColor, EndColor)
2010-12-27 17:39:15 +08:00
/** end color variance of each particle */
CC_PROPERTY_PASS_BY_REF(ccColor4F, m_tEndColorVar, EndColorVar)
2010-12-27 17:39:15 +08:00
//* initial angle of each particle
CC_PROPERTY(float, m_fStartSpin, StartSpin)
2010-12-27 17:39:15 +08:00
//* initial angle of each particle
CC_PROPERTY(float, m_fStartSpinVar, StartSpinVar)
2010-12-27 17:39:15 +08:00
//* initial angle of each particle
CC_PROPERTY(float, m_fEndSpin, EndSpin)
2010-12-27 17:39:15 +08:00
//* initial angle of each particle
CC_PROPERTY(float, m_fEndSpinVar, EndSpinVar)
2010-12-27 17:39:15 +08:00
/** emission rate of the particles */
CC_PROPERTY(float, m_fEmissionRate, EmissionRate)
2010-12-27 17:39:15 +08:00
/** maximum particles of the system */
CC_PROPERTY(unsigned int, m_uTotalParticles, TotalParticles)
2010-12-27 17:39:15 +08:00
/** conforms to CocosNodeTexture protocol */
CC_PROPERTY(CCTexture2D*, m_pTexture, Texture)
2010-12-27 17:39:15 +08:00
/** conforms to CocosNodeTexture protocol */
CC_PROPERTY(ccBlendFunc, m_tBlendFunc, BlendFunc)
2010-12-27 17:39:15 +08:00
/** whether or not the particles are using blend additive.
If enabled, the following blending function will be used.
2010-08-23 14:57:37 +08:00
@code
2010-12-27 17:39:15 +08:00
source blend function = GL_SRC_ALPHA;
dest blend function = GL_ONE;
2010-08-23 14:57:37 +08:00
@endcode
2010-12-27 17:39:15 +08:00
*/
CC_PROPERTY(bool, m_bIsBlendAdditive, IsBlendAdditive)
2010-12-27 17:39:15 +08:00
/** particles movement type: Free or Grouped
@since v0.8
*/
CC_PROPERTY(tCCPositionType, m_ePositionType, PositionType)
2010-12-27 17:39:15 +08:00
/** whether or not the node will be auto-removed when it has no particles left.
By default it is false.
@since v0.8
*/
CC_PROPERTY(bool, m_bIsAutoRemoveOnFinish, IsAutoRemoveOnFinish)
2010-12-27 17:39:15 +08:00
/** Switch between different kind of emitter modes:
- kCCParticleModeGravity: uses gravity, speed, radial and tangential acceleration
- kCCParticleModeRadius: uses radius movement + rotation
*/
CC_PROPERTY(int, m_nEmitterMode, EmitterMode)
2010-12-27 17:39:15 +08:00
public:
CCParticleSystem();
virtual ~CCParticleSystem();
/** creates an initializes a CCParticleSystem from a plist file.
This plist files can be creted manually or with Particle Designer:
http://particledesigner.71squared.com/
@since v0.99.3
*/
static CCParticleSystem * particleWithFile(const char *plistFile);
2010-08-23 14:57:37 +08:00
/** initializes a CCParticleSystem*/
bool init();
2010-12-27 17:39:15 +08:00
/** initializes a CCParticleSystem from a plist file.
This plist files can be creted manually or with Particle Designer:
http://particledesigner.71squared.com/
@since v0.99.3
2010-08-23 14:57:37 +08:00
*/
2010-12-27 17:39:15 +08:00
bool initWithFile(const char *plistFile);
2010-08-23 14:57:37 +08:00
2011-03-07 18:20:48 +08:00
/** initializes a CCQuadParticleSystem from a CCDictionary.
2010-12-27 17:39:15 +08:00
@since v0.99.3
*/
2011-03-07 18:20:48 +08:00
bool initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary);
2010-12-27 17:39:15 +08:00
//! Initializes a system with a fixed number of particles
virtual bool initWithTotalParticles(unsigned int numberOfParticles);
2010-12-27 17:39:15 +08:00
//! Add a particle to the emitter
bool addParticle();
//! Initializes a particle
void initParticle(tCCParticle* particle);
//! stop emitting particles. Running particles will continue to run until they die
void stopSystem();
//! Kill all living particles.
void resetSystem();
//! whether or not the system is full
bool isFull();
//! should be overriden by subclasses
virtual void updateQuadWithParticle(tCCParticle* particle, const CCPoint& newPosition);
2010-12-27 17:39:15 +08:00
//! should be overriden by subclasses
virtual void postStep();
virtual void update(ccTime dt);
virtual void updateWithNoTime(void);
2010-12-27 17:39:15 +08:00
private:
/** Private method, return the string found by key in dict.
@return "" if not found; return the string if found.
*/
2011-03-07 18:20:48 +08:00
inline const char * valueForKey(const char *key, CCDictionary<std::string, CCObject*> *dict)
2010-12-27 17:39:15 +08:00
{
if (dict)
{
CCString *pString = (CCString*)dict->objectForKey(std::string(key));
return pString ? pString->m_sString.c_str() : "";
}
2010-12-27 17:39:15 +08:00
return "";
}
2010-08-23 14:57:37 +08:00
};
}// namespace cocos2d
#endif //__CCPARTICLE_SYSTEM_H__