silphius/app/ecs/systems/control-direction.js
2024-07-25 02:07:36 -05:00

30 lines
643 B
JavaScript

import {System} from '@/ecs/index.js';
import {TAU} from '@/util/math.js';
export default class ControlDirection extends System {
tick() {
for (const {Controlled, Direction} of this.ecs.changed(['Controlled'])) {
const {locked, moveUp, moveRight, moveDown, moveLeft} = Controlled;
if (locked) {
continue;
}
if (
0 === moveRight
&& 0 === moveDown
&& 0 === moveLeft
&& 0 === moveUp
) {
continue;
}
Direction.direction = (
TAU + Math.atan2(
moveDown - moveUp,
moveRight - moveLeft,
)
) % TAU;
}
}
}