silphius/app/ecs-systems/interactions.js

34 lines
890 B
JavaScript
Raw Normal View History

2024-07-01 18:12:53 -05:00
import {System} from '@/ecs/index.js';
import {distance} from '@/util/math.js';
2024-07-01 18:12:53 -05:00
2024-07-02 14:41:54 -05:00
export default class Interactions extends System {
2024-07-01 18:12:53 -05:00
static queries() {
return {
default: ['Interacts'],
};
}
tick() {
for (const entity of this.select('default')) {
const {Interacts} = entity;
2024-07-01 18:12:53 -05:00
Interacts.willInteractWith = 0
// todo sort
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;
2024-07-01 18:12:53 -05:00
}
}
}
}
}