silphius/app/ecs/systems/apply-control-movement.js
2024-07-20 05:07:39 -05:00

35 lines
756 B
JavaScript

import {System} from '@/ecs/index.js';
import {normalizeVector} from '@/util/math.js';
export default class ApplyControlMovement extends System {
static get priority() {
return {
before: 'IntegratePhysics',
};
}
static queries() {
return {
default: ['Controlled', 'Forces', 'Speed'],
};
}
tick() {
for (const {Controlled, Forces, Speed} of this.select('default')) {
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,
});
}
}
}
}