onsight
Version:
Interactive, easy to use JavaScript game framework.
9 lines • 132 kB
JavaScript
/**
* @description Onsight Engine
* @about Interactive, easy to use JavaScript game framework.
* @author Stephens Nunnally <@stevinz>
* @version v0.0.8
* @license MIT - Copyright (c) 2024 Stephens Nunnally
* @source https://github.com/onsightengine/onsight
*/
const t="0.0.8",e=1e3,s=350,i=500,n=2,o=2,r=["init","update","destroy","keydown","keyup","pointerdown","pointerup","pointermove"],a={PORTRAIT:"portrait",LANDSCAPE:"landscape"},h={STAGE_2D:"Stage2D",STAGE_3D:"Stage3D",STAGE_UI:"StageUI"},l={WORLD_2D:"World2D",WORLD_3D:"World3D",WORLD_UI:"WorldUI"},c={JAVASCRIPT:"javascript",PYTHON:"python"};class d{static isIterable(t){return t&&"function"==typeof t[Symbol.iterator]||Array.isArray(t)}static swapItems(t,e,s){return t[e]=t.splice(s,1,t[e])[0],t}static combineThingArrays(t,e){const s=[...t];for(const i of e)!1===d.includesThing(i,t)&&s.push(i);return s}static compareThingArrays(t,e){if(t=Array.isArray(t)?t:[t],e=Array.isArray(e)?e:[e],0===t.length&&0===e.length)return!0;for(const s of t)if(!1===d.includesThing(s,e))return!1;for(const s of e)if(!1===d.includesThing(s,t))return!1;return!0}static filterThings(t,e={}){return t.filter((t=>Object.keys(e).every((s=>t[s]==e[s]))))}static includesThing(t,...e){if(!t||!t.uuid)return!1;if(0===e.length)return!1;e.length>0&&Array.isArray(e[0])&&(e=e[0]);for(const s of e)if(s.uuid&&s.uuid===t.uuid)return!0;return!1}static removeThingFromArray(t,...e){if(e.length>0&&Array.isArray(e[0])&&(e=e[0]),!t||!t.uuid)return[...e];const s=[];for(const i of e)i.uuid!==t.uuid&&s.push(i);return s}static shareValues(t,e){for(let s=0;s<t.length;s++)if(e.includes(t[s]))return!0;return!1}}const u={};class p{static get(t){return t&&t.uuid&&(t=t.uuid),u[t]}static library(t,e){const s=[];t&&"string"==typeof t&&(t=t.toLowerCase()),e&&"string"==typeof e&&(e=e.toLowerCase());for(const[i,n]of Object.entries(u))t&&"string"==typeof n.type&&n.type.toLowerCase()!==t||(null==e||"string"==typeof n.category&&n.category.toLowerCase()===e)&&s.push(n);return s}static add(...t){let e;t.length>0&&Array.isArray(t[0])&&(t=t[0]);for(const s of t)s&&s.uuid&&(s.name&&""!==s.name||(s.name=s.constructor.name),u[s.uuid]=s,e=e??s);return e}static clear(){for(const t in u){u[t].isBuiltIn||p.remove(u[t],!0)}}static remove(t,e=!0){const s=Array.isArray(t)?t:[t];for(const t of s)t&&t.uuid&&u[t.uuid]&&(e&&"function"==typeof t.dispose&&t.dispose(),delete u[t.uuid])}static toJSON(){const t={};for(const e of y.keys()){const s=p.library(e);if(s.length>0){t[e]=[];for(const i of s)t[e].push(i.toJSON())}}return t}static fromJSON(t,e=()=>{}){p.clear();for(const e of y.keys())if(t[e])for(const s of t[e]){const t=p.type(e);if(t){const e=(new t).fromJSON(s);p.add(e)}else console.warn(`AssetManager.fromJSON(): Unknown asset type '${s.type}'`)}"function"==typeof e&&e()}static register(t,e){y.set(t,e)}static type(t){return y.get(t)}}const y=new Map;class m{#t=!1;#e=0;#s=0;#i=0;#n=0;#o=0;#r=0;#a=null;constructor(t=!0,e=0){t&&this.start(),this.#e-=e,this.#i-=e}start(t=!1){t&&this.reset(),this.#e=performance.now(),this.#i=this.#e,this.#t=!0}stop(){this.getDeltaTime(),this.#t=!1}toggle(){this.#t?this.stop():this.start()}reset(){this.#e=performance.now(),this.#i=this.#e,this.#s=0,this.#n=0}getElapsedTime(){return this.#s}getDeltaTime(){if(!this.#t)return this.#a=null,0;const t=performance.now(),e=(t-this.#i)/1e3;return this.#i=t,this.#s+=e,this.#n++,this.#o+=e,this.#r++,this.#o>1&&(this.#a=this.#r,this.#o=0,this.#r=0),e}isRunning(){return this.#t}isStopped(){return!this.#t}count(){return this.#n}averageDelta(){const t=null!==this.#a?1/this.#a:this.#o/this.#r;return Math.min(1,t)}fps(){return null!==this.#a?this.#a:this.#r/this.#o}}class f{constructor(t=0,e=0,s=0){"object"==typeof t?(this.x=t.x,this.y=t.y,this.z=t.z):(this.x=t,this.y=e,this.z=s)}set(t,e,s){return"object"==typeof t?this.copy(t):(this.x=t,this.y=e,this.z=s,this)}setScalar(t){return this.x=t,this.y=t,this.z=t,this}clone(){return new f(this.x,this.y,this.z)}copy(t,e,s){return"object"==typeof t?(this.x=t.x,this.y=t.y,this.z=t.z):(this.x=t,this.y=e,this.z=s),this}add(t,e,s){return"object"==typeof t?(this.x+=t.x,this.y+=t.y,this.z+=t.z):(this.x+=t,this.y+=e,this.z+=s),this}addScalar(t){return this.x+=t,this.y+=t,this.z+=t,this}addVectors(t,e){return this.x=t.x+e.x,this.y=t.y+e.y,this.z=t.z+e.z,this}addScaledVector(t,e){return this.x+=t.x*e,this.y+=t.y*e,this.z+=t.z*e,this}sub(t,e,s){return"object"==typeof t?(this.x-=t.x,this.y-=t.y,this.z-=t.z):(this.x-=t,this.y-=e,this.z-=s),this}subScalar(t){return this.x-=t,this.y-=t,this.z-=t,this}subVectors(t,e){return this.x=t.x-e.x,this.y=t.y-e.y,this.z=t.z-e.z,this}multiply(t,e,s){return"object"==typeof t?(this.x*=t.x,this.y*=t.y,this.z*=t.z):(this.x*=t,this.y*=e,this.z*=s),this}multiplyScalar(t){return this.x*=t,this.y*=t,this.z*=t,this}divide(t,e){return"object"==typeof t?(this.x/=t.x,this.y/=t.y,this.z/=t.z):(this.x/=t,this.y/=e,this.z/=z),this}divideScalar(t){return this.multiplyScalar(1/t)}min(t){return this.x=Math.min(this.x,t.x),this.y=Math.min(this.y,t.y),this.z=Math.min(this.z,t.z),this}max(t){return this.x=Math.max(this.x,t.x),this.y=Math.max(this.y,t.y),this.z=Math.max(this.z,t.z),this}clamp(t,e){return t.x<e.x?this.x=Math.max(t.x,Math.min(e.x,this.x)):this.x=Math.max(e.x,Math.min(t.x,this.x)),t.y<e.y?this.y=Math.max(t.y,Math.min(e.y,this.y)):this.y=Math.max(e.y,Math.min(t.y,this.y)),t.z<e.z?this.z=Math.max(t.z,Math.min(e.z,this.z)):this.z=Math.max(e.z,Math.min(t.z,this.z)),this}clampScalar(t,e){return this.x=Math.max(t,Math.min(e,this.x)),this.y=Math.max(t,Math.min(e,this.y)),this.z=Math.max(t,Math.min(e,this.z)),this}clampLength(t,e){const s=this.length();return this.divideScalar(s||1).multiplyScalar(Math.max(t,Math.min(e,s)))}floor(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this.z=Math.floor(this.z),this}ceil(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this.z=Math.ceil(this.z),this}round(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this.z=Math.round(this.z),this}negate(){return this.x=-this.x,this.y=-this.y,this.z=-this.z,this}abs(){return this.x=Math.abs(this.x),this.y=Math.abs(this.y),this.z=Math.abs(this.z),this}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}cross(t){return this.crossVectors(this,t)}crossVectors(t,e){const s=t.x,i=t.y,n=t.z,o=e.x,r=e.y,a=e.z;return this.x=i*a-n*r,this.y=n*o-s*a,this.z=s*r-i*o,this}length(){return Math.sqrt(this.lengthSq())}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z}manhattanLength(){return Math.abs(this.x)+Math.abs(this.y)+Math.abs(this.z)}normalize(){return this.divideScalar(this.length()||1)}angle(t){g.copy(this).normalize(),x.copy(t).normalize();const e=g.dot(x);return e>1?0:e<-1?Math.PI:Math.acos(e)}distanceTo(t){return Math.sqrt(this.distanceToSquared(t))}distanceToSquared(t){const e=this.x-t.x,s=this.y-t.y,i=this.z-t.z;return e*e+s*s+i*i}manhattanDistanceTo(t){return Math.abs(this.x-t.x)+Math.abs(this.y-t.y)+Math.abs(this.z-t.z)}lerp(t,e){return this.lerpVectors(this,t,e)}lerpVectors(t,e,s){return this.x=t.x+(e.x-t.x)*s,this.y=t.y+(e.y-t.y)*s,this.z=t.z+(e.z-t.z)*s,this}applyMatrix3(t){return this.x=this.x*t[0]+this.y*t[3]+this.z*t[6],this.y=this.x*t[1]+this.y*t[4]+this.z*t[7],this.z=this.x*t[2]+this.y*t[5]+this.z*t[8],this}applyMatrix4(t){const e=t.m;if(!e)return this;let s=this.x,i=this.y,n=this.z,o=e[3]*s+e[7]*i+e[11]*n+e[15]||1;return this.x=(e[0]*s+e[4]*i+e[8]*n+e[12])/o,this.y=(e[1]*s+e[5]*i+e[9]*n+e[13])/o,this.z=(e[2]*s+e[6]*i+e[10]*n+e[14])/o,this}scaleRotateMatrix4(t){const e=t.m;if(!e)return this;let s=this.x,i=this.y,n=this.z,o=e[3]*s+e[7]*i+e[11]*n+e[15]||1;return this.x=(e[0]*s+e[4]*i+e[8]*n)/o,this.y=(e[1]*s+e[5]*i+e[9]*n)/o,this.z=(e[2]*s+e[6]*i+e[10]*n)/o,this}applyQuaternion(t){let e=this.x,s=this.y,i=this.z,n=t[0],o=t[1],r=t[2],a=o*i-r*s,h=r*e-n*i,l=n*s-o*e,c=o*l-r*h,d=r*a-n*l,u=n*h-o*a,p=2*t[3];return a*=p,h*=p,l*=p,c*=2,d*=2,u*=2,this.x=e+a+c,this.y=s+h+d,this.z=i+l+u,this}transformDirection(t){const e=this.x,s=this.y,i=this.z;return this.x=t[0]*e+t[4]*s+t[8]*i,this.y=t[1]*e+t[5]*s+t[9]*i,this.z=t[2]*e+t[6]*s+t[10]*i,this.normalize()}calculateNormal(t=new f,e,s,i){return g.subVectors(e,s),t.subVectors(s,i),t.cross(g),t.normalize(),t}equals(t){return t.x===this.x&&t.y===this.y&&t.z===this.z}fuzzyEquals(t,e=.001){return!1!==b(this.x,t.x,e)&&(!1!==b(this.y,t.y,e)&&!1!==b(this.z,t.z,e))}random(){this.x=Math.random(),this.y=Math.random(),this.z=Math.random()}log(t=""){return""!==t&&(t+=" - "),console.log(`${t}X: ${this.x}, Y: ${this.y}, Z: ${this.z}`),this}toArray(){return[this.x,this.y,this.z]}fromArray(t,e=0){return this.set(t[e+0],t[e+1],t[e+2]),this}}const g=new f,x=new f;function b(t,e,s=.001){return t<e+s&&t>e-s}const w=["00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f","10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f","20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f","30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f","40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f","50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f","60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f","70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f","80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f","90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f","a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af","b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf","c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf","d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df","e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef","f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"],v=new f,M=new f,S=new f;class T{static radiansToDegrees(t){return t*(180/Math.PI)}static degreesToRadians(t){return Math.PI/180*t}static equalizeAngle0to360(t,e=!0){let s=e?t:T.radiansToDegrees(t);for(;s<0;)s+=360;for(;s>=360;)s-=360;return e?s:T.degreesToRadians(s)}static clamp(t,e,s){return(t=Number(t))<e&&(t=e),t>s&&(t=s),t}static roundTo(t,e=0){const s=Math.pow(10,e);return Math.round(t*s)/s}static damp(t,e,s,i){return T.lerp(t,e,1-Math.exp(-s*i))}static lerp(t,e,s){return(1-s)*t+s*e}static smoothstep(t,e,s){return s=3*s*s-2*s*s*s,t+(e-t)*(s=Math.min(1,Math.max(0,s)))}static smootherstep(t,e,s){return s=s*s*s*(s*(6*s-15)+10),t+(e-t)*(s=Math.min(1,Math.max(0,s)))}static fuzzyFloat(t,e,s=.001){return t<e+s&&t>e-s}static fuzzyVector(t,e,s=.001){return!1!==T.fuzzyFloat(t.x,e.x,s)&&(!1!==T.fuzzyFloat(t.y,e.y,s)&&((!("z"in t)||!("z"in e)||!1!==T.fuzzyFloat(t.z,e.z,s))&&(!("w"in t)||!("w"in e)||!1!==T.fuzzyFloat(t.w,e.w,s))))}static isPowerOfTwo(t){return!(t&t-1)&&0!==t}static addCommas(t){return t.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1,")}static countDecimals(t){return Math.floor(t.valueOf())===t.valueOf()?0:t.toString().split(".")[1].length||0}static isNumber(t){return null!=t&&"number"==typeof t&&Number.isFinite(t)}static noZero(t,e=1e-5){return e=Math.abs(e),(t=T.sanity(t))>=0&&t<e&&(t=e),t<0&&t>-1*e&&(t=-1*e),t}static sanity(t){return T.isNumber(t)?t:0}static lineCollision(t,e,s,i,n,o,r,a){let h=(a-o)*(s-t)-(r-n)*(i-e);if(T.fuzzyFloat(h,0,1e-7))return!1;let l=((r-n)*(e-o)-(a-o)*(t-n))/h,c=((s-t)*(e-o)-(i-e)*(t-n))/h;return l>=0&&l<=1&&c>=0&&c<=1}static lineRectCollision(t,e,s,i,n,o,r,a){const h=T.lineCollision(t,e,s,i,n,o,n,a),l=T.lineCollision(t,e,s,i,r,o,r,a),c=T.lineCollision(t,e,s,i,n,o,r,o),d=T.lineCollision(t,e,s,i,n,a,r,a);return h||l||c||d}static triangleArea(t,e,s){return v.subVectors(s,e),M.subVectors(t,e),S.crossVectors(v,M),.5*S.length()}static randomFloat(t,e){return t+Math.random()*(e-t)}static randomInt(t=0,e=1){return t+Math.floor(Math.random()*(e-t))}static randomUUID(){if(window.crypto&&window.crypto.randomUUID)return crypto.randomUUID();const t=4294967295*Math.random()|0,e=4294967295*Math.random()|0,s=4294967295*Math.random()|0,i=4294967295*Math.random()|0;return(w[255&t]+w[t>>8&255]+w[t>>16&255]+w[t>>24&255]+"-"+w[255&e]+w[e>>8&255]+"-"+w[e>>16&15|64]+w[e>>24&255]+"-"+w[63&s|128]+w[s>>8&255]+"-"+w[s>>16&255]+w[s>>24&255]+w[255&i]+w[i>>8&255]+w[i>>16&255]+w[i>>24&255]).toLowerCase()}static toUUIDArray(...t){t.length>0&&Array.isArray(t[0])&&(t=t[0]);const e=[];for(const s of t)"object"==typeof s&&s.uuid&&e.push(s.uuid);return e}}class C{static isObject(t){return t&&"object"==typeof t&&!Array.isArray(t)}static save(t,e){try{const s=document.createElement("a");s.href=t,s.download=e||"data.json",s.click(),setTimeout((function(){window.URL.revokeObjectURL(t)}),0)}catch(t){return console.warn(t)}}static saveBuffer(t,e,s={type:"application/octet-stream"}){const i=URL.createObjectURL(new Blob([t],{type:s}));C.save(i,e)}static saveImage(t,e){C.save(t,e)}static saveString(t,e){const s=URL.createObjectURL(new Blob([t],{type:"text/plain"}));C.save(s,e)}static detectOS(){const t={Android:["android"],iOS:["iphone","ipad","ipod","ios"],Linux:["linux","x11","wayland"],MacOS:["mac","darwin","osx","os x"],Windows:["win"]},e=window.navigator.userAgent,s=window.navigator.userAgentData,i=(s?s.platform:e).toLowerCase();for(const e in t)for(const s of t[e])if(-1!==i.indexOf(s))return e;return"Unknown OS"}static fullscreen(t){if(document.fullscreenElement||document.mozFullScreenElement||document.webkitFullscreenElement||document.msFullscreenElement){const t=document;(t.cancelFullScreen||t.exitFullscreen||t.webkitCancelFullScreen||t.webkitExitFullscreen||t.mozCancelFullScreen).call(t)}else{const e=t??document.body;(e.requestFullScreen||e.webkitRequestFullScreen||e.mozRequestFullScreen||e.msRequestFullScreen).call(e)}}static metaKeyOS(){return"Mac"===C.detectOS()?"⌘":"⌃"}static sleep(t){const e=performance.now();let s=e;for(;s-e<t;)s=performance.now()}static waitForObject(t="",e,s,i=100,n=-1,o=5e3){let r=performance.now(),a=performance.now();!function h(){n>0&&performance.now()-r>n?console.info(`Operation: ${t} timed out`):(o>0&&performance.now()-a>o&&(console.info(`Still waiting on operation: ${t}`),a=performance.now()),e&&"function"==typeof e&&!e()?setTimeout(h,i):s&&"function"==typeof s&&s())}()}}const E=new Map;class P{constructor(t="Thing"){this.isThing=!0,this.type="Thing",this.name=t,this.uuid=T.randomUUID()}clone(t=!1){return(new this.constructor).copy(this,t)}copy(t,e=!0){return this.dispose(),this.name=t.name,this}toJSON(){const e={};return e.meta={type:this.type,version:t},e.name=this.name,e.uuid=this.uuid,e}fromJSON(t){return C.isObject(t)?(void 0!==t.name&&(this.name=t.name),void 0!==t.uuid&&(this.uuid=t.uuid),this):(console.warn(`Thing.fromJSON(): No json data provided for ${this.constructor.name}`),this)}static register(t,e){E.set(t,e)}static type(t){return E.get(t)}}P.register("Thing",P);const k={};class D{static defaultValue(t){switch(t){case"select":case"asset":case"divider":return null;case"number":case"int":case"angle":case"slider":return 0;case"variable":return[0,0];case"vector":return[0];case"option":return[!1];case"boolean":return!1;case"color":return 16777215;case"string":case"key":return"";case"object":return{};default:console.warn(`ComponentManager.defaultValue(): Unknown property type: '${t}'`)}return null}static registered(t=""){const e=k[t];return e||console.warn(`ComponentManager.registered(): Component '${t}' not registered'`),e}static registeredTypes(){return Object.keys(k)}static register(t="",e){if(t=t.toLowerCase(),k[t])return console.warn(`ComponentManager.register(): Component '${t}' already registered`);C.isObject(e.config)||(e.config={}),C.isObject(e.config.schema)||(e.config.schema={});const s=e.config.schema;for(const t in s){const e=Array.isArray(s[t])?s[t]:[s[t]];for(const t of e){if(void 0===t.type)console.warn("ComponentManager.register(): All schema properties require a 'type' value");else if("divider"===t.type)continue;void 0===t.default&&(t.default=D.defaultValue(t.type)),void 0!==t.proMode&&(t.promode=t.proMode)}}k[t]=class extends e{constructor(){super(),this.isComponent=!0,this.type=t,this.attached=!0,this.expanded=!0,this.order=0,this.tag="",this.entity=null,this.backend=void 0,this.data={}}init(t={}){this.dispose(),"function"==typeof super.init&&super.init(t)}dispose(){"function"==typeof super.dispose&&super.dispose(),"object"==typeof this.backend&&"function"==typeof this.backend.dispose&&this.backend.dispose(),this.backend=void 0}attach(){this.attached=!0,"function"==typeof super.attach&&super.attach()}detach(){this.attached=!1,"function"==typeof super.detach&&super.detach()}defaultData(){const t={};for(let e=0,s=arguments.length;e<s;e+=2)t[arguments[e]]=arguments[e+1];return D.sanitizeData(this.type,t),t.base={isComponent:!0,attached:this.attached,expanded:this.expanded,order:this.order,tag:this.tag,type:this.type},t}toJSON(){let t;t=this.data&&this.data.style?this.defaultData("style",this.data.style):this.defaultData();for(const e in t)void 0!==this.data[e]&&(this.data[e]&&this.data[e].isTexture?t[e]=this.data[e].uuid:t[e]=structuredClone(this.data[e]));return t}}}static includeData(t,e,s=void 0){for(const i in t.if){const n=Array.isArray(t.if[i])?t.if[i]:[t.if[i]];let o=!1,r=!1;for(const t of n)o=o||e[i]===t,r=!(!r&&void 0!==s)||s[i]===t;if(!o||!r)return!1}for(const i in t.not){const n=Array.isArray(t.not[i])?t.not[i]:[t.not[i]];let o=!1,r=!1;for(const t of n)o=o||e[i]===t,r=!r&&void 0!==s&&s[i]===t;if(o||r)return!1}return!0}static sanitizeData(t,e){e&&"object"==typeof e||(e={});const s=D.registered(t);if(!s||!s.config||!s.config.schema)return;const i=s.config.schema;if(C.isObject(i))for(const t in i){const s=Array.isArray(i[t])?i[t]:[i[t]];let n;for(const t of s)if("divider"!==t.type&&D.includeData(t,e)){n=t;break}if(void 0!==n){if(void 0===e[t]&&(Array.isArray(n.default)?e[t]=[...n.default]:"object"==typeof n.default?e[t]=structuredClone(n.default):e[t]=n.default),T.isNumber(e[t])){const s=n.min??-1/0,i=n.max??1/0;e[t]<s&&(e[t]=s),e[t]>i&&(e[t]=i)}}else delete e[t]}}static stripData(t,e,s){const i=D.registered(t);if(!i||!i.config||!i.config.schema)return;const n=i.config.schema;if(C.isObject(n))for(const t in n){let i=!1;const o=Array.isArray(n[t])?n[t]:[n[t]];for(const t of o)if("divider"!==t.type&&D.includeData(t,e,s)){i=!0;break}!0!==i&&delete s[t]}}}class B{constructor(t=0,e=0){"object"==typeof t?(this.x=t.x,this.y=t.y):(this.x=t,this.y=e)}set(t,e){return"object"==typeof t?this.copy(t):(this.x=t,this.y=e,this)}setScalar(t){return this.x=t,this.y=t,this}clone(){return new B(this.x,this.y)}copy(t,e){return"object"==typeof t?(this.x=t.x,this.y=t.y):(this.x=t,this.y=e),this}add(t,e){return"object"==typeof t?(this.x+=t.x,this.y+=t.y):(this.x+=t,this.y+=e),this}addScalar(t){return this.x+=t,this.y+=t,this}addVectors(t,e){return this.x=t.x+e.x,this.y=t.y+e.y,this}addScaledVector(t,e){return this.x+=t.x*e,this.y+=t.y*e,this}sub(t,e){return"object"==typeof t?(this.x-=t.x,this.y-=t.y):(this.x-=t,this.y-=e),this}subScalar(t){return this.x-=t,this.y-=t,this}subVectors(t,e){return this.x=t.x-e.x,this.y=t.y-e.y,this}multiply(t,e){return"object"==typeof t?(this.x*=t.x,this.y*=t.y):(this.x*=t,this.y*=e),this}multiplyScalar(t){return this.x*=t,this.y*=t,this}divide(t,e){return"object"==typeof t?(this.x/=t.x,this.y/=t.y):(this.x/=t,this.y/=e),this}divideScalar(t){return this.multiplyScalar(1/t)}min(t){return this.x=Math.min(this.x,t.x),this.y=Math.min(this.y,t.y),this}max(t){return this.x=Math.max(this.x,t.x),this.y=Math.max(this.y,t.y),this}clamp(t,e){return t.x<e.x?this.x=Math.max(t.x,Math.min(e.x,this.x)):this.x=Math.max(e.x,Math.min(t.x,this.x)),t.y<e.y?this.y=Math.max(t.y,Math.min(e.y,this.y)):this.y=Math.max(e.y,Math.min(t.y,this.y)),this}clampScalar(t,e){return this.x=Math.max(t,Math.min(e,this.x)),this.y=Math.max(t,Math.min(e,this.y)),this}clampLength(t,e){const s=this.length();return this.divideScalar(s||1).multiplyScalar(Math.max(t,Math.min(e,s)))}floor(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this}ceil(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this}round(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this}negate(){return this.x=-this.x,this.y=-this.y,this}abs(){return this.x=Math.abs(this.x),this.y=Math.abs(this.y),this}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}lengthSq(){return this.x*this.x+this.y*this.y}manhattanLength(){return Math.abs(this.x)+Math.abs(this.y)}normalize(){return this.divideScalar(this.length()||1)}angle(t){let e=Math.atan2(this.y,this.x);return t&&e<0&&(e+=2*Math.PI),e}angleBetween(t){const e=this.length()*t.length(),s=this.dot(t)/e,i=Math.min(Math.max(s,-1),1);return Math.acos(i)}distanceTo(t){return Math.sqrt(this.distanceToSquared(t))}distanceToSquared(t){const e=this.x-t.x,s=this.y-t.y;return e*e+s*s}manhattanDistanceTo(t){return Math.abs(this.x-t.x)+Math.abs(this.y-t.y)}setLength(t){return this.normalize().multiplyScalar(t)}lerp(t,e){return this.lerpVectors(this,t,e)}lerpVectors(t,e,s){return this.x=t.x*(1-s)+e.x*s,this.y=t.y*(1-s)+e.y*s,this}smoothstep(t,e){return e=3*e*e-2*e*e*e,e=Math.min(1,Math.max(0,e)),this.x=this.x+(t.x-this.x)*e,this.y=this.y+(t.y-this.y)*e,this}equals(t){return t.x===this.x&&t.y===this.y}fuzzyEquals(t,e=.001){return!1!==j(this.x,t.x,e)&&!1!==j(this.y,t.y,e)}random(){this.x=Math.random(),this.y=Math.random()}log(t=""){return""!==t&&(t+=" - "),console.log(`${t}X: ${this.x}, Y: ${this.y}`),this}toArray(){return[this.x,this.y]}fromArray(t,e=0){return this.set(t[e+0],t[e+1]),this}}function j(t,e,s=.001){return t<e+s&&t>e-s}const I=new B,A=new B;class O{constructor(t,e){this.min=new B(1/0,1/0),this.max=new B(-1/0,-1/0),"object"==typeof t&&this.min.copy(t),"object"==typeof e&&this.max.copy(e)}set(t,e){return this.min.copy(t),this.max.copy(e),this}setFromPoints(...t){this.clear(),t.length>0&&Array.isArray(t[0])&&(t=t[0]);for(const e of t)this.expandByPoint(e);return this}setFromCenterAndSize(t,e){return A.copy(e).multiplyScalar(.5),this.min.copy(t).sub(A),this.max.copy(t).add(A),this}clone(){return(new O).copy(this)}copy(t){return this.min.copy(t.min),this.max.copy(t.max),this}clear(){this.min.set(1/0,1/0),this.max.set(-1/0,-1/0)}isEmpty(){return this.max.x<this.min.x||this.max.y<this.min.y}getCenter(t=new B){return this.isEmpty()?t.set(0,0):t.addVectors(this.min,this.max).multiplyScalar(.5),t}getSize(t=new B){return this.isEmpty()?t.set(0,0):t.subVectors(this.max,this.min).abs(),t}expandByPoint(t){return this.min.min(t),this.max.max(t),this}expandByVector(t,e){let s,i;return"object"==typeof t?(s=t.x/2,i=t.y/2):(s=t/2,i=e/2),this.min.sub(s,i),this.max.add(s,i),this}expandByScalar(t){return this.min.addScalar(-1*t),this.max.addScalar(1*t),this}multiply(t,e){return"object"==typeof t&&(e=t.y,t=t.x),this.min.multiply(t,e),this.max.multiply(t,e),this}containsPoint(t){return!(t.x<this.min.x||t.x>this.max.x||t.y<this.min.y||t.y>this.max.y)}containsBox(t){return this.min.x<=t.min.x&&t.max.x<=this.max.x&&this.min.y<=t.min.y&&t.max.y<=this.max.y}intersectsBox(t){return!(t.max.x<this.min.x||t.min.x>this.max.x||t.max.y<this.min.y||t.min.y>this.max.y)}distanceToPoint(t){return I.copy(t).clamp(this.min,this.max).sub(t),I.length()}intersect(t){return this.min.max(t.min),this.max.min(t.max),this}union(t){return this.min.min(t.min),this.max.max(t.max),this}translate(t,e){return this.min.add(t,e),this.max.add(t,e),this}equals(t){return t.min.equals(this.min)&&t.max.equals(this.max)}toArray(){return[this.min.x,this.min.y,this.max.x,this.max.y]}fromArray(t,e=0){return this.min.set(t[e+0],t[e+1]),this.max.set(t[e+2],t[e+3]),this}}class N{constructor(...t){this.m=[1,0,0,1,0,0],t&&t.length>0&&(Array.isArray(t[0])?this.set(...t[0]):this.set(...t))}set(t,e,s,i,n,o){return this.m[0]=t,this.m[1]=e,this.m[2]=s,this.m[3]=i,this.m[4]=n,this.m[5]=o,this}copy(t){return t&&t instanceof N&&this.set(...t.m),this}clone(){return new N(...this.m)}identity(){return this.set(1,0,0,1,0,0)}multiply(t){const e=this.m[0]*t.m[0]+this.m[2]*t.m[1],s=this.m[1]*t.m[0]+this.m[3]*t.m[1],i=this.m[0]*t.m[2]+this.m[2]*t.m[3],n=this.m[1]*t.m[2]+this.m[3]*t.m[3],o=this.m[0]*t.m[4]+this.m[2]*t.m[5]+this.m[4],r=this.m[1]*t.m[4]+this.m[3]*t.m[5]+this.m[5];return this.set(e,s,i,n,o,r)}premultiply(t){const e=t.m[0]*this.m[0]+t.m[2]*this.m[1],s=t.m[1]*this.m[0]+t.m[3]*this.m[1],i=t.m[0]*this.m[2]+t.m[2]*this.m[3],n=t.m[1]*this.m[2]+t.m[3]*this.m[3],o=t.m[0]*this.m[4]+t.m[2]*this.m[5]+t.m[4],r=t.m[1]*this.m[4]+t.m[3]*this.m[5]+t.m[5];return this.set(e,s,i,n,o,r)}compose(t,e,s,i,n){if(this.identity(),this.multiply(L.set(1,0,0,1,t,e)),0!==n){const t=Math.cos(n),e=Math.sin(n);this.multiply(U.set(t,e,-e,t,0,0))}return 1===s&&1===i||this.scale(s,i),this}decompose(t){return t&&"object"==typeof t?(t.position&&this.getPosition(t.position),t.rotation=this.getRotation(),t.scale&&this.getScale(t.scale),this):this}setPosition(t,e){return this.m[4]=t,this.m[5]=e,this}translate(t,e){return"object"==typeof t?(this.m[4]+=this.m[0]*t.x+this.m[2]*t.y,this.m[5]+=this.m[1]*t.x+this.m[3]*t.y):(this.m[4]+=this.m[0]*t+this.m[2]*e,this.m[5]+=this.m[1]*t+this.m[3]*e),this}rotate(t){const e=Math.cos(t),s=Math.sin(t),i=this.m[0]*e+this.m[2]*s,n=this.m[1]*e+this.m[3]*s,o=this.m[0]*-s+this.m[2]*e,r=this.m[1]*-s+this.m[3]*e;return this.m[0]=i,this.m[1]=n,this.m[2]=o,this.m[3]=r,this}scale(t,e){return"object"==typeof t?(this.m[0]*=t.x,this.m[1]*=t.x,this.m[2]*=t.y,this.m[3]*=t.y):(null==e&&(e=t),this.m[0]*=t,this.m[1]*=t,this.m[2]*=e,this.m[3]*=e),this}skew(t,e){return this.multiply(W.set(1,Math.tan(e),Math.tan(t),1,0,0))}getScale(t=new B){const e=Math.sqrt(this.m[0]*this.m[0]+this.m[1]*this.m[1]),s=Math.sqrt(this.m[2]*this.m[2]+this.m[3]*this.m[3]);return t.set(e,s),t}getPosition(t=new B){return t.set(this.m[4],this.m[5]),t}getRotation(){return Math.atan2(this.m[1],this.m[0])}getSkew(t=new B){const e=Math.sqrt(this.m[0]*this.m[0]+this.m[1]*this.m[1]),s=Math.sqrt(this.m[2]*this.m[2]+this.m[3]*this.m[3]),i=Math.atan2(-this.m[2]/s,this.m[0]/e),n=Math.atan2(this.m[1]/e,this.m[3]/s);return t.set(i,n),t}getSign(t=new B){const e=this.m[0]<0?-1:1,s=this.m[3]<0?-1:1;return t.set(e,s),t}determinant(){return this.m[0]*this.m[3]-this.m[1]*this.m[2]}getInverse(t=new N){const e=this.determinant();0===e&&console.error("Matrix2.getInverse(): Matrix is non-invertible");const s=1/e,i=this.m[3]*s,n=-this.m[1]*s,o=-this.m[2]*s,r=this.m[0]*s,a=s*(this.m[2]*this.m[5]-this.m[3]*this.m[4]),h=s*(this.m[1]*this.m[4]-this.m[0]*this.m[5]);return t.set(i,n,o,r,a,h)}applyToVector(t){t||console.warn("Matrix2.applyToVector(): Missing vector target");const e=t.x*this.m[0]+t.y*this.m[2]+this.m[4],s=t.x*this.m[1]+t.y*this.m[3]+this.m[5];return t.set(e,s)}transformPoint(t,e){let s,i;return"object"==typeof t?(s=t.x*this.m[0]+t.y*this.m[2]+this.m[4],i=t.x*this.m[1]+t.y*this.m[3]+this.m[5]):(s=t*this.m[0]+e*this.m[2]+this.m[4],i=t*this.m[1]+e*this.m[3]+this.m[5]),new B(s,i)}setContextTransform(t){return t.setTransform(1,0,0,1,0,0),this.transformContext(t),this}transformContext(t){return t.transform(this.m[0],-this.m[1],-this.m[2],this.m[3],this.m[4],-this.m[5]),this}cssTransform(){return`matrix(${this.m[0]}, ${-this.m[1]}, ${-this.m[2]}, ${this.m[3]}, ${this.m[4]}, ${-this.m[5]}`}toArray(){return[...this.m]}fromArray(t,e=0){return this.set(t[e+0],t[e+1],t[e+2],t[e+3],t[e+4],t[e+5]),this}}const L=new N,U=new N,W=new N;class F{static DOWN=-1;static UP=1;static RESET=0;constructor(){this.pressed=!1,this.justPressed=!1,this.justReleased=!1}update(t){this.justPressed=!1,this.justReleased=!1,t===F.DOWN?(!1===this.pressed&&(this.justPressed=!0),this.pressed=!0):t===F.UP?(this.pressed&&(this.justReleased=!0),this.pressed=!1):t===F.RESET&&(this.justReleased=!1,this.justPressed=!1)}set(t,e,s){this.justPressed=t,this.pressed=e,this.justReleased=s}reset(){this.justPressed=!1,this.pressed=!1,this.justReleased=!1}}class R{static LEFT=0;static MIDDLE=1;static RIGHT=2;static BACK=3;static FORWARD=4;#h=!1;#l=1;constructor(t,e=!0){if(!t||!t.dom)return void console.error("Pointer: No element was provided");const s=this;this._keys=new Array(5),this._location=new B(0,0),this._locationUpdated=!1,this._delta=new B(0,0),this._wheel=0,this._wheelUpdated=!1,this._doubleClicked=new Array(5),this._clickTime=new Array(5),this.keys=new Array(5),this.position=new B(0,0),this.location=new B(0,0),this.delta=new B(0,0),this.wheel=0,this.doubleClicked=new Array(5),this.downAt=new B(0,0),this.pointerInside=!1,this.dragging=!1;for(let t=0;t<5;t++)this._doubleClicked[t]=!1,this.doubleClicked[t]=!1,this._keys[t]=new F,this.keys[t]=new F;function i(t,e){t>=0&&s._keys[t].update(e)}e&&t.on("contextmenu",(t=>{t.preventDefault(),t.stopPropagation()})),t.on("pointermove",(e=>{!function(e,i){if(t&&t.dom){const s=t.dom.getBoundingClientRect();e-=s.left,i-=s.top}const n=e-s._location.x,o=i-s._location.y;s._delta.x+=n,s._delta.y+=o,s._location.set(e,i),s._locationUpdated=!0,s.position.set(e,-i)}(e.clientX,e.clientY)})),t.on("pointerdown",(e=>{t.dom.setPointerCapture(e.pointerId),i(e.button,F.DOWN),s._clickTime[e.button]=performance.now()})),t.on("pointerup",(e=>{t.dom.releasePointerCapture(e.pointerId),i(e.button,F.UP)})),t.on("pointerenter",(()=>{s.pointerInside=!0})),t.on("pointerleave",(()=>{s.pointerInside=!1})),t.on("wheel",(t=>{s._wheel=t.deltaY,s._wheelUpdated=!0})),t.on("dragstart",(t=>{i(t.button,F.UP)})),t.on("dblclick",(t=>{s._doubleClicked[t.button]=performance.now()-s._clickTime[t.button]<500}))}buttonPressed(t,e=-1){return(!this.#h||this.#h===e)&&this.keys[t].pressed}buttonDoubleClicked(t,e=-1){return(!this.#h||this.#h===e)&&this.doubleClicked[t]}buttonJustPressed(t,e=-1){return(!this.#h||this.#h===e)&&this.keys[t].justPressed}buttonJustReleased(t,e=-1){return(!this.#h||this.#h===e)&&this.keys[t].justReleased}insideDom(){return this.pointerInside}lock(){return this.#h=this.#l,this.#l++,this.#h}unlock(){this.#h=!1}update(){for(let t=0;t<5;t++)this._keys[t].justPressed&&this.keys[t].justPressed&&(this._keys[t].justPressed=!1),this._keys[t].justReleased&&this.keys[t].justReleased&&(this._keys[t].justReleased=!1),this.keys[t].set(this._keys[t].justPressed,this._keys[t].pressed,this._keys[t].justReleased),!0===this._doubleClicked[t]?(this.doubleClicked[t]=!0,this._doubleClicked[t]=!1):this.doubleClicked[t]=!1;this._wheelUpdated?(this.wheel=this._wheel,this._wheelUpdated=!1):this.wheel=0,this._locationUpdated?(this.delta.copy(this._delta),this.location.copy(this._location),this._delta.set(0,0),this._locationUpdated=!1):(this.delta.x=0,this.delta.y=0)}}const _=new B,J=new B,V=new B,H=new B,$=new B;class Y extends P{constructor(t="Object"){super(t),this.type="Object2D",this.children=[],this.parent=null,this.visible=!0,this.opacity=1,this.layer=0,this.level=0,this.globalOpacity=1,this.position=new B(0,0),this.scale=new B(1,1),this.rotation=0,this.matrix=new N,this.globalMatrix=new N,this.inverseGlobalMatrix=new N,this.matrixAutoUpdate=!0,this.matrixNeedsUpdate=!0,this.lateUpdate=!1,this.boundingBox=new O,this.masks=[],this.pointerEvents=!0,this.draggable=!0,this.focusable=!0,this.selectable=!0,this.pointerInside=!1,this.inViewport=!0,this.isSelected=!1,this.isDragging=!1}add(...t){if(!t)return this;t.length>0&&Array.isArray(t[0])&&(t=t[0]);for(const e of t){if(!e||!e.uuid)continue;-1===this.children.indexOf(e)&&(e.parent&&e.parent.remove(e),this.children.push(e),e.parent=this,e.level=this.level+1,e.computeBoundingBox(),e.traverse((function(t){"function"==typeof t.onAdd&&t.onAdd(this),t.matrixNeedsUpdate=!0})))}return this}remove(...t){if(!t)return this;t.length>0&&Array.isArray(t[0])&&(t=t[0]);for(const e of t){if(!e||!e.uuid)continue;const t=this.children.indexOf(e);-1!==t&&(this.children.splice(t,1),e.parent=null,e.level=0,e.traverse((function(t){"function"==typeof t.onRemove&&t.onRemove(this),t.matrixNeedsUpdate=!0})))}return this}removeFromParent(){const t=this.parent;return t&&t.remove(this),this}getChildByUUID(t){return this.getEntityByProperty("uuid",t)}getChildByProperty(t,e){if(this[t]===e)return this;for(const s of this.children){const i=s.getChildByProperty(t,e);if(i)return i}}traverse(t){if("function"==typeof t&&t(this))return!0;for(const e of this.children)if(e.traverse(t))return!0;return!1}traverseVisible(t){if(!this.visible)return!1;if("function"==typeof t&&t(this))return!0;for(const e of this.children)if(e.traverseVisible(t))return!0;return!1}traverseAncestors(t){const e=this.parent;return!!e&&(!("function"!=typeof t||!t(e))||e.traverseAncestors(t))}clear(){return this.remove(...this.children)}destroy(){return this.clear(),this.removeFromParent(),this}computeBoundingBox(t){return this.boundingBox}isInside(t,e=!0){if(e)for(const e of this.children)if(e.isInside(t,!0))return!0;return!1}getWorldPointIntersections(t){const e=[];return this.traverseVisible((s=>{const i=s.worldToLocal(t);s.isInside(i)&&e.push(s)})),e.sort(((t,e)=>e.layer===t.layer?e.level-t.level:e.layer-t.layer)),e}getWorldBoundingBox(t=!0){const e=new O;if(e.union(function(t){const e=t.boundingBox;return!1===Number.isFinite(e.min.x)||!1===Number.isFinite(e.min.y)||!1===Number.isFinite(e.max.x)||!1===Number.isFinite(e.max.y)?e:(t.globalMatrix.applyToVector(J.copy(e.min.x,e.min.y)),t.globalMatrix.applyToVector(V.copy(e.min.x,e.max.y)),t.globalMatrix.applyToVector(H.copy(e.max.x,e.min.y)),t.globalMatrix.applyToVector($.copy(e.max.x,e.max.y)),(new O).setFromPoints(J,V,H,$))}(this)),t)for(const t of this.children)e.union(t.getWorldBoundingBox(!0));return e}totalBoundingBox(t=!0){const e=this.boundingBox.clone();if(t)for(const t of this.children){const s=t.boundingBox.clone();Number.isFinite(s.min.x)&&Number.isFinite(s.min.y)&&Number.isFinite(s.max.x)&&Number.isFinite(s.max.y)&&(s.translate(t.position.x,t.position.y),e.union(s))}return e}localToWorld(t){return this.globalMatrix.transformPoint(t)}worldToLocal(t){return this.inverseGlobalMatrix.transformPoint(t)}applyMatrix(t){return this.updateMatrix(!0),this.matrix.premultiply(t),this.matrix.getPosition(this.position),this.rotation=this.matrix.getRotation(),this.matrix.getScale(this.scale),this.updateMatrix(!0),this}attach(t){if(!t||!t.uuid)return this;if(-1!==this.children.indexOf(t))return this;const e=t.parent;this.updateMatrix(!0);const s=(new N).copy(this.inverseGlobalMatrix);return e&&(e.updateMatrix(!0),s.multiply(e.globalMatrix)),t.applyMatrix(s),t.removeFromParent(),this.children.push(t),t.parent=this,t.level=this.level+1,t.traverse((function(t){"function"==typeof t.onAdd&&t.onAdd(this),t.matrixNeedsUpdate=!0})),this}getWorldPosition(){return this.updateMatrix(!0),this.globalMatrix.getPosition()}getWorldRotation(){return this.updateMatrix(!0),this.globalMatrix.getRotation()}getWorldScale(){return this.updateMatrix(!0),this.globalMatrix.getScale()}setPosition(t,e){return this.position.copy(t,e),this.updateMatrix(!0),this}setRotation(t){return this.rotation=t,this.updateMatrix(!0),this}setScale(t,e){return this.scale.copy(t,e),this.updateMatrix(!0),this}updateMatrix(t=!1){if(t||this.matrixAutoUpdate||this.matrixNeedsUpdate){this.globalOpacity=this.opacity*(this.parent?this.parent.globalOpacity:1),this.scale.x=T.noZero(T.sanity(this.scale.x)),this.scale.y=T.noZero(T.sanity(this.scale.y)),this.matrix.compose(this.position.x,this.position.y,this.scale.x,this.scale.y,this.rotation),this.globalMatrix.copy(this.matrix);const t=this.ghostParent??this.parent;t&&this.globalMatrix.premultiply(t.globalMatrix),this.globalMatrix.getInverse(this.inverseGlobalMatrix),this.matrixNeedsUpdate=!1}}transform(t){this.globalMatrix.transformContext(t.context)}onPointerDrag(t){const e=t.pointer;t.camera;!e.buttonJustPressed(R.LEFT)&&this.pointerStartPosition&&this.dragStartPosition||(this.pointerStartPosition=e.position.clone(),this.dragStartPosition=this.position.clone());const s=this.pointerStartPosition.clone(),i=e.position.clone();if(e.buttonPressed(R.LEFT)){if(s.manhattanDistanceTo(i)>=2||this.isDragging){this.isDragging=!0;const e=this.ghostParent??this.parent??this,n=t.screenToWorld(s),o=e.inverseGlobalMatrix.transformPoint(n),r=t.screenToWorld(i),a=e.inverseGlobalMatrix.transformPoint(r),h=o.clone().sub(a);_.copy(this.dragStartPosition).sub(h),this.position.copy(_),this.matrixNeedsUpdate=!0}}}copy(t,e=!0){if(super.copy(t,e),this.visible=t.visible,this.opacity=t.opacity,this.layer=t.layer,this.position.copy(t.position),this.scale.copy(t.scale),this.rotation=t.rotation,this.matrixAutoUpdate=t.matrixAutoUpdate,this.lateUpdate=t.lateUpdate,this.pointerEvents=t.pointerEvents,this.draggable=t.draggable,this.focusable=t.focusable,this.selectable=t.selectable,e&&Array.isArray(t.masks))for(const e of t.masks)this.masks.push(e.clone());if(e&&Array.isArray(t.children))for(const e of t.children)this.add(e.clone());return this}toJSON(t=!0){const e=super.toJSON(t);e.visible=this.visible,e.opacity=this.opacity,e.layer=this.layer,e.position=this.position.toArray(),e.scale=this.scale.toArray(),e.rotation=this.rotation,e.matrixAutoUpdate=this.matrixAutoUpdate,e.lateUpdate=this.lateUpdate,e.pointerEvents=this.pointerEvents,e.draggable=this.draggable,e.focusable=this.focusable,e.selectable=this.selectable,e.masks=[];for(const s of this.masks)e.masks.push(s.toJSON(t));if(t)for(const s of this.children)e.children.push(s.toJSON(t));return e}fromJSON(t){if(super.fromJSON(t),void 0!==t.visible&&(this.visible=t.visible),void 0!==t.opacity&&(this.opacity=t.opacity),void 0!==t.layer&&(this.layer=t.layer),void 0!==t.position&&this.position.fromArray(t.position),void 0!==t.scale&&this.scale.fromArray(t.scale),void 0!==t.rotation&&(this.rotation=t.rotation),void 0!==t.matrixAutoUpdate&&(this.matrixAutoUpdate=t.matrixAutoUpdate),void 0!==t.lateUpdate&&(this.lateUpdate=t.lateUpdate),void 0!==t.pointerEvents&&(this.pointerEvents=t.pointerEvents),void 0!==t.draggable&&(this.draggable=t.draggable),void 0!==t.focusable&&(this.focusable=t.focusable),void 0!==t.selectable&&(this.selectable=t.selectable),t.masks)for(const e of t.masks){const t=P.type(e.type);if(t){const s=(new t).fromJSON(e);this.masks.push(s)}else console.warn(`Object2D.fromJSON(): Unknown mask type '${e.type}'`)}if(t.children)for(const e of t.children){const t=P.type(e.type);if(t){const s=(new t).fromJSON(e);this.add(s)}else console.warn(`Object2D.fromJSON(): Unknown child type '${e.type}'`)}return this}}P.register("Object2D",Y);class q extends Y{constructor(t="Entity"){super(t),this.isEntity=!0,this.type="Entity",this.category=null,this.locked=!1,this.visible=!0,this.components=[],this.children=[],this.parent=null,this.loadedDistance=0}componentFamily(){return["Entity"]}addComponent(t,e={},s=!0){const i=D.registered(t);if(void 0===i)return;const n=i.config;let o=this.getComponent(t);if(o&&!i.config.multiple||(o=new i,this.components.push(o)),n.dependencies&&s)for(const t of n.dependencies)null==this.getComponent(t)&&this.addComponent(t,{},!1);return D.sanitizeData(t,e),o.detach(),o.entity=this,o.init(e),o.attach(),o}attachComponent(t){return this.components.push(t),t.detach(),t.entity=this,t.init(t.toJSON()),t.attach(),t}updateComponent(t,e={},s=0){const i=this.getComponentsWithProperties("type",t)[s];if(!i||!i.isComponent)return;const n=i.data??{};return Object.assign(n,e),D.sanitizeData(t,n),this.detach(),this.init(n),this.attach(),i}replaceComponent(t,e={},s=0){const i=this.getComponentsWithProperties("type",t)[s];if(i&&i.isComponent)return D.sanitizeData(t,e),i.detach(),i.init(e),i.attach(),i}getComponent(t,e){if(void 0===e)return this.getComponentByProperty("type",t);const s=this.getComponentsWithProperties("type",t,"id",e);return s.length>0?s[0]:void 0}getComponentByID(t){return this.getComponentByProperty("id",t)}getComponentByType(t){return this.getComponentByProperty("type",t)}getComponentsByType(t){return this.getComponentsWithProperties("type",t)}getComponentByProperty(t,e){for(const s of this.components)if(s[t]===e)return s}getComponentsWithProperties(){const t=[];for(const e of this.components){let s=!0;for(let t=0;t<arguments.length;t+=2){const i=arguments[t+1];if(e[arguments[t]]!==i){s=!1;break}}s&&t.push(e)}return t}removeComponent(t){if(!t)return;const e=this.components.indexOf(t);if(-1!==e)return this.components.splice(e,1),t.detach(),t;console.warn(`Entity.removeComponent(): Component ${t.uuid}, type '${t.type}' not found`)}rebuildComponents(){for(const t of this.components)t.detach(),t.init(t.toJSON()),t.attach();return this}traverseComponents(t){for(const e of this.components)if("function"==typeof t&&t(e))return}addEntity(...t){for(const e of t)e&&e.isEntity&&-1===this.children.indexOf(e)&&e!==this&&(e.removeFromParent(),e.parent=this,this.children.push(e));return this}getEntities(){const t=[];for(const e of this.children)e&&e.isEntity&&t.push(e);return t}getEntityById(t){return this.getEntityByProperty("id",parseInt(t))}getEntityByName(t){return this.getEntityByProperty("name",t)}getEntityByUUID(t){return this.getEntityByProperty("uuid",t)}getEntityByProperty(t,e){if(this[t]===e)return this;for(const s of this.getEntities()){const i=s.getEntityByProperty(t,e);if(i)return i}}removeEntity(t,e=!1){if(!t)return;if(!e&&t.locked)return;const s=this.children.indexOf(t);return-1!==s&&(t.parent=null,this.children.splice(s,1)),t}traverse(t,e=!0){if("function"==typeof t&&t(this))return!0;for(const s of this.children)if(s.traverse(t,e))return!0}traverseEntities(t,e=!0){if("function"==typeof t&&t(this))return!0;for(const s of this.getEntities())if(s.traverseEntities(t,e))return!0}changeParent(t=void 0,e=-1){if(t||(t=this.parent),!t||!t.isEntity)return;const s=this.parent;return-1===e&&s&&(e=s.children.indexOf(this)),t.addEntity(this),-1!==e&&(t.children.splice(e,0,this),t.children.pop()),this}parentEntity(){let t=this;for(;t&&t.parent;){if(t.parent.isStage)return t;if(t.parent.isWorld)return t;t=t.parent}return t}parentStage(){return this.isStage||this.isWorld?this:this.parent&&this.parent.isEntity?this.parent.parentStage():null}parentWorld(){return this.isWorld?this:this.parent&&this.parent.isEntity?this.parent.parentWorld():null}removeFromParent(){const t=this.parent;return t&&t.removeEntity(this),this}copy(t,e=!0){super.copy(t,e),this.category=t.category,this.locked=t.locked,this.visible=t.visible;for(const e of t.components){this.addComponent(e.type,e.toJSON(),!1)}if(e)for(const e of t.getEntities())this.addEntity(e.clone());return this}dispose(){for(;this.components.length>0;){const t=this.components[0];this.removeComponent(t),"function"==typeof t.dispose&&t.dispose()}for(;this.children.length>0;)this.children[0].dispose();return this.removeFromParent(),this}toJSON(t=!0){const e=super.toJSON(t);e.category=this.category,e.locked=this.locked,e.visible=this.visible,e.components=[],e.children=[];for(const t of this.components)e.components.push(t.toJSON());if(t)for(const s of this.getEntities())e.children.push(s.toJSON(t));return e}fromJSON(t){if(super.fromJSON(t),void 0!==t.category&&(this.category=t.category),void 0!==t.locked&&(this.locked=t.locked),void 0!==t.visible&&(this.visible=t.visible),t.components)for(const e of t.components)if(e&&e.base&&e.base.type){this.addComponent(e.base.type,e,!1)}if(t.children)for(const e of t.children){const t=P.type(e.type);if(t){const s=(new t).fromJSON(e);this.addEntity(s)}else console.warn(`Entity.fromJSON(): Unknown child type '${e.type}'`)}return this}}P.register("Entity",q);class G extends q{constructor(t=l.WORLD_2D,e="World 1"){super(e),-1===Object.values(l).indexOf(t)&&(console.warn(`World: Invalid world type '${t}', using 'World2D`),t=l.WORLD_2D),this.isWorld=!0,this.type=t,this.activeStageUUID=null,this.loadPosition=new B,this.loadDistance=0,this.pointerEvents=!1,this.draggable=!1,this.focusable=!1,this.selectable=!1}componentFamily(){return["World",this.type]}activeStage(){return this.getStageByUUID(this.activeStageUUID)??this}setActiveStage(t){return this.activeStageUUID=null,t&&t.isEntity&&this.getStageByUUID(t.uuid)&&(this.activeStageUUID=t.uuid),this}addEntity(...t){if(super.addEntity(...t),!this.activeStageUUID){const t=this.getStages();t.length>0&&this.setActiveStage(t[0])}return this}getEntities(t=!0){const e=[];for(const s of super.getEntities())!t&&s.isStage||e.push(s);return e}getFirstStage(){const t=this.getStages();if(t.length>0)return t[0]}getStages(){const t=[];for(const e of super.getEntities())e.isStage&&t.push(e);return t}getStageByName(t){const e=this.getEntityByProperty("name",t);if(e&&e.isStage)return e}getStageByUUID(t){const e=this.getEntityByProperty("uuid",t);if(e&&e.isStage)return e}getStageByProperty(t,e){for(const s of this.getStages())if(s[t]===e)return s}traverseStages(t,e=!0){if(!("function"==typeof t&&t(this)))for(const s of this.getStages())s.traverse(t,e)}copy(t,e=!0){super.copy(t,e),this.type=t.type,this.position.copy(t.position);const s=t.getStages().indexOf(t.activeStage());return this.activeStageUUID=-1!==s?this.getStages()[s].uuid:null,this.loadPosition.copy(t.loadPosition),this.loadDistance=t.loadDistance,this}dispose(){super.dispose()}toJSON(t=!0){const e=super.toJSON(t);return e.type=this.type,e.position=JSON.stringify(this.position.toArray()),e.activeStageUUID=this.activeStageUUID,e.loadPosition=JSON.stringify(this.loadPosition.toArray()),e.loadDistance=this.loadDistance,e}fromJSON(t){return super.fromJSON(t),void 0!==t.type&&(this.type=t.type),void 0!==t.position&&this.position.copy(JSON.parse(t.position)),void 0!==t.activeStageUUID&&(this.activeStageUUID=t.activeStageUUID),void 0!==t.loadPosition&&this.loadPosition.copy(JSON.parse(t.loadPosition)),void 0!==t.loadDistance&&(this.loadDistance=t.loadDistance),this}}P.register("World2D",G),P.register("World3D",G),P.register("WorldUI",G);class Z extends P{constructor(t="My Project"){super(t),this.isProject=!0,this.type="Onsight",this.notes="",this.settings={orientation:a.PORTRAIT,preload:10,unload:10},this.worlds={},this.activeWorldUUID=null,this.startWorldUUID=null}setting(t){switch("object"!=typeof this.settings&&(this.settings={}),t){case"orientation":case"orient":return this.settings.orientation??a.PORTRAIT;case"preload":case"add":return this.settings.preload??10;case"unload":case"remove":return this.settings.unload??10}}activeWorld(){let t=this.getWorldByUUID(this.activeWorldUUID);if(!t||!t.isWorld){const e=Object.keys(this.worlds);e.length>0&&(t=this.worlds[e[0]])}return t}setActiveWorld(t){return t&&t.isWorld?(this.worlds[t.uuid]&&(this.activeWorldUUID=t.uuid),this):this}addWorld(t){return t&&t.isWorld?(-1!==Object.values(l).indexOf(t.type)?(this.worlds[t.uuid]=t,null==this.activeWorldUUID&&(this.activeWorldUUID=t.uuid)):console.error(`Project.addWorld(): Invalid world type '${t.type}'`,t),this):this}getWorldByName(t){return this.getWorldByProperty("name",t)}getWorldByUUID(t){if(t)return this.worlds[t]}getWorldByProperty(t,e){for(const s in this.worlds){const i=this.worlds[s];if(i[t]===e)return i}}removeWorld(t){t&&t.isWorld&&delete this.worlds[t.uuid]}traverseWorlds(t,e=!0){for(const s in this.worlds){const i=this.worlds[s];"function"==typeof t&&t(i),e&&i.traverseStages(t,e)}}worldCount(t){if(!t)return Object.keys(this.worlds).length;let e=0;for(const s in this.worlds)this.worlds[s].type===t&&e++;return e}findEntityByUUID(t,e=!1){let s=[];e&&(s=[...this.worlds]);for(const e of s){if(t&&e.uuid&&t===e.uuid)return e;const s=e.getEntityByProperty("uuid",t);if(s)return s}}clear(){for(const t in this.worlds){const e=this.worlds[t];this.removeWorld(e),"function"==typeof e.dispose&&e.dispose()}this.name="My Project",this.uuid=T.randomUUID(),this.activeWorldUUID=null}toJSON(){const t=super.toJSON();t.assets=p.toJSON(),t.notes=this.notes,t.settings=structuredClone(this.settings),t.worlds=[];for(const e in this.worlds){const s=this.worlds[e];t.worlds.push(s.toJSON())}return t.activeWorldUUID=this.activeWorldUUID,t.startWorldUUID=this.startWorldUUID,t}fromJSON(e,s=!0){const i=e?.meta?.type??"unknown",n=e?.meta?.version??"unknown";if(!C.isObject(e))return console.error("Project.fromJSON(): No json data provided"),this;if(!C.isObject(e.meta))return console.error("Project.fromJSON(): No meta data found within JSON data"),this;if("Onsight"!==i)return console.error(`Project.fromJSON(): Unknown project type '${i}', expected '${this.type}'`),this;n!==t&&console.warn(`Project.fromJSON(): Project saved in 'v${n}', attempting to load with 'v${t}'`),this.clear(),super.fromJSON(e),s&&p.fromJSON(e.assets),this.notes=e.notes,this.settings=structuredClone(e.settings);for(const t of e.worlds){const e=(new G).fromJSON(t);console.log(e.type),this.addWorld(e)}return this.activeWorldUUID=e.activeWorldUUID,this.startWorldUUID=e.startWorldUUID,this}}const Q=r.toString(),X={};for(const t of r)X[t]=t;const K="app,"+Q,tt=JSON.stringify(X).replace(/\"/g,"");class et{static app=void 0;static cloneChildren(t,e){for(const s of e.getEntities()){const i=s.clone(!1);if(et.loadScriptsFromComponents(i,s),s.isStage||et.cloneChildren(i,s),e.isStage&&t.isWorld){const e=t.loadDistance+Math.abs(i.position.length());i.traverse((t=>{t.loadedDistance=e}))}t.add(i);for(const t of i.components)"function"==typeof t.onLoad&&t.onLoad()}}static loadScriptsFromComponents(t,e){if(t&&t.isEntity&&e&&e.isEntity&&e.components)for(const s of e.components){if("script"!==s.type||!s.data)continue;const e=s.data.script,i=p.get(e);if(!i||!i.isScript)continue;let n=`${i.source}\n`;for(const t in s.data.variables){const e=s.data.variables[t];if(e&&"object"==typeof e)if(void 0!==e.value){let s=JSON.stringify(e.value);s=s.replace(/[.*+`'"?^${}()|[\]\\]/g,"\\$&"),n+=`let ${t} = JSON.parse('${s}');\n`}else n+=`let ${t} = undefined;\n`}n+=`return ${tt};`;const o=new Function(K,n).bind(t)(et.app);for(const e in o){if("function"!=typeof o[e])continue;const s=o[e].bind(t);et.app.addEvent(e,t,s)}}}static removeEntity(t,e){t&&t.isWorld&&e&&e.isEntity&&(et.app.dispatch("destroy",{},[e.uuid]),t.removeEntity(e,!0),"function"==typeof e