silphius/app/ecs/systems/control-direction.js
2024-09-05 16:48:57 -05:00

42 lines
800 B
JavaScript

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