silphius/app/ecs/systems/sprite-direction.js
2024-07-25 10:48:22 -05:00

46 lines
1.0 KiB
JavaScript

import {System} from '@/ecs/index.js';
export default class SpriteDirection extends System {
static queries() {
return {
default: ['Sprite'],
};
}
tick() {
for (const {Controlled, Direction, Sprite} of this.select('default')) {
const parts = [];
if (Controlled) {
const {locked, moveUp, moveRight, moveDown, moveLeft} = Controlled;
if (locked) {
continue;
}
if ((moveUp > 0 || moveRight > 0 || moveDown > 0 || moveLeft > 0)) {
parts.push('moving');
}
else {
parts.push('idle');
}
}
if (Direction) {
if (!Sprite.rotates) {
const name = {
0: 'right',
1: 'down',
2: 'left',
3: 'up',
};
parts.push(name[Direction.quantize(4)]);
}
}
if (parts.length > 0) {
if (Sprite.hasAnimation(parts.join(':'))) {
Sprite.animation = parts.join(':');
}
}
}
}
}