(function(h,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(h=typeof globalThis<"u"?globalThis:h||self,d(h["sparkle-engine"]={}))})(this,function(h){"use strict";var Se=Object.defineProperty;var Me=(h,d,f)=>d in h?Se(h,d,{enumerable:!0,configurable:!0,writable:!0,value:f}):h[d]=f;var i=(h,d,f)=>(Me(h,typeof d!="symbol"?d+"":d,f),f);class d{constructor(){i(this,"Matrix");i(this,"Color");i(this,"Vector2");i(this,"Rect")}register(){this.Matrix=new f(F),this.Color=new f(U),this.Vector2=new f(y),this.Rect=new f(Z)}}class f{constructor(e){i(this,"factory");i(this,"objects",[]);i(this,"maxObject",1024);this.factory=e}push(e){this.objects.length0?(t=this.objects.pop(),t.poolReset(...e)):t=new this.factory(...e),t}}const l=new d;class F{constructor(e){i(this,"element");this.element=new Float32Array(6),this.poolReset(e)}poolReset(e){e?this.setMatrix(e):this.identity()}identity(){this.setMatrix([1,0,0,1,0,0])}rotate(e){const t=this.element[0],s=this.element[1],r=this.element[2],o=this.element[3],a=Math.sin(e),c=Math.cos(e);this.element[0]=t*c+r*a,this.element[1]=s*c+o*a,this.element[2]=t*-a+r*c,this.element[3]=s*-a+o*c}scale(e,t=e){this.element[0]=this.element[0]*e,this.element[1]=this.element[1]*e,this.element[2]=this.element[2]*t,this.element[3]=this.element[3]*t}translate(e,t){this.element[4]=this.element[0]*e+this.element[2]*t+this.element[4],this.element[5]=this.element[1]*e+this.element[3]*t+this.element[5]}skew(e,t=e){const s=Math.tan(e),r=Math.tan(t),o=this.element[0],a=this.element[1],c=this.element[2],u=this.element[3];this.element[0]+=r*a,this.element[2]+=r*u,this.element[1]+=s*o,this.element[3]+=s*c}setMatrix(e){e.forEach((t,s)=>{this.element[s]=t})}apply(e,t){return[e*this.element[0]+t*this.element[2]+this.element[4],e*this.element[1]+t*this.element[3]+this.element[5]]}copy(e){this.setMatrix(e.element)}clone(){return l.Matrix.pull(this.element)}invert(){const e=this.element[0]*this.element[3]-this.element[1]*this.element[2];if(e===0)return console.error("Matrix is not invertible."),null;const t=new Float32Array(6);return t[0]=this.element[3]/e,t[1]=-this.element[1]/e,t[2]=-this.element[2]/e,t[3]=this.element[0]/e,t[4]=(this.element[2]*this.element[5]-this.element[3]*this.element[4])/e,t[5]=(this.element[1]*this.element[4]-this.element[0]*this.element[5])/e,l.Matrix.pull(t)}getRotation(){return Math.atan2(this.element[1],this.element[0])}getScale(){const e=Math.sqrt(this.element[0]*this.element[0]+this.element[1]*this.element[1]),t=Math.sqrt(this.element[2]*this.element[2]+this.element[3]*this.element[3]);return[e,t]}setAbsoluteScale(e,t=e){const s=this.getScale(),r=e/s[0],o=t/s[1];this.scale(r,o)}}class P{constructor(e,t=e){i(this,"x",0);i(this,"y",0);this.poolReset(e,t)}get magnitude(){return Math.sqrt(this.x*this.x+this.y*this.y)}set magnitude(e){const t=this.direction;this.x=Math.cos(t)*e,this.y=Math.sin(t)*e}get direction(){return Math.atan2(this.y,this.x)}set direction(e){const t=this.magnitude;this.x=Math.cos(e)*t,this.y=Math.sin(e)*t}poolReset(e,t=e){this.set(e,t)}set(e,t=e){return this.x=e,this.y=t,this}copy(e){this.set(e.x,e.y)}clone(){return l.Vector2.pull(this.x,this.y)}dot(e){return this.x*e.x+this.y*e.y}cross(e){return this.x*e.y-this.y*e.x}sub(e,t=!1){const[s,r]=[this.x-e.x,this.y-e.y];return t?l.Vector2.pull(s,r):(this.set(s,r),this)}add(e,t=!1){const[s,r]=[this.x+e.x,this.y+e.y];return t?l.Vector2.pull(s,r):(this.set(s,r),this)}mul(e,t=!1){const[s,r]=[this.x*e,this.y*e];return t?l.Vector2.pull(s,r):(this.set(s,r),this)}div(e,t=!1){const[s,r]=[this.x/e,this.y/e];return t?l.Vector2.pull(s,r):(this.set(s,r),this)}unit(e=!1){const t=this.magnitude;return this.div(t,e)}normal(e=!1){const[t,s]=[-this.y,this.x];return e?l.Vector2.pull(t,s):(this.set(t,s),this)}scale(e,t=!1){return this.mul(e,t)}static lerp(e,t,s){const r=e.x+s*(t.x-e.x),o=e.y+s*(t.y-e.y);return l.Vector2.pull(r,o)}static fromAngle(e){return new P(Math.cos(e),Math.sin(e))}}const y=P;class b{constructor(){i(this,"eventMap",{})}on(e,t){return this.eventMap[e]||(this.eventMap[e]=[]),this.eventMap[e].push(t),this}emit(e,...t){const s=this.eventMap[e];return!s||s.length===0?!1:(s.forEach(r=>{r(...t)}),!0)}off(e,t){const s=this.eventMap[e];if(s&&s.length>0){const r=s.indexOf(t);r>-1&&s.splice(r,1)}return this}once(e,t){const s=(...r)=>{this.off(e,s),t(...r)};return this.on(e,s)}}class E{constructor(e={}){i(this,"engine");i(this,"renderer");i(this,"parent");i(this,"children",[]);i(this,"tag");i(this,"pool");i(this,"resident");i(this,"event");i(this,"inTree",!1);i(this,"destroyed",!1);i(this,"isReady",!1);i(this,"listened",[]);this.engine=e.engine??window.sparkleEngine,this.resident=e.resident??!1,this.resident&&this.engine.addResident(this),this.renderer=this.engine.renderer,this.pool=l,this.tag=e.tags?new Set(e.tags):new Set,this.event=new b}get root(){return this.engine.root}onEvent(e,t,s){const r=e.event;this.inTree&&r.on(t,s),this.listened.push({emitter:r,eventName:t,func:s})}offEvent(e,t,s){const r=e.event;this.inTree&&r.off(t,s),this.listened=this.listened.filter(o=>!(o.emitter==r&&o.eventName==t))}waitEvent(e,t){const s=e.event;return new Promise(r=>{s.once(t,r)})}addChild(e){return e.setParent(this),e}setParent(e){if(this.parent&&this.parent.removeChild(this),this.parent=e,e)e.children.push(this),this.enterTree();else throw new Error("can't set parent to null")}removeFromParent(){this.parent&&this.parent.removeChild(this)}removeChild(e){const t=this.children.indexOf(e);t>=0&&(e.exitTree(),e.parent=void 0,this.children.splice(t,1))}setParentIndex(e){if(this.parent){const t=this.parent.children,s=t.indexOf(this);s>=0&&s!==e&&(t.splice(s,1),t.splice(e,0,this))}}isParent(e){let t=this;for(;t;){if(!t.parent)return!1;if(t===e)return!0;t=t.parent}return!1}isChild(e){return e.isParent(this)}draw(){}postDraw(){this.forEachChildren(e=>{e.draw(),e.postDraw(),e.isReady||e.ready()})}update(e){this.onUpdate(e)}postUpdate(e){this.forEachChildren(t=>{t.isReady&&t.update(e),t.postUpdate(e)})}doDestory(){this.resident&&this.engine.removeResident(this),this.removeFromParent(),this.forEachChildren(e=>{e.doDestory()})}enterTree(){this.listened.forEach(e=>{e.emitter.on(e.eventName,e.func)}),this.inTree=!0,this.event.emit("onEnterTree"),this.onEnterTree()}exitTree(){this.listened.forEach(e=>{e.emitter.off(e.eventName,e.func)}),this.inTree=!1,this.event.emit("onExitTree"),this.onExitTree()}ready(){this.isReady=!0,this.onReady()}onEnterTree(){}onExitTree(){}onReady(){}onUpdate(e){}getMouseGlobalPositon(){return this.engine.mouse.mousePosition}forEachChildren(e){this.children.forEach(e)}findAllByTag(e,t=!0){const s=[],r=Array.isArray(e)?e:[e];return this.forEachChildren(o=>{if(r.some(a=>o.tag.has(a))&&s.push(o),t){const a=o.findAllByTag(e,t);s.push(...a)}}),s}findByTag(e,t=!0){const s=Array.isArray(e)?e:[e];let r=null;return this.forEachChildren(o=>{r||(s.some(a=>o.tag.has(a))?r=o:t&&(r=o.findByTag(e,t)))}),r}traverseChildren(e){let t=e(this);if(t!==!1)return t;for(let s of this.children)if(t=s.traverseChildren(e),t!==!1)return t;return!1}destory(){this.destroyed=!0,this.engine.toDestory.push(this)}}class C extends E{constructor(t){super(t);i(this,"position");i(this,"scale");i(this,"rotation");i(this,"skew");i(this,"offset");i(this,"z",0);i(this,"globalPosition",this.pool.Vector2.pull(0,0));i(this,"globalRotation",0);i(this,"modelMatrix");this.modelMatrix=this.pool.Matrix.pull(),this.z=t.z??0,this.position=t.position??this.pool.Vector2.pull(0,0),this.scale=t.scale??this.pool.Vector2.pull(1,1),this.skew=t.skew??this.pool.Vector2.pull(0,0),this.offset=t.offset??this.pool.Vector2.pull(0,0),this.rotation=t.rotation??0}getMouseLocalPositon(){return this.globalPosition.sub(this.engine.mouse.mousePosition,!0)}getMouseGlobalPositon(){return this.engine.mouse.mousePosition}draw(){super.draw(),this.renderer.save(),this.renderer.modelMatrix.translate(this.position.x,this.position.y),this.renderer.modelMatrix.rotate(this.rotation),this.globalRotation=this.renderer.modelMatrix.getRotation(),this.renderer.modelMatrix.scale(this.scale.x,this.scale.y),this.renderer.modelMatrix.skew(this.skew.x,this.skew.y);const[t,s]=this.renderer.modelMatrix.apply(0,0);this.globalPosition.set(t,s),this.renderer.toDraw.push(this),this.modelMatrix.copy(this.renderer.modelMatrix),this.modelMatrix.translate(-this.offset.x,-this.offset.y)}flush(){this.renderer.modelMatrix.copy(this.modelMatrix)}flushDebug(){var t;(t=this.engine.debugger)==null||t.addDebugCross()}postDraw(){super.postDraw(),this.renderer.restore()}doDestory(){super.doDestory(),this.pool.Vector2.push(this.position),this.pool.Vector2.push(this.scale),this.pool.Vector2.push(this.skew),this.pool.Vector2.push(this.offset),this.pool.Matrix.push(this.modelMatrix)}}class M extends C{constructor(t){super(t);i(this,"color");i(this,"visible",!0);i(this,"drawSize",this.pool.Vector2.pull(0,0));this.color=t.color??this.pool.Color.pull(1,1,1,1)}show(){this.visible=!0}hide(){this.visible=!1}draw(){super.draw(),this.visible=this.visible&&this.renderer.visable,this.renderer.visable=this.visible}doDestory(){super.doDestory(),this.pool.Color.push(this.color),this.pool.Vector2.push(this.drawSize)}}function ie(n,e,t,s){const r=n.createTexture();if(n.bindTexture(n.TEXTURE_2D,r),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,t||s?n.LINEAR:n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,t||s?n.LINEAR:n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),s&&n.pixelStorei(n.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,n.RGBA,n.UNSIGNED_BYTE,e),!r)throw new Error("unable to create texture");return r}class T{constructor(){i(this,"resourcesId");i(this,"resourcesType")}}class I{constructor(e){i(this,"engine");i(this,"baseTextures",new Map);this.engine=e}textureFromImage(e){return new w(this.createBaseTexture(e))}altasFromTexture(e,t){return new L(e.baseTexture,t)}async textureFromUrl(e){const t=this.baseTextures.get(e);if(t)return new w(t);{const s=await this.engine.loader.loadImage(e);return new w(this.createBaseTexture(s))}}createBaseTexture(e){return new D(this.engine,e)}}class D{constructor(e,t,s=!1){i(this,"texture");i(this,"width");i(this,"height");i(this,"source");i(this,"engine");this.source=t,this.engine=e,this.setImage(t,s)}setImage(e,t=!1){const s=this.engine;this.texture=ie(s.renderer.gl,e,s.renderer.antialias,t),this.width=e.width,this.height=e.height}}class w extends T{constructor(t){super();i(this,"baseTexture");this.baseTexture=t}get width(){var t;return((t=this.baseTexture)==null?void 0:t.width)??0}get height(){var t;return((t=this.baseTexture)==null?void 0:t.height)??0}}class L extends w{constructor(t,s){super(t);i(this,"region",l.Rect.pull());s&&this.region.copy(s)}get width(){return this.region.w}get height(){return this.region.h}}class V extends E{constructor(t){super(t);i(this,"waitTime");i(this,"oneShot");i(this,"paused",!1);i(this,"timeLeft");i(this,"isStart",!1);this.waitTime=t.waitTime,this.oneShot=t.oneShot??!1,t.start&&this.start(),this.timeLeft=t.initTimeLeft??0}update(t){if(super.update(t),this.isStart&&!this.paused){if(this.timeLeft>this.waitTime){this.timeout();return}this.timeLeft+=t}}timeout(){this.oneShot&&(this.isStart=!1),this.event.emit("timeout"),this.timeLeft=0}stop(){this.timeLeft=0,this.isStart=!1}start(){this.timeLeft=0,this.isStart=!0}onTimeout(){}}class re extends M{constructor(t){super(t);i(this,"texture");i(this,"textureRegion",this.pool.Rect.pull());i(this,"enableRegion",!1);i(this,"animations");i(this,"aniPaused",!1);i(this,"aniLoop",!1);i(this,"region",this.pool.Rect.pull());i(this,"animationTimer");i(this,"animatiosFarme",0);i(this,"currentAniName");i(this,"currentAni");this.texture=t.texture,this.animations=t.animations,this.animationTimer=new V({start:!1,waitTime:0,engine:this.engine}),this.animationTimer.event.on("timeout",this.animationsTimeOut.bind(this))}play(t,s=!1,r=!1){t==this.currentAniName&&!r||(this.stop(),this.texture=this.animations.texture,this.aniLoop=s,this.currentAniName=t,this.currentAni=this.animations.getAniData(t),this.animationTimer.waitTime=this.currentAni.time,this.animationTimer.start(),this.animatiosFarme=0,this.setAnimation(0))}stop(){this.aniLoop=!1,this.animationTimer.stop(),this.currentAniName=void 0}animationsTimeOut(){if(this.currentAniName){if(this.animatiosFarme++,this.currentAni.fromFrames+this.animatiosFarme>this.currentAni.toFrames){this.aniLoop?this.play(this.currentAniName,!0,!0):this.stop();return}this.setAnimation(this.animatiosFarme)}}update(t){super.update(t),this.animationTimer.paused=this.aniPaused,this.animationTimer.update(t)}clipRegion(){this.region.copy(this.textureRegion);let t=this.enableRegion;return this.texture instanceof L&&(this.enableRegion?this.texture.region.clip(this.region,this.region):this.region.copy(this.texture.region),t=!0),t}flush(){if(super.flush(),!this.texture||!this.visible)return;this.renderer.setCompositors("texture");const t=this.renderer.currentCompositors,s=this.texture.baseTexture,r=this.clipRegion(),{w:o,h:a}=t.addQuad(s,r,this.region);this.drawSize.set(o,a),t.setColor(this.color),t.flush()}flushDebug(){var t;(t=this.engine.debugger)==null||t.drawDebugFrame(this.drawSize.x,this.drawSize.y),super.flushDebug()}setAnimation(t){if(this.currentAni){const s=this.currentAni.fromFrames+t,r=this.animations.getAnimationRegion(s);r&&(this.textureRegion.copy(r),this.enableRegion=!0)}}}class p{constructor(e=0,t=0,s=0,r=1){i(this,"r",0);i(this,"g",0);i(this,"b",0);i(this,"alpha",0);this.poolReset(e,t,s,r)}poolReset(e=0,t=0,s=0,r=1){this.setColor(e,t,s,r)}setColor(e,t,s,r=1){this.r=e,this.g=t,this.b=s,this.alpha=r}copy(e){this.setColor(e.r,e.g,e.b,e.alpha)}clone(){return l.Color.pull(this.r,this.g,this.b,this.alpha)}equals(e){return e.r==this.r&&e.g==this.g&&e.b==this.b&&e.alpha==this.alpha}static fromHex(e){e.startsWith("#")&&(e=e.slice(1));const t=parseInt(e.slice(0,2),16)/255,s=parseInt(e.slice(2,4),16)/255,r=parseInt(e.slice(4,6),16)/255;let o=1;return e.length>=8&&(o=parseInt(e.slice(6,8),16)/255),new p(t,s,r,o)}static red(){return new p(255,0,0)}static green(){return new p(0,255,0)}static blue(){return new p(0,0,255)}static yellow(){return new p(255,255,0)}static white(){return new p(255,255,255)}static black(){return new p(0,0,0)}}const U=p;function k(n,e){return n.substring(0,9)!=="precision"?"precision "+e+" float;"+n:n}function z(n){return n.getShaderPrecisionFormat(n.VERTEX_SHADER,n.HIGH_FLOAT).precision>0&&n.getShaderPrecisionFormat(n.FRAGMENT_SHADER,n.HIGH_FLOAT).precision>0?"highp":n.getShaderPrecisionFormat(n.VERTEX_SHADER,n.MEDIUM_FLOAT).precision>0&&n.getShaderPrecisionFormat(n.FRAGMENT_SHADER,n.MEDIUM_FLOAT).precision>0?"mediump":"lowp"}function _(n,e,t){const s=n.createShader(e);if(n.shaderSource(s,t),n.compileShader(s),!n.getShaderParameter(s,n.COMPILE_STATUS))throw new Error(n.getShaderInfoLog(s));return s}function ne(n,e,t){const s=_(n,n.VERTEX_SHADER,e),r=_(n,n.FRAGMENT_SHADER,t);let o=n.createProgram();if(!o)throw new Error("can't create webgl program");if(n.attachShader(o,s),n.attachShader(o,r),n.linkProgram(o),!n.getProgramParameter(o,n.LINK_STATUS)){const a="Error initializing Shader gl.VALIDATE_STATUS: "+n.getProgramParameter(o,n.VALIDATE_STATUS)+` gl.getError()`+n.getError()+` gl.getProgramInfoLog()`+n.getProgramInfoLog(o);throw n.deleteProgram(o),o=null,new Error(a)}return n.useProgram(o),n.deleteShader(s),n.deleteShader(r),o}function oe(n){const e=n.createBuffer();if(!e)throw new Error("Unable to create buffer");return e}class N{constructor(e,t,s,r){i(this,"gl");i(this,"program");i(this,"vertex");i(this,"fragment");this.gl=e,this.vertex=k(t,r||z(e)),this.fragment=k(s,r||z(e)),this.program=ne(e,this.vertex,this.fragment)}bind(){this.gl.useProgram(this.program)}getAttribLocation(e){return this.gl.getAttribLocation(this.program,e)}getUnifromLocation(e){return this.gl.getUniformLocation(this.program,e)}setVertexAttributes(e,t,s){for(let r=0;r=this.maxVertex}resize(e){for(;e>this.maxVertex;)this.maxVertex<<=1;const t=this.bufferF32;return this.buffer=new ArrayBuffer(this.maxVertex*this.vertexSize*this.objSize),this.bufferF32=new Float32Array(this.buffer),this.bufferU32=new Uint32Array(this.buffer),this.bufferF32.set(t),this}pushVertex(...e){const t=this.vertexCount*this.vertexSize;return this.vertexCount>=this.maxVertex&&this.resize(this.vertexCount),e.forEach((s,r)=>{this.bufferF32[r+t]=s}),this.vertexCount++,this}toFloat32(e,t){return typeof t<"u"?this.bufferF32.subarray(e,t):this.bufferF32}toUint32(e,t){return typeof t<"u"?this.bufferU32.subarray(e,t):this.bufferU32}length(){return this.vertexCount}isEmpty(){return this.vertexCount===0}}class B{constructor(e){i(this,"gl");i(this,"renderer");i(this,"shader");i(this,"attributes",[]);i(this,"vertexByteSize",0);i(this,"vertexFloatSize",0);i(this,"bufferArray");i(this,"buffer");i(this,"color");i(this,"colorDirty",!0);i(this,"path");i(this,"lastVert",[]);i(this,"currentShader");this.gl=e.renderer.gl,e.attributes.forEach(t=>{this.addAttributeInfo(t)}),this.renderer=e.renderer,this.shader=new N(this.gl,e.vertexShader,e.fragmentShader),this.currentShader=this.shader,this.buffer=oe(this.gl),this.bufferArray=new O(this.vertexFloatSize,e.vertexPerObj??6),this.color=l.Color.pull(1,1,1,1),this.path=this.renderer.path}setColor(e){this.color.equals(e)||(this.colorDirty=!0,this.color.copy(e))}setColorByRGBA(e=1,t=1,s=1,r=1){const o=l.Color.pull(e,t,s,r);this.color.equals(o)||(this.colorDirty=!0,this.color.copy(o)),l.Color.push(o)}setUnifrom(){this.colorDirty&&this.gl.uniform4f(this.currentShader.getUnifromLocation("uColor"),this.color.r,this.color.g,this.color.b,this.color.alpha)}addAttributeInfo(e){let t=0;switch(e.type){case this.gl.BYTE:t=Int8Array.BYTES_PER_ELEMENT;break;case this.gl.UNSIGNED_BYTE:t=Uint8Array.BYTES_PER_ELEMENT;break;case this.gl.SHORT:t=Int16Array.BYTES_PER_ELEMENT;break;case this.gl.UNSIGNED_SHORT:t=Uint16Array.BYTES_PER_ELEMENT;break;case this.gl.INT:t=Int32Array.BYTES_PER_ELEMENT;break;case this.gl.UNSIGNED_INT:t=Uint32Array.BYTES_PER_ELEMENT;break;case this.gl.FLOAT:t=Float32Array.BYTES_PER_ELEMENT;break;default:throw new Error("Invalid GL Attribute type")}this.attributes.push({name:e.name,size:e.size,type:e.type,normalized:e.normalized,offset:e.offset}),this.vertexByteSize+=t*e.size,this.vertexFloatSize=this.vertexByteSize/Float32Array.BYTES_PER_ELEMENT}bind(e){const t=this.gl;this.currentShader=e??this.shader,this.renderer.useShader(this.currentShader),t.uniformMatrix4fv(this.currentShader.getUnifromLocation("uProjectionMatrix"),!1,this.renderer.projectionMatrix),t.bindBuffer(t.ARRAY_BUFFER,this.buffer),this.currentShader.setVertexAttributes(this.gl,this.attributes,this.vertexByteSize),this.colorDirty=!0}flush(){const e=this.renderer.gl,t=this.bufferArray.vertexCount,s=this.bufferArray.vertexSize;this.setUnifrom(),e.bufferData(e.ARRAY_BUFFER,this.bufferArray.toFloat32(0,t*s),e.STATIC_DRAW),e.drawArrays(this.drawCallMode,0,t),this.colorDirty=!1,this.bufferArray.clear()}}const ae=`// Current vertex point attribute vec2 aPosition; attribute vec2 aRegion; uniform mat4 uProjectionMatrix; varying vec2 vRegion; void main(void) { gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0); vRegion = aRegion; } `,he=`uniform sampler2D uSampler; uniform vec4 uColor; varying vec2 vRegion; void main(void) { gl_FragColor = texture2D(uSampler, vRegion) * uColor; } `;class le extends B{constructor(t){const s=t.gl;super({renderer:t,attributes:[{name:"aPosition",size:2,type:s.FLOAT,normalized:!1,offset:0},{name:"aRegion",size:2,type:s.FLOAT,normalized:!1,offset:2*Float32Array.BYTES_PER_ELEMENT}],vertexShader:ae,fragmentShader:he});i(this,"drawCallMode");this.drawCallMode=s.TRIANGLES}addVertex(t,s,r,o){const a=this.renderer.modelMatrix.apply(t,s);return this.bufferArray.pushVertex(...a,r,o),a}addQuad(t,s,r){const o=this.gl;o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,t.texture);const a=s?r.x/t.width:0,c=s?r.y/t.height:0,u=s?a+r.w/t.width:1,g=s?c+r.h/t.height:1,S=s?r.w:t.width,R=s?r.h:t.height;return this.addVertex(0,0,a,c),this.addVertex(S,0,u,c),this.addVertex(S,R,u,g),this.addVertex(0,0,a,c),this.addVertex(S,R,u,g),this.addVertex(0,R,a,g),{w:S,h:R}}}const ce=(n,e)=>{const t=n.getContext("webgl",e);if(!t)throw new Error("Your browser does not support webgl");return t},ue=`// Current vertex point\r attribute vec2 aPosition;\r uniform mat4 uProjectionMatrix;\r uniform vec4 uColor;\r \r void main(void) {\r gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r }\r `,de=`uniform vec4 uColor;\r \r void main(void) {\r gl_FragColor = uColor;\r }\r `;class fe extends B{constructor(t){const s=t.gl;super({renderer:t,attributes:[{name:"aPosition",size:2,type:s.FLOAT,normalized:!1,offset:0}],vertexShader:ue,fragmentShader:de,vertexPerObj:6});i(this,"drawCallMode");i(this,"lineWidth",1);i(this,"drawMode",m.LINE);this.drawCallMode=s.LINE_STRIP}setMode(t){switch(this.drawMode=t,t){case m.FILL:this.drawCallMode=this.gl.TRIANGLE_FAN;break;case m.LINE:this.drawCallMode=this.gl.TRIANGLES;break}}addVertex(t,s){arguments.length==1?this.bufferArray.pushVertex(...this.renderer.modelMatrix.apply(t.x,t.y)):this.bufferArray.pushVertex(...this.renderer.modelMatrix.apply(t,s))}bind(t){this.setMode(m.LINE),super.bind(t)}linePathToVertex(){this.path.forEachLine((t,s)=>{const r=t.sub(s,!0);r.unit().normal().scale(this.lineWidth*.5);const o=t.add(r,!0),a=t.sub(r,!0),c=s.add(r,!0),u=s.sub(r,!0);this.addVertex(a),this.addVertex(o),this.addVertex(u),this.addVertex(o),this.addVertex(c),this.addVertex(u)})}pointPathToVertex(){this.path.forEachPoint(t=>this.addVertex(t))}pathToVertex(){switch(this.drawMode){case m.LINE:this.linePathToVertex();break;case m.FILL:this.pointPathToVertex();break}}flush(){this.pathToVertex(),super.flush(),this.path.clearPath()}}class j{constructor(){i(this,"startPoint",l.Vector2.pull(0));i(this,"path",[]);i(this,"fistStack",[]);i(this,"firstPoint",!0)}moveTo(e,t){this.firstPoint=!0,this.startPoint.set(e,t)}lineTo(e,t){this.path.push(this.startPoint.clone()),this.path.push(l.Vector2.pull(e,t)),this.fistStack.push(this.firstPoint),this.fistStack.push(!1),this.firstPoint=!1,this.startPoint.set(e,t)}rectPath(e){this.moveTo(e.x,e.y),this.lineTo(e.x+e.w,e.y),this.lineTo(e.x+e.w,e.y+e.h),this.lineTo(e.x,e.y+e.h),this.lineTo(e.x,e.y)}beginPath(){this.startPoint.set(0),this.firstPoint=!0,this.clearPath()}clearPath(){this.path.forEach(e=>{l.Vector2.push(e)}),this.path=[],this.fistStack=[]}forEachLine(e){for(let t=0;t{s%2==1&&e(t,s)})}circlePath(e){let t=!0;const s=Math.max(100,e*5+10);for(let r=0;r<=s;r++){const o=r/s*2*Math.PI,a=e*Math.cos(o),c=e*Math.sin(o);t&&(this.moveTo(a,c),t=!1),this.lineTo(a,c)}}polygonPath(e){this.moveTo(e[0].x,e[0].y),e.forEach(t=>{this.lineTo(t.x,t.y)}),this.lineTo(e[0].x,e[0].y)}}class G{constructor(e,t){i(this,"backgroundColor");i(this,"currentCompositors");i(this,"currentShader");i(this,"projectionMatrix");i(this,"modelMatrix");i(this,"visable",!0);i(this,"antialias");i(this,"path",new j);i(this,"scaleMode");i(this,"canvas");i(this,"gl");i(this,"engine");i(this,"nativeSize");i(this,"devicePixelRatio");i(this,"matrixStack",[]);i(this,"visableStack",[]);i(this,"compositors",new Map);i(this,"toDraw",[]);this.gl=ce(t.canvas,{alpha:!0,antialias:t.antialias??!1,stencil:!0});const s=this.gl;this.engine=e,this.canvas=t.canvas,this.backgroundColor=t.backgroundColor??l.Color.pull(.6,.6,.6,1),this.antialias=t.antialias??!1,this.devicePixelRatio=t.pixelDensity??window.devicePixelRatio??1,this.nativeSize=l.Vector2.pull(t.width??300,t.height??300),this.scaleMode=t.scaleMode??A.ADAPTIVE,this.updateNativeSize(),this.resize(),this.modelMatrix=l.Matrix.pull(),this.addCompositors("texture",new le(this)),this.addCompositors("primitive",new fe(this)),this.setCompositors("texture"),s.enable(s.BLEND),s.blendFunc(s.SRC_ALPHA,s.ONE_MINUS_SRC_ALPHA),new ResizeObserver(o=>{for(let a of o){const{width:c,height:u}=a.contentRect;this.scaleMode==A.ADAPTIVE&&this.setNativeSize(c,u),this.resize()}}).observe(this.canvas)}get lastMartix(){const e=this.matrixStack.length;return e==0?this.modelMatrix:this.matrixStack[e]}get root(){return this.engine.root}createOrthographicProjectionMatrix(e,t,s,r){return new Float32Array([2/(t-e),0,0,0,0,2/(r-s),0,0,0,0,-1,0,-(t+e)/(t-e),-(r+s)/(r-s),0,1])}reset(){this.matrixStack.forEach(e=>{l.Matrix.push(e)}),this.visableStack=[],this.clear()}save(){this.matrixStack.push(this.modelMatrix.clone()),this.visableStack.push(this.visable)}restore(){if(this.matrixStack.length>0){const e=this.matrixStack.pop();this.modelMatrix.copy(e),l.Matrix.push(e),this.visable=this.visableStack.pop()}}addCompositors(e,t){this.compositors.has(e)||this.compositors.set(e,t)}setCompositors(e,t){const s=this.compositors.get(e);s&&(this.currentCompositors!==s||t!==this.currentShader)&&(this.currentCompositors=s,s.bind(t))}useShader(e){this.currentShader=e,e.bind()}clear(){const e=this.gl,t=this.backgroundColor;this.toDraw=[],this.modelMatrix.identity(),this.visable=!0,e.clearColor(t.r,t.g,t.b,t.alpha),e.clear(e.COLOR_BUFFER_BIT|e.STENCIL_BUFFER_BIT)}setNativeSize(e,t){this.nativeSize.x=e/this.devicePixelRatio,this.nativeSize.y=t/this.devicePixelRatio,this.updateNativeSize()}updateNativeSize(){const{x:e,y:t}=this.nativeSize;this.projectionMatrix=this.createOrthographicProjectionMatrix(0,e,t,0)}resize(){const e=this.canvas,{x:t,y:s}=this.nativeSize,r=this.devicePixelRatio;e.width=t*r,e.height=s*r,this.gl.viewport(0,0,e.width,e.height)}draw(){var e;this.clear(),this.root.draw(),this.root.postDraw(),this.toDraw.sort((t,s)=>t.z-s.z);for(const t of this.toDraw)t.flush(),(e=this.engine.debugger)!=null&&e.debugger&&t.flushDebug()}initCostumDraw(e){this.setCompositors("primitive",e.shader),e.color?this.currentCompositors.setColor(e.color):this.currentCompositors.setColorByRGBA(0,0,0,1)}drawLine(e){this.initCostumDraw(e);const t=this.currentCompositors;t.setMode(m.LINE),t.lineWidth=e.lineWdith,this.currentCompositors.flush()}drawPolygon(e){this.initCostumDraw(e),this.currentCompositors.setMode(m.FILL),this.currentCompositors.flush()}}class Y{constructor(){i(this,"pressedKeys",new Set);i(this,"event",new b);window.addEventListener("keydown",this.handleKeyDown.bind(this)),window.addEventListener("keyup",this.handleKeyRelease.bind(this))}handleKeyDown(e){this.pressedKeys.has(e.key)?this.event.emit("onKeyPressRepeat",e.key):this.event.emit("onKeyDown",e.key),this.pressedKeys.add(e.key)}handleKeyRelease(e){this.event.emit("onKeyRelease",e.key),this.pressedKeys.delete(e.key)}getAllPressedKeys(){return this.pressedKeys}}class X{constructor(e,t){i(this,"engine");i(this,"canvas");i(this,"mousePosition",l.Vector2.pull(0));i(this,"isMouseDown",!1);i(this,"event",new b);this.engine=e,this.canvas=t,this.initMouseEvents()}getMouseData(e){const t=this.canvas.getBoundingClientRect(),s=this.engine.renderer.devicePixelRatio,r={position:l.Vector2.pull((e.clientX-t.left)/s,(e.clientY-t.top)/s),event:e};return this.mousePosition.copy(r.position),r}initMouseEvents(){this.canvas.addEventListener("mousedown",this.handleMouseDown.bind(this)),this.canvas.addEventListener("mousemove",this.handleMouseMove.bind(this)),window.addEventListener("mouseup",this.handleMouseUp.bind(this))}handleMouseDown(e){const t=this.getMouseData(e);this.event.emit("onMouseDown",t),this.isMouseDown=!0}handleMouseMove(e){const t=this.getMouseData(e);this.event.emit("onMouseMove",t)}handleMouseUp(e){const t=this.getMouseData(e);this.event.emit("onMouseUp",t),this.isMouseDown=!1}}class me{constructor(e){i(this,"engine");i(this,"frames",[]);i(this,"input");i(this,"pressedDebugger",!1);i(this,"debugger",!1);i(this,"color",l.Color.pull());i(this,"scale",[]);this.engine=e,this.input=e.input}toggleDebugger(){this.debugger=!this.debugger}update(){this.frames=[],this.input.pressedKeys.has("b")&&this.input.pressedKeys.has("Control")?(this.pressedDebugger||this.toggleDebugger(),this.pressedDebugger=!0):this.pressedDebugger=!1}drawDebugFrame(e,t){const s=this.init();this.color.setColor(0,0,1,1);const r=l.Rect.pull(0,0,e*this.scale[0],t*this.scale[1]);s.rectPath(r);const o=l.Vector2.pull(0,0);this.engine.renderer.drawLine({color:this.color,position:o,lineWdith:3}),l.Rect.push(r),l.Vector2.push(o),this.engine.renderer.restore()}drawDebugCollision(e){if(e.length==0)return;const t=this.init(),s=l.Vector2.pull(0,0);this.color.setColor(0,1,0,.6),t.beginPath(),t.moveTo(e[0].x,e[0].y),e.forEach(r=>{t.lineTo(r.x*this.scale[0],r.y*this.scale[1])}),t.lineTo(e[0].x,e[0].y),this.engine.renderer.drawPolygon({color:this.color,position:s}),l.Vector2.push(s),this.engine.renderer.restore()}addDebugCross(){const e=this.init(),t=l.Vector2.pull(0,0);e.beginPath(),e.moveTo(-10,0),e.lineTo(10,0),e.moveTo(0,-10),e.lineTo(0,10),this.color.setColor(1,0,0),this.engine.renderer.drawLine({color:this.color,position:t,lineWdith:2}),l.Vector2.push(t),this.engine.renderer.restore()}init(){const e=this.engine.renderer;return e.save(),this.scale=e.modelMatrix.getScale(),e.modelMatrix.setAbsoluteScale(1),e.path}}class ${constructor(e){i(this,"engine");i(this,"physicsObjects",new Set);this.engine=e}add(e){this.physicsObjects.add(e)}remove(e){this.physicsObjects.delete(e)}reset(){this.physicsObjects.clear()}collisionDetection(e){const t=[];return this.physicsObjects.forEach(s=>{if(s!=e){let r=this.satPolygonInPolygon(s.ShapePosition,e.ShapePosition);r&&t.push({overlap:r,body:s})}}),t}satPolygonInPolygon(e,t){let s=1/0,r=null;const o=this.getAxes(e).concat(this.getAxes(t));for(const a of o){const c=this.project(e,a),u=this.project(t,a);if(this.overlap(c,u)){const g=this.overlapMagnitude(c,u);g0&&(r=r.mul(-1)),l.Vector2.push(a),r.unit().mul(s)}return null}getAxes(e){let t=[];for(let s=0;se.y!=g>e.y&&e.x<(u-a)*(e.y-c)/(g-c)+a&&(s=!s)}return s}project(e,t){let s=1/0,r=-1/0;for(const o of e){const a=o.dot(t);ar&&(r=a)}return[s,r]}overlap([e,t],[s,r]){return e<=r&&s<=t}overlapMagnitude([e,t],[s,r]){return Math.min(t,r)-Math.max(e,s)}getPolygonCenter(e){let t=0,s=0;return e.forEach(r=>{t+=r.x,s+=r.y}),l.Vector2.pull(t/e.length,s/e.length)}}class H{constructor(e){i(this,"canvas");this.canvas=this.getContext()}getContext(){const e=document.createElement("canvas").getContext("2d");if(!e)throw new Error("unable to create canvas");return e}drawText(e,t="24px monospace",s){const r=this.canvas;r.font=t;let a=r.measureText(e).width,c=parseInt(t,10);return r.canvas.width=a+2,r.canvas.height=c*1.25,r.clearRect(0,0,r.canvas.width,r.canvas.height),r.fillStyle=`rgba(${s.r*255},${s.g*255},${s.b*255},${s.alpha*255})`,r.font=t,r.fillText(e,0,c),r.canvas}}class K extends b{constructor(t){super();i(this,"assets",{});i(this,"loadingAssets",{});i(this,"currectRegion",-1);i(this,"regions",{});i(this,"engine");i(this,"regionsCallback",{});i(this,"regionsCount",0);this.engine=t}getRegion(){return this.regionsCount++}async loadTexture(t,s){return typeof s=="string"?await this.load(t,this.engine.texture.textureFromUrl(s)):(this.assets[t]=this.engine.texture.textureFromImage(s),this.assets[t])}async loadAltasTexture(t,s,r){const o=async()=>{const a=await this.getAsyncAssets(s);if(a instanceof w)return this.assets[t]=this.engine.texture.altasFromTexture(a,r),this.assets[t];throw new Error(`assets ${s} is not a texture`)};return await this.load(t,o())}async loadAudio(t,s){return await this.load(t,this.engine.audio.audioFromUrl(s))}async loadAnimation(t,s,r){const o=async()=>{const a=await this.getAsyncAssets(s);if(a instanceof w){let c={};typeof r.animations=="string"?c=(await this.getAsyncAssets(r.animations)).data:c=r.animations;const u={...r,texture:a,animations:c};return this.assets[t]=new ee(u),this.assets[t]}else throw new Error(`assets ${s} is not a texture`)};return await this.load(t,o())}async loadJSON(t,s){return await this.load(t,this.engine.loader.loadData(s))}async load(t,s){const r=this.currectRegion!==-1,o=this.currectRegion;this.loadingAssets[t]=s,r&&this.regions[o]++;const a=await s;return a.resourcesId=t,delete this.loadingAssets[t],r&&this.regions[o]--,r&&this.regions[o]==0&&this.regionsCallback[o](),this.emit("loaded",a),Object.keys(this.loadingAssets)&&this.emit("idle"),this.assets[t]=a,a}get(t){if(typeof this.assets[t]>"u")throw new Error(`assets ${t} dose not exist`);return this.assets[t]}async getAsyncAssets(t){const s=this.assets[t];if(s)return s;const r=this.loadingAssets[t];if(r)return r;throw new Error(`Assets ${t} dose not exsit`)}startRegion(){this.currectRegion=this.getRegion(),this.regions[this.currectRegion]=0}endRegion(t){this.regionsCallback[this.currectRegion]=t,this.regions[this.currectRegion]==0&&t(),this.currectRegion=-1}}class W{constructor(e,t){i(this,"engine");i(this,"baseUrl");this.engine=e,this.baseUrl=t}async loadData(e){const t=await fetch(this.baseUrl?`${this.baseUrl}/${e}`:e);if(!t.ok)throw new Error(`An error occurred while loading the data: ${t.statusText}`);return new q(await t.json())}async loadAudio(e){const t=await fetch(this.baseUrl?`${this.baseUrl}/${e}`:e);if(!t.ok)throw new Error(`An error occurred while loading the audio: ${t.statusText}`);const s=await t.arrayBuffer();return await this.engine.audio.decode(s)}async loadImage(e){return new Promise((t,s)=>{const r=new Image;r.onload=()=>{t(r)},r.onerror=s,r.src=this.baseUrl?`${this.baseUrl}/${e}`:e})}}class q extends T{constructor(t){super();i(this,"data");this.data=t}}class ge{constructor(e){i(this,"renderer");i(this,"root");i(this,"input");i(this,"mouse");i(this,"loader");i(this,"texture");i(this,"physics");i(this,"debugger");i(this,"audio");i(this,"text");i(this,"resource");i(this,"getAssets");i(this,"lastTime",0);i(this,"residents",new Set);i(this,"maxFPS");i(this,"toDestory",[]);i(this,"loadedScenes",new Set);if(!e.canvas)throw new Error("Please provide a canvas");l.register(),this.input=new Y,this.mouse=new X(this,e.canvas),this.loader=new W(this),this.texture=new I(this),this.physics=new $(this),this.audio=new J(this),this.text=new H(this),this.renderer=new G(this,{...e}),this.debugger=e.disableDebugger?void 0:new me(this),this.resource=new K(this),window.sparkleEngine=this,this.changeSceneToContainer(new E({engine:this})),this.maxFPS=e.maxFPS??60,this.loop(0),this.getAssets=this.resource.get.bind(this.resource)}changeSceneToContainer(e){this.residents.forEach(t=>{t.setParent(e)}),this.root&&this.root.doDestory(),this.root=e}removeResident(e){e.resident=!1,this.residents.delete(e)}addResident(e){e.resident=!0,e.setParent(this.root),this.residents.add(e)}update(e){this.root.update(e),this.root.postUpdate(e)}loop(e){const t=(e-this.lastTime)/1e3;this.lastTime=e,this.debugger&&this.debugger.update(),this.update(t),this.renderer.draw(),this.destoryChild(),window.requestAnimationFrame(this.loop.bind(this))}destoryChild(){this.toDestory.forEach(()=>{this.toDestory.pop().doDestory()})}async changeToScene(e){this.changeSceneToContainer(await this.instantiateScene(e))}instantiateScene(e){return new Promise(t=>{this.resource.startRegion();const s=new e;this.loadedScenes.has(e)||(s.preload(),this.loadedScenes.add(e)),this.resource.endRegion(()=>{t(s.create(this))})})}reset(){const e=new E({engine:this});this.changeSceneToContainer(e)}}class Q extends T{constructor(t,s){super();i(this,"event",new b);i(this,"buffer");i(this,"context");i(this,"source");this.context=t.context,this.buffer=s}play(t=0){this.source=this.context.createBufferSource();const s=this.source;return s.buffer=this.buffer,s.connect(this.context.destination),s.start(t),s.onended=()=>{this.event.emit("onEnd"),s.disconnect(this.context.destination),this.source=void 0},s}}class pe{constructor(e){i(this,"context");i(this,"engine");this.engine=e,this.context=new AudioContext}async audioFromUrl(e){const t=await this.engine.loader.loadAudio(e);return new Q(this,t)}decode(e){return this.context.decodeAudioData(e)}}const J=pe;class xe extends M{constructor(t){super(t);i(this,"texture");i(this,"font");i(this,"anchor");i(this,"_text");this.color=t.color??this.pool.Color.pull(1,1,1,1),this.font=t.font??"16px Arial",this.text=t.text??"",this.anchor=t.anchor??v.LEFT}get text(){return this._text}set text(t){this.setText(t),this._text=t}flush(){if(super.flush(),!this.visible)return;const t=this.renderer.modelMatrix;switch(this.anchor){case v.CENTER:t.translate(-this.drawSize.x/2,0);break;case v.RIGHT:t.translate(-this.drawSize.x,0);break}this.renderer.setCompositors("texture");const s=this.renderer.currentCompositors;s.addQuad(this.texture,!1),s.setColor(this.color),s.flush()}setText(t){const s=this.engine.text.drawText(t,this.font,this.color);this.texture?this.texture.setImage(s,!0):this.texture=new D(this.engine,s,!0),this.drawSize.set(this.texture.width,this.texture.height)}}class we extends C{constructor(t){super(t);i(this,"shape",[]);i(this,"ShapePosition",[]);i(this,"physics");i(this,"results",[]);i(this,"collisions",[]);i(this,"shapeReady",!1);this.setShape(t.shape??[]),this.physics=this.engine.physics,this.onEvent(this.engine.mouse,"onMouseUp",this.onMouseClick.bind(this))}update(t){if(this.shapeReady){const s=this.collisionDetection(),r=[];s.forEach(o=>{this.collisions.includes(o.body)||(this.onBodyEnter(o),this.event.emit("onBodyEnter",o)),r.push(o.body)}),this.collisions.forEach(o=>{r.includes(o)||(this.onBodyExit(o),this.event.emit("onBodyExit",o))}),this.collisions=r,this.results=s}super.update(t)}setShape(t){this.clearShape(),t.forEach(s=>{this.shape.push(s.clone()),this.ShapePosition.push(s.clone())}),this.shapeReady=!1}collisionDetection(){return this.physics.collisionDetection(this)}mouseDetection(){return this.isReady?this.physics.satPointInPolygon(this.getMouseGlobalPositon(),this.ShapePosition):!1}exitTree(){super.exitTree(),this.physics.remove(this)}enterTree(){super.enterTree(),this.physics.add(this)}onMouseClick(){this.mouseDetection()&&(this.event.emit("onClick"),this.onClick())}onClick(){}onBodyEnter(t){}onBodyExit(t){}draw(){super.draw(),this.shape.forEach((t,s)=>{const[r,o]=this.modelMatrix.apply(t.x,t.y);this.ShapePosition[s].set(r,o)}),this.shapeReady=!0}flushDebug(){var t;super.flushDebug(),(t=this.engine.debugger)==null||t.drawDebugCollision(this.shape)}clearShape(){this.ShapePosition.forEach(t=>{this.pool.Vector2.push(t)}),this.shape.forEach(t=>{this.pool.Vector2.push(t)}),this.shape=[],this.ShapePosition=[]}doDestory(){super.doDestory(),this.clearShape()}static rectShape(t,s,r,o){return[new y(t,s),new y(t+r,s),new y(t+r,s+o),new y(t,s+o)]}}class ye{constructor(e,t,s,r){i(this,"x",0);i(this,"y",0);i(this,"w",0);i(this,"h",0);this.poolReset(e,t,s,r)}poolReset(e=0,t=0,s=0,r=0){this.setRect(e,t,s,r)}setRect(e=this.x,t=this.y,s=this.w,r=this.h){this.x=e,this.y=t,this.w=s,this.h=r}clip(e,t){const s=[e.x+this.x,e.y+this.y,Math.min(this.w,e.w),Math.min(this.h,e.h)];t?t.setRect(...s):this.setRect(...s)}copy(e){this.setRect(e.x,e.y,e.w,e.h)}clone(){return l.Rect.pull(this.x,this.y,this.w,this.h)}}const Z=ye;class be{preload(){}}class Ee{static degreesToRadians(e){return e*(Math.PI/180)}static radiansToDegrees(e){return e*(180/Math.PI)}static random(){return Math.random()}static randomInt(e,t){return Math.floor(Math.random()*(t-e+1))+e}static choose(e){const t=Math.floor(Math.random()*e.length);return e[t]}static lerp(e,t,s){return e*(1-s)+t*s}static clamp(e,t,s){return Math.max(t,Math.min(s,e))}}class Te extends T{constructor(t){super();i(this,"hFrames");i(this,"vFrames");i(this,"gapSize");i(this,"texture");i(this,"animations");this.hFrames=t.hFrames,this.vFrames=t.vFrames,this.gapSize=t.gapSize,this.texture=t.texture,this.animations=t.animations}getAniData(t){return this.animations[t]}getAnimationRegion(t){if(!(!this.texture||!this.texture.baseTexture))if(this.vFrames>0&&this.hFrames>0){if(t>this.vFrames*this.hFrames-1)throw new Error("There aren't as many animations, please adjust vFrames and hFrames");const s=t%this.hFrames,r=Math.floor(t/this.hFrames),o=Math.floor(this.texture.width/this.hFrames)+this.gapSize,a=Math.floor(this.texture.height/this.vFrames)+this.gapSize;return l.Rect.pull(s*o,r*a,o-this.gapSize,a-this.gapSize)}else throw new Error("vFrames and hFrames should be greater than 0")}}const ee=Te;class ve extends M{constructor(t){super(t);i(this,"type");i(this,"path");i(this,"radius");i(this,"rect");i(this,"fill");i(this,"lineWidth");if(t.type==null)throw new Error("must provide a type");switch(this.type=t.type,this.fill=t.fill??!0,this.lineWidth=t.lineWidth??1,t.type){case x.RECT:this.rect=t.rect;break;case x.POLYGON:this.path=t.path;break;case x.CIRCLE:this.radius=t.radius;break}}draw(){super.draw();const t=this.pool.Vector2.pull(0,0),s=this.renderer.path;switch(s.beginPath(),this.type){case x.RECT:this.rect&&s.rectPath(this.rect);break;case x.POLYGON:this.path&&s.polygonPath(this.path);break;case x.CIRCLE:this.radius&&s.circlePath(this.radius);break}this.fill?this.engine.renderer.drawPolygon({color:this.color,position:t}):this.engine.renderer.drawLine({color:this.color,position:t,lineWdith:this.lineWidth}),this.pool.Vector2.push(t)}}var v=(n=>(n[n.CENTER=0]="CENTER",n[n.LEFT=1]="LEFT",n[n.RIGHT=2]="RIGHT",n))(v||{}),A=(n=>(n[n.ADAPTIVE=0]="ADAPTIVE",n[n.FIXED=1]="FIXED",n))(A||{}),m=(n=>(n[n.FILL=0]="FILL",n[n.LINE=1]="LINE",n))(m||{}),te=(n=>(n[n.NORMAL=0]="NORMAL",n[n.COLLISION=1]="COLLISION",n[n.CROSS=2]="CROSS",n))(te||{}),se=(n=>(n[n.TEXTURE=0]="TEXTURE",n[n.AUDIO=1]="AUDIO",n[n.DATA=2]="DATA",n[n.ANIMATION=3]="ANIMATION",n))(se||{}),x=(n=>(n[n.RECT=0]="RECT",n[n.POLYGON=1]="POLYGON",n[n.CIRCLE=2]="CIRCLE",n))(x||{});h.AltasTexture=L,h.Animations=ee,h.Audio=Q,h.AudioManager=J,h.BaseTexture=D,h.Collision=we,h.Color=U,h.Container=E,h.DataResources=q,h.DebuggerDrawType=te,h.Drawable=M,h.EventEmitter=b,h.GLShader=N,h.Graphical=ve,h.GraphicalType=x,h.InputManager=Y,h.Loader=W,h.MathUtils=Ee,h.Matrix=F,h.MouseManager=X,h.ObjectPool=f,h.PRIMITIVE_MODE=m,h.Path=j,h.PhysicsManager=$,h.Rect=Z,h.Renderer=G,h.Resources=T,h.ResourcesManager=K,h.ResourcesType=se,h.SCALE_MODE=A,h.Scene=be,h.SparkleEngine=ge,h.Sprite=re,h.Text=xe,h.TextAnchor=v,h.TextManager=H,h.Texture=w,h.TextureManager=I,h.Timer=V,h.Transform2D=C,h.Vector2=y,h.VertexArrayBuffer=O,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});