-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRotationOverLife.ts
47 lines (39 loc) · 1.41 KB
/
RotationOverLife.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
import {Behavior} from './Behavior';
import {Particle} from '../Particle';
import {FunctionValueGenerator, ValueGenerator, ValueGeneratorFromJSON} from '../functions/ValueGenerator';
/**
* Apply rotation to particles over their life.
*/
export class RotationOverLife implements Behavior {
type = 'RotationOverLife';
constructor(public angularVelocity: ValueGenerator | FunctionValueGenerator) {}
initialize(particle: Particle): void {
if (typeof particle.rotation === 'number') {
(this.angularVelocity as ValueGenerator).startGen(particle.memory);
}
}
update(particle: Particle, delta: number): void {
if (typeof particle.rotation === 'number') {
(particle.rotation as number) +=
delta *
(this.angularVelocity as FunctionValueGenerator).genValue(
particle.memory,
particle.age / particle.life
);
}
}
toJSON(): any {
return {
type: this.type,
angularVelocity: this.angularVelocity.toJSON(),
};
}
static fromJSON(json: any): Behavior {
return new RotationOverLife(ValueGeneratorFromJSON(json.angularVelocity) as FunctionValueGenerator);
}
frameUpdate(delta: number): void {}
clone(): Behavior {
return new RotationOverLife(this.angularVelocity.clone());
}
reset(): void {}
}