@litecanvas/utils
Version:
Utilities to help build litecanvas games
61 lines (57 loc) • 1.47 kB
JavaScript
(() => {
// src/_global.js
globalThis.utils = globalThis.utils || {};
globalThis.utils.global = () => {
for (const key in globalThis.utils) {
if ("global" === key) continue;
globalThis[key] = globalThis.utils[key];
}
};
// src/collision/intersection.js
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
const left = Math.max(x1, x2);
const width = Math.min(x1 + w1, x2 + w2) - left;
const top = Math.max(y1, y2);
const height = Math.min(y1 + h1, y2 + h2) - top;
return [left, top, width, height];
};
// src/collision/resolve.js
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
const [left, top, width, height] = intersection_default(
x1,
y1,
w1,
h1,
x2,
y2,
w2,
h2
);
let direction = "";
let resolveX = x1;
let resolveY = y1;
if (width < height) {
if (x1 < x2) {
direction = "right";
resolveX = x2 - w1;
} else {
direction = "left";
resolveX = x2 + w2;
}
} else {
if (y1 < y2) {
direction = "bottom";
resolveY = y2 - h1;
} else {
direction = "top";
resolveY = y2 + h2;
}
}
return { direction, x: resolveX, y: resolveY };
};
// src/collision/_web.js
globalThis.utils = Object.assign(globalThis.utils || {}, {
resolve: resolve_default,
intersection: intersection_default
});
})();