fix: collisions

This commit is contained in:
cha0s 2024-07-23 11:46:52 -05:00
parent 88f0ec4715
commit 9f0c3f3c07

View File

@ -39,32 +39,31 @@ export default class Colliders extends System {
}
tick() {
const seen = {};
const collisions = new Map();
for (const entity of this.ecs.changed(['Position'])) {
this.updateHash(entity);
}
for (const entity of this.ecs.changed(['Position'])) {
if (seen[entity.id]) {
continue;
}
seen[entity.id] = true;
if (!entity.Collider) {
continue;
}
const {collidingWith: wasCollidingWith} = entity.Collider;
entity.Collider.collidingWith = {};
this.updateHash(entity);
collisions.set(entity, new Set());
for (const other of this.within(entity.Collider.aabb)) {
if (seen[other.id]) {
if (entity === other) {
continue;
}
seen[other.id] = true;
if (!other.Collider) {
if (!collisions.has(other)) {
collisions.set(other, new Set());
}
if (!other.Collider || collisions.get(other).has(entity)) {
continue;
}
delete other.Collider.collidingWith[entity.id];
const intersections = entity.Collider.isCollidingWith(other.Collider);
if (intersections.length > 0) {
entity.Collider.collidingWith[other.id] = true;
other.Collider.collidingWith[entity.id] = true;
if (!wasCollidingWith[other.id]) {
collisions.get(entity).add(other);
if (!entity.Collider.collidingWith[other.id]) {
entity.Collider.collidingWith[other.id] = true;
other.Collider.collidingWith[entity.id] = true;
if (entity.Collider.collisionStartScriptInstance) {
const script = entity.Collider.collisionStartScriptInstance.clone();
script.context.intersections = intersections;
@ -117,22 +116,20 @@ export default class Colliders extends System {
}
}
}
}
for (const otherId in wasCollidingWith) {
if (!entity.Collider.collidingWith[otherId]) {
const other = this.ecs.get(otherId);
if (!other || !other.Collider) {
continue;
}
if (entity.Collider.collisionEndScriptInstance) {
const script = entity.Collider.collisionEndScriptInstance.clone();
script.context.other = other;
entity.Ticking.add(script.ticker());
}
if (other.Collider.collisionEndScriptInstance) {
const script = other.Collider.collisionEndScriptInstance.clone();
script.context.other = entity;
other.Ticking.add(script.ticker());
else {
if (entity.Collider.collidingWith[other.id]) {
if (entity.Collider.collisionEndScriptInstance) {
const script = entity.Collider.collisionEndScriptInstance.clone();
script.context.other = other;
entity.Ticking.add(script.ticker());
}
if (other.Collider.collisionEndScriptInstance) {
const script = other.Collider.collisionEndScriptInstance.clone();
script.context.other = entity;
other.Ticking.add(script.ticker());
}
delete entity.Collider.collidingWith[other.id];
delete other.Collider.collidingWith[entity.id];
}
}
}