-
Notifications
You must be signed in to change notification settings - Fork 124
/
Rotation.ts
200 lines (182 loc) · 5.09 KB
/
Rotation.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
import { Particle } from '../Particle';
import { DEG_TO_RADS } from '../ParticleUtils';
import { IEmitterBehavior, BehaviorOrder } from './Behaviors';
import { BehaviorEditorConfig } from './editor/Types';
/**
* A Rotation behavior that handles starting rotation, rotation speed, and rotational acceleration.
*
* Example configuration:
* ```javascript
* {
* "type": "rotation",
* "config": {
* "minStart": 0,
* "maxStart": 180,
* "minSpeed": 30,
* "maxSpeed": 45,
* "accel": 20
* }
*}
* ```
*/
export class RotationBehavior implements IEmitterBehavior
{
public static type = 'rotation';
public static editorConfig: BehaviorEditorConfig = null;
public order = BehaviorOrder.Normal;
private minStart: number;
private maxStart: number;
private minSpeed: number;
private maxSpeed: number;
private accel: number;
constructor(config: {
/**
* Minimum starting rotation of the particles, in degrees. 0 is facing right, 90 is upwards.
*/
minStart: number;
/**
* Maximum starting rotation of the particles, in degrees. 0 is facing right, 90 is upwards.
*/
maxStart: number;
/**
* Minimum rotation speed of the particles, in degrees/second. Positive is counter-clockwise.
*/
minSpeed: number;
/**
* Maximum rotation speed of the particles, in degrees/second. Positive is counter-clockwise.
*/
maxSpeed: number;
/**
* Constant rotational acceleration of the particles, in degrees/second/second.
*/
accel: number;
})
{
this.minStart = config.minStart * DEG_TO_RADS;
this.maxStart = config.maxStart * DEG_TO_RADS;
this.minSpeed = config.minSpeed * DEG_TO_RADS;
this.maxSpeed = config.maxSpeed * DEG_TO_RADS;
this.accel = config.accel * DEG_TO_RADS;
}
initParticles(first: Particle): void
{
let next = first;
while (next)
{
if (this.minStart === this.maxStart)
{
next.rotation += this.maxStart;
}
else
{
next.rotation += (Math.random() * (this.maxStart - this.minStart)) + this.minStart;
}
next.config.rotSpeed = (Math.random() * (this.maxSpeed - this.minSpeed)) + this.minSpeed;
next = next.next;
}
}
updateParticle(particle: Particle, deltaSec: number): void
{
if (this.accel)
{
const oldSpeed = particle.config.rotSpeed;
particle.config.rotSpeed += this.accel * deltaSec;
particle.rotation += (particle.config.rotSpeed + oldSpeed) / 2 * deltaSec;
}
else
{
particle.rotation += particle.config.rotSpeed * deltaSec;
}
}
}
/**
* A Rotation behavior that handles starting rotation.
*
* Example configuration:
* ```javascript
* {
* "type": "rotationStatic",
* "config": {
* "min": 0,
* "max": 180,
* }
*}
* ```
*/
export class StaticRotationBehavior implements IEmitterBehavior
{
public static type = 'rotationStatic';
public static editorConfig: BehaviorEditorConfig = null;
public order = BehaviorOrder.Normal;
private min: number;
private max: number;
constructor(config: {
/**
* Minimum starting rotation of the particles, in degrees. 0 is facing right, 90 is upwards.
*/
min: number;
/**
* Maximum starting rotation of the particles, in degrees. 0 is facing right, 90 is upwards.
*/
max: number;
})
{
this.min = config.min * DEG_TO_RADS;
this.max = config.max * DEG_TO_RADS;
}
initParticles(first: Particle): void
{
let next = first;
while (next)
{
if (this.min === this.max)
{
next.rotation += this.max;
}
else
{
next.rotation += (Math.random() * (this.max - this.min)) + this.min;
}
next = next.next;
}
}
}
/**
* A Rotation behavior that blocks all rotation caused by spawn settings,
* by resetting it to the specified rotation (or 0).
*
* Example configuration:
* ```javascript
* {
* "type": "noRotation",
* "config": {
* "rotation": 0
* }
*}
* ```
*/
export class NoRotationBehavior implements IEmitterBehavior
{
public static type = 'noRotation';
public static editorConfig: BehaviorEditorConfig = null;
public order = BehaviorOrder.Late + 1;
private rotation: number;
constructor(config: {
/**
* Locked rotation of the particles, in degrees. 0 is facing right, 90 is upwards.
*/
rotation?: number;
})
{
this.rotation = (config.rotation || 0) * DEG_TO_RADS;
}
initParticles(first: Particle): void
{
let next = first;
while (next)
{
next.rotation = this.rotation;
next = next.next;
}
}
}