-
Notifications
You must be signed in to change notification settings - Fork 46
/
animation.ts
76 lines (67 loc) · 1.79 KB
/
animation.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
import { Ticker } from '@pixi/ticker';
import { EventEmitter } from '@pixi/utils';
/**
* Represents an animation.
*/
export abstract class Animation extends EventEmitter {
private _ticker?: Ticker
private _update?: (...params: any[]) => void
/** The duration (in seconds) of this animation. */
abstract readonly duration: number
/** The current position (in seconds) of this animation. */
abstract position: number
/** The speed that the animation will play at. */
speed = 1
/** A value indicating if the animation is looping. */
loop = false
/**
* Creates a new animation with the specified name.
* @param name Name for the animation.
*/
constructor(public name?: string) {
super()
}
/**
* Starts playing the animation using the specified ticker.
* @param ticker The ticker to use for updating the animation. If a ticker
* is not given, the shared ticker will be used.
*/
play(ticker = Ticker.shared) {
this.position = 0
if (!this._ticker) {
this._update = () => {
this.update(ticker.deltaMS / 1000 * this.speed)
}
this._ticker = ticker.add(this._update)
}
}
/**
* Stops playing the animation.
*/
stop() {
if (this._ticker && this._update) {
this._ticker.remove(this._update)
this._ticker = this._update = undefined
}
}
/**
* Updates the animation by the specified delta time.
* @param delta The time in seconds since last frame.
*/
update(delta: number) {
this.position += delta
if (this.position < this.duration) {
return
}
if (this.loop) {
if (this.position > this.duration) {
this.position = this.position % this.duration
}
}
else {
this.position = this.duration
this.stop()
}
this.emit("complete")
}
}