42 lines
903 B
JavaScript
42 lines
903 B
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) {
|
|
const name = {
|
|
0: 'up',
|
|
1: 'right',
|
|
2: 'down',
|
|
3: 'left',
|
|
};
|
|
parts.push(name[Direction.direction]);
|
|
}
|
|
if (parts.length > 0) {
|
|
Sprite.animation = parts.join(':');
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|