-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.ts
49 lines (42 loc) · 1.62 KB
/
particle.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
class Particle extends Entity {
constructor(pos: Point, sprite: Sprite, lifetime: number, fade: boolean, velocity:number) {
super(pos)
this.sprite = sprite
this.sprite.drawOrder = CONST.LAYER_FX
this.behaviors.push(new SelfDestructBehavior(lifetime, fade))
if ("setProgress" in this.sprite) {
this.behaviors.push(new AnimatedBehavior(lifetime, false))
}
if (velocity > 0) {
let angle = Math.random() * Math.PI * 2
this.behaviors.push(new FlyForwardBehavior(this, { x: Math.sin(angle), y:Math.cos(angle)}, velocity, { x:0, y:0}))
}
}
}
class ParticleEmitterBehavior extends Behavior {
private time: number = 0
private particleLifetime: number
private particleSpread: number
private particleSprite: ImageResource
public frequency: number
constructor(particleLifetime: number, particleSprite: ImageResource, frequency: number, spread:number) {
super()
this.particleLifetime = particleLifetime
this.particleSprite = particleSprite
this.particleSpread = spread
this.frequency = frequency
}
update(entity:Entity, deltaTime: number, state: State) : void {
this.time += deltaTime
while (this.time > this.frequency) {
state.addEntity(new Particle(
entity.pos,
new SimpleSprite(this.particleSprite),
this.particleLifetime,
true,
Math.random() * Math.random() * this.particleSpread)
)
this.time -= this.frequency
}
}
}