/
Particles.ts
512 lines (456 loc) · 14.8 KB
/
Particles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import { Engine } from './Engine';
import { Actor } from './Actor';
import { Sprite } from './Drawing/Sprite';
import { Color } from './Drawing/Color';
import { Vector } from './Algebra';
import * as Util from './Util/Util';
import * as DrawUtil from './Util/DrawUtil';
import * as Traits from './Traits/Index';
import { Configurable } from './Configurable';
import { Random } from './Math/Random';
import { CollisionType } from './Collision/CollisionType';
/**
* An enum that represents the types of emitter nozzles
*/
export enum EmitterType {
/**
* Constant for the circular emitter type
*/
Circle,
/**
* Constant for the rectangular emitter type
*/
Rectangle
}
/**
* @hidden
*/
export class ParticleImpl {
public position: Vector = new Vector(0, 0);
public velocity: Vector = new Vector(0, 0);
public acceleration: Vector = new Vector(0, 0);
public particleRotationalVelocity: number = 0;
public currentRotation: number = 0;
public focus: Vector = null;
public focusAccel: number = 0;
public opacity: number = 1;
public beginColor: Color = Color.White;
public endColor: Color = Color.White;
// Life is counted in ms
public life: number = 300;
public fadeFlag: boolean = false;
// Color transitions
private _rRate: number = 1;
private _gRate: number = 1;
private _bRate: number = 1;
private _aRate: number = 0;
private _currentColor: Color = Color.White;
public emitter: ParticleEmitter = null;
public particleSize: number = 5;
public particleSprite: Sprite = null;
public startSize: number;
public endSize: number;
public sizeRate: number = 0;
public elapsedMultiplier: number = 0;
constructor(
emitterOrConfig: ParticleEmitter | ParticleArgs,
life?: number,
opacity?: number,
beginColor?: Color,
endColor?: Color,
position?: Vector,
velocity?: Vector,
acceleration?: Vector,
startSize?: number,
endSize?: number
) {
let emitter = emitterOrConfig;
if (emitter && !(emitterOrConfig instanceof ParticleEmitter)) {
const config = emitterOrConfig;
emitter = config.emitter;
life = config.life;
opacity = config.opacity;
endColor = config.endColor;
beginColor = config.beginColor;
position = config.position;
velocity = config.velocity;
acceleration = config.acceleration;
startSize = config.startSize;
endSize = config.endSize;
}
this.emitter = <ParticleEmitter>emitter;
this.life = life || this.life;
this.opacity = opacity || this.opacity;
this.endColor = endColor || this.endColor.clone();
this.beginColor = beginColor || this.beginColor.clone();
this._currentColor = this.beginColor.clone();
this.position = position || this.position;
this.velocity = velocity || this.velocity;
this.acceleration = acceleration || this.acceleration;
this._rRate = (this.endColor.r - this.beginColor.r) / this.life;
this._gRate = (this.endColor.g - this.beginColor.g) / this.life;
this._bRate = (this.endColor.b - this.beginColor.b) / this.life;
this._aRate = this.opacity / this.life;
this.startSize = startSize || 0;
this.endSize = endSize || 0;
if (this.endSize > 0 && this.startSize > 0) {
this.sizeRate = (this.endSize - this.startSize) / this.life;
this.particleSize = this.startSize;
}
}
public kill() {
this.emitter.removeParticle(this);
}
public update(delta: number) {
this.life = this.life - delta;
this.elapsedMultiplier = this.elapsedMultiplier + delta;
if (this.life < 0) {
this.kill();
}
if (this.fadeFlag) {
this.opacity = Util.clamp(this._aRate * this.life, 0.0001, 1);
}
if (this.startSize > 0 && this.endSize > 0) {
this.particleSize = Util.clamp(
this.sizeRate * delta + this.particleSize,
Math.min(this.startSize, this.endSize),
Math.max(this.startSize, this.endSize)
);
}
this._currentColor.r = Util.clamp(this._currentColor.r + this._rRate * delta, 0, 255);
this._currentColor.g = Util.clamp(this._currentColor.g + this._gRate * delta, 0, 255);
this._currentColor.b = Util.clamp(this._currentColor.b + this._bRate * delta, 0, 255);
this._currentColor.a = Util.clamp(this.opacity, 0.0001, 1);
if (this.focus) {
const accel = this.focus
.sub(this.position)
.normalize()
.scale(this.focusAccel)
.scale(delta / 1000);
this.velocity = this.velocity.add(accel);
} else {
this.velocity = this.velocity.add(this.acceleration.scale(delta / 1000));
}
this.position = this.position.add(this.velocity.scale(delta / 1000));
if (this.particleRotationalVelocity) {
this.currentRotation = (this.currentRotation + (this.particleRotationalVelocity * delta) / 1000) % (2 * Math.PI);
}
}
public draw(ctx: CanvasRenderingContext2D) {
if (this.particleSprite) {
this.particleSprite.rotation = this.currentRotation;
this.particleSprite.scale.setTo(this.particleSize, this.particleSize);
this.particleSprite.draw(ctx, this.position.x, this.position.y);
return;
}
this._currentColor.a = Util.clamp(this.opacity, 0.0001, 1);
ctx.fillStyle = this._currentColor.toString();
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.particleSize, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
export interface ParticleArgs extends Partial<ParticleImpl> {
emitter: ParticleEmitter;
position?: Vector;
velocity?: Vector;
acceleration?: Vector;
particleRotationalVelocity?: number;
currentRotation?: number;
particleSize?: number;
particleSprite?: Sprite;
}
/**
* Particle is used in a [[ParticleEmitter]]
*/
export class Particle extends Configurable(ParticleImpl) {
constructor(config: ParticleArgs);
constructor(
emitter: ParticleEmitter,
life?: number,
opacity?: number,
beginColor?: Color,
endColor?: Color,
position?: Vector,
velocity?: Vector,
acceleration?: Vector,
startSize?: number,
endSize?: number
);
constructor(
emitterOrConfig: ParticleEmitter | ParticleArgs,
life?: number,
opacity?: number,
beginColor?: Color,
endColor?: Color,
position?: Vector,
velocity?: Vector,
acceleration?: Vector,
startSize?: number,
endSize?: number
) {
super(emitterOrConfig, life, opacity, beginColor, endColor, position, velocity, acceleration, startSize, endSize);
}
}
/**
* @hidden
*/
export class ParticleEmitterImpl extends Actor {
private _particlesToEmit: number;
public numParticles: number = 0;
/**
* Random number generator
*/
public random: Random;
/**
* Gets or sets the isEmitting flag
*/
public isEmitting: boolean = true;
/**
* Gets or sets the backing particle collection
*/
public particles: Util.Collection<Particle> = null;
/**
* Gets or sets the backing deadParticle collection
*/
public deadParticles: Util.Collection<Particle> = null;
/**
* Gets or sets the minimum particle velocity
*/
public minVel: number = 0;
/**
* Gets or sets the maximum particle velocity
*/
public maxVel: number = 0;
/**
* Gets or sets the acceleration vector for all particles
*/
public acceleration: Vector = new Vector(0, 0);
/**
* Gets or sets the minimum angle in radians
*/
public minAngle: number = 0;
/**
* Gets or sets the maximum angle in radians
*/
public maxAngle: number = 0;
/**
* Gets or sets the emission rate for particles (particles/sec)
*/
public emitRate: number = 1; //particles/sec
/**
* Gets or sets the life of each particle in milliseconds
*/
public particleLife: number = 2000;
/**
* Gets or sets the opacity of each particle from 0 to 1.0
*/
public opacity: number = 1;
/**
* Gets or sets the fade flag which causes particles to gradually fade out over the course of their life.
*/
public fadeFlag: boolean = false;
/**
* Gets or sets the optional focus where all particles should accelerate towards
*/
public focus: Vector = null;
/**
* Gets or sets the acceleration for focusing particles if a focus has been specified
*/
public focusAccel: number = 1;
/*
* Gets or sets the optional starting size for the particles
*/
public startSize: number = null;
/*
* Gets or sets the optional ending size for the particles
*/
public endSize: number = null;
/**
* Gets or sets the minimum size of all particles
*/
public minSize: number = 5;
/**
* Gets or sets the maximum size of all particles
*/
public maxSize: number = 5;
/**
* Gets or sets the beginning color of all particles
*/
public beginColor: Color = Color.White;
/**
* Gets or sets the ending color of all particles
*/
public endColor: Color = Color.White;
/**
* Gets or sets the sprite that a particle should use
* @warning Performance intensive
*/
public particleSprite: Sprite = null;
/**
* Gets or sets the emitter type for the particle emitter
*/
public emitterType: EmitterType = EmitterType.Rectangle;
/**
* Gets or sets the emitter radius, only takes effect when the [[emitterType]] is [[EmitterType.Circle]]
*/
public radius: number = 0;
/**
* Gets or sets the particle rotational speed velocity
*/
public particleRotationalVelocity: number = 0;
/**
* Indicates whether particles should start with a random rotation
*/
public randomRotation: boolean = false;
/**
* @param xOrConfig The x position of the emitter, or the particle emitter options bag
* @param y The y position of the emitter
* @param width The width of the emitter
* @param height The height of the emitter
*/
constructor(xOrConfig?: number | ParticleEmitterArgs, y?: number, width?: number, height?: number) {
super(typeof xOrConfig === 'number' ? { pos: new Vector(xOrConfig, y), width: width, height: height } : xOrConfig);
this._particlesToEmit = 0;
this.body.collider.type = CollisionType.PreventCollision;
this.particles = new Util.Collection<Particle>();
this.deadParticles = new Util.Collection<Particle>();
this.random = new Random();
// Remove offscreen culling from particle emitters
for (let i = 0; i < this.traits.length; i++) {
if (this.traits[i] instanceof Traits.OffscreenCulling) {
this.traits.splice(i, 1);
}
}
}
public removeParticle(particle: Particle) {
this.deadParticles.push(particle);
}
/**
* Causes the emitter to emit particles
* @param particleCount Number of particles to emit right now
*/
public emitParticles(particleCount: number) {
for (let i = 0; i < particleCount; i++) {
this.particles.push(this._createParticle());
}
}
public clearParticles() {
this.particles.clear();
}
// Creates a new particle given the constraints of the emitter
private _createParticle(): Particle {
// todo implement emitter constraints;
let ranX = 0;
let ranY = 0;
const angle = Util.randomInRange(this.minAngle, this.maxAngle, this.random);
const vel = Util.randomInRange(this.minVel, this.maxVel, this.random);
const size = this.startSize || Util.randomInRange(this.minSize, this.maxSize, this.random);
const dx = vel * Math.cos(angle);
const dy = vel * Math.sin(angle);
if (this.emitterType === EmitterType.Rectangle) {
ranX = Util.randomInRange(0, this.width, this.random);
ranY = Util.randomInRange(0, this.height, this.random);
} else if (this.emitterType === EmitterType.Circle) {
const radius = Util.randomInRange(0, this.radius, this.random);
ranX = radius * Math.cos(angle);
ranY = radius * Math.sin(angle);
}
const p = new Particle(
this,
this.particleLife,
this.opacity,
this.beginColor,
this.endColor,
new Vector(ranX, ranY),
new Vector(dx, dy),
this.acceleration,
this.startSize,
this.endSize
);
p.fadeFlag = this.fadeFlag;
p.particleSize = size;
if (this.particleSprite) {
p.particleSprite = this.particleSprite;
}
p.particleRotationalVelocity = this.particleRotationalVelocity;
if (this.randomRotation) {
p.currentRotation = Util.randomInRange(0, Math.PI * 2, this.random);
}
if (this.focus) {
p.focus = this.focus.add(new Vector(this.pos.x, this.pos.y));
p.focusAccel = this.focusAccel;
}
return p;
}
public update(engine: Engine, delta: number) {
super.update(engine, delta);
if (this.isEmitting) {
this._particlesToEmit += this.emitRate * (delta / 1000);
//var numParticles = Math.ceil(this.emitRate * delta / 1000);
if (this._particlesToEmit > 1.0) {
this.emitParticles(Math.floor(this._particlesToEmit));
this._particlesToEmit = this._particlesToEmit - Math.floor(this._particlesToEmit);
}
}
this.particles.forEach((p) => p.update(delta));
this.deadParticles.forEach((p) => this.particles.removeElement(p));
this.deadParticles.clear();
}
public draw(ctx: CanvasRenderingContext2D) {
// todo is there a more efficient to draw
// possibly use a webgl offscreen canvas and shaders to do particles?
this.particles.forEach((p) => p.draw(ctx));
}
public debugDraw(ctx: CanvasRenderingContext2D) {
super.debugDraw(ctx);
ctx.fillStyle = Color.Black.toString();
ctx.fillText('Particles: ' + this.particles.count(), this.pos.x, this.pos.y + 20);
if (this.focus) {
ctx.fillRect(this.focus.x + this.pos.x, this.focus.y + this.pos.y, 3, 3);
DrawUtil.line(ctx, Color.Yellow, this.focus.x + this.pos.x, this.focus.y + this.pos.y, this.center.x, this.center.y);
ctx.fillText('Focus', this.focus.x + this.pos.x, this.focus.y + this.pos.y);
}
}
}
export interface ParticleEmitterArgs extends Partial<ParticleEmitterImpl> {
width?: number;
height?: number;
isEmitting?: boolean;
minVel?: number;
maxVel?: number;
acceleration?: Vector;
minAngle?: number;
maxAngle?: number;
emitRate?: number;
particleLife?: number;
opacity?: number;
fadeFlag?: boolean;
focus?: Vector;
focusAccel?: number;
startSize?: number;
endSize?: number;
minSize?: number;
maxSize?: number;
beginColor?: Color;
endColor?: Color;
particleSprite?: Sprite;
emitterType?: EmitterType;
radius?: number;
particleRotationalVelocity?: number;
randomRotation?: boolean;
random?: Random;
}
/**
* Using a particle emitter is a great way to create interesting effects
* in your game, like smoke, fire, water, explosions, etc. `ParticleEmitter`
* extend [[Actor]] allowing you to use all of the features that come with.
*/
export class ParticleEmitter extends Configurable(ParticleEmitterImpl) {
constructor(config?: ParticleEmitterArgs);
constructor(x?: number | ParticleEmitterArgs, y?: number, width?: number, height?: number);
constructor(xOrConfig?: number | ParticleEmitterArgs, y?: number, width?: number, height?: number) {
super(xOrConfig, y, width, height);
}
}