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

47 lines
939 B
JavaScript

import {System} from '@/ecs/index.js';
import {normalizeVector} from '@/util/math.js';
export default class ApplyControlMovement extends System {
predict(entity) {
this.tickSingle(entity);
}
static get priority() {
return {
before: 'IntegratePhysics',
};
}
static queries() {
return {
default: ['Controlled', 'Forces', 'Speed'],
};
}
tick() {
for (const entity of this.select('default')) {
this.tickSingle(entity);
}
}
tickSingle(entity) {
const {Controlled, Forces, Speed} = entity;
if (!Controlled || !Forces | !Speed) {
return;
}
if (!Controlled.locked) {
const movement = normalizeVector({
x: (Controlled.moveRight - Controlled.moveLeft),
y: (Controlled.moveDown - Controlled.moveUp),
});
Forces.applyImpulse({
x: Speed.speed * movement.x,
y: Speed.speed * movement.y,
});
}
}
}