forked from cocos/cocos-engine
/
animation-clip.test.ts
114 lines (100 loc) · 2.81 KB
/
animation-clip.test.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
import { AnimationClip, js, AnimationState, Node, Component, Vec3, AnimationManager } from '../../cocos/core';
import { ComponentPath } from '../../cocos/core/animation/animation';
import { ccclass } from '../../cocos/core/data/class-decorator';
test('Common target', () => {
@ccclass('TestComponent')
class TestComponent extends Component {
get value () {
return this.c;
}
set value (value: Vec3) {
Vec3.copy(this.c, value);
}
public c: Vec3 = new Vec3(0, -1, 0);
}
const node = new Node();
const testComponent = node.addComponent(TestComponent);
const animationClip = new AnimationClip();
animationClip.wrapMode = AnimationClip.WrapMode.Loop;
animationClip.duration = 2;
animationClip.keys = [
[0, 1, 2],
];
animationClip.commonTargets = [{
modifiers: [
new ComponentPath(js.getClassName(TestComponent)),
'value',
],
}];
animationClip.curves = [
{
commonTarget: 0,
modifiers: [ 'x' ],
data: {
keys: 0,
values: [ 0, 0.5, 1 ],
}
},
{
commonTarget: 0,
modifiers: [ 'z' ],
data: {
keys: 0,
values: [ 0, 0.5, 1 ],
}
},
];
const expects: Record<number, Vec3> = {
0: new Vec3(0, -1, 0),
1: new Vec3(0.5, -1, 0.5),
2: new Vec3(1, -1, 1),
};
const animationState = new AnimationState(animationClip);
animationState.initialize(node);
for (const time of Object.keys(expects)) {
const c = expects[time];
animationState.setTime(parseInt(time));
animationState.sample();
expect(testComponent.c).toEqual(c);
}
});
test('animation state', () => {
const animationManager = new AnimationManager();
const mockInstance = jest.spyOn((global as any).cc.director, 'getAnimationManager').mockImplementation(() => {
return animationManager;
});
const clip = new AnimationClip('default');
clip.duration = 2.0;
const keys = [
0,
0.1,
0.2,
];
const values = [
new Vec3(),
new Vec3(1),
new Vec3(2),
];
clip.keys = [keys];
clip.curves = [{
modifiers: [
'position',
],
data: {
keys: 0,
values,
},
}];
const state = new AnimationState(clip, clip.name);
state.weight = 1.0;
const node = new Node();
state.initialize(node);
animationManager.update(0);
state.play();
state.pause();
state.setTime(keys[1]);
state.update(0);
animationManager.update(0);
expect(node.getPosition()).toEqual(values[1]);
mockInstance.mockRestore();
});