/libs/cocos2d/CCParticleSystem.h

http://github.com/kstenerud/ObjectAL-for-iPhone · C Header · 436 lines · 165 code · 57 blank · 214 comment · 0 complexity · 1d5b79fb0f8ed6e89a784f960a6eeab0 MD5 · raw file

  1. /*
  2. * cocos2d for iPhone: http://www.cocos2d-iphone.org
  3. *
  4. * Copyright (c) 2008-2010 Ricardo Quesada
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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. */
  25. #import "CCProtocols.h"
  26. #import "CCNode.h"
  27. #import "ccTypes.h"
  28. #import "ccConfig.h"
  29. #if CC_ENABLE_PROFILERS
  30. @class CCProfilingTimer;
  31. #endif
  32. //* @enum
  33. enum {
  34. /** The Particle emitter lives forever */
  35. kCCParticleDurationInfinity = -1,
  36. /** The starting size of the particle is equal to the ending size */
  37. kCCParticleStartSizeEqualToEndSize = -1,
  38. /** The starting radius of the particle is equal to the ending radius */
  39. kCCParticleStartRadiusEqualToEndRadius = -1,
  40. // backward compatible
  41. kParticleStartSizeEqualToEndSize = kCCParticleStartSizeEqualToEndSize,
  42. kParticleDurationInfinity = kCCParticleDurationInfinity,
  43. };
  44. //* @enum
  45. enum {
  46. /** Gravity mode (A mode) */
  47. kCCParticleModeGravity,
  48. /** Radius mode (B mode) */
  49. kCCParticleModeRadius,
  50. };
  51. /** @typedef tCCPositionType
  52. possible types of particle positions
  53. */
  54. typedef enum {
  55. /** If the emitter is repositioned, the living particles won't be repositioned */
  56. kCCPositionTypeFree,
  57. /** If the emitter is repositioned, the living particles will be repositioned too */
  58. kCCPositionTypeGrouped,
  59. }tCCPositionType;
  60. // backward compatible
  61. enum {
  62. kPositionTypeFree = kCCPositionTypeFree,
  63. kPositionTypeGrouped = kCCPositionTypeGrouped,
  64. };
  65. /** @struct tCCParticle
  66. Structure that contains the values of each particle
  67. */
  68. typedef struct sCCParticle {
  69. CGPoint pos;
  70. CGPoint startPos;
  71. ccColor4F color;
  72. ccColor4F deltaColor;
  73. float size;
  74. float deltaSize;
  75. float rotation;
  76. float deltaRotation;
  77. ccTime timeToLive;
  78. union {
  79. // Mode A: gravity, direction, radial accel, tangential accel
  80. struct {
  81. CGPoint dir;
  82. float radialAccel;
  83. float tangentialAccel;
  84. } A;
  85. // Mode B: radius mode
  86. struct {
  87. float angle;
  88. float degreesPerSecond;
  89. float radius;
  90. float deltaRadius;
  91. } B;
  92. } mode;
  93. }tCCParticle;
  94. typedef void (*CC_UPDATE_PARTICLE_IMP)(id, SEL, tCCParticle*, CGPoint);
  95. @class CCTexture2D;
  96. /** Particle System base class
  97. Attributes of a Particle System:
  98. - emmision rate of the particles
  99. - Gravity Mode (Mode A):
  100. - gravity
  101. - direction
  102. - speed +- variance
  103. - tangential acceleration +- variance
  104. - radial acceleration +- variance
  105. - Radius Mode (Mode B):
  106. - startRadius +- variance
  107. - endRadius +- variance
  108. - rotate +- variance
  109. - Properties common to all modes:
  110. - life +- life variance
  111. - start spin +- variance
  112. - end spin +- variance
  113. - start size +- variance
  114. - end size +- variance
  115. - start color +- variance
  116. - end color +- variance
  117. - life +- variance
  118. - blending function
  119. - texture
  120. cocos2d also supports particles generated by Particle Designer (http://particledesigner.71squared.com/).
  121. 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guarateed in cocos2d,
  122. cocos2d uses a another approach, but the results are almost identical.
  123. cocos2d supports all the variables used by Particle Designer plus a bit more:
  124. - spinning particles (supported when using CCQuadParticleSystem)
  125. - tangential acceleration (Gravity mode)
  126. - radial acceleration (Gravity mode)
  127. - radius direction (Radius mode) (Particle Designer supports outwards to inwards direction only)
  128. It is possible to customize any of the above mentioned properties in runtime. Example:
  129. @code
  130. emitter.radialAccel = 15;
  131. emitter.startSpin = 0;
  132. @endcode
  133. */
  134. @interface CCParticleSystem : CCNode <CCTextureProtocol>
  135. {
  136. // is the particle system active ?
  137. BOOL active;
  138. // duration in seconds of the system. -1 is infinity
  139. float duration;
  140. // time elapsed since the start of the system (in seconds)
  141. float elapsed;
  142. // position is from "superclass" CocosNode
  143. // Emitter centerOfGravity position
  144. CGPoint centerOfGravity;
  145. // Position variance
  146. CGPoint posVar;
  147. // The angle (direction) of the particles measured in degrees
  148. float angle;
  149. // Angle variance measured in degrees;
  150. float angleVar;
  151. // Different modes
  152. int emitterMode_;
  153. union {
  154. // Mode A:Gravity + Tangential Accel + Radial Accel
  155. struct {
  156. // gravity of the particles
  157. CGPoint gravity;
  158. // The speed the particles will have.
  159. float speed;
  160. // The speed variance
  161. float speedVar;
  162. // Tangential acceleration
  163. float tangentialAccel;
  164. // Tangential acceleration variance
  165. float tangentialAccelVar;
  166. // Radial acceleration
  167. float radialAccel;
  168. // Radial acceleration variance
  169. float radialAccelVar;
  170. } A;
  171. // Mode B: circular movement (gravity, radial accel and tangential accel don't are not used in this mode)
  172. struct {
  173. // The starting radius of the particles
  174. float startRadius;
  175. // The starting radius variance of the particles
  176. float startRadiusVar;
  177. // The ending radius of the particles
  178. float endRadius;
  179. // The ending radius variance of the particles
  180. float endRadiusVar;
  181. // Number of degress to rotate a particle around the source pos per second
  182. float rotatePerSecond;
  183. // Variance in degrees for rotatePerSecond
  184. float rotatePerSecondVar;
  185. } B;
  186. } mode;
  187. // start ize of the particles
  188. float startSize;
  189. // start Size variance
  190. float startSizeVar;
  191. // End size of the particle
  192. float endSize;
  193. // end size of variance
  194. float endSizeVar;
  195. // How many seconds will the particle live
  196. float life;
  197. // Life variance
  198. float lifeVar;
  199. // Start color of the particles
  200. ccColor4F startColor;
  201. // Start color variance
  202. ccColor4F startColorVar;
  203. // End color of the particles
  204. ccColor4F endColor;
  205. // End color variance
  206. ccColor4F endColorVar;
  207. // start angle of the particles
  208. float startSpin;
  209. // start angle variance
  210. float startSpinVar;
  211. // End angle of the particle
  212. float endSpin;
  213. // end angle ariance
  214. float endSpinVar;
  215. // Array of particles
  216. tCCParticle *particles;
  217. // Maximum particles
  218. int totalParticles;
  219. // Count of active particles
  220. int particleCount;
  221. // color modulate
  222. // BOOL colorModulate;
  223. // How many particles can be emitted per second
  224. float emissionRate;
  225. float emitCounter;
  226. // Texture of the particles
  227. CCTexture2D *texture_;
  228. // blend function
  229. ccBlendFunc blendFunc_;
  230. // movment type: free or grouped
  231. tCCPositionType positionType_;
  232. // Whether or not the node will be auto-removed when there are not particles
  233. BOOL autoRemoveOnFinish_;
  234. // particle idx
  235. int particleIdx;
  236. // Optimization
  237. CC_UPDATE_PARTICLE_IMP updateParticleImp;
  238. SEL updateParticleSel;
  239. // profiling
  240. #if CC_ENABLE_PROFILERS
  241. CCProfilingTimer* _profilingTimer;
  242. #endif
  243. }
  244. /** Is the emitter active */
  245. @property (nonatomic,readonly) BOOL active;
  246. /** Quantity of particles that are being simulated at the moment */
  247. @property (nonatomic,readonly) int particleCount;
  248. /** How many seconds the emitter wil run. -1 means 'forever' */
  249. @property (nonatomic,readwrite,assign) float duration;
  250. /** centerOfGravity of the emitter */
  251. @property (nonatomic,readwrite,assign) CGPoint centerOfGravity;
  252. /** Position variance of the emitter */
  253. @property (nonatomic,readwrite,assign) CGPoint posVar;
  254. /** life, and life variation of each particle */
  255. @property (nonatomic,readwrite,assign) float life;
  256. /** life variance of each particle */
  257. @property (nonatomic,readwrite,assign) float lifeVar;
  258. /** angle and angle variation of each particle */
  259. @property (nonatomic,readwrite,assign) float angle;
  260. /** angle variance of each particle */
  261. @property (nonatomic,readwrite,assign) float angleVar;
  262. /** Gravity value. Only available in 'Gravity' mode. */
  263. @property (nonatomic,readwrite,assign) CGPoint gravity;
  264. /** speed of each particle. Only available in 'Gravity' mode. */
  265. @property (nonatomic,readwrite,assign) float speed;
  266. /** speed variance of each particle. Only available in 'Gravity' mode. */
  267. @property (nonatomic,readwrite,assign) float speedVar;
  268. /** tangential acceleration of each particle. Only available in 'Gravity' mode. */
  269. @property (nonatomic,readwrite,assign) float tangentialAccel;
  270. /** tangential acceleration variance of each particle. Only available in 'Gravity' mode. */
  271. @property (nonatomic,readwrite,assign) float tangentialAccelVar;
  272. /** radial acceleration of each particle. Only available in 'Gravity' mode. */
  273. @property (nonatomic,readwrite,assign) float radialAccel;
  274. /** radial acceleration variance of each particle. Only available in 'Gravity' mode. */
  275. @property (nonatomic,readwrite,assign) float radialAccelVar;
  276. /** The starting radius of the particles. Only available in 'Radius' mode. */
  277. @property (nonatomic,readwrite,assign) float startRadius;
  278. /** The starting radius variance of the particles. Only available in 'Radius' mode. */
  279. @property (nonatomic,readwrite,assign) float startRadiusVar;
  280. /** The ending radius of the particles. Only available in 'Radius' mode. */
  281. @property (nonatomic,readwrite,assign) float endRadius;
  282. /** The ending radius variance of the particles. Only available in 'Radius' mode. */
  283. @property (nonatomic,readwrite,assign) float endRadiusVar;
  284. /** Number of degress to rotate a particle around the source pos per second. Only available in 'Radius' mode. */
  285. @property (nonatomic,readwrite,assign) float rotatePerSecond;
  286. /** Variance in degrees for rotatePerSecond. Only available in 'Radius' mode. */
  287. @property (nonatomic,readwrite,assign) float rotatePerSecondVar;
  288. /** start size in pixels of each particle */
  289. @property (nonatomic,readwrite,assign) float startSize;
  290. /** size variance in pixels of each particle */
  291. @property (nonatomic,readwrite,assign) float startSizeVar;
  292. /** end size in pixels of each particle */
  293. @property (nonatomic,readwrite,assign) float endSize;
  294. /** end size variance in pixels of each particle */
  295. @property (nonatomic,readwrite,assign) float endSizeVar;
  296. /** start color of each particle */
  297. @property (nonatomic,readwrite,assign) ccColor4F startColor;
  298. /** start color variance of each particle */
  299. @property (nonatomic,readwrite,assign) ccColor4F startColorVar;
  300. /** end color and end color variation of each particle */
  301. @property (nonatomic,readwrite,assign) ccColor4F endColor;
  302. /** end color variance of each particle */
  303. @property (nonatomic,readwrite,assign) ccColor4F endColorVar;
  304. //* initial angle of each particle
  305. @property (nonatomic,readwrite,assign) float startSpin;
  306. //* initial angle of each particle
  307. @property (nonatomic,readwrite,assign) float startSpinVar;
  308. //* initial angle of each particle
  309. @property (nonatomic,readwrite,assign) float endSpin;
  310. //* initial angle of each particle
  311. @property (nonatomic,readwrite,assign) float endSpinVar;
  312. /** emission rate of the particles */
  313. @property (nonatomic,readwrite,assign) float emissionRate;
  314. /** maximum particles of the system */
  315. @property (nonatomic,readwrite,assign) int totalParticles;
  316. /** conforms to CocosNodeTexture protocol */
  317. @property (nonatomic,readwrite, retain) CCTexture2D * texture;
  318. /** conforms to CocosNodeTexture protocol */
  319. @property (nonatomic,readwrite) ccBlendFunc blendFunc;
  320. /** whether or not the particles are using blend additive.
  321. If enabled, the following blending function will be used.
  322. @code
  323. source blend function = GL_SRC_ALPHA;
  324. dest blend function = GL_ONE;
  325. @endcode
  326. */
  327. @property (nonatomic,readwrite) BOOL blendAdditive;
  328. /** particles movement type: Free or Grouped
  329. @since v0.8
  330. */
  331. @property (nonatomic,readwrite) tCCPositionType positionType;
  332. /** whether or not the node will be auto-removed when it has no particles left.
  333. By default it is NO.
  334. @since v0.8
  335. */
  336. @property (nonatomic,readwrite) BOOL autoRemoveOnFinish;
  337. /** Switch between different kind of emitter modes:
  338. - kCCParticleModeGravity: uses gravity, speed, radial and tangential acceleration
  339. - kCCParticleModeRadius: uses radius movement + rotation
  340. */
  341. @property (nonatomic,readwrite) int emitterMode;
  342. /** creates an initializes a CCParticleSystem from a plist file.
  343. This plist files can be creted manually or with Particle Designer:
  344. http://particledesigner.71squared.com/
  345. @since v0.99.3
  346. */
  347. +(id) particleWithFile:(NSString*)plistFile;
  348. /** initializes a CCParticleSystem from a plist file.
  349. This plist files can be creted manually or with Particle Designer:
  350. http://particledesigner.71squared.com/
  351. @since v0.99.3
  352. */
  353. -(id) initWithFile:(NSString*) plistFile;
  354. /** initializes a CCQuadParticleSystem from a NSDictionary.
  355. @since v0.99.3
  356. */
  357. -(id) initWithDictionary:(NSDictionary*)dictionary;
  358. //! Initializes a system with a fixed number of particles
  359. -(id) initWithTotalParticles:(int) numberOfParticles;
  360. //! Add a particle to the emitter
  361. -(BOOL) addParticle;
  362. //! Initializes a particle
  363. -(void) initParticle: (tCCParticle*) particle;
  364. //! stop emitting particles. Running particles will continue to run until they die
  365. -(void) stopSystem;
  366. //! Kill all living particles.
  367. -(void) resetSystem;
  368. //! whether or not the system is full
  369. -(BOOL) isFull;
  370. //! should be overriden by subclasses
  371. -(void) updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos;
  372. //! should be overriden by subclasses
  373. -(void) postStep;
  374. @end