var kaplay = (() => { var sn = Object.defineProperty; var mi = Object.getOwnPropertyDescriptor; var di = Object.getOwnPropertyNames; var hi = Object.prototype.hasOwnProperty; var i = (t, e) => sn(t, "name", { value: e, configurable: !0 }); var pi = (t, e) => { for (var r in e) sn(t, r, { get: e[r], enumerable: !0 }); }, fi = (t, e, r, s) => { if ((e && typeof e == "object") || typeof e == "function") for (let a of di(e)) !hi.call(t, a) && a !== r && sn(t, a, { get: () => e[a], enumerable: !(s = mi(e, a)) || s.enumerable, }); return t; }; var gi = (t) => fi(sn({}, "__esModule", { value: !0 }), t); var Br = (() => { for (var t = new Uint8Array(128), e = 0; e < 64; e++) t[e < 26 ? e + 65 : e < 52 ? e + 71 : e < 62 ? e - 4 : e * 4 - 205] = e; return (r) => { for ( var s = r.length, a = new Uint8Array( (((s - (r[s - 1] == "=") - (r[s - 2] == "=")) * 3) / 4) | 0 ), l = 0, u = 0; l < s; ) { var f = t[r.charCodeAt(l++)], y = t[r.charCodeAt(l++)], m = t[r.charCodeAt(l++)], V = t[r.charCodeAt(l++)]; (a[u++] = (f << 2) | (y >> 4)), (a[u++] = (y << 4) | (m >> 2)), (a[u++] = (m << 6) | V); } return a; }; })(); var aa = {}; pi(aa, { anchorPt: () => ht, default: () => ia, getInternalContext: () => fe, getKaboomContext: () => J, isKaboomCtx: () => as, }); function ve(t) { return (t * Math.PI) / 180; } i(ve, "deg2rad"); function lt(t) { return (t * 180) / Math.PI; } i(lt, "rad2deg"); function Qe(t, e, r) { return e > r ? Qe(t, r, e) : Math.min(Math.max(t, e), r); } i(Qe, "clamp"); function Je(t, e, r) { if (typeof t == "number" && typeof e == "number") return t + (e - t) * r; if (t instanceof T && e instanceof T) return t.lerp(e, r); if (t instanceof ee && e instanceof ee) return t.lerp(e, r); throw new Error( `Bad value for lerp(): ${t}, ${e}. Only number, Vec2 and Color is supported.` ); } i(Je, "lerp"); function dt(t, e, r, s, a) { return s + ((t - e) / (r - e)) * (a - s); } i(dt, "map"); function Ir(t, e, r, s, a) { return Qe(dt(t, e, r, s, a), s, a); } i(Ir, "mapc"); var T = class t { static { i(this, "Vec2"); } x = 0; y = 0; constructor(e = 0, r = e) { (this.x = e), (this.y = r); } static fromAngle(e) { let r = ve(e); return new t(Math.cos(r), Math.sin(r)); } static LEFT = new t(-1, 0); static RIGHT = new t(1, 0); static UP = new t(0, -1); static DOWN = new t(0, 1); clone() { return new t(this.x, this.y); } add(...e) { let r = C(...e); return new t(this.x + r.x, this.y + r.y); } sub(...e) { let r = C(...e); return new t(this.x - r.x, this.y - r.y); } scale(...e) { let r = C(...e); return new t(this.x * r.x, this.y * r.y); } dist(...e) { let r = C(...e); return this.sub(r).len(); } sdist(...e) { let r = C(...e); return this.sub(r).slen(); } len() { return Math.sqrt(this.dot(this)); } slen() { return this.dot(this); } unit() { let e = this.len(); return e === 0 ? new t(0) : this.scale(1 / e); } normal() { return new t(this.y, -this.x); } reflect(e) { return this.sub(e.scale(2 * this.dot(e))); } project(e) { return e.scale(e.dot(this) / e.len()); } reject(e) { return this.sub(this.project(e)); } dot(e) { return this.x * e.x + this.y * e.y; } cross(e) { return this.x * e.y - this.y * e.x; } angle(...e) { let r = C(...e); return lt(Math.atan2(this.y - r.y, this.x - r.x)); } angleBetween(...e) { let r = C(...e); return lt(Math.atan2(this.cross(r), this.dot(r))); } lerp(e, r) { return new t(Je(this.x, e.x, r), Je(this.y, e.y, r)); } slerp(e, r) { let s = this.dot(e), a = this.cross(e), l = Math.atan2(a, s); return this.scale(Math.sin((1 - r) * l)) .add(e.scale(Math.sin(r * l))) .scale(1 / a); } isZero() { return this.x === 0 && this.y === 0; } toFixed(e) { return new t(Number(this.x.toFixed(e)), Number(this.y.toFixed(e))); } transform(e) { return e.multVec2(this); } eq(e) { return this.x === e.x && this.y === e.y; } bbox() { return new ue(this, 0, 0); } toString() { return `vec2(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`; } }; function C(...t) { if (t.length === 1) { if (t[0] instanceof T) return new T(t[0].x, t[0].y); if (Array.isArray(t[0]) && t[0].length === 2) return new T(...t[0]); } return new T(...t); } i(C, "vec2"); var ee = class t { static { i(this, "Color"); } r = 255; g = 255; b = 255; constructor(e, r, s) { (this.r = Qe(e, 0, 255)), (this.g = Qe(r, 0, 255)), (this.b = Qe(s, 0, 255)); } static fromArray(e) { return new t(e[0], e[1], e[2]); } static fromHex(e) { if (typeof e == "number") return new t((e >> 16) & 255, (e >> 8) & 255, (e >> 0) & 255); if (typeof e == "string") { let r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e); return new t( parseInt(r[1], 16), parseInt(r[2], 16), parseInt(r[3], 16) ); } else throw new Error("Invalid hex color format"); } static fromHSL(e, r, s) { if (r == 0) return new t(255 * s, 255 * s, 255 * s); let a = i( (V, x, P) => ( P < 0 && (P += 1), P > 1 && (P -= 1), P < 1 / 6 ? V + (x - V) * 6 * P : P < 1 / 2 ? x : P < 2 / 3 ? V + (x - V) * (2 / 3 - P) * 6 : V ), "hue2rgb" ), l = s < 0.5 ? s * (1 + r) : s + r - s * r, u = 2 * s - l, f = a(u, l, e + 1 / 3), y = a(u, l, e), m = a(u, l, e - 1 / 3); return new t( Math.round(f * 255), Math.round(y * 255), Math.round(m * 255) ); } static RED = new t(255, 0, 0); static GREEN = new t(0, 255, 0); static BLUE = new t(0, 0, 255); static YELLOW = new t(255, 255, 0); static MAGENTA = new t(255, 0, 255); static CYAN = new t(0, 255, 255); static WHITE = new t(255, 255, 255); static BLACK = new t(0, 0, 0); clone() { return new t(this.r, this.g, this.b); } lighten(e) { return new t(this.r + e, this.g + e, this.b + e); } darken(e) { return this.lighten(-e); } invert() { return new t(255 - this.r, 255 - this.g, 255 - this.b); } mult(e) { return new t( (this.r * e.r) / 255, (this.g * e.g) / 255, (this.b * e.b) / 255 ); } lerp(e, r) { return new t(Je(this.r, e.r, r), Je(this.g, e.g, r), Je(this.b, e.b, r)); } toHSL() { let e = this.r / 255, r = this.g / 255, s = this.b / 255, a = Math.max(e, r, s), l = Math.min(e, r, s), u = (a + l) / 2, f = u, y = u; if (a == l) u = f = 0; else { let m = a - l; switch (((f = y > 0.5 ? m / (2 - a - l) : m / (a + l)), a)) { case e: u = (r - s) / m + (r < s ? 6 : 0); break; case r: u = (s - e) / m + 2; break; case s: u = (e - r) / m + 4; break; } u /= 6; } return [u, f, y]; } eq(e) { return this.r === e.r && this.g === e.g && this.b === e.b; } toString() { return `rgb(${this.r}, ${this.g}, ${this.b})`; } toHex() { return ( "#" + ((1 << 24) + (this.r << 16) + (this.g << 8) + this.b) .toString(16) .slice(1) ); } toArray() { return [this.r, this.g, this.b]; } }; function oe(...t) { if (t.length === 0) return new ee(255, 255, 255); if (t.length === 1) { if (t[0] instanceof ee) return t[0].clone(); if (typeof t[0] == "string") return ee.fromHex(t[0]); if (Array.isArray(t[0]) && t[0].length === 3) return ee.fromArray(t[0]); } return new ee(...t); } i(oe, "rgb"); var Lr = i((t, e, r) => ee.fromHSL(t, e, r), "hsl2rgb"), me = class t { static { i(this, "Quad"); } x = 0; y = 0; w = 1; h = 1; constructor(e, r, s, a) { (this.x = e), (this.y = r), (this.w = s), (this.h = a); } scale(e) { return new t( this.x + this.w * e.x, this.y + this.h * e.y, this.w * e.w, this.h * e.h ); } pos() { return new T(this.x, this.y); } clone() { return new t(this.x, this.y, this.w, this.h); } eq(e) { return ( this.x === e.x && this.y === e.y && this.w === e.w && this.h === e.h ); } toString() { return `quad(${this.x}, ${this.y}, ${this.w}, ${this.h})`; } }; function pe(t, e, r, s) { return new me(t, e, r, s); } i(pe, "quad"); var Ot = class t { static { i(this, "Mat2"); } a; b; c; d; constructor(e, r, s, a) { (this.a = e), (this.b = r), (this.c = s), (this.d = a); } mul(e) { return new t( this.a * e.a + this.b * e.c, this.a * e.b + this.b * e.d, this.c * e.a + this.d * e.c, this.c * e.b + this.d * e.d ); } transform(e) { return C(this.a * e.x + this.b * e.y, this.c * e.x + this.d * e.y); } get inverse() { let e = this.det; return new t(this.d / e, -this.b / e, -this.c / e, this.a / e); } get transpose() { return new t(this.a, this.c, this.b, this.d); } get eigenvalues() { let e = this.trace / 2, r = this.det, s = e + Math.sqrt(e * e - r), a = e - Math.sqrt(e * e - r); return [s, a]; } eigenvectors(e, r) { return this.c != 0 ? [ [e - this.d, this.c], [r - this.d, this.c], ] : this.b != 0 ? [ [this.b, e - this.a], [this.b, r - this.a], ] : Math.abs(this.transform(C(1, 0)).x - e) < Number.EPSILON ? [ [1, 0], [0, 1], ] : [ [0, 1], [1, 0], ]; } get det() { return this.a * this.d - this.b * this.c; } get trace() { return this.a + this.d; } static rotation(e) { let r = Math.cos(e), s = Math.sin(e); return new t(r, s, -s, r); } static scale(e, r) { return new t(e, 0, 0, r); } }; var bt = class t { static { i(this, "Mat3"); } m11; m12; m13; m21; m22; m23; m31; m32; m33; constructor(e, r, s, a, l, u, f, y, m) { (this.m11 = e), (this.m12 = r), (this.m13 = s), (this.m21 = a), (this.m22 = l), (this.m23 = u), (this.m31 = f), (this.m32 = y), (this.m33 = m); } static fromMat2(e) { return new t(e.a, e.b, 0, e.c, e.d, 0, 0, 0, 1); } toMat2() { return new Ot(this.m11, this.m12, this.m21, this.m22); } mul(e) { return new t( this.m11 * e.m11 + this.m12 * e.m21 + this.m13 * e.m31, this.m11 * e.m12 + this.m12 * e.m22 + this.m13 * e.m32, this.m11 * e.m13 + this.m12 * e.m23 + this.m13 * e.m33, this.m21 * e.m11 + this.m22 * e.m21 + this.m23 * e.m31, this.m21 * e.m12 + this.m22 * e.m22 + this.m23 * e.m32, this.m21 * e.m13 + this.m22 * e.m23 + this.m23 * e.m33, this.m31 * e.m11 + this.m32 * e.m21 + this.m33 * e.m31, this.m31 * e.m12 + this.m32 * e.m22 + this.m33 * e.m32, this.m31 * e.m13 + this.m32 * e.m23 + this.m33 * e.m33 ); } get det() { return ( this.m11 * this.m22 * this.m33 + this.m12 * this.m23 * this.m31 + this.m13 * this.m21 * this.m32 - this.m13 * this.m22 * this.m31 - this.m12 * this.m21 * this.m33 - this.m11 * this.m23 * this.m32 ); } rotate(e) { let r = Math.cos(e), s = Math.sin(e), a = this.m11, l = this.m12; return ( (this.m11 = r * this.m11 + s * this.m21), (this.m12 = r * this.m12 + s * this.m22), (this.m21 = r * this.m21 - s * a), (this.m22 = r * this.m22 - s * l), this ); } scale(e, r) { return ( (this.m11 *= e), (this.m12 *= e), (this.m21 *= r), (this.m22 *= r), this ); } get inverse() { let e = this.det; return new t( (this.m22 * this.m33 - this.m23 * this.m32) / e, (this.m13 * this.m32 - this.m12 * this.m33) / e, (this.m12 * this.m23 - this.m13 * this.m22) / e, (this.m23 * this.m31 - this.m21 * this.m33) / e, (this.m11 * this.m33 - this.m13 * this.m31) / e, (this.m13 * this.m21 - this.m11 * this.m23) / e, (this.m21 * this.m32 - this.m22 * this.m31) / e, (this.m12 * this.m31 - this.m11 * this.m32) / e, (this.m11 * this.m22 - this.m12 * this.m21) / e ); } get transpose() { return new t( this.m11, this.m21, this.m31, this.m12, this.m22, this.m32, this.m13, this.m23, this.m33 ); } }, Ge = class t { static { i(this, "Mat4"); } m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; constructor(e) { e && (this.m = e); } static translate(e) { return new t([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, e.x, e.y, 0, 1]); } static scale(e) { return new t([e.x, 0, 0, 0, 0, e.y, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]); } static rotateX(e) { e = ve(-e); let r = Math.cos(e), s = Math.sin(e); return new t([1, 0, 0, 0, 0, r, -s, 0, 0, s, r, 0, 0, 0, 0, 1]); } static rotateY(e) { e = ve(-e); let r = Math.cos(e), s = Math.sin(e); return new t([r, 0, s, 0, 0, 1, 0, 0, -s, 0, r, 0, 0, 0, 0, 1]); } static rotateZ(e) { e = ve(-e); let r = Math.cos(e), s = Math.sin(e); return new t([r, -s, 0, 0, s, r, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]); } translate(e) { return ( (this.m[12] += this.m[0] * e.x + this.m[4] * e.y), (this.m[13] += this.m[1] * e.x + this.m[5] * e.y), (this.m[14] += this.m[2] * e.x + this.m[6] * e.y), (this.m[15] += this.m[3] * e.x + this.m[7] * e.y), this ); } scale(e) { return ( (this.m[0] *= e.x), (this.m[4] *= e.y), (this.m[1] *= e.x), (this.m[5] *= e.y), (this.m[2] *= e.x), (this.m[6] *= e.y), (this.m[3] *= e.x), (this.m[7] *= e.y), this ); } rotate(e) { e = ve(-e); let r = Math.cos(e), s = Math.sin(e), a = this.m[0], l = this.m[1], u = this.m[4], f = this.m[5]; return ( (this.m[0] = a * r + l * s), (this.m[1] = -a * s + l * r), (this.m[4] = u * r + f * s), (this.m[5] = -u * s + f * r), this ); } mult(e) { let r = []; for (let s = 0; s < 4; s++) for (let a = 0; a < 4; a++) r[s * 4 + a] = this.m[0 * 4 + a] * e.m[s * 4 + 0] + this.m[1 * 4 + a] * e.m[s * 4 + 1] + this.m[2 * 4 + a] * e.m[s * 4 + 2] + this.m[3 * 4 + a] * e.m[s * 4 + 3]; return new t(r); } multVec2(e) { return new T( e.x * this.m[0] + e.y * this.m[4] + this.m[12], e.x * this.m[1] + e.y * this.m[5] + this.m[13] ); } getTranslation() { return new T(this.m[12], this.m[13]); } getScale() { if (this.m[0] != 0 || this.m[1] != 0) { let e = this.m[0] * this.m[5] - this.m[1] * this.m[4], r = Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1]); return new T(r, e / r); } else if (this.m[4] != 0 || this.m[5] != 0) { let e = this.m[0] * this.m[5] - this.m[1] * this.m[4], r = Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5]); return new T(e / r, r); } else return new T(0, 0); } getRotation() { if (this.m[0] != 0 || this.m[1] != 0) { let e = Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1]); return lt( this.m[1] > 0 ? Math.acos(this.m[0] / e) : -Math.acos(this.m[0] / e) ); } else if (this.m[4] != 0 || this.m[5] != 0) { let e = Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5]); return lt( Math.PI / 2 - (this.m[5] > 0 ? Math.acos(-this.m[4] / e) : -Math.acos(this.m[4] / e)) ); } else return 0; } getSkew() { if (this.m[0] != 0 || this.m[1] != 0) { let e = Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1]); return new T( Math.atan(this.m[0] * this.m[4] + this.m[1] * this.m[5]) / (e * e), 0 ); } else if (this.m[4] != 0 || this.m[5] != 0) { let e = Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5]); return new T( 0, Math.atan(this.m[0] * this.m[4] + this.m[1] * this.m[5]) / (e * e) ); } else return new T(0, 0); } invert() { let e = [], r = this.m[10] * this.m[15] - this.m[14] * this.m[11], s = this.m[9] * this.m[15] - this.m[13] * this.m[11], a = this.m[9] * this.m[14] - this.m[13] * this.m[10], l = this.m[8] * this.m[15] - this.m[12] * this.m[11], u = this.m[8] * this.m[14] - this.m[12] * this.m[10], f = this.m[8] * this.m[13] - this.m[12] * this.m[9], y = this.m[6] * this.m[15] - this.m[14] * this.m[7], m = this.m[5] * this.m[15] - this.m[13] * this.m[7], V = this.m[5] * this.m[14] - this.m[13] * this.m[6], x = this.m[4] * this.m[15] - this.m[12] * this.m[7], P = this.m[4] * this.m[14] - this.m[12] * this.m[6], w = this.m[5] * this.m[15] - this.m[13] * this.m[7], K = this.m[4] * this.m[13] - this.m[12] * this.m[5], X = this.m[6] * this.m[11] - this.m[10] * this.m[7], z = this.m[5] * this.m[11] - this.m[9] * this.m[7], N = this.m[5] * this.m[10] - this.m[9] * this.m[6], $ = this.m[4] * this.m[11] - this.m[8] * this.m[7], O = this.m[4] * this.m[10] - this.m[8] * this.m[6], se = this.m[4] * this.m[9] - this.m[8] * this.m[5]; (e[0] = this.m[5] * r - this.m[6] * s + this.m[7] * a), (e[4] = -(this.m[4] * r - this.m[6] * l + this.m[7] * u)), (e[8] = this.m[4] * s - this.m[5] * l + this.m[7] * f), (e[12] = -(this.m[4] * a - this.m[5] * u + this.m[6] * f)), (e[1] = -(this.m[1] * r - this.m[2] * s + this.m[3] * a)), (e[5] = this.m[0] * r - this.m[2] * l + this.m[3] * u), (e[9] = -(this.m[0] * s - this.m[1] * l + this.m[3] * f)), (e[13] = this.m[0] * a - this.m[1] * u + this.m[2] * f), (e[2] = this.m[1] * y - this.m[2] * m + this.m[3] * V), (e[6] = -(this.m[0] * y - this.m[2] * x + this.m[3] * P)), (e[10] = this.m[0] * w - this.m[1] * x + this.m[3] * K), (e[14] = -(this.m[0] * V - this.m[1] * P + this.m[2] * K)), (e[3] = -(this.m[1] * X - this.m[2] * z + this.m[3] * N)), (e[7] = this.m[0] * X - this.m[2] * $ + this.m[3] * O), (e[11] = -(this.m[0] * z - this.m[1] * $ + this.m[3] * se)), (e[15] = this.m[0] * N - this.m[1] * O + this.m[2] * se); let _ = this.m[0] * e[0] + this.m[1] * e[4] + this.m[2] * e[8] + this.m[3] * e[12]; for (let ae = 0; ae < 4; ae++) for (let re = 0; re < 4; re++) e[ae * 4 + re] *= 1 / _; return new t(e); } clone() { return new t([...this.m]); } toString() { return this.m.toString(); } }; function Yn(t, e, r, s = (a) => -Math.cos(a)) { return t + ((s(r) + 1) / 2) * (e - t); } i(Yn, "wave"); var bi = 1103515245, vi = 12345, Fr = 2147483648, It = class { static { i(this, "RNG"); } seed; constructor(e) { this.seed = e; } gen() { return (this.seed = (bi * this.seed + vi) % Fr), this.seed / Fr; } genNumber(e, r) { return e + this.gen() * (r - e); } genVec2(e, r) { return new T(this.genNumber(e.x, r.x), this.genNumber(e.y, r.y)); } genColor(e, r) { return new ee( this.genNumber(e.r, r.r), this.genNumber(e.g, r.g), this.genNumber(e.b, r.b) ); } genAny(...e) { if (e.length === 0) return this.gen(); if (e.length === 1) { if (typeof e[0] == "number") return this.genNumber(0, e[0]); if (e[0] instanceof T) return this.genVec2(C(0, 0), e[0]); if (e[0] instanceof ee) return this.genColor(oe(0, 0, 0), e[0]); } else if (e.length === 2) { if (typeof e[0] == "number" && typeof e[1] == "number") return this.genNumber(e[0], e[1]); if (e[0] instanceof T && e[1] instanceof T) return this.genVec2(e[0], e[1]); if (e[0] instanceof ee && e[1] instanceof ee) return this.genColor(e[0], e[1]); } } }, zn = new It(Date.now()); function jr(t) { return t != null && (zn.seed = t), zn.seed; } i(jr, "randSeed"); function Lt(...t) { return zn.genAny(...t); } i(Lt, "rand"); function Xn(...t) { return Math.floor(Lt(...t)); } i(Xn, "randi"); function kr(t) { return Lt() <= t; } i(kr, "chance"); function Wn(t) { for (let e = t.length - 1; e > 0; e--) { let r = Math.floor(Math.random() * (e + 1)); [t[e], t[r]] = [t[r], t[e]]; } return t; } i(Wn, "shuffle"); function Nr(t, e) { return t.length <= e ? t.slice() : Wn(t.slice()).slice(0, e); } i(Nr, "chooseMultiple"); function _r(t) { return t[Xn(t.length)]; } i(_r, "choose"); function $n(t, e) { return ( t.pos.x + t.width > e.pos.x && t.pos.x < e.pos.x + e.width && t.pos.y + t.height > e.pos.y && t.pos.y < e.pos.y + e.height ); } i($n, "testRectRect"); function xi(t, e) { if ( (t.p1.x === t.p2.x && t.p1.y === t.p2.y) || (e.p1.x === e.p2.x && e.p1.y === e.p2.y) ) return null; let r = (e.p2.y - e.p1.y) * (t.p2.x - t.p1.x) - (e.p2.x - e.p1.x) * (t.p2.y - t.p1.y); if (r === 0) return null; let s = ((e.p2.x - e.p1.x) * (t.p1.y - e.p1.y) - (e.p2.y - e.p1.y) * (t.p1.x - e.p1.x)) / r, a = ((t.p2.x - t.p1.x) * (t.p1.y - e.p1.y) - (t.p2.y - t.p1.y) * (t.p1.x - e.p1.x)) / r; return s < 0 || s > 1 || a < 0 || a > 1 ? null : s; } i(xi, "testLineLineT"); function cn(t, e) { let r = xi(t, e); return r ? C(t.p1.x + r * (t.p2.x - t.p1.x), t.p1.y + r * (t.p2.y - t.p1.y)) : null; } i(cn, "testLineLine"); function un(t, e) { let r = e.p2.sub(e.p1), s = Number.NEGATIVE_INFINITY, a = Number.POSITIVE_INFINITY; if (r.x != 0) { let l = (t.pos.x - e.p1.x) / r.x, u = (t.pos.x + t.width - e.p1.x) / r.x; (s = Math.max(s, Math.min(l, u))), (a = Math.min(a, Math.max(l, u))); } if (r.y != 0) { let l = (t.pos.y - e.p1.y) / r.y, u = (t.pos.y + t.height - e.p1.y) / r.y; (s = Math.max(s, Math.min(l, u))), (a = Math.min(a, Math.max(l, u))); } return a >= s && a >= 0 && s <= 1; } i(un, "testRectLine"); function Qn(t, e) { return ( e.x > t.pos.x && e.x < t.pos.x + t.width && e.y > t.pos.y && e.y < t.pos.y + t.height ); } i(Qn, "testRectPoint"); function Hr(t, e) { let r = Math.max(t.pos.x, Math.min(e.center.x, t.pos.x + t.width)), s = Math.max(t.pos.y, Math.min(e.center.y, t.pos.y + t.height)); return C(r, s).sdist(e.center) <= e.radius * e.radius; } i(Hr, "testRectCircle"); function Kr(t, e) { return qr(e, new Oe(t.points())); } i(Kr, "testRectPolygon"); function Jn(t, e) { let r = e.sub(t.p1), s = t.p2.sub(t.p1); if (Math.abs(r.cross(s)) > Number.EPSILON) return !1; let a = r.dot(s) / s.dot(s); return a >= 0 && a <= 1; } i(Jn, "testLinePoint"); function Rt(t, e) { let r = t.p2.sub(t.p1), s = r.dot(r), a = t.p1.sub(e.center), l = 2 * r.dot(a), u = a.dot(a) - e.radius * e.radius, f = l * l - 4 * s * u; if (s <= Number.EPSILON || f < 0) return !1; if (f == 0) { let y = -l / (2 * s); if (y >= 0 && y <= 1) return !0; } else { let y = (-l + Math.sqrt(f)) / (2 * s), m = (-l - Math.sqrt(f)) / (2 * s); if ((y >= 0 && y <= 1) || (m >= 0 && m <= 1)) return !0; } return er(e, t.p1); } i(Rt, "testLineCircle"); function Zn(t, e) { if (mt(e, t.p1) || mt(e, t.p2)) return !0; for (let r = 0; r < e.pts.length; r++) { let s = e.pts[r], a = e.pts[(r + 1) % e.pts.length]; if (cn(t, new Be(s, a))) return !0; } return !1; } i(Zn, "testLinePolygon"); function er(t, e) { return t.center.sdist(e) < t.radius * t.radius; } i(er, "testCirclePoint"); function yi(t, e) { return ( t.center.sdist(e.center) < (t.radius + e.radius) * (t.radius + e.radius) ); } i(yi, "testCircleCircle"); function jt(t, e) { let r = e.pts[e.pts.length - 1]; for (let s of e.pts) { if (Rt(new Be(r, s), t)) return !0; r = s; } return er(t, e.pts[0]) ? !0 : mt(e, t.center); } i(jt, "testCirclePolygon"); function qr(t, e) { for (let r = 0; r < t.pts.length; r++) if (Zn(new Be(t.pts[r], t.pts[(r + 1) % t.pts.length]), e)) return !0; return !!(t.pts.some((r) => mt(e, r)) || e.pts.some((r) => mt(t, r))); } i(qr, "testPolygonPolygon"); function mt(t, e) { let r = !1, s = t.pts; for (let a = 0, l = s.length - 1; a < s.length; l = a++) s[a].y > e.y != s[l].y > e.y && e.x < ((s[l].x - s[a].x) * (e.y - s[a].y)) / (s[l].y - s[a].y) + s[a].x && (r = !r); return r; } i(mt, "testPolygonPoint"); function zr(t, e) { e = e.sub(t.center); let r = ve(t.angle), s = Math.cos(r), a = Math.sin(r), l = e.x * s + e.y * a, u = -e.x * a + e.y * s; return ( (l * l) / (t.radiusX * t.radiusX) + (u * u) / (t.radiusY * t.radiusY) < 1 ); } i(zr, "testEllipsePoint"); function an(t, e) { let r = e.center.sub(t.center), s = ve(t.angle), a = Math.cos(s), l = Math.sin(s), u = r.x * a + r.y * l, f = -r.x * l + r.y * a; return zr( new We(C(), t.radiusX + e.radius, t.radiusY + e.radius, 0), C(u, f) ); } i(an, "testEllipseCircle"); function Yr(t, e) { let r = t.toMat2().inverse; return ( (e = new Be( r.transform(e.p1.sub(t.center)), r.transform(e.p2.sub(t.center)) )), Rt(e, new Fe(C(), 1)) ); } i(Yr, "testEllipseLine"); function wi(t, e) { if (t.radiusX === t.radiusY) return an(e, new Fe(t.center, t.radiusX)); if (e.radiusX === e.radiusY) return an(t, new Fe(e.center, e.radiusX)); let r = new bt( 1 / t.radiusX ** 2, 0, 0, 0, 1 / t.radiusY ** 2, 0, 0, 0, -1 ), s = new bt(1 / e.radiusX ** 2, 0, 0, 0, 1 / e.radiusY ** 2, 0, 0, 0, -1), a = t.center.x, l = t.center.y, u = e.center.x, f = e.center.y, y = ve(t.angle), m = ve(e.angle), V = new bt( Math.cos(y), -Math.sin(y), a, Math.sin(y), Math.cos(y), l, 0, 0, 1 ), x = new bt( Math.cos(m), -Math.sin(m), u, Math.sin(m), Math.cos(m), f, 0, 0, 1 ), P = V.inverse, w = x.inverse, K = P.transpose.mul(r).mul(P), X = w.transpose.mul(s).mul(w), z = K.m11, N = K.m12, $ = K.m13, O = K.m21, se = K.m22, _ = K.m23, ae = K.m31, re = K.m32, ce = K.m33, ge = X.m11, Ue = X.m12, Ce = X.m13, Le = X.m21, je = X.m22, Ee = X.m23, Ne = X.m31, _e = X.m32, He = X.m33, pt = z * se * ce - z * _ * re - N * O * ce + N * _ * ae + $ * O * re - $ * se * ae, Se = (z * se * He - z * _ * _e - z * re * Ee + z * ce * je - N * O * He + N * _ * Ne + N * ae * Ee - N * ce * Le + $ * O * _e - $ * se * Ne - $ * ae * je + $ * re * Le + O * re * Ce - O * ce * Ue - se * ae * Ce + se * ce * ge + _ * ae * Ue - _ * re * ge) / pt, Ve = (z * je * He - z * Ee * _e - N * Le * He + N * Ee * Ne + $ * Le * _e - $ * je * Ne - O * Ue * He + O * Ce * _e + se * ge * He - se * Ce * Ne - _ * ge * _e + _ * Ue * Ne + ae * Ue * Ee - ae * Ce * je - re * ge * Ee + re * Ce * Le + ce * ge * je - ce * Ue * Le) / pt, ze = (ge * je * He - ge * Ee * _e - Ue * Le * He + Ue * Ee * Ne + Ce * Le * _e - Ce * je * Ne) / pt; if (Se >= 0) { let rt = -3 * Ve + Se ** 2, ot = 3 * Se * ze + Ve * Se ** 2 - 4 * Ve ** 2, wt = -27 * ze ** 2 + 18 * ze * Se * Ve + Se ** 2 * Ve ** 2 - 4 * Se ** 3 * ze - 4 * Ve ** 3; return !(rt > 0 && ot < 0 && wt > 0); } else { let rt = -3 * Ve + Se ** 2, ot = -27 * ze ** 2 + 18 * ze * Se * Ve + Se ** 2 * Ve ** 2 - 4 * Se ** 3 * ze - 4 * Ve ** 3; return !(rt > 0 && ot > 0); } } i(wi, "testEllipseEllipse"); function Xr(t, e) { return tr(t, new Oe(e.points())); } i(Xr, "testEllipseRect"); function tr(t, e) { let r = t.toMat2().inverse; return ( (e = new Oe(e.pts.map((s) => r.transform(s.sub(t.center))))), jt(new Fe(C(), 1), e) ); } i(tr, "testEllipsePolygon"); function Ci(t, e) { return e instanceof T ? Jn(t, e) : e instanceof Fe ? Rt(t, e) : e instanceof Be ? cn(t, e) != null : e instanceof ue ? un(e, t) : e instanceof Oe ? Zn(t, e) : e instanceof We ? Yr(e, t) : !1; } i(Ci, "testLineShape"); function Ti(t, e) { return e instanceof T ? er(t, e) : e instanceof Fe ? yi(t, e) : e instanceof Be ? Rt(e, t) : e instanceof ue ? Hr(e, t) : e instanceof Oe ? jt(t, e) : e instanceof We ? an(e, t) : !1; } i(Ti, "testCircleShape"); function Ei(t, e) { return e instanceof T ? Qn(t, e) : e instanceof Fe ? Hr(t, e) : e instanceof Be ? un(t, e) : e instanceof ue ? $n(t, e) : e instanceof Oe ? Kr(t, e) : e instanceof We ? Xr(e, t) : !1; } i(Ei, "testRectShape"); function Si(t, e) { return e instanceof T ? mt(t, e) : e instanceof Fe ? jt(e, t) : e instanceof Be ? Zn(e, t) : e instanceof ue ? Kr(e, t) : e instanceof Oe ? qr(e, t) : e instanceof We ? tr(e, t) : !1; } i(Si, "testPolygonShape"); function Ai(t, e) { return e instanceof T ? zr(t, e) : e instanceof Fe ? an(t, e) : e instanceof Be ? Yr(t, e) : e instanceof ue ? Xr(t, e) : e instanceof Oe ? tr(t, e) : e instanceof We ? wi(e, t) : !1; } i(Ai, "testEllipseShape"); function Wr(t, e, r) { let s = t, a = r.p1, l = r.p2, u = e, f = l.sub(a), y = u.cross(f); if (Math.abs(y) < Number.EPSILON) return null; let m = a.sub(s), V = m.cross(f) / y; if (V <= 0 || V >= 1) return null; let x = m.cross(u) / y; if (x <= 0 || x >= 1) return null; let P = f.normal().unit(); return ( e.dot(P) > 0 && ((P.x *= -1), (P.y *= -1)), { point: s.add(u.scale(V)), normal: P, fraction: V } ); } i(Wr, "raycastLine"); function Oi(t, e, r) { let s = Number.NEGATIVE_INFINITY, a = Number.POSITIVE_INFINITY, l; if (t.x != 0) { let u = (r.pos.x - t.x) / e.x, f = (r.pos.x + r.width - t.x) / e.x; (l = C(-Math.sign(e.x), 0)), (s = Math.max(s, Math.min(u, f))), (a = Math.min(a, Math.max(u, f))); } if (t.y != 0) { let u = (r.pos.y - t.y) / e.y, f = (r.pos.y + r.height - t.y) / e.y; Math.min(u, f) > s && (l = C(0, -Math.sign(e.y))), (s = Math.max(s, Math.min(u, f))), (a = Math.min(a, Math.max(u, f))); } return a >= s && s >= 0 && s <= 1 ? { point: t.add(e.scale(s)), normal: l, fraction: s } : null; } i(Oi, "raycastRect"); function $r(t, e, r) { let s = t, a = r.center, l = e, u = l.dot(l), f = s.sub(a), y = 2 * l.dot(f), m = f.dot(f) - r.radius * r.radius, V = y * y - 4 * u * m; if (u <= Number.EPSILON || V < 0) return null; if (V == 0) { let x = -y / (2 * u); if (x >= 0 && x <= 1) { let P = s.add(l.scale(x)); return { point: P, normal: P.sub(a), fraction: x }; } } else { let x = (-y + Math.sqrt(V)) / (2 * u), P = (-y - Math.sqrt(V)) / (2 * u), w = null; if ( (x >= 0 && x <= 1 && (w = x), P >= 0 && P <= 1 && (w = Math.min(P, w ?? P)), w != null) ) { let K = s.add(l.scale(w)); return { point: K, normal: K.sub(a).unit(), fraction: w }; } } return null; } i($r, "raycastCircle"); function Ri(t, e, r) { let s = r.pts, a = null, l = s[s.length - 1]; for (let u = 0; u < s.length; u++) { let f = s[u], y = Wr(t, e, new Be(l, f)); y && (!a || a.fraction > y.fraction) && (a = y), (l = f); } return a; } i(Ri, "raycastPolygon"); function Ui(t, e, r) { let s = r.toMat2(), a = s.inverse, l = a.transform(t.sub(r.center)), u = a.transform(e), f = $r(l, u, new Fe(C(), 1)); if (f) { let y = Ot.rotation(ve(-r.angle)), V = Ot.scale(r.radiusX, r.radiusY).transform(f.point), x = s.transform(f.point).add(r.center), P = x.dist(t) / e.len(); return { point: x, normal: y.transform(C(r.radiusY ** 2 * V.x, r.radiusX ** 2 * V.y)), fraction: P, }; } return f; } i(Ui, "raycastEllipse"); function Qr(t, e, r, s = 64) { let a = t, l = e.len(), u = e.scale(1 / l), f = 0, y = C(Math.floor(t.x), Math.floor(t.y)), m = C(u.x > 0 ? 1 : -1, u.y > 0 ? 1 : -1), V = C(Math.abs(1 / u.x), Math.abs(1 / u.y)), x = C( m.x > 0 ? y.x + 1 - t.x : t.x - y.x, m.y > 0 ? y.y + 1 - t.y : t.y - y.y ), P = C(V.x < 1 / 0 ? V.x * x.x : 1 / 0, V.y < 1 / 0 ? V.y * x.y : 1 / 0), w = -1; for (; f <= s; ) { let K = r(y); if (K === !0) return { point: a.add(u.scale(f)), normal: C(w === 0 ? -m.x : 0, w === 1 ? -m.y : 0), fraction: f / l, gridPos: y, }; if (K) return K; P.x < P.y ? ((y.x += m.x), (f = P.x), (P.x += V.x), (w = 0)) : ((y.y += m.y), (f = P.y), (P.y += V.y), (w = 1)); } return null; } i(Qr, "raycastGrid"); var Be = class t { static { i(this, "Line"); } p1; p2; constructor(e, r) { (this.p1 = e.clone()), (this.p2 = r.clone()); } transform(e) { return new t(e.multVec2(this.p1), e.multVec2(this.p2)); } bbox() { return ue.fromPoints(this.p1, this.p2); } area() { return this.p1.dist(this.p2); } clone() { return new t(this.p1, this.p2); } collides(e) { return Ci(this, e); } contains(e) { return this.collides(e); } raycast(e, r) { return Wr(e, r, this); } }, ue = class t { static { i(this, "Rect"); } pos; width; height; constructor(e, r, s) { (this.pos = e.clone()), (this.width = r), (this.height = s); } static fromPoints(e, r) { return new t(e.clone(), r.x - e.x, r.y - e.y); } center() { return new T(this.pos.x + this.width / 2, this.pos.y + this.height / 2); } points() { return [ this.pos, this.pos.add(this.width, 0), this.pos.add(this.width, this.height), this.pos.add(0, this.height), ]; } transform(e) { return new Oe(this.points().map((r) => e.multVec2(r))); } bbox() { return this.clone(); } area() { return this.width * this.height; } clone() { return new t(this.pos.clone(), this.width, this.height); } distToPoint(e) { return Math.sqrt(this.sdistToPoint(e)); } sdistToPoint(e) { let r = this.pos, s = this.pos.add(this.width, this.height), a = Math.max(r.x - e.x, 0, e.x - s.x), l = Math.max(r.y - e.y, 0, e.y - s.y); return a * a + l * l; } collides(e) { return Ei(this, e); } contains(e) { return this.collides(e); } raycast(e, r) { return Oi(e, r, this); } }, Fe = class t { static { i(this, "Circle"); } center; radius; constructor(e, r) { (this.center = e.clone()), (this.radius = r); } transform(e) { return new We(this.center, this.radius, this.radius).transform(e); } bbox() { return ue.fromPoints( this.center.sub(C(this.radius)), this.center.add(C(this.radius)) ); } area() { return this.radius * this.radius * Math.PI; } clone() { return new t(this.center, this.radius); } collides(e) { return Ti(this, e); } contains(e) { return this.collides(e); } raycast(e, r) { return $r(e, r, this); } }, We = class t { static { i(this, "Ellipse"); } center; radiusX; radiusY; angle; constructor(e, r, s, a = 0) { (this.center = e.clone()), (this.radiusX = r), (this.radiusY = s), (this.angle = a); } static fromMat2(e) { let r = e.inverse, s = r.transpose.mul(r), [a, l] = s.eigenvalues, [u, f] = s.eigenvectors(a, l), [y, m] = [1 / Math.sqrt(a), 1 / Math.sqrt(l)]; return y > m ? new t(C(), y, m, lt(Math.atan2(-u[1], u[0]))) : new t(C(), m, y, lt(Math.atan2(-f[1], f[0]))); } toMat2() { let e = ve(this.angle), r = Math.cos(e), s = Math.sin(e); return new Ot( r * this.radiusX, s * this.radiusY, -s * this.radiusX, r * this.radiusY ); } transform(e) { if (this.angle == 0 && e.getRotation() == 0) return new t( e.multVec2(this.center), e.m[0] * this.radiusX, e.m[5] * this.radiusY ); { let r = this.toMat2(), s = e.getRotation(), a = e.getScale(); r = bt.fromMat2(r).scale(a.x, a.y).rotate(s).toMat2(); let u = t.fromMat2(r); return (u.center = e.multVec2(this.center)), u; } } bbox() { if (this.angle == 0) return ue.fromPoints( this.center.sub(C(this.radiusX, this.radiusY)), this.center.add(C(this.radiusX, this.radiusY)) ); { let e = ve(this.angle), r = Math.cos(e), s = Math.sin(e), a = this.radiusX * r, l = this.radiusX * s, u = this.radiusY * s, f = this.radiusY * r, y = Math.sqrt(a * a + u * u), m = Math.sqrt(l * l + f * f); return ue.fromPoints( this.center.sub(C(y, m)), this.center.add(C(y, m)) ); } } area() { return this.radiusX * this.radiusY * Math.PI; } clone() { return new t(this.center, this.radiusX, this.radiusY, this.angle); } collides(e) { return Ai(this, e); } contains(e) { e = e.sub(this.center); let r = ve(this.angle), s = Math.cos(r), a = Math.sin(r), l = e.x * s + e.y * a, u = -e.x * a + e.y * s; return ( (l * l) / (this.radiusX * this.radiusX) + (u * u) / (this.radiusY * this.radiusY) < 1 ); } raycast(e, r) { return Ui(e, r, this); } }, Oe = class t { static { i(this, "Polygon"); } pts; constructor(e) { if (e.length < 3) throw new Error("Polygons should have at least 3 vertices"); this.pts = e; } transform(e) { return new t(this.pts.map((r) => e.multVec2(r))); } bbox() { let e = C(Number.MAX_VALUE), r = C(-Number.MAX_VALUE); for (let s of this.pts) (e.x = Math.min(e.x, s.x)), (r.x = Math.max(r.x, s.x)), (e.y = Math.min(e.y, s.y)), (r.y = Math.max(r.y, s.y)); return ue.fromPoints(e, r); } area() { let e = 0, r = this.pts.length; for (let s = 0; s < r; s++) { let a = this.pts[s], l = this.pts[(s + 1) % r]; (e += a.x * l.y * 0.5), (e -= l.x * a.y * 0.5); } return Math.abs(e); } clone() { return new t(this.pts.map((e) => e.clone())); } collides(e) { return Si(this, e); } contains(e) { return this.collides(e); } raycast(e, r) { return Ri(e, r, this); } }; function nr(t, e, r, s, a) { let l = a * a, u = l * a, f = 1 - a, y = f * f, m = y * f; return t .scale(m) .add(e.scale(3 * y * a)) .add(r.scale(3 * f * l)) .add(s.scale(u)); } i(nr, "evaluateBezier"); function Jr(t, e) { let r = Number.MAX_VALUE, s = C(0); for (let a of [t, e]) for (let l = 0; l < a.pts.length; l++) { let u = a.pts[l], y = a.pts[(l + 1) % a.pts.length].sub(u).normal().unit(), m = Number.MAX_VALUE, V = -Number.MAX_VALUE; for (let K = 0; K < t.pts.length; K++) { let X = t.pts[K].dot(y); (m = Math.min(m, X)), (V = Math.max(V, X)); } let x = Number.MAX_VALUE, P = -Number.MAX_VALUE; for (let K = 0; K < e.pts.length; K++) { let X = e.pts[K].dot(y); (x = Math.min(x, X)), (P = Math.max(P, X)); } let w = Math.min(V, P) - Math.max(m, x); if (w < 0) return null; if (w < Math.abs(r)) { let K = P - m, X = x - V; (r = Math.abs(K) < Math.abs(X) ? K : X), (s = y.scale(r)); } } return s; } i(Jr, "sat"); function Zr(t, e, r) { return (e.x - t.x) * (r.y - t.y) - (e.y - t.y) * (r.x - t.x) >= 0; } i(Zr, "isOrientedCcw"); function Vi(t) { let e = 0, r = t[t.length - 1]; for (let s = 0; s < t.length; s++) (e += (t[s].x - r.x) * (t[s].y + r.y)), (r = t[s]); return e < 0; } i(Vi, "isOrientedCcwPolygon"); function qn(t, e, r, s) { let a = s.x - r.x, l = s.y - r.y, u = a * (t.y - r.y) - l * (t.x - r.x), f = a * (e.y - r.y) - l * (e.x - r.x); return u * f >= 0; } i(qn, "onSameSide"); function Pi(t, e, r, s) { return qn(t, e, r, s) && qn(t, r, e, s) && qn(t, s, e, r); } i(Pi, "pointInTriangle"); function Di(t, e, r, s) { for (let a of t) if (a !== e && a !== r && a !== s && Pi(a, e, r, s)) return !0; return !1; } i(Di, "someInTriangle"); function Mi(t, e, r, s) { return Zr(t, e, r) && !Di(s, t, e, r); } i(Mi, "isEar"); function rr(t) { if (t.length < 3) return []; if (t.length == 3) return [t]; let e = [], r = [], s = 0; for (let x = 0; x < t.length; x++) { let P = t[s], w = t[x]; (w.x < P.x || (w.x == P.x && w.y < P.y)) && (s = s), (e[x] = x + 1), (r[x] = x - 1); } (e[e.length - 1] = 0), (r[0] = r.length - 1), Vi(t) || ([e, r] = [r, e]); let a = []; for (let x = 0; x < t.length; ++x) Zr(t[r[x]], t[x], t[e[x]]) || a.push(t[x]); let l = [], u = t.length, f = 1, y = 0, m, V; for (; u > 3; ) { (m = e[f]), (V = r[f]); let x = t[V], P = t[f], w = t[m]; if (Mi(x, P, w, a)) l.push([x, P, w]), (e[V] = m), (r[m] = V), a.splice(a.indexOf(P), 1), --u, (y = 0); else if (++y > u) return []; f = m; } return (m = e[f]), (V = r[f]), l.push([t[V], t[f], t[m]]), l; } i(rr, "triangulate"); function or(t) { if (t.length < 3) return !1; let e = t.length - 2, r = t.length - 1, s = 0, a = t[r].sub(t[e]), l = t[s].sub(t[r]), u = a.cross(l); for (; s + 1 < t.length; ) if ( ((e = r), (r = s), s++, (a = t[r].sub(t[e])), (l = t[s].sub(t[r])), a.cross(l) * u < 0) ) return !1; return !0; } i(or, "isConvex"); var kt = class extends Map { static { i(this, "Registry"); } lastID = 0; push(e) { let r = this.lastID; return this.set(r, e), this.lastID++, r; } pushd(e) { let r = this.push(e); return () => this.delete(r); } }, et = class t { static { i(this, "EventController"); } paused = !1; cancel; constructor(e) { this.cancel = e; } static join(e) { let r = new t(() => e.forEach((s) => s.cancel())); return ( Object.defineProperty(r, "paused", { get: () => e[0].paused, set: (s) => e.forEach((a) => (a.paused = s)), }), (r.paused = !1), r ); } }, we = class { static { i(this, "Event"); } handlers = new kt(); add(e) { let r = this.handlers.pushd((...a) => { s.paused || e(...a); }), s = new et(r); return s; } addOnce(e) { let r = this.add((...s) => { r.cancel(), e(...s); }); return r; } next() { return new Promise((e) => this.addOnce(e)); } trigger(...e) { this.handlers.forEach((r) => r(...e)); } numListeners() { return this.handlers.size; } clear() { this.handlers.clear(); } }, tt = class { static { i(this, "EventHandler"); } handlers = {}; on(e, r) { return ( this.handlers[e] || (this.handlers[e] = new we()), this.handlers[e].add(r) ); } onOnce(e, r) { let s = this.on(e, (...a) => { s.cancel(), r(...a); }); return s; } next(e) { return new Promise((r) => { this.onOnce(e, (...s) => r(s[0])); }); } trigger(e, ...r) { this.handlers[e] && this.handlers[e].trigger(...r); } remove(e) { delete this.handlers[e]; } clear() { this.handlers = {}; } numListeners(e) { return this.handlers[e]?.numListeners() ?? 0; } }; function mn(t, e) { if (t === e) return !0; let r = typeof t, s = typeof e; if (r !== s) return !1; if (r === "object" && s === "object" && t !== null && e !== null) { if (Array.isArray(t) !== Array.isArray(e)) return !1; let a = Object.keys(t), l = Object.keys(e); if (a.length !== l.length) return !1; for (let u of a) { let f = t[u], y = e[u]; if (!mn(f, y)) return !1; } return !0; } return !1; } i(mn, "deepEq"); function Gi(t) { let e = window.atob(t), r = e.length, s = new Uint8Array(r); for (let a = 0; a < r; a++) s[a] = e.charCodeAt(a); return s.buffer; } i(Gi, "base64ToArrayBuffer"); function to(t) { return Gi(t.split(",")[1]); } i(to, "dataURLToArrayBuffer"); function dn(t, e) { let r = document.createElement("a"); (r.href = e), (r.download = t), r.click(); } i(dn, "download"); function sr(t, e) { dn(t, "data:text/plain;charset=utf-8," + e); } i(sr, "downloadText"); function no(t, e) { sr(t, JSON.stringify(e)); } i(no, "downloadJSON"); function ir(t, e) { let r = URL.createObjectURL(e); dn(t, r), URL.revokeObjectURL(r); } i(ir, "downloadBlob"); var ar = i((t) => t.match(/^data:\w+\/\w+;base64,.+/), "isDataURL"); var ro = i((t) => t.split(".").slice(0, -1).join("."), "getFileName"); function Ie(t, e) { return (...r) => { let s = r.length; if (s === t.length) return t(...r); if (s === e.length) return e(...r); }; } i(Ie, "overload2"); var oo = (() => { let t = 0; return () => t++; })(), so = i( (t) => (t instanceof Error ? t.message : String(t)), "getErrorMessage" ); var ln = class { static { i(this, "BinaryHeap"); } _items; _compareFn; constructor(e = (r, s) => r < s) { (this._compareFn = e), (this._items = []); } insert(e) { this._items.push(e), this.moveUp(this._items.length - 1); } remove() { if (this._items.length === 0) return null; let e = this._items[0], r = this._items.pop(); return ( this._items.length !== 0 && ((this._items[0] = r), this.moveDown(0)), e ); } clear() { this._items.splice(0, this._items.length); } moveUp(e) { for (; e > 0; ) { let r = Math.floor((e - 1) / 2); if ( !this._compareFn(this._items[e], this._items[r]) && this._items[e] >= this._items[r] ) break; this.swap(e, r), (e = r); } } moveDown(e) { for (; e < Math.floor(this._items.length / 2); ) { let r = 2 * e + 1; if ( (r < this._items.length - 1 && !this._compareFn(this._items[r], this._items[r + 1]) && ++r, this._compareFn(this._items[e], this._items[r])) ) break; this.swap(e, r), (e = r); } } swap(e, r) { [this._items[e], this._items[r]] = [this._items[r], this._items[e]]; } get length() { return this._items.length; } }; var Bi = Object.freeze([ 776, 2359, 2367, 2984, 3007, 3021, 3633, 3635, 3648, 3657, 4352, 4449, 4520, ]); function io(t) { if (typeof t != "string") throw new TypeError("string cannot be undefined or null"); let e = [], r = 0, s = 0; for (; r < t.length; ) { if ( ((s += Fi(r + s, t)), Hi(t[r + s]) && s++, ki(t[r + s]) && s++, Ni(t[r + s]) && s++, Ki(t[r + s])) ) { s++; continue; } e.push(t.substring(r, r + s)), (r += s), (s = 0); } return e; } i(io, "runes"); function Fi(t, e) { let r = e[t]; if (!Ii(r) || t === e.length - 1) return 1; let s = r + e[t + 1], a = e.substring(t + 2, t + 5); return eo(s) && eo(a) ? 4 : Li(s) && _i(a) ? e.slice(t).indexOf(String.fromCodePoint(917631)) + 2 : ji(a) ? 4 : 2; } i(Fi, "nextUnits"); function Ii(t) { return t && vt(t[0].charCodeAt(0), 55296, 56319); } i(Ii, "isFirstOfSurrogatePair"); function eo(t) { return vt(cr(t), 127462, 127487); } i(eo, "isRegionalIndicator"); function Li(t) { return vt(cr(t), 127988, 127988); } i(Li, "isSubdivisionFlag"); function ji(t) { return vt(cr(t), 127995, 127999); } i(ji, "isFitzpatrickModifier"); function ki(t) { return typeof t == "string" && vt(t.charCodeAt(0), 65024, 65039); } i(ki, "isVariationSelector"); function Ni(t) { return typeof t == "string" && vt(t.charCodeAt(0), 8400, 8447); } i(Ni, "isDiacriticalMark"); function _i(t) { let e = t.codePointAt(0); return ( typeof t == "string" && typeof e == "number" && vt(e, 917504, 917631) ); } i(_i, "isSupplementarySpecialpurposePlane"); function Hi(t) { return typeof t == "string" && Bi.includes(t.charCodeAt(0)); } i(Hi, "isGrapheme"); function Ki(t) { return typeof t == "string" && t.charCodeAt(0) === 8205; } i(Ki, "isZeroWidthJoiner"); function cr(t) { let e = t.charCodeAt(0) - 55296, r = t.charCodeAt(1) - 56320; return (e << 10) + r + 65536; } i(cr, "codePointFromSurrogatePair"); function vt(t, e, r) { return t >= e && t <= r; } i(vt, "betweenInclusive"); var ur = { "Joy-Con L+R (STANDARD GAMEPAD Vendor: 057e Product: 200e)": { buttons: { 0: "south", 1: "east", 2: "west", 3: "north", 4: "lshoulder", 5: "rshoulder", 6: "ltrigger", 7: "rtrigger", 8: "select", 9: "start", 10: "lstick", 11: "rstick", 12: "dpad-up", 13: "dpad-down", 14: "dpad-left", 15: "dpad-right", 16: "home", 17: "capture", }, sticks: { left: { x: 0, y: 1 }, right: { x: 2, y: 3 } }, }, "Joy-Con (L) (STANDARD GAMEPAD Vendor: 057e Product: 2006)": { buttons: { 0: "south", 1: "east", 2: "west", 3: "north", 4: "lshoulder", 5: "rshoulder", 9: "select", 10: "lstick", 16: "start", }, sticks: { left: { x: 0, y: 1 } }, }, "Joy-Con (R) (STANDARD GAMEPAD Vendor: 057e Product: 2007)": { buttons: { 0: "south", 1: "east", 2: "west", 3: "north", 4: "lshoulder", 5: "rshoulder", 9: "start", 10: "lstick", 16: "select", }, sticks: { left: { x: 0, y: 1 } }, }, "Pro Controller (STANDARD GAMEPAD Vendor: 057e Product: 2009)": { buttons: { 0: "south", 1: "east", 2: "west", 3: "north", 4: "lshoulder", 5: "rshoulder", 6: "ltrigger", 7: "rtrigger", 8: "select", 9: "start", 10: "lstick", 11: "rstick", 12: "dpad-up", 13: "dpad-down", 14: "dpad-left", 15: "dpad-right", 16: "home", 17: "capture", }, sticks: { left: { x: 0, y: 1 }, right: { x: 2, y: 3 } }, }, default: { buttons: { 0: "south", 1: "east", 2: "west", 3: "north", 4: "lshoulder", 5: "rshoulder", 6: "ltrigger", 7: "rtrigger", 8: "select", 9: "start", 10: "lstick", 11: "rstick", 12: "dpad-up", 13: "dpad-down", 14: "dpad-left", 15: "dpad-right", 16: "home", }, sticks: { left: { x: 0, y: 1 }, right: { x: 2, y: 3 } }, }, }; var Ut = class { static { i(this, "ButtonState"); } pressed = new Set([]); pressedRepeat = new Set([]); released = new Set([]); down = new Set([]); update() { this.pressed.clear(), this.released.clear(), this.pressedRepeat.clear(); } press(e) { this.pressed.add(e), this.pressedRepeat.add(e), this.down.add(e); } pressRepeat(e) { this.pressedRepeat.add(e); } release(e) { this.down.delete(e), this.pressed.delete(e), this.released.add(e); } }, lr = class { static { i(this, "GamepadState"); } buttonState = new Ut(); stickState = new Map(); }, mr = class { static { i(this, "FPSCounter"); } dts = []; timer = 0; fps = 0; tick(e) { this.dts.push(e), (this.timer += e), this.timer >= 1 && ((this.timer = 0), (this.fps = Math.round( 1 / (this.dts.reduce((r, s) => r + s) / this.dts.length) )), (this.dts = [])); } }, ao = i((t) => { if (!t.canvas) throw new Error("Please provide a canvas"); let e = { canvas: t.canvas, loopID: null, stopped: !1, dt: 0, time: 0, realTime: 0, fpsCounter: new mr(), timeScale: 1, skipTime: !1, isHidden: !1, numFrames: 0, mousePos: new T(0), mouseDeltaPos: new T(0), keyState: new Ut(), mouseState: new Ut(), mergedGamepadState: new lr(), gamepadStates: new Map(), gamepads: [], charInputted: [], isMouseMoved: !1, lastWidth: t.canvas.offsetWidth, lastHeight: t.canvas.offsetHeight, events: new tt(), }; function r() { return e.dt * e.timeScale; } i(r, "dt"); function s() { return e.isHidden; } i(s, "isHidden"); function a() { return e.time; } i(a, "time"); function l() { return e.fpsCounter.fps; } i(l, "fps"); function u() { return e.numFrames; } i(u, "numFrames"); function f() { return e.canvas.toDataURL(); } i(f, "screenshot"); function y(p) { e.canvas.style.cursor = p; } i(y, "setCursor"); function m() { return e.canvas.style.cursor; } i(m, "getCursor"); function V(p) { if (p) try { let S = e.canvas.requestPointerLock(); S.catch && S.catch((L) => console.error(L)); } catch (S) { console.error(S); } else document.exitPointerLock(); } i(V, "setCursorLocked"); function x() { return !!document.pointerLockElement; } i(x, "isCursorLocked"); function P(p) { p.requestFullscreen ? p.requestFullscreen() : p.webkitRequestFullscreen && p.webkitRequestFullscreen(); } i(P, "enterFullscreen"); function w() { document.exitFullscreen ? document.exitFullscreen() : document.webkitExitFullScreen && document.webkitExitFullScreen(); } i(w, "exitFullscreen"); function K() { return document.fullscreenElement || document.webkitFullscreenElement; } i(K, "getFullscreenElement"); function X(p = !0) { p ? P(e.canvas) : w(); } i(X, "setFullscreen"); function z() { return !!K(); } i(z, "isFullscreen"); function N() { e.stopped = !0; for (let p in he) e.canvas.removeEventListener(p, he[p]); for (let p in Ke) document.removeEventListener(p, Ke[p]); for (let p in xe) window.removeEventListener(p, xe[p]); at.disconnect(); } i(N, "quit"); function $(p) { e.loopID !== null && cancelAnimationFrame(e.loopID); let S = 0, L = i((Y) => { if (e.stopped) return; if (document.visibilityState !== "visible") { e.loopID = requestAnimationFrame(L); return; } let ye = Y / 1e3, ie = ye - e.realTime, Ye = t.maxFPS ? 1 / t.maxFPS : 0; (e.realTime = ye), (S += ie), S > Ye && (e.skipTime || ((e.dt = S), (e.time += r()), e.fpsCounter.tick(e.dt)), (S = 0), (e.skipTime = !1), e.numFrames++, Mn(), p(), Mt()), (e.loopID = requestAnimationFrame(L)); }, "frame"); L(0); } i($, "run"); function O() { return "ontouchstart" in window || navigator.maxTouchPoints > 0; } i(O, "isTouchscreen"); function se() { return e.mousePos.clone(); } i(se, "mousePos"); function _() { return e.mouseDeltaPos.clone(); } i(_, "mouseDeltaPos"); function ae(p = "left") { return e.mouseState.pressed.has(p); } i(ae, "isMousePressed"); function re(p = "left") { return e.mouseState.down.has(p); } i(re, "isMouseDown"); function ce(p = "left") { return e.mouseState.released.has(p); } i(ce, "isMouseReleased"); function ge() { return e.isMouseMoved; } i(ge, "isMouseMoved"); function Ue(p) { return p === void 0 ? e.keyState.pressed.size > 0 : e.keyState.pressed.has(p); } i(Ue, "isKeyPressed"); function Ce(p) { return p === void 0 ? e.keyState.pressedRepeat.size > 0 : e.keyState.pressedRepeat.has(p); } i(Ce, "isKeyPressedRepeat"); function Le(p) { return p === void 0 ? e.keyState.down.size > 0 : e.keyState.down.has(p); } i(Le, "isKeyDown"); function je(p) { return p === void 0 ? e.keyState.released.size > 0 : e.keyState.released.has(p); } i(je, "isKeyReleased"); function Ee(p) { return p === void 0 ? e.mergedGamepadState.buttonState.pressed.size > 0 : e.mergedGamepadState.buttonState.pressed.has(p); } i(Ee, "isGamepadButtonPressed"); function Ne(p) { return p === void 0 ? e.mergedGamepadState.buttonState.down.size > 0 : e.mergedGamepadState.buttonState.down.has(p); } i(Ne, "isGamepadButtonDown"); function _e(p) { return p === void 0 ? e.mergedGamepadState.buttonState.released.size > 0 : e.mergedGamepadState.buttonState.released.has(p); } i(_e, "isGamepadButtonReleased"); function He(p) { return e.events.on("resize", p); } i(He, "onResize"); let pt = Ie( (p) => e.events.on("keyDown", p), (p, S) => e.events.on("keyDown", (L) => L === p && S(p)) ), Se = Ie( (p) => e.events.on("keyPress", p), (p, S) => e.events.on("keyPress", (L) => L === p && S(p)) ), Ve = Ie( (p) => e.events.on("keyPressRepeat", p), (p, S) => e.events.on("keyPressRepeat", (L) => L === p && S(p)) ), ze = Ie( (p) => e.events.on("keyRelease", p), (p, S) => e.events.on("keyRelease", (L) => L === p && S(p)) ), rt = Ie( (p) => e.events.on("mouseDown", (S) => p(S)), (p, S) => e.events.on("mouseDown", (L) => L === p && S(L)) ), ot = Ie( (p) => e.events.on("mousePress", (S) => p(S)), (p, S) => e.events.on("mousePress", (L) => L === p && S(L)) ), wt = Ie( (p) => e.events.on("mouseRelease", (S) => p(S)), (p, S) => e.events.on("mouseRelease", (L) => L === p && S(L)) ); function Yt(p) { return e.events.on("mouseMove", () => p(se(), _())); } i(Yt, "onMouseMove"); function Xt(p) { return e.events.on("charInput", p); } i(Xt, "onCharInput"); function An(p) { return e.events.on("touchStart", p); } i(An, "onTouchStart"); function Wt(p) { return e.events.on("touchMove", p); } i(Wt, "onTouchMove"); function On(p) { return e.events.on("touchEnd", p); } i(On, "onTouchEnd"); function Rn(p) { return e.events.on("scroll", p); } i(Rn, "onScroll"); function $t(p) { return e.events.on("hide", p); } i($t, "onHide"); function Un(p) { return e.events.on("show", p); } i(Un, "onShow"); function Vn(p, S) { if (typeof p == "function") return e.events.on("gamepadButtonDown", p); if (typeof p == "string" && typeof S == "function") return e.events.on("gamepadButtonDown", (L) => L === p && S(p)); } i(Vn, "onGamepadButtonDown"); function Qt(p, S) { if (typeof p == "function") return e.events.on("gamepadButtonPress", p); if (typeof p == "string" && typeof S == "function") return e.events.on("gamepadButtonPress", (L) => L === p && S(p)); } i(Qt, "onGamepadButtonPress"); function Jt(p, S) { if (typeof p == "function") return e.events.on("gamepadButtonRelease", p); if (typeof p == "string" && typeof S == "function") return e.events.on("gamepadButtonRelease", (L) => L === p && S(p)); } i(Jt, "onGamepadButtonRelease"); function Pn(p, S) { return e.events.on("gamepadStick", (L, Y) => L === p && S(Y)); } i(Pn, "onGamepadStick"); function Dt(p) { e.events.on("gamepadConnect", p); } i(Dt, "onGamepadConnect"); function Dn(p) { e.events.on("gamepadDisconnect", p); } i(Dn, "onGamepadDisconnect"); function st(p) { return e.mergedGamepadState.stickState.get(p) || new T(0); } i(st, "getGamepadStick"); function Pe() { return [...e.charInputted]; } i(Pe, "charInputted"); function Ct() { return [...e.gamepads]; } i(Ct, "getGamepads"); function Mn() { e.events.trigger("input"), e.keyState.down.forEach((p) => e.events.trigger("keyDown", p)), e.mouseState.down.forEach((p) => e.events.trigger("mouseDown", p)), de(); } i(Mn, "processInput"); function Mt() { e.keyState.update(), e.mouseState.update(), e.mergedGamepadState.buttonState.update(), e.mergedGamepadState.stickState.forEach((p, S) => { e.mergedGamepadState.stickState.set(S, new T(0)); }), (e.charInputted = []), (e.isMouseMoved = !1), e.gamepadStates.forEach((p) => { p.buttonState.update(), p.stickState.forEach((S, L) => { p.stickState.set(L, new T(0)); }); }); } i(Mt, "resetInput"); function Zt(p) { let S = { index: p.index, isPressed: (L) => e.gamepadStates.get(p.index).buttonState.pressed.has(L), isDown: (L) => e.gamepadStates.get(p.index).buttonState.down.has(L), isReleased: (L) => e.gamepadStates.get(p.index).buttonState.released.has(L), getStick: (L) => e.gamepadStates.get(p.index).stickState.get(L), }; return ( e.gamepads.push(S), e.gamepadStates.set(p.index, { buttonState: new Ut(), stickState: new Map([ ["left", new T(0)], ["right", new T(0)], ]), }), S ); } i(Zt, "registerGamepad"); function Gn(p) { (e.gamepads = e.gamepads.filter((S) => S.index !== p.index)), e.gamepadStates.delete(p.index); } i(Gn, "removeGamepad"); function de() { for (let p of navigator.getGamepads()) p && !e.gamepadStates.has(p.index) && Zt(p); for (let p of e.gamepads) { let S = navigator.getGamepads()[p.index], Y = (t.gamepads ?? {})[S.id] ?? ur[S.id] ?? ur.default, ye = e.gamepadStates.get(p.index); for (let ie = 0; ie < S.buttons.length; ie++) S.buttons[ie].pressed ? (ye.buttonState.down.has(Y.buttons[ie]) || (e.mergedGamepadState.buttonState.press(Y.buttons[ie]), ye.buttonState.press(Y.buttons[ie]), e.events.trigger("gamepadButtonPress", Y.buttons[ie])), e.events.trigger("gamepadButtonDown", Y.buttons[ie])) : ye.buttonState.down.has(Y.buttons[ie]) && (e.mergedGamepadState.buttonState.release(Y.buttons[ie]), ye.buttonState.release(Y.buttons[ie]), e.events.trigger("gamepadButtonRelease", Y.buttons[ie])); for (let ie in Y.sticks) { let Ye = Y.sticks[ie], ft = new T(S.axes[Ye.x], S.axes[Ye.y]); ye.stickState.set(ie, ft), e.mergedGamepadState.stickState.set(ie, ft), e.events.trigger("gamepadStick", ie, ft); } } } i(de, "processGamepad"); let he = {}, Ke = {}, xe = {}, Ae = t.pixelDensity || window.devicePixelRatio || 1; he.mousemove = (p) => { let S = new T(p.offsetX, p.offsetY), L = new T(p.movementX, p.movementY); if (z()) { let Y = e.canvas.width / Ae, ye = e.canvas.height / Ae, ie = window.innerWidth, Ye = window.innerHeight, ft = ie / Ye, tn = Y / ye; if (ft > tn) { let Xe = Ye / ye, Tt = (ie - Y * Xe) / 2; (S.x = dt(p.offsetX - Tt, 0, Y * Xe, 0, Y)), (S.y = dt(p.offsetY, 0, ye * Xe, 0, ye)); } else { let Xe = ie / Y, Tt = (Ye - ye * Xe) / 2; (S.x = dt(p.offsetX, 0, Y * Xe, 0, Y)), (S.y = dt(p.offsetY - Tt, 0, ye * Xe, 0, ye)); } } e.events.onOnce("input", () => { (e.isMouseMoved = !0), (e.mousePos = S), (e.mouseDeltaPos = L), e.events.trigger("mouseMove"); }); }; let it = ["left", "middle", "right", "back", "forward"]; (he.mousedown = (p) => { e.events.onOnce("input", () => { let S = it[p.button]; S && (e.mouseState.press(S), e.events.trigger("mousePress", S)); }); }), (he.mouseup = (p) => { e.events.onOnce("input", () => { let S = it[p.button]; S && (e.mouseState.release(S), e.events.trigger("mouseRelease", S)); }); }); let Gt = new Set([ " ", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Tab", ]), en = { ArrowLeft: "left", ArrowRight: "right", ArrowUp: "up", ArrowDown: "down", " ": "space", }; (he.keydown = (p) => { Gt.has(p.key) && p.preventDefault(), e.events.onOnce("input", () => { let S = en[p.key] || p.key.toLowerCase(); S.length === 1 ? (e.events.trigger("charInput", S), e.charInputted.push(S)) : S === "space" && (e.events.trigger("charInput", " "), e.charInputted.push(" ")), p.repeat ? (e.keyState.pressRepeat(S), e.events.trigger("keyPressRepeat", S)) : (e.keyState.press(S), e.events.trigger("keyPressRepeat", S), e.events.trigger("keyPress", S)); }); }), (he.keyup = (p) => { e.events.onOnce("input", () => { let S = en[p.key] || p.key.toLowerCase(); e.keyState.release(S), e.events.trigger("keyRelease", S); }); }), (he.touchstart = (p) => { p.preventDefault(), e.events.onOnce("input", () => { let S = [...p.changedTouches], L = e.canvas.getBoundingClientRect(); t.touchToMouse !== !1 && ((e.mousePos = new T(S[0].clientX - L.x, S[0].clientY - L.y)), e.mouseState.press("left"), e.events.trigger("mousePress", "left")), S.forEach((Y) => { e.events.trigger( "touchStart", new T(Y.clientX - L.x, Y.clientY - L.y), Y ); }); }); }), (he.touchmove = (p) => { p.preventDefault(), e.events.onOnce("input", () => { let S = [...p.changedTouches], L = e.canvas.getBoundingClientRect(); if (t.touchToMouse !== !1) { let Y = e.mousePos; (e.mousePos = new T(S[0].clientX - L.x, S[0].clientY - L.y)), (e.mouseDeltaPos = e.mousePos.sub(Y)), e.events.trigger("mouseMove"); } S.forEach((Y) => { e.events.trigger( "touchMove", new T(Y.clientX - L.x, Y.clientY - L.y), Y ); }); }); }), (he.touchend = (p) => { e.events.onOnce("input", () => { let S = [...p.changedTouches], L = e.canvas.getBoundingClientRect(); t.touchToMouse !== !1 && ((e.mousePos = new T(S[0].clientX - L.x, S[0].clientY - L.y)), (e.mouseDeltaPos = new T(0, 0)), e.mouseState.release("left"), e.events.trigger("mouseRelease", "left")), S.forEach((Y) => { e.events.trigger( "touchEnd", new T(Y.clientX - L.x, Y.clientY - L.y), Y ); }); }); }), (he.touchcancel = (p) => { e.events.onOnce("input", () => { let S = [...p.changedTouches], L = e.canvas.getBoundingClientRect(); t.touchToMouse !== !1 && ((e.mousePos = new T(S[0].clientX - L.x, S[0].clientY - L.y)), e.mouseState.release("left"), e.events.trigger("mouseRelease", "left")), S.forEach((Y) => { e.events.trigger( "touchEnd", new T(Y.clientX - L.x, Y.clientY - L.y), Y ); }); }); }), (he.wheel = (p) => { p.preventDefault(), e.events.onOnce("input", () => { e.events.trigger("scroll", new T(p.deltaX, p.deltaY)); }); }), (he.contextmenu = (p) => p.preventDefault()), (Ke.visibilitychange = () => { document.visibilityState === "visible" ? ((e.skipTime = !0), (e.isHidden = !1), e.events.trigger("show")) : ((e.isHidden = !0), e.events.trigger("hide")); }), (xe.gamepadconnected = (p) => { let S = Zt(p.gamepad); e.events.onOnce("input", () => { e.events.trigger("gamepadConnect", S); }); }), (xe.gamepaddisconnected = (p) => { let S = Ct().filter((L) => L.index === p.gamepad.index)[0]; Gn(p.gamepad), e.events.onOnce("input", () => { e.events.trigger("gamepadDisconnect", S); }); }); for (let p in he) e.canvas.addEventListener(p, he[p]); for (let p in Ke) document.addEventListener(p, Ke[p]); for (let p in xe) window.addEventListener(p, xe[p]); let at = new ResizeObserver((p) => { for (let S of p) if (S.target === e.canvas) { if ( e.lastWidth === e.canvas.offsetWidth && e.lastHeight === e.canvas.offsetHeight ) return; (e.lastWidth = e.canvas.offsetWidth), (e.lastHeight = e.canvas.offsetHeight), e.events.onOnce("input", () => { e.events.trigger("resize"); }); } }); return ( at.observe(e.canvas), { dt: r, time: a, run: $, canvas: e.canvas, fps: l, numFrames: u, quit: N, isHidden: s, setFullscreen: X, isFullscreen: z, setCursor: y, screenshot: f, getGamepads: Ct, getCursor: m, setCursorLocked: V, isCursorLocked: x, isTouchscreen: O, mousePos: se, mouseDeltaPos: _, isKeyDown: Le, isKeyPressed: Ue, isKeyPressedRepeat: Ce, isKeyReleased: je, isMouseDown: re, isMousePressed: ae, isMouseReleased: ce, isMouseMoved: ge, isGamepadButtonPressed: Ee, isGamepadButtonDown: Ne, isGamepadButtonReleased: _e, getGamepadStick: st, charInputted: Pe, onResize: He, onKeyDown: pt, onKeyPress: Se, onKeyPressRepeat: Ve, onKeyRelease: ze, onMouseDown: rt, onMousePress: ot, onMouseRelease: wt, onMouseMove: Yt, onCharInput: Xt, onTouchStart: An, onTouchMove: Wt, onTouchEnd: On, onScroll: Rn, onHide: $t, onShow: Un, onGamepadButtonDown: Vn, onGamepadButtonPress: Qt, onGamepadButtonRelease: Jt, onGamepadStick: Pn, onGamepadConnect: Dt, onGamepadDisconnect: Dn, events: e.events, } ); }, "default"); var qe = class t { static { i(this, "Texture"); } ctx; src = null; glTex; width; height; constructor(e, r, s, a = {}) { this.ctx = e; let l = e.gl; (this.glTex = e.gl.createTexture()), e.onDestroy(() => this.free()), (this.width = r), (this.height = s); let u = { linear: l.LINEAR, nearest: l.NEAREST }[ a.filter ?? e.opts.texFilter ] ?? l.NEAREST, f = { repeat: l.REPEAT, clampToEadge: l.CLAMP_TO_EDGE }[a.wrap] ?? l.CLAMP_TO_EDGE; this.bind(), r && s && l.texImage2D( l.TEXTURE_2D, 0, l.RGBA, r, s, 0, l.RGBA, l.UNSIGNED_BYTE, null ), l.texParameteri(l.TEXTURE_2D, l.TEXTURE_MIN_FILTER, u), l.texParameteri(l.TEXTURE_2D, l.TEXTURE_MAG_FILTER, u), l.texParameteri(l.TEXTURE_2D, l.TEXTURE_WRAP_S, f), l.texParameteri(l.TEXTURE_2D, l.TEXTURE_WRAP_T, f), this.unbind(); } static fromImage(e, r, s = {}) { let a = new t(e, r.width, r.height, s); return a.update(r), (a.src = r), a; } update(e, r = 0, s = 0) { let a = this.ctx.gl; this.bind(), a.texSubImage2D(a.TEXTURE_2D, 0, r, s, a.RGBA, a.UNSIGNED_BYTE, e), this.unbind(); } bind() { this.ctx.pushTexture2D(this.glTex); } unbind() { this.ctx.popTexture2D(); } free() { this.ctx.gl.deleteTexture(this.glTex); } }, yt = class { static { i(this, "FrameBuffer"); } ctx; tex; glFramebuffer; glRenderbuffer; constructor(e, r, s, a = {}) { this.ctx = e; let l = e.gl; e.onDestroy(() => this.free()), (this.tex = new qe(e, r, s, a)), (this.glFramebuffer = l.createFramebuffer()), (this.glRenderbuffer = l.createRenderbuffer()), this.bind(), l.renderbufferStorage(l.RENDERBUFFER, l.DEPTH_STENCIL, r, s), l.framebufferTexture2D( l.FRAMEBUFFER, l.COLOR_ATTACHMENT0, l.TEXTURE_2D, this.tex.glTex, 0 ), l.framebufferRenderbuffer( l.FRAMEBUFFER, l.DEPTH_STENCIL_ATTACHMENT, l.RENDERBUFFER, this.glRenderbuffer ), this.unbind(); } get width() { return this.tex.width; } get height() { return this.tex.height; } toImageData() { let e = this.ctx.gl, r = new Uint8ClampedArray(this.width * this.height * 4); this.bind(), e.readPixels( 0, 0, this.width, this.height, e.RGBA, e.UNSIGNED_BYTE, r ), this.unbind(); let s = this.width * 4, a = new Uint8Array(s); for (let l = 0; l < ((this.height / 2) | 0); l++) { let u = l * s, f = (this.height - l - 1) * s; a.set(r.subarray(u, u + s)), r.copyWithin(u, f, f + s), r.set(a, f); } return new ImageData(r, this.width, this.height); } toDataURL() { let e = document.createElement("canvas"), r = e.getContext("2d"); return ( (e.width = this.width), (e.height = this.height), r.putImageData(this.toImageData(), 0, 0), e.toDataURL() ); } clear() { let e = this.ctx.gl; e.clear(e.COLOR_BUFFER_BIT); } draw(e) { this.bind(), e(), this.unbind(); } bind() { this.ctx.pushFramebuffer(this.glFramebuffer), this.ctx.pushRenderbuffer(this.glRenderbuffer), this.ctx.pushViewport({ x: 0, y: 0, w: this.width, h: this.height }); } unbind() { this.ctx.popFramebuffer(), this.ctx.popRenderbuffer(), this.ctx.popViewport(); } free() { let e = this.ctx.gl; e.deleteFramebuffer(this.glFramebuffer), e.deleteRenderbuffer(this.glRenderbuffer), this.tex.free(); } }, hn = class { static { i(this, "Shader"); } ctx; glProgram; constructor(e, r, s, a) { (this.ctx = e), e.onDestroy(() => this.free()); let l = e.gl, u = l.createShader(l.VERTEX_SHADER), f = l.createShader(l.FRAGMENT_SHADER); l.shaderSource(u, r), l.shaderSource(f, s), l.compileShader(u), l.compileShader(f); let y = l.createProgram(); if ( ((this.glProgram = y), l.attachShader(y, u), l.attachShader(y, f), a.forEach((m, V) => l.bindAttribLocation(y, V, m)), l.linkProgram(y), !l.getProgramParameter(y, l.LINK_STATUS)) ) { let m = l.getShaderInfoLog(u); if (m) throw new Error("VERTEX SHADER " + m); let V = l.getShaderInfoLog(f); if (V) throw new Error("FRAGMENT SHADER " + V); } l.deleteShader(u), l.deleteShader(f); } bind() { this.ctx.pushProgram(this.glProgram); } unbind() { this.ctx.popProgram(); } send(e) { let r = this.ctx.gl; for (let s in e) { let a = e[s], l = r.getUniformLocation(this.glProgram, s); if (typeof a == "number") r.uniform1f(l, a); else if (a instanceof Ge) r.uniformMatrix4fv(l, !1, new Float32Array(a.m)); else if (a instanceof ee) r.uniform3f(l, a.r, a.g, a.b); else if (a instanceof T) r.uniform2f(l, a.x, a.y); else if (Array.isArray(a)) { let u = a[0]; typeof u == "number" ? r.uniform1fv(l, a) : u instanceof T ? r.uniform2fv(l, a.map((f) => [f.x, f.y]).flat()) : u instanceof ee && r.uniform3fv(l, a.map((f) => [f.r, f.g, f.b]).flat()); } else throw new Error("Unsupported uniform data type"); } } free() { this.ctx.gl.deleteProgram(this.glProgram); } }, pn = class { static { i(this, "BatchRenderer"); } ctx; glVBuf; glIBuf; vqueue = []; iqueue = []; stride; maxVertices; maxIndices; vertexFormat; numDraws = 0; curPrimitive = null; curTex = null; curShader = null; curUniform = {}; constructor(e, r, s, a) { let l = e.gl; (this.vertexFormat = r), (this.ctx = e), (this.stride = r.reduce((u, f) => u + f.size, 0)), (this.maxVertices = s), (this.maxIndices = a), (this.glVBuf = l.createBuffer()), e.pushArrayBuffer(this.glVBuf), l.bufferData(l.ARRAY_BUFFER, s * 4, l.DYNAMIC_DRAW), e.popArrayBuffer(), (this.glIBuf = l.createBuffer()), e.pushElementArrayBuffer(this.glIBuf), l.bufferData(l.ELEMENT_ARRAY_BUFFER, a * 4, l.DYNAMIC_DRAW), e.popElementArrayBuffer(); } push(e, r, s, a, l = null, u = {}) { (e !== this.curPrimitive || l !== this.curTex || a !== this.curShader || !mn(this.curUniform, u) || this.vqueue.length + r.length * this.stride > this.maxVertices || this.iqueue.length + s.length > this.maxIndices) && this.flush(); let f = this.vqueue.length / this.stride; for (let y of r) this.vqueue.push(y); for (let y of s) this.iqueue.push(y + f); (this.curPrimitive = e), (this.curShader = a), (this.curTex = l), (this.curUniform = u); } flush() { if ( !this.curPrimitive || !this.curShader || this.vqueue.length === 0 || this.iqueue.length === 0 ) return; let e = this.ctx.gl; this.ctx.pushArrayBuffer(this.glVBuf), e.bufferSubData(e.ARRAY_BUFFER, 0, new Float32Array(this.vqueue)), this.ctx.pushElementArrayBuffer(this.glIBuf), e.bufferSubData( e.ELEMENT_ARRAY_BUFFER, 0, new Uint16Array(this.iqueue) ), this.ctx.setVertexFormat(this.vertexFormat), this.curShader.bind(), this.curShader.send(this.curUniform), this.curTex?.bind(), e.drawElements( this.curPrimitive, this.iqueue.length, e.UNSIGNED_SHORT, 0 ), this.curTex?.unbind(), this.curShader.unbind(), this.ctx.popArrayBuffer(), this.ctx.popElementArrayBuffer(), (this.vqueue = []), (this.iqueue = []), this.numDraws++; } free() { let e = this.ctx.gl; e.deleteBuffer(this.glVBuf), e.deleteBuffer(this.glIBuf); } }; function xt(t) { let e = [], r = i((l) => { e.push(l), t(l); }, "push"), s = i(() => { e.pop(), t(a() ?? null); }, "pop"), a = i(() => e[e.length - 1], "cur"); return [r, s, a]; } i(xt, "genStack"); function dr(t, e = {}) { let r = []; function s(_) { r.push(_); } i(s, "onDestroy"); function a() { r.forEach((_) => _()), t.getExtension("WEBGL_lose_context").loseContext(); } i(a, "destroy"); let l = null; function u(_) { if (mn(_, l)) return; l = _; let ae = _.reduce((re, ce) => re + ce.size, 0); _.reduce( (re, ce, ge) => ( t.vertexAttribPointer(ge, ce.size, t.FLOAT, !1, ae * 4, re), t.enableVertexAttribArray(ge), re + ce.size * 4 ), 0 ); } i(u, "setVertexFormat"); let [f, y] = xt((_) => t.bindTexture(t.TEXTURE_2D, _)), [m, V] = xt((_) => t.bindBuffer(t.ARRAY_BUFFER, _)), [x, P] = xt((_) => t.bindBuffer(t.ELEMENT_ARRAY_BUFFER, _)), [w, K] = xt((_) => t.bindFramebuffer(t.FRAMEBUFFER, _)), [X, z] = xt((_) => t.bindRenderbuffer(t.RENDERBUFFER, _)), [N, $] = xt(({ x: _, y: ae, w: re, h: ce }) => { t.viewport(_, ae, re, ce); }), [O, se] = xt((_) => t.useProgram(_)); return ( N({ x: 0, y: 0, w: t.drawingBufferWidth, h: t.drawingBufferHeight }), { gl: t, opts: e, onDestroy: s, destroy: a, pushTexture2D: f, popTexture2D: y, pushArrayBuffer: m, popArrayBuffer: V, pushElementArrayBuffer: x, popElementArrayBuffer: P, pushFramebuffer: w, popFramebuffer: K, pushRenderbuffer: X, popRenderbuffer: z, pushViewport: N, popViewport: $, pushProgram: O, popProgram: se, setVertexFormat: u, } ); } i(dr, "initGfx"); var Re = class t { static { i(this, "Asset"); } loaded = !1; data = null; error = null; onLoadEvents = new we(); onErrorEvents = new we(); onFinishEvents = new we(); constructor(e) { e.then((r) => { (this.loaded = !0), (this.data = r), this.onLoadEvents.trigger(r); }) .catch((r) => { if (((this.error = r), this.onErrorEvents.numListeners() > 0)) this.onErrorEvents.trigger(r); else throw r; }) .finally(() => { this.onFinishEvents.trigger(), (this.loaded = !0); }); } static loaded(e) { let r = new t(Promise.resolve(e)); return (r.data = e), (r.loaded = !0), r; } onLoad(e) { return ( this.loaded && this.data ? e(this.data) : this.onLoadEvents.add(e), this ); } onError(e) { return ( this.loaded && this.error ? e(this.error) : this.onErrorEvents.add(e), this ); } onFinish(e) { return this.loaded ? e() : this.onFinishEvents.add(e), this; } then(e) { return this.onLoad(e); } catch(e) { return this.onError(e); } finally(e) { return this.onFinish(e); } }, nt = class { static { i(this, "AssetBucket"); } assets = new Map(); lastUID = 0; add(e, r) { let s = e ?? this.lastUID++ + "", a = new Re(r); return this.assets.set(s, a), a; } addLoaded(e, r) { let s = e ?? this.lastUID++ + "", a = Re.loaded(r); return this.assets.set(s, a), a; } get(e) { return this.assets.get(e); } progress() { if (this.assets.size === 0) return 1; let e = 0; return ( this.assets.forEach((r) => { r.loaded && e++; }), e / this.assets.size ); } }; function hr(t) { return fetch(t).then((e) => { if (!e.ok) throw new Error(`Failed to fetch "${t}"`); return e; }); } i(hr, "fetchURL"); function Nt(t) { return hr(t).then((e) => e.json()); } i(Nt, "fetchJSON"); function co(t) { return hr(t).then((e) => e.text()); } i(co, "fetchText"); function uo(t) { return hr(t).then((e) => e.arrayBuffer()); } i(uo, "fetchArrayBuffer"); function _t(t) { let e = new Image(); return ( (e.crossOrigin = "anonymous"), (e.src = t), new Promise((r, s) => { (e.onload = () => r(e)), (e.onerror = () => s(new Error(`Failed to load image from "${t}"`))); }) ); } i(_t, "loadImg"); var pr = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", Vt = "topleft"; var lo = "monospace", Ht = "monospace"; var fn = "linear"; var gn = [ { name: "a_pos", size: 2 }, { name: "a_uv", size: 2 }, { name: "a_color", size: 4 }, ], zi = gn.reduce((t, e) => t + e.size, 0), mo = 2048, ho = mo * 4 * zi, po = mo * 6, fo = ` attribute vec2 a_pos; attribute vec2 a_uv; attribute vec4 a_color; varying vec2 v_pos; varying vec2 v_uv; varying vec4 v_color; vec4 def_vert() { return vec4(a_pos, 0.0, 1.0); } {{user}} void main() { vec4 pos = vert(a_pos, a_uv, a_color); v_pos = a_pos; v_uv = a_uv; v_color = a_color; gl_Position = pos; } `, go = ` precision mediump float; varying vec2 v_pos; varying vec2 v_uv; varying vec4 v_color; uniform sampler2D u_tex; vec4 def_frag() { return v_color * texture2D(u_tex, v_uv); } {{user}} void main() { gl_FragColor = frag(v_pos, v_uv, v_color, u_tex); if (gl_FragColor.a == 0.0) { discard; } } `, bn = ` vec4 vert(vec2 pos, vec2 uv, vec4 color) { return def_vert(); } `, vn = ` vec4 frag(vec2 pos, vec2 uv, vec4 color, sampler2D tex) { return def_frag(); } `, bo = new Set(["id", "require"]), vo = new Set([ "add", "update", "draw", "destroy", "inspect", "drawInspect", ]), fr = /\[(?