silphius/app/ecs/systems/apply-control-movement.js

35 lines
756 B
JavaScript
Raw Normal View History

2024-06-19 02:02:14 -05:00
import {System} from '@/ecs/index.js';
2024-07-02 08:05:36 -05:00
import {normalizeVector} from '@/util/math.js';
2024-06-19 02:02:14 -05:00
export default class ApplyControlMovement extends System {
2024-06-25 10:44:37 -05:00
static get priority() {
return {
2024-07-02 08:05:36 -05:00
before: 'IntegratePhysics',
2024-06-25 10:44:37 -05:00
};
}
static queries() {
return {
default: ['Controlled', 'Forces', 'Speed'],
};
}
2024-07-03 19:05:40 -05:00
2024-06-19 02:02:14 -05:00
tick() {
2024-06-26 07:41:07 -05:00
for (const {Controlled, Forces, Speed} of this.select('default')) {
2024-06-25 10:44:37 -05:00
if (!Controlled.locked) {
2024-07-02 08:05:36 -05:00
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,
});
2024-06-25 10:44:37 -05:00
}
2024-06-19 02:02:14 -05:00
}
}
}