Fixed issues in AnimManager.js

This commit is contained in:
Arne van Iterson 2020-03-22 13:57:15 +01:00
parent 3a210d1b60
commit cb46be0088

View File

@ -4,7 +4,13 @@ class Anim {
this.rate = rate; this.rate = rate;
this.reset(); this.reset();
} }
reset() {
this.frame = this.frames[0];
this.curFrame = 0;
this.curTime = 0;
}
update(dt) { update(dt) {
const { rate, frames } = this; const { rate, frames } = this;
if ((this.curTime += dt) > rate) { if ((this.curTime += dt) > rate) {
@ -13,51 +19,47 @@ class Anim {
this.curTime -= rate; this.curTime -= rate;
} }
} }
reset() {
this.frame = this.frames[0];
this.curFrame = 0;
this.curTime = 0;
}
} }
class AnimManager { class AnimManager {
constructor(e) { constructor(e = { x: 0, y: 0 }) {
this.anims = {}; this.anims = {};
this.running = false; this.running = false;
this.frameSource = e.frame || e; this.frameSource = e.frame || e;
this.currrent = null; this.current = null;
} }
add(name, frames, speed) { add(name, frames, speed) {
this.anims[name] = new Anim(frames, speed); this.anims[name] = new Anim(frames, speed);
return this.anims[name]; return this.anims[name];
} }
update(dt) { update(dt) {
const { current, anims, frameSource } = this; const { current, anims, frameSource } = this;
if (!current) { if (!current) {
return; return;
} }
const anim = anims[current]; const anim = anims[current];
anim.update(dt); anim.update(dt);
// Sync the tileSprite frame
frameSource.x = anim.frame.x; frameSource.x = anim.frame.x;
frameSource.y = anim.frame.y; frameSource.y = anim.frame.y;
} }
play(anim) { play(anim) {
const { current, anims } = this; const { current, anims } = this;
if (anim === current ) { if (anim === current) {
return; return;
} }
this.current = anim; this.current = anim;
anims[anim].reset(); anims[anim].reset();
} }
stop() { stop() {
this.current = null; this.current = null;
} }
} }
module.exports = AnimManager; module.exports = AnimManager;