Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/particles/Particle.java
1457 views
1
package org.newdawn.slick.particles;
2
3
import org.newdawn.slick.Color;
4
import org.newdawn.slick.Image;
5
import org.newdawn.slick.opengl.TextureImpl;
6
import org.newdawn.slick.opengl.renderer.Renderer;
7
import org.newdawn.slick.opengl.renderer.SGL;
8
9
/**
10
* A single particle within a system
11
*
12
* @author kevin
13
*/
14
public class Particle {
15
/** The renderer to use for all GL operations */
16
protected static SGL GL = Renderer.get();
17
18
/** Indicates the particle should inherit it's use of points */
19
public static final int INHERIT_POINTS = 1;
20
/** Indicates the particle should explicitly use points */
21
public static final int USE_POINTS = 2;
22
/** Indicates the particle should explicitly not use points */
23
public static final int USE_QUADS = 3;
24
25
/** The x coordinate of the particle */
26
protected float x;
27
/** The y coordinate of the particle */
28
protected float y;
29
/** The x component of the direction vector of the particle */
30
protected float velx;
31
/** The y component of the direction vector of the particle */
32
protected float vely;
33
/** The current size in pixels of the particle */
34
protected float size = 10;
35
/** The colour of the particle */
36
protected Color color = new Color(1f, 1f, 1f, 1f);
37
/** The life left in the particle */
38
protected float life;
39
/** The original life of this particle */
40
protected float originalLife;
41
/** The engine this particle belongs to */
42
private ParticleSystem engine;
43
/** The emitter controllng this particle */
44
private ParticleEmitter emitter;
45
/** The image for this particle */
46
protected Image image;
47
/** The type identifier of this particle */
48
protected int type;
49
/** How this particle should be rendered */
50
protected int usePoints = INHERIT_POINTS;
51
/** True if this particle's quad should be oritented based on it's direction */
52
protected boolean oriented = false;
53
/** The currently scalar applied on the y axis */
54
protected float scaleY = 1.0f;
55
56
/**
57
* Create a new particle belonging to given engine
58
*
59
* @param engine
60
* The engine the new particle belongs to
61
*/
62
public Particle(ParticleSystem engine) {
63
this.engine = engine;
64
}
65
66
/**
67
* Get the x offset of this particle
68
*
69
* @return The x offset of this particle
70
*/
71
public float getX() {
72
return x;
73
}
74
75
/**
76
* Get the y offset of this particle
77
*
78
* @return The y offset of this particle
79
*/
80
public float getY() {
81
return y;
82
}
83
84
/**
85
* Move this particle a fixed amount
86
*
87
* @param x The amount to move the particle on the horizontal axis
88
* @param y The amount to move the particle on the vertical axis
89
*/
90
public void move(float x, float y) {
91
this.x += x;
92
this.y += y;
93
}
94
95
/**
96
* Get the size of this particle
97
*
98
* @return The size of this particle
99
*/
100
public float getSize() {
101
return size;
102
}
103
104
/**
105
* Get the color of this particle
106
*
107
* @return The color of this particle
108
*/
109
public Color getColor() {
110
return color;
111
}
112
113
/**
114
* Set the image used to render this particle
115
*
116
* @param image
117
* The image used to render this particle
118
*/
119
public void setImage(Image image) {
120
this.image = image;
121
}
122
123
/**
124
* Get the original life of this particle
125
*
126
* @return The original life of this particle
127
*/
128
public float getOriginalLife() {
129
return originalLife;
130
}
131
132
/**
133
* Get the life remaining in the particle in milliseconds
134
*
135
* @return The life remaining in the particle
136
*/
137
public float getLife() {
138
return life;
139
}
140
141
/**
142
* Check if this particle is currently in use (i.e. is it rendering?)
143
*
144
* @return True if the particle is currently in use
145
*/
146
public boolean inUse() {
147
return life > 0;
148
}
149
150
/**
151
* Render this particle
152
*/
153
public void render() {
154
if ((engine.usePoints() && (usePoints == INHERIT_POINTS))
155
|| (usePoints == USE_POINTS)) {
156
TextureImpl.bindNone();
157
GL.glEnable(SGL.GL_POINT_SMOOTH);
158
GL.glPointSize(size / 2);
159
color.bind();
160
GL.glBegin(SGL.GL_POINTS);
161
GL.glVertex2f(x, y);
162
GL.glEnd();
163
} else if (oriented || scaleY != 1.0f) {
164
GL.glPushMatrix();
165
166
GL.glTranslatef(x, y, 0f);
167
168
if (oriented) {
169
float angle = (float) (Math.atan2(y, x) * 180 / Math.PI);
170
GL.glRotatef(angle, 0f, 0f, 1.0f);
171
}
172
173
// scale
174
GL.glScalef(1.0f, scaleY, 1.0f);
175
176
image.draw((int) (-(size / 2)), (int) (-(size / 2)), (int) size,
177
(int) size, color);
178
GL.glPopMatrix();
179
} else {
180
color.bind();
181
image.drawEmbedded((int) (x - (size / 2)), (int) (y - (size / 2)),
182
(int) size, (int) size);
183
}
184
}
185
186
/**
187
* Update the state of this particle
188
*
189
* @param delta
190
* The time since the last update
191
*/
192
public void update(int delta) {
193
emitter.updateParticle(this, delta);
194
life -= delta;
195
196
if (life > 0) {
197
x += delta * velx;
198
y += delta * vely;
199
} else {
200
engine.release(this);
201
}
202
}
203
204
/**
205
* Initialise the state of the particle as it's reused
206
*
207
* @param emitter
208
* The emitter controlling this particle
209
* @param life
210
* The life the particle should have (in milliseconds)
211
*/
212
public void init(ParticleEmitter emitter, float life) {
213
x = 0;
214
this.emitter = emitter;
215
y = 0;
216
velx = 0;
217
vely = 0;
218
size = 10;
219
type = 0;
220
this.originalLife = this.life = life;
221
oriented = false;
222
scaleY = 1.0f;
223
}
224
225
/**
226
* Set the type of this particle
227
*
228
* @param type
229
* The type of this particle
230
*/
231
public void setType(int type) {
232
this.type = type;
233
}
234
235
/**
236
* Indicate how this particle should be renered
237
*
238
* @param usePoints
239
* The indicator for rendering
240
* @see #USE_POINTS
241
* @see #USE_QUADS
242
* @see #INHERIT_POINTS
243
*/
244
public void setUsePoint(int usePoints) {
245
this.usePoints = usePoints;
246
}
247
248
/**
249
* Get the type of this particle
250
*
251
* @return The type of this particle
252
*/
253
public int getType() {
254
return type;
255
}
256
257
/**
258
* Set the size of the particle
259
*
260
* @param size
261
* The size of the particle (in pixels)
262
*/
263
public void setSize(float size) {
264
this.size = size;
265
}
266
267
/**
268
* Adjust the size of the particle
269
*
270
* @param delta
271
* The amount to adjust the size by (in pixels)
272
*/
273
public void adjustSize(float delta) {
274
size += delta;
275
size = Math.max(0, size);
276
}
277
278
/**
279
* Set the life of the particle
280
*
281
* @param life
282
* The life of the particle in milliseconds
283
*/
284
public void setLife(float life) {
285
this.life = life;
286
}
287
288
/**
289
* Adjust the life othe particle
290
*
291
* @param delta
292
* The amount to adjust the particle by (in milliseconds)
293
*/
294
public void adjustLife(float delta) {
295
life += delta;
296
}
297
298
/**
299
* Kill the particle, stop it rendering and send it back to the engine for
300
* use.
301
*/
302
public void kill() {
303
life = 1;
304
}
305
306
/**
307
* Set the color of the particle
308
*
309
* @param r
310
* The red component of the color
311
* @param g
312
* The green component of the color
313
* @param b
314
* The blue component of the color
315
* @param a
316
* The alpha component of the color
317
*/
318
public void setColor(float r, float g, float b, float a) {
319
color.r = r;
320
color.g = g;
321
color.b = b;
322
color.a = a;
323
}
324
325
/**
326
* Set the position of this particle
327
*
328
* @param x
329
* The new x position of the particle
330
* @param y
331
* The new y position of the particle
332
*/
333
public void setPosition(float x, float y) {
334
this.x = x;
335
this.y = y;
336
}
337
338
/**
339
* Set the velocity of the particle
340
*
341
* @param dirx
342
* The x component of the new velocity
343
* @param diry
344
* The y component of the new velocity
345
* @param speed
346
* The speed in the given direction
347
*/
348
public void setVelocity(float dirx, float diry, float speed) {
349
this.velx = dirx * speed;
350
this.vely = diry * speed;
351
}
352
353
/**
354
* Set the current speed of this particle
355
*
356
* @param speed The speed of this particle
357
*/
358
public void setSpeed(float speed) {
359
float currentSpeed = (float) Math.sqrt((velx*velx) + (vely*vely));
360
velx *= speed;
361
vely *= speed;
362
velx /= currentSpeed;
363
vely /= currentSpeed;
364
}
365
366
/**
367
* Set the velocity of the particle
368
*
369
* @param velx The x component of the new velocity
370
* @param vely The y component of the new velocity
371
*/
372
public void setVelocity(float velx, float vely) {
373
setVelocity(velx,vely,1);
374
}
375
376
/**
377
* Adjust (add) the position of this particle
378
*
379
* @param dx
380
* The amount to adjust the x component by
381
* @param dy
382
* The amount to adjust the y component by
383
*/
384
public void adjustPosition(float dx, float dy) {
385
x += dx;
386
y += dy;
387
}
388
389
/**
390
* Adjust (add) the color of the particle
391
*
392
* @param r
393
* The amount to adjust the red component by
394
* @param g
395
* The amount to adjust the green component by
396
* @param b
397
* The amount to adjust the blue component by
398
* @param a
399
* The amount to adjust the alpha component by
400
*/
401
public void adjustColor(float r, float g, float b, float a) {
402
color.r += r;
403
color.g += g;
404
color.b += b;
405
color.a += a;
406
}
407
408
/**
409
* Adjust (add) the color of the particle
410
*
411
* @param r
412
* The amount to adjust the red component by
413
* @param g
414
* The amount to adjust the green component by
415
* @param b
416
* The amount to adjust the blue component by
417
* @param a
418
* The amount to adjust the alpha component by
419
*/
420
public void adjustColor(int r, int g, int b, int a) {
421
color.r += (r / 255.0f);
422
color.g += (g / 255.0f);
423
color.b += (b / 255.0f);
424
color.a += (a / 255.0f);
425
}
426
427
/**
428
* Adjust (add) the direction of this particle
429
*
430
* @param dx
431
* The amount to adjust the x component by
432
* @param dy
433
* The amount to adjust the y component by
434
*/
435
public void adjustVelocity(float dx, float dy) {
436
velx += dx;
437
vely += dy;
438
}
439
440
/**
441
* Get the emitter that owns this particle
442
*
443
* @return The emitter that owns this particle
444
*/
445
public ParticleEmitter getEmitter() {
446
return emitter;
447
}
448
449
/**
450
* @see java.lang.Object#toString()
451
*/
452
public String toString() {
453
return super.toString() + " : " + life;
454
}
455
456
/**
457
* Check if this particle is being oriented based on it's velocity
458
*
459
* @return True if this particle being oriented based on it's velocity
460
*/
461
public boolean isOriented() {
462
return oriented;
463
}
464
465
/**
466
* Indicate if this particle should be oriented based on it's velocity
467
*
468
* @param oriented True if this particle is being oriented based on it's velocity
469
*/
470
public void setOriented(boolean oriented) {
471
this.oriented = oriented;
472
}
473
474
/**
475
* Get the current scalar applied on the y axis
476
*
477
* @return The scalar applied on the y axis
478
*/
479
public float getScaleY() {
480
return scaleY;
481
}
482
483
/**
484
* Set the current scalar applied on the y axis
485
*
486
* @param scaleY The new scalar to apply on the y axis
487
*/
488
public void setScaleY(float scaleY) {
489
this.scaleY = scaleY;
490
}
491
}
492
493