UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

104 lines (95 loc) 3.5 kB
(() => { var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/_global.js globalThis.utils = globalThis.utils || {}; globalThis.utils.global = (overrides = true) => { for (const key in globalThis.utils) { if ("global" === key) continue; if (overrides || globalThis[key] === void 0) { globalThis[key] = globalThis.utils[key]; } } }; // src/collision/index.js var index_exports = {}; __export(index_exports, { colcirc: () => colcirc_default, colrect: () => colrect_default, intersection: () => intersection_default, resolverect: () => resolverect_default }); // 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/resolverect.js var resolverect_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/debug/assert.js var assert_default = (condition, message = "Assertion failed") => { if (!condition) throw new Error(message); }; // src/collision/colrect.js var colrect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => { DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number"); DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number"); DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number"); DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number"); DEV: assert_default(isFinite(x2), "colrect: 5th param must be a number"); DEV: assert_default(isFinite(y2), "colrect: 6th param must be a number"); DEV: assert_default(isFinite(w2), "colrect: 7th param must be a number"); DEV: assert_default(isFinite(h2), "colrect: 8th param must be a number"); return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2; }; // src/collision/colcirc.js var colcirc_default = (x1, y1, r1, x2, y2, r2) => { DEV: assert_default(isFinite(x1), "colcirc: 1st param must be a number"); DEV: assert_default(isFinite(y1), "colcirc: 2nd param must be a number"); DEV: assert_default(isFinite(r1), "colcirc: 3rd param must be a number"); DEV: assert_default(isFinite(x2), "colcirc: 4th param must be a number"); DEV: assert_default(isFinite(y2), "colcirc: 5th param must be a number"); DEV: assert_default(isFinite(r2), "colcirc: 6th param must be a number"); return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2); }; // src/collision/_web.js globalThis.utils = Object.assign(globalThis.utils || {}, index_exports); })();