silphius/app/ecs/systems/interactions.js
2024-07-20 05:07:39 -05:00

37 lines
972 B
JavaScript

import {System} from '@/ecs/index.js';
import {distance} from '@/util/math.js';
export default class Interactions extends System {
static queries() {
return {
default: ['Interacts'],
};
}
tick() {
for (const entity of this.select('default')) {
const {Interacts} = entity;
let willInteract = false;
const entities = Array.from(this.ecs.system('Colliders').within(Interacts.aabb()))
.filter((other) => other !== entity)
.sort(({Position: l}, {Position: r}) => {
return distance(entity.Position, l) > distance(entity.Position, r) ? -1 : 1;
});
for (const other of entities) {
if (other === entity) {
continue;
}
if (other.Interactive && other.Interactive.interacting) {
Interacts.willInteractWith = other.id;
willInteract = true;
}
}
if (!willInteract) {
Interacts.willInteractWith = 0;
}
}
}
}