silphius/app/ecs/systems/run-animations.js
2024-09-05 16:48:57 -05:00

38 lines
677 B
JavaScript

import {System} from '@/ecs/index.js';
export default class RunAnimations extends System {
predict(entity, elapsed) {
this.tickSingle(entity, elapsed);
}
static queries() {
return {
default: ['Sprite'],
};
}
tick(elapsed) {
for (const entity of this.select('default')) {
this.tickSingle(entity, elapsed);
}
}
tickSingle(entity, elapsed) {
const {Sprite} = entity;
if (!Sprite) {
return;
}
if (0 === Sprite.speed || !Sprite.isAnimating) {
return;
}
Sprite.elapsed += elapsed / Sprite.speed;
while (Sprite.elapsed >= 1) {
Sprite.elapsed -= 1;
Sprite.frame += 1;
}
}
}