47 lines
939 B
JavaScript
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,
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
|