the-world-engine
Version:
three.js based, unity like game engine for browser
66 lines (64 loc) • 1.61 kB
JavaScript
import { Component } from "../../hierarchy_object/Component";
import { CssSpriteAtlasRenderer } from "../render/CssSpriteAtlasRenderer";
export class SpriteAtlasAnimator extends Component {
disallowMultipleComponent=true;
requiredComponents=[ CssSpriteAtlasRenderer ];
Aa=null;
oa={};
ma=null;
pa=null;
la=0;
_a=false;
ua=2;
da=0;
fa=null;
awake() {
this.Aa = this.gameObject.getComponent(CssSpriteAtlasRenderer);
if (this.fa !== null) {
this.playAnimation(this.fa);
this.fa = null;
}
}
update() {
if (this.Aa === null) return;
if (!this._a) return;
this.da += this.engine.time.deltaTime;
if (this.da >= this.ua) {
this.da = 0;
this.la += 1;
if (this.la >= this.pa.length) {
this.la = 0;
}
this.Aa.imageIndex = this.pa[this.la];
}
}
playAnimation(t) {
if (this.Aa === null) {
this.fa = t;
return;
}
if (this.ma === t) return;
this.ma = t;
this.pa = this.oa[t];
this.la = 0;
this.Aa.imageIndex = this.oa[t][0];
this._a = true;
}
stopAnimation() {
this._a = false;
this.fa = null;
}
addAnimation(t, i) {
if (i.length === 0) {
console.warn(`Animation "${t}" has no frames.`);
return;
}
this.oa[t] = i;
}
get frameDuration() {
return this.ua;
}
set frameDuration(t) {
this.ua = t;
}
}