-
Notifications
You must be signed in to change notification settings - Fork 123
/
BurstSpawn.ts
80 lines (73 loc) · 2.05 KB
/
BurstSpawn.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
import { Particle } from '../Particle';
import { IEmitterBehavior, BehaviorOrder } from './Behaviors';
import { DEG_TO_RADS, rotatePoint } from '../ParticleUtils';
import { BehaviorEditorConfig } from './editor/Types';
/**
* A Spawn behavior that sends particles out from a single point or ring, and is capable of evenly spacing
* the particle's starting angles.
*
* Example config:
* ```javascript
* {
* type: 'spawnBurst',
* config: {
* spacing: 90,
* start: 0,
* distance: 40,
* }
* }
* ```
*/
export class BurstSpawnBehavior implements IEmitterBehavior
{
public static type = 'spawnBurst';
public static editorConfig: BehaviorEditorConfig = null;
order = BehaviorOrder.Spawn;
private spacing: number;
private start: number;
private distance: number;
constructor(config: {
/**
* Description: Spacing between each particle spawned in a wave, in degrees.
*/
spacing: number;
/**
* Description: Angle to start placing particles at, in degrees. 0 is facing right, 90 is facing upwards.
*/
start: number;
/**
* Description: Distance from the emitter to spawn particles, forming a ring/arc.
*/
distance: number;
})
{
this.spacing = config.spacing * DEG_TO_RADS;
this.start = config.start * DEG_TO_RADS;
this.distance = config.distance;
}
initParticles(first: Particle): void
{
let count = 0;
let next = first;
while (next)
{
let angle: number;
if (this.spacing)
{
angle = this.start + (this.spacing * count);
}
else
{
angle = Math.random() * Math.PI * 2;
}
next.rotation = angle;
if (this.distance)
{
next.position.x = this.distance;
rotatePoint(angle, next.position);
}
next = next.next;
++count;
}
}
}