silphius/app/ecs-systems/sprite-direction.js

40 lines
869 B
JavaScript
Raw Normal View History

2024-06-12 19:35:51 -05:00
import {System} from '@/ecs/index.js';
export default class SpriteDirection extends System {
static queries() {
return {
default: ['Sprite'],
};
}
tick() {
for (const [Sprite, entityId] of this.select('default')) {
const entity = this.ecs.get(entityId);
const parts = [];
if (entity.Controlled) {
const {up, right, down, left} = entity.Controlled;
if (up > 0 || right > 0 || down > 0 || left > 0) {
parts.push('moving');
}
else {
parts.push('idle');
}
}
if (entity.Direction) {
const name = {
0: 'up',
1: 'right',
2: 'down',
3: 'left',
};
parts.push(name[entity.Direction.direction]);
}
if (parts.length > 0) {
Sprite.animation = parts.join(':');
}
}
}
}