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; } } } }