playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
75 lines (74 loc) • 2.36 kB
JavaScript
import { Component } from "../component.js";
import { ComponentSystem } from "../system.js";
import { AnimationComponent } from "./component.js";
import { AnimationComponentData } from "./data.js";
const _schema = [
"enabled"
];
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.app.systems.on("update", this.onUpdate, this);
}
initializeComponentData(component, data, properties) {
properties = ["activate", "enabled", "loop", "speed", "assets"];
for (const property of properties) {
if (data.hasOwnProperty(property)) {
component[property] = data[property];
}
}
super.initializeComponentData(component, data, _schema);
}
cloneComponent(entity, clone) {
this.addComponent(clone, {});
clone.animation.assets = entity.animation.assets.slice();
clone.animation.speed = entity.animation.speed;
clone.animation.loop = entity.animation.loop;
clone.animation.activate = entity.animation.activate;
clone.animation.enabled = entity.animation.enabled;
const clonedAnimations = {};
const animations = entity.animation.animations;
for (const key in animations) {
if (animations.hasOwnProperty(key)) {
clonedAnimations[key] = animations[key];
}
}
clone.animation.animations = clonedAnimations;
const clonedAnimationsIndex = {};
const animationsIndex = entity.animation.animationsIndex;
for (const key in animationsIndex) {
if (animationsIndex.hasOwnProperty(key)) {
clonedAnimationsIndex[key] = animationsIndex[key];
}
}
clone.animation.animationsIndex = clonedAnimationsIndex;
return clone.animation;
}
onBeforeRemove(entity, component) {
component.onBeforeRemove();
}
onUpdate(dt) {
const components = this.store;
for (const id in components) {
if (components.hasOwnProperty(id)) {
const component = components[id];
if (component.data.enabled && component.entity.enabled) {
component.entity.animation.update(dt);
}
}
}
}
destroy() {
super.destroy();
this.app.systems.off("update", this.onUpdate, this);
}
}
Component._buildAccessors(AnimationComponent.prototype, _schema);
export {
AnimationComponentSystem
};