UNPKG

mylingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

125 lines 4.16 kB
import { debounce } from "@lincode/utils"; import AnimationManager from "./AnimationManager"; import StaticObjectManager from "../StaticObjectManager"; const buildAnimationTracks = debounce((val) => { const entries = Object.entries(val); let maxLength = 0; for (const [, { length }] of entries) length > maxLength && (maxLength = length); const duration = 1000; const timeStep = (duration * 0.001) / maxLength; const result = {}; for (const [name, values] of entries) result[name] = Object.fromEntries(values.map((v, i) => [(i * timeStep).toFixed(2), v])); return result; }, 0, "trailingPromise"); export default class AnimatedObjectManager extends StaticObjectManager { animationManagers; get animations() { return (this.animationManagers ??= {}); } set animations(val) { this.animationManagers = val; } createAnimation(name) { if (name in this.animations) { const animation = this.animations[name]; if (typeof animation !== "string") return animation; } const animation = this.watch(new AnimationManager(name, this)); this.animations[name] = animation; return animation; } buildAnimation(val) { buildAnimationTracks(val).then((tracks) => { const name = "lingo3d-animation"; this.createAnimation(name).setTracks(tracks); this.playAnimation(name); }); } makeAnimationProxy(source) { return new Proxy(source, { get: (anim, prop) => { return anim[prop]; }, set: (anim, prop, value) => { anim[prop] = value; this.buildAnimation(anim); return true; } }); } animationManager; _animationPaused; get animationPaused() { return this._animationPaused; } set animationPaused(value) { this._animationPaused = value; this.animationManager?.setPaused(!!value); } animationRepeat; onAnimationFinish; playAnimation(name, o) { const manager = (this.animationManager = typeof name === "string" ? this.animations[name] : Object.values(this.animations)[name ?? 0]); if (!manager) return; manager.play(o); this._animationPaused && manager.setPaused(true); } stopAnimation() { this.animationManager?.stop(); } serializeAnimation; setAnimation(val, o) { this._animation = val; if (typeof val === "string" || typeof val === "number") { this.serializeAnimation = val; this.playAnimation(val, o); return; } if (typeof val === "boolean") { val ? this.playAnimation(undefined, o) : this.stopAnimation(); return; } if (!val) { this.stopAnimation(); return; } this._animation = this.makeAnimationProxy(val); this.buildAnimation(val); } _animation; get animation() { return this._animation; } set animation(val) { if (Array.isArray(val)) { let currentIndex = 0; const o = { onFinish: () => { if (++currentIndex >= val.length) { if (this.animationRepeat === false) { this.onAnimationFinish?.(); return; } currentIndex = 0; } this.setAnimation(val[currentIndex], o); }, repeat: false }; this.setAnimation(val[0], o); return; } this.queueMicrotask(() => this.setAnimation(val, { repeat: this.animationRepeat, onFinish: this.onAnimationFinish })); } } //# sourceMappingURL=index.js.map