-
Notifications
You must be signed in to change notification settings - Fork 16
/
WorldClock.java
206 lines (184 loc) · 5.46 KB
/
WorldClock.java
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
201
202
203
204
205
206
package com.dragonbones.animation;
import com.dragonbones.armature.Armature;
import com.dragonbones.util.Array;
import org.jetbrains.annotations.Nullable;
/**
* WorldClock 提供时钟支持,为每个加入到时钟的 IAnimatable 对象更新时间。
*
* @version DragonBones 3.0
* @language zh_CN
* @see IAnimatable
* @see Armature
*/
public class WorldClock implements IAnimatable {
/**
* 一个可以直接使用的全局 WorldClock 实例.
*
* @version DragonBones 3.0
* @language zh_CN
*/
public static final WorldClock clock = new WorldClock();
/**
* 当前时间。 (以秒为单位)
*
* @version DragonBones 3.0
* @language zh_CN
*/
public float time = 0f;
/**
* 时间流逝速度,用于控制动画变速播放。 [0: 停止播放, (0~1): 慢速播放, 1: 正常播放, (1~N): 快速播放]
*
* @default 1f
* @version DragonBones 3.0
* @language zh_CN
*/
public float timeScale = 1f;
private final Array<IAnimatable> _animatebles = new Array<>();
@Nullable
private WorldClock _clock = null;
/**
* 创建一个新的 WorldClock 实例。
* 通常并不需要单独创建 WorldClock 实例,可以直接使用 WorldClock.clock 静态实例。
* (创建更多独立的 WorldClock 实例可以更灵活的为需要更新的 IAnimateble 实例分组,用于控制不同组不同的播放速度)
*
* @version DragonBones 3.0
* @language zh_CN
*/
public WorldClock() {
this(-1f);
}
/**
* 创建一个新的 WorldClock 实例。
* 通常并不需要单独创建 WorldClock 实例,可以直接使用 WorldClock.clock 静态实例。
* (创建更多独立的 WorldClock 实例可以更灵活的为需要更新的 IAnimateble 实例分组,用于控制不同组不同的播放速度)
*
* @version DragonBones 3.0
* @language zh_CN
*/
public WorldClock(float time) {
if (time < 0f) {
this.time = System.currentTimeMillis() * 0.001f;
} else {
this.time = time;
}
}
/**
* 为所有的 IAnimatable 实例更新时间。
*
* @param passedTime 前进的时间。 (以秒为单位,当设置为 -1 时将自动计算当前帧与上一帧的时间差)
* @version DragonBones 3.0
* @language zh_CN
*/
public void advanceTime(float passedTime) {
if (passedTime != passedTime) { // isNaN
passedTime = 0f;
}
if (passedTime < 0f) {
passedTime = System.currentTimeMillis() * 0.001f - this.time;
}
if (this.timeScale != 1f) {
passedTime *= this.timeScale;
}
if (passedTime < 0f) {
this.time -= passedTime;
} else {
this.time += passedTime;
}
if (passedTime == 0f) {
return;
}
int i = 0, r = 0, l = this._animatebles.size();
for (; i < l; ++i) {
IAnimatable animatable = this._animatebles.get(i);
if (animatable != null) {
if (r > 0) {
this._animatebles.set(i - r, animatable);
this._animatebles.set(i, null);
}
animatable.advanceTime(passedTime);
} else {
r++;
}
}
if (r > 0) {
l = this._animatebles.size();
for (; i < l; ++i) {
IAnimatable animateble = this._animatebles.get(i);
if (animateble != null) {
this._animatebles.set(i - r, animateble);
} else {
r++;
}
}
this._animatebles.setLength(this._animatebles.size() - r);
}
}
/**
* 是否包含 IAnimatable 实例
*
* @param value IAnimatable 实例。
* @version DragonBones 3.0
* @language zh_CN
*/
public boolean contains(IAnimatable value) {
return this._animatebles.indexOf(value) >= 0;
}
/**
* 添加 IAnimatable 实例。
*
* @param value IAnimatable 实例。
* @version DragonBones 3.0
* @language zh_CN
*/
public void add(IAnimatable value) {
if (this._animatebles.indexOf(value) < 0) {
this._animatebles.add(value);
value.setClock(this);
}
}
/**
* 移除 IAnimatable 实例。
*
* @param value IAnimatable 实例。
* @version DragonBones 3.0
* @language zh_CN
*/
public void remove(IAnimatable value) {
int index = this._animatebles.indexOf(value);
if (index >= 0) {
this._animatebles.set(index, null);
value.setClock(null);
}
}
/**
* 清除所有的 IAnimatable 实例。
*
* @version DragonBones 3.0
* @language zh_CN
*/
public void clear() {
for (IAnimatable animatable : this._animatebles) {
if (animatable != null) {
animatable.setClock(null);
}
}
}
/**
* @inheritDoc
*/
public WorldClock getClock() {
return this._clock;
}
public void setClock(WorldClock value) {
if (this._clock == value) {
return;
}
if (this._clock != null) {
this._clock.remove(this);
}
this._clock = value;
if (this._clock != null) {
this._clock.add(this);
}
}
}