forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimators.js
244 lines (205 loc) · 6.39 KB
/
animators.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var JS = cc.js;
var Playable = require('./playable');
var AnimationNode = require('./types').AnimationNode;
var DynamicAnimCurve = require('./animation-curves').DynamicAnimCurve;
// The base of animators
function Animator (target) {
this.target = target;
// {AnimationNodeBase}
this._anims = new JS.array.MutableForwardIterator([]);
}
JS.extend(Animator, Playable);
var animProto = Animator.prototype;
// 由 AnimationManager 调用,只有在该 animator 处于播放状态时才会被调用
animProto.update = function (dt) {
var iterator = this._anims;
var array = iterator.array;
var stoppedCount = 0;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
var anim = array[iterator.i];
if (anim._isPlaying && !anim._isPaused) {
anim.update(dt);
if (!anim._isPlaying) {
stoppedCount ++;
}
}
}
if (array.length === 0 || stoppedCount >= array.length) {
this.stop();
}
};
animProto.stopStatesExcept = function (state) {
var iterator = this._anims;
var array = iterator.array;
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
var anim = array[iterator.i];
if (anim === state) {
continue;
}
this.stopState(anim);
}
};
animProto.on = function (type, callback, target, useCapture) {
var array = this._anims.array;
for (var i = 0; i < array.length; ++i) {
array[i].on(type, callback, target, useCapture);
}
}
animProto.onPlay = function () {
cc.director.getAnimationManager().addAnimator(this);
};
animProto.onStop = function () {
if (!this._isPaused) {
cc.director.getAnimationManager().removeAnimator(this);
}
};
animProto.onResume = function () {
cc.director.getAnimationManager().addAnimator(this);
};
animProto.onPause = function () {
cc.director.getAnimationManager().removeAnimator(this);
};
animProto.addAnimation = function (anim) {
var index = this._anims.array.indexOf(anim);
if (index === -1) {
this._anims.push(anim);
}
};
animProto.removeAnimation = function (anim) {
var index = this._anims.array.indexOf(anim);
if (index >= 0) {
this._anims.fastRemoveAt(index);
}
else {
cc.errorID(3908);
}
};
// The actual animator for Entity
function EntityAnimator (target) {
Animator.call(this, target);
}
JS.extend(EntityAnimator, Animator);
var entProto = EntityAnimator.prototype;
// 通用逻辑
function computeNullRatios (keyFrames) {
var lastIndex = 0;
var lastRatio = 0;
var len = keyFrames.length;
for (var i = 0; i < len; i++) {
var frame = keyFrames[i];
var ratio = frame.ratio;
if (i === 0 && typeof ratio !== "number") {
// 如果一开始就没有 ratio,则默认从 0 开始
frame.computedRatio = ratio = 0;
}
else if (i === len - 1 && typeof ratio !== "number") {
// 如果最后没有 ratio,则设置为 1
frame.computedRatio = ratio = 1;
}
if (typeof ratio === "number") {
if (lastIndex + 1 < i) {
var count = i - lastIndex;
var step = (ratio - lastRatio) / count;
for (var j = lastIndex + 1; j < i; j++) {
lastRatio += step;
keyFrames[j].computedRatio = lastRatio; // 不占用已有变量,这样 keyFrames 才能重用
}
}
lastIndex = i;
lastRatio = ratio;
}
}
}
if (CC_TEST) {
cc._Test.computeNullRatios = computeNullRatios;
}
///**
// * @param {object[]} keyFrames
// * @param {object} [timingInput] - This dictionary is used as a convenience for specifying the timing properties of an Animation in bulk.
// * @return {AnimationNode}
// */
entProto.animate = function (keyFrames, timingInput) {
if (! keyFrames) {
cc.errorID(3909);
return null;
}
// compute absolute ratio of each keyframe with a null ratio
computeNullRatios(keyFrames);
var anim = this._doAnimate(keyFrames, timingInput);
this.play();
return anim;
};
// 具体逻辑
function findCurve (curves, target, propName) {
var i = 0, curve;
for (; i < curves.length; i++) {
curve = curves[i];
if (curve.target === target && curve.prop === propName) {
return curve;
}
}
return null;
}
function createPropCurve (curves, target, propName, value, ratio) {
var curve = findCurve(curves, target, propName);
if (! curve) {
curve = new DynamicAnimCurve();
curves.push(curve);
// 缓存目标对象,所以 Component 必须一开始都创建好并且不能运行时动态替换……
curve.target = target;
curve.prop = propName;
}
curve.values.push(value);
curve.ratios.push(ratio);
}
entProto._doAnimate = function (keyFrames, timingInput) {
var anim = new AnimationNode(this, null, timingInput);
anim.play();
var curves = anim.curves;
// create curves
var lastRatio = -1;
for (var i = 0; i < keyFrames.length; i++) {
var frame = keyFrames[i];
// get ratio
var ratio = frame.ratio;
if (typeof ratio !== "number") {
ratio = frame.computedRatio;
}
if (ratio < 0) {
cc.errorID(3910);
continue;
}
if (ratio < lastRatio) {
cc.errorID(3911);
continue;
}
lastRatio = ratio;
// 先遍历每一帧,获得所有曲线
for (var key in frame) {
var data = frame[key];
if (key === 'props') {
for (var propName in data) {
createPropCurve(curves, this.target, propName, data[propName], ratio);
}
}
else if (key === 'comps') {
for (var compName in data) {
var comp = this.target.getComponent(compName);
var compData = data[compName];
for (var propName in compData) {
createPropCurve(curves, comp, propName, compData[propName], ratio);
}
}
}
}
}
this._anims.push(anim);
return anim;
};
if (CC_TEST) {
cc._Test.EntityAnimator = EntityAnimator;
}
module.exports = {
Animator: Animator,
EntityAnimator: EntityAnimator
};