silphius/app/ecs/systems/colliders.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-07-02 16:16:39 -05:00
import {System} from '@/ecs/index.js';
export default class Colliders extends System {
static get priority() {
return {
2024-07-28 18:40:58 -05:00
after: 'MaintainColliderHash',
2024-07-02 16:16:39 -05:00
};
}
tick() {
2024-07-26 06:00:37 -05:00
const checked = new Map();
2024-07-02 16:16:39 -05:00
for (const entity of this.ecs.changed(['Position'])) {
if (!entity.Collider) {
continue;
}
2024-07-26 06:00:37 -05:00
if (!checked.has(entity)) {
checked.set(entity, new Set());
}
2024-07-28 18:40:58 -05:00
const within = this.ecs.system('MaintainColliderHash').within(entity.Collider.aabb);
2024-07-26 06:00:37 -05:00
for (const other of within) {
if (entity === other || !other.Collider) {
2024-07-02 16:16:39 -05:00
continue;
}
2024-07-26 06:00:37 -05:00
if (!checked.has(other)) {
checked.set(other, new Set());
2024-07-23 11:46:52 -05:00
}
2024-07-26 06:00:37 -05:00
if (checked.get(entity).has(other)) {
2024-07-02 16:16:39 -05:00
continue;
}
2024-07-26 06:00:37 -05:00
checked.get(other).add(entity);
entity.Collider.checkCollision(other.Collider);
}
for (const [other, intersections] of entity.Collider.$$intersections) {
if (!within.has(this.ecs.get(other.entity))) {
entity.Collider.endIntersections(other, intersections);
2024-07-02 16:16:39 -05:00
}
}
}
}
}