silphius/app/ecs-systems/sprite-direction.js
2024-06-19 02:09:00 -05:00

40 lines
901 B
JavaScript

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 {moveUp, moveRight, moveDown, moveLeft} = entity.Controlled;
if (moveUp > 0 || moveRight > 0 || moveDown > 0 || moveLeft > 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(':');
}
}
}
}