arcade-physics
Version:
Use Arcade Physics without Phaser.
87 lines • 3.66 kB
JavaScript
;
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetOverlapX = void 0;
const const_1 = __importDefault(require("./const"));
/**
* Calculates and returns the horizontal overlap between two arcade physics bodies and sets their properties
* accordingly, including: `touching.left`, `touching.right`, `touching.none` and `overlapX'.
*
* @function Phaser.Physics.Arcade.GetOverlapX
* @since 3.0.0
*
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate.
* @param {boolean} overlapOnly - Is this an overlap only check, or part of separation?
* @param {number} bias - A value added to the delta values during collision checks. Increase it to prevent sprite tunneling(sprites passing through another instead of colliding).
*
* @return {number} The amount of overlap.
*/
const GetOverlapX = (body1, body2, overlapOnly, bias) => {
let overlap = 0;
const maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + bias;
if (body1._dx === 0 && body2._dx === 0) {
// They overlap but neither of them are moving
body1.embedded = true;
body2.embedded = true;
}
else if (body1._dx > body2._dx) {
// Body1 is moving right and / or Body2 is moving left
overlap = body1.right - body2.x;
if ((overlap > maxOverlap && !overlapOnly) ||
body1.checkCollision.right === false ||
body2.checkCollision.left === false) {
overlap = 0;
}
else {
body1.touching.none = false;
body1.touching.right = true;
body2.touching.none = false;
body2.touching.left = true;
if (body2.physicsType === const_1.default.PHYSICS_TYPE.STATIC_BODY && !overlapOnly) {
body1.blocked.none = false;
body1.blocked.right = true;
}
if (body1.physicsType === const_1.default.PHYSICS_TYPE.STATIC_BODY && !overlapOnly) {
body2.blocked.none = false;
body2.blocked.left = true;
}
}
}
else if (body1._dx < body2._dx) {
// Body1 is moving left and/or Body2 is moving right
overlap = body1.x - body2.width - body2.x;
if ((-overlap > maxOverlap && !overlapOnly) ||
body1.checkCollision.left === false ||
body2.checkCollision.right === false) {
overlap = 0;
}
else {
body1.touching.none = false;
body1.touching.left = true;
body2.touching.none = false;
body2.touching.right = true;
if (body2.physicsType === const_1.default.PHYSICS_TYPE.STATIC_BODY && !overlapOnly) {
body1.blocked.none = false;
body1.blocked.left = true;
}
if (body1.physicsType === const_1.default.PHYSICS_TYPE.STATIC_BODY && !overlapOnly) {
body2.blocked.none = false;
body2.blocked.right = true;
}
}
}
// Resets the overlapX to zero if there is no overlap, or to the actual pixel value if there is
body1.overlapX = overlap;
body2.overlapX = overlap;
return overlap;
};
exports.GetOverlapX = GetOverlapX;
//# sourceMappingURL=GetOverlapX.js.map