refactor: revertPosition

This commit is contained in:
cha0s 2024-11-16 06:33:46 -06:00
parent 5aa17441a1
commit e540d98c46

View File

@ -99,37 +99,10 @@ export default class Collider extends Component {
}
// undo restricted movement
if (!body.unstoppable && otherBody.impassable) {
const j = this.bodies.indexOf(body);
const oj = other.bodies.indexOf(otherBody);
const aabb = this.$$aabbs[j];
const otherAabb = other.aabbs[oj];
const {Position} = thisEntity;
const {lastX, lastY} = Position;
const {x, y} = Position;
if (intersects(
{
x0: aabb.x0 + lastX,
x1: aabb.x1 + lastX,
y0: aabb.y0 + y,
y1: aabb.y1 + y,
},
otherAabb,
)) {
Position.y = lastY;
Position.lastY = lastY;
}
if (intersects(
{
x0: aabb.x0 + x,
x1: aabb.x1 + x,
y0: aabb.y0 + lastY,
y1: aabb.y1 + lastY,
},
otherAabb,
)) {
Position.x = lastX;
Position.lastX = lastX;
}
this.revertPosition(
this.$$aabbs[this.bodies.indexOf(body)],
other.aabbs[other.bodies.indexOf(otherBody)],
);
break;
}
}
@ -228,6 +201,35 @@ export default class Collider extends Component {
}
return false;
}
revertPosition(aabb, otherAabb) {
const {Position} = ecs.get(this.entity);
const {lastX, lastY} = Position;
const {x, y} = Position;
if (intersects(
{
x0: aabb.x0 + lastX,
x1: aabb.x1 + lastX,
y0: aabb.y0 + y,
y1: aabb.y1 + y,
},
otherAabb,
)) {
Position.y = lastY;
Position.lastY = lastY;
}
if (intersects(
{
x0: aabb.x0 + x,
x1: aabb.x1 + x,
y0: aabb.y0 + lastY,
y1: aabb.y1 + lastY,
},
otherAabb,
)) {
Position.x = lastX;
Position.lastX = lastX;
}
}
updateAabbs() {
this.$$aabb = {x0: Infinity, x1: -Infinity, y0: Infinity, y1: -Infinity};
this.$$aabbs = [];