/
playable.ts
119 lines (97 loc) · 2.21 KB
/
playable.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
/**
* @category animation
*/
import { getError } from '../platform/debug';
export class Playable {
/**
* @en Is playing or paused in play mode?
* @zh 当前是否正在播放。
* @default false
*/
get isPlaying () {
return this._isPlaying;
}
/**
* @en Is currently paused? This can be true even if in edit mode(isPlaying == false).
* @zh 当前是否正在暂停。
* @default false
*/
get isPaused () {
return this._isPaused;
}
private _isPlaying = false;
private _isPaused = false;
private _stepOnce = false;
/**
* @en Play this animation.
* @zh 播放动画。
*/
public play () {
if (this._isPlaying) {
if (this._isPaused) {
this._isPaused = false;
this.onResume();
} else {
this.onError(getError(3912));
}
} else {
this._isPlaying = true;
this.onPlay();
}
}
/**
* @en Stop this animation.
* @zh 停止动画播放。
*/
public stop () {
if (this._isPlaying) {
this._isPlaying = false;
this.onStop();
// need reset pause flag after onStop
this._isPaused = false;
}
}
/**
* @en Pause this animation.
* @zh 暂停动画。
*/
public pause () {
if (this._isPlaying && !this._isPaused) {
this._isPaused = true;
this.onPause();
}
}
/**
* @en Resume this animation.
* @zh 重新播放动画。
*/
public resume () {
if (this._isPlaying && this._isPaused) {
this._isPaused = false;
this.onResume();
}
}
/**
* @en Perform a single frame step.
* @zh 执行一帧动画。
*/
public step () {
this.pause();
this._stepOnce = true;
if (!this._isPlaying) {
this.play();
}
}
public update (deltaTime: number) {
}
protected onPlay () {
}
protected onPause () {
}
protected onResume () {
}
protected onStop () {
}
protected onError (message: string) {
}
}