@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
62 lines (61 loc) • 1.74 kB
JavaScript
import { CircleCollider } from "./index11.js";
import { AABBCollider } from "./index12.js";
import { entityToPolygonConfig, PolygonCollider } from "./index19.js";
import { CapsuleCollider } from "./index13.js";
const colliderCache = /* @__PURE__ */ new WeakMap();
function createCollider(entity) {
const cached = colliderCache.get(entity);
if (cached) {
return cached;
}
let collider = null;
if (entityToPolygonConfig.has(entity)) {
collider = new PolygonCollider(entity);
} else if (entity.capsule) {
collider = new CapsuleCollider(entity);
} else if (entity.radius > 0) {
collider = new CircleCollider(entity);
} else if (entity.width > 0 && entity.height > 0) {
collider = new AABBCollider(entity);
}
if (collider) {
colliderCache.set(entity, collider);
}
return collider;
}
function testCollision(entityA, entityB) {
if (!entityA.canCollideWith(entityB)) {
return null;
}
if (entityA.isStatic() && entityB.isStatic()) {
return null;
}
const colliderA = createCollider(entityA);
const colliderB = createCollider(entityB);
if (!colliderA || !colliderB) {
return null;
}
return colliderA.testCollision(colliderB);
}
function findCollisions(entities) {
const collisions = [];
for (let i = 0; i < entities.length; i++) {
const entityA = entities[i];
if (!entityA) continue;
for (let j = i + 1; j < entities.length; j++) {
const entityB = entities[j];
if (!entityB) continue;
const collision = testCollision(entityA, entityB);
if (collision) {
collisions.push(collision);
}
}
}
return collisions;
}
export {
createCollider,
findCollisions,
testCollision
};
//# sourceMappingURL=index18.js.map