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