forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
167 lines (139 loc) · 5.89 KB
/
system.js
File metadata and controls
167 lines (139 loc) · 5.89 KB
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
import { Component } from '../component.js';
import { ComponentSystem } from '../system.js';
import { AnimationComponent } from './component.js';
import { AnimationComponentData } from './data.js';
const _schema = [
'enabled',
'assets',
'speed',
'loop',
'activate',
'animations',
'skeleton',
'model',
'prevAnim',
'currAnim',
'fromSkel',
'toSkel',
'blending',
'blendTimeRemaining',
'playing'
];
/**
* @class
* @name AnimationComponentSystem
* @augments ComponentSystem
* @classdesc The AnimationComponentSystem manages creating and deleting AnimationComponents.
* @description Create an AnimationComponentSystem.
* @param {Application} app - The application managing this system.
*/
class AnimationComponentSystem extends ComponentSystem {
constructor(app) {
super(app);
this.id = 'animation';
this.ComponentType = AnimationComponent;
this.DataType = AnimationComponentData;
this.schema = _schema;
this.on('beforeremove', this.onBeforeRemove, this);
this.on('update', this.onUpdate, this);
ComponentSystem.bind('update', this.onUpdate, this);
}
initializeComponentData(component, data, properties) {
properties = ['activate', 'enabled', 'loop', 'speed', 'assets'];
super.initializeComponentData(component, data, properties);
}
cloneComponent(entity, clone) {
var key;
this.addComponent(clone, {});
clone.animation.assets = entity.animation.assets.slice();
clone.animation.data.speed = entity.animation.speed;
clone.animation.data.loop = entity.animation.loop;
clone.animation.data.activate = entity.animation.activate;
clone.animation.data.enabled = entity.animation.enabled;
var clonedAnimations = { };
var animations = entity.animation.animations;
for (key in animations) {
if (animations.hasOwnProperty(key)) {
clonedAnimations[key] = animations[key];
}
}
clone.animation.animations = clonedAnimations;
var clonedAnimationsIndex = { };
var animationsIndex = entity.animation.animationsIndex;
for (key in animationsIndex) {
if (animationsIndex.hasOwnProperty(key)) {
clonedAnimationsIndex[key] = animationsIndex[key];
}
}
clone.animation.animationsIndex = clonedAnimationsIndex;
}
onBeforeRemove(entity, component) {
component.onBeforeRemove();
}
onUpdate(dt) {
var components = this.store;
for (var id in components) {
if (components.hasOwnProperty(id)) {
var component = components[id];
var componentData = component.data;
if (componentData.enabled && component.entity.enabled) {
// update blending
if (componentData.blending) {
componentData.blend += dt * componentData.blendSpeed;
if (componentData.blend >= 1.0) {
componentData.blend = 1.0;
}
}
// update skeleton
if (componentData.playing) {
var skeleton = componentData.skeleton;
if (skeleton !== null && componentData.model !== null) {
if (componentData.blending) {
skeleton.blend(componentData.fromSkel, componentData.toSkel, componentData.blend);
} else {
// Advance the animation, interpolating keyframes at each animated node in
// skeleton
var delta = dt * componentData.speed;
skeleton.addTime(delta);
if (componentData.speed > 0 && (skeleton._time === skeleton._animation.duration) && !componentData.loop) {
componentData.playing = false;
} else if (componentData.speed < 0 && skeleton._time === 0 && !componentData.loop) {
componentData.playing = false;
}
}
if (componentData.blending && (componentData.blend === 1.0)) {
skeleton.animation = componentData.toSkel._animation;
}
skeleton.updateGraph();
}
}
// update anim controller
var animEvaluator = componentData.animEvaluator;
if (animEvaluator) {
// force all clip's speed and playing state from the component
for (var i = 0; i < animEvaluator.clips.length; ++i) {
var clip = animEvaluator.clips[i];
clip.speed = componentData.speed;
if (!componentData.playing) {
clip.pause();
} else {
clip.resume();
}
}
// update blend weight
if (componentData.blending) {
animEvaluator.clips[1].blendWeight = componentData.blend;
}
animEvaluator.update(dt);
}
// clear blending flag
if (componentData.blending && componentData.blend === 1.0) {
componentData.blending = false;
}
}
}
}
}
}
Component._buildAccessors(AnimationComponent.prototype, _schema);
export { AnimationComponentSystem };