rapid-render 0.1.21 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -2
- package/README.md +70 -82
- package/dist/buffer.d.ts +153 -0
- package/dist/color.d.ts +144 -0
- package/dist/draw.d.ts +53 -0
- package/dist/index.d.ts +12 -15
- package/dist/line.d.ts +17 -3
- package/dist/math.d.ts +17 -492
- package/dist/matrix-engine.d.ts +360 -0
- package/dist/particle.d.ts +146 -36
- package/dist/rapid-render.js +3258 -0
- package/dist/rapid-render.umd.cjs +268 -0
- package/dist/region/atlasSpriteRegion.d.ts +9 -0
- package/dist/region/graphicRegion.d.ts +40 -0
- package/dist/region/region.d.ts +42 -0
- package/dist/region/spriteRegion.d.ts +33 -0
- package/dist/render.d.ts +342 -205
- package/dist/texture.d.ts +219 -152
- package/dist/utils.d.ts +28 -25
- package/dist/vite.svg +1 -0
- package/dist/webgl/glshader.d.ts +107 -25
- package/dist/webgl/utils.d.ts +18 -8
- package/package.json +36 -38
- package/dist/depth.d.ts +0 -8
- package/dist/input.d.ts +0 -60
- package/dist/interface.d.ts +0 -334
- package/dist/light.d.ts +0 -7
- package/dist/log.d.ts +0 -2
- package/dist/rapid.global.js +0 -1
- package/dist/rapid.js +0 -1
- package/dist/rapid.umd.cjs +0 -1
- package/dist/regions/attributes.d.ts +0 -44
- package/dist/regions/graphic_region.d.ts +0 -15
- package/dist/regions/region.d.ts +0 -41
- package/dist/regions/sprite_region.d.ts +0 -18
- package/dist/tilemap.d.ts +0 -98
- package/dist/webgl/uniform.d.ts +0 -14
package/dist/rapid.global.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var rapid=function(t){"use strict";var e,i,r,s,a,n,h,o;t.ScaleRadio=void 0,(e=t.ScaleRadio||(t.ScaleRadio={})).KEEP="keep",e.KEEP_H="keep_h",e.KEEP_W="keep_w",e.IGNORE="ignore",e.EXPAND="expand",t.LineTextureMode=void 0,(i=t.LineTextureMode||(t.LineTextureMode={})).STRETCH="stretch",i.REPEAT="repeat",t.TextureWrapMode=void 0,(r=t.TextureWrapMode||(t.TextureWrapMode={})).REPEAT="repeat",r.CLAMP="clamp",r.MIRROR="mirror",t.MaskType=void 0,(s=t.MaskType||(t.MaskType={})).Include="normal",s.Exclude="inverse",t.TilemapShape=void 0,(a=t.TilemapShape||(t.TilemapShape={})).SQUARE="square",a.ISOMETRIC="isometric",t.ShaderType=void 0,(n=t.ShaderType||(t.ShaderType={})).SPRITE="sprite",n.GRAPHIC="graphic",t.BlendMode=void 0,(h=t.BlendMode||(t.BlendMode={})).Additive="additive",h.Subtractive="subtractive",h.Mix="mix",t.ParticleShape=void 0,(o=t.ParticleShape||(t.ParticleShape={})).POINT="point",o.CIRCLE="circle",o.RECT="rect";var l;t.ArrayType=void 0,(l=t.ArrayType||(t.ArrayType={}))[l.Float32=0]="Float32",l[l.Uint32=1]="Uint32",l[l.Uint16=2]="Uint16";class u{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(e){switch(e){case t.ArrayType.Float32:return Float32Array;case t.ArrayType.Uint32:return Uint32Array;case t.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case t.ArrayType.Float32:this.typedArray=this.float32;break;case t.ArrayType.Uint32:this.typedArray=this.uint32;break;case t.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class c extends u{constructor(t,e,i=t.ARRAY_BUFFER,r=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=i,this.usage=r}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class d extends u{constructor(){super(t.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=r[i+0]*t+r[i+2]*e+r[i+4],r[i+5]=r[i+1]*t+r[i+3]*e+r[i+5]}rotate(t){const e=this.usedElemNum-6,i=this.typedArray,r=Math.cos(t),s=Math.sin(t),a=i[e+0],n=i[e+1],h=i[e+2],o=i[e+3];i[e+0]=a*r-n*s,i[e+1]=a*s+n*r,i[e+2]=h*r-o*s,i[e+3]=h*s+o*r}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const i=this.usedElemNum-6,r=this.typedArray;r[i+0]=r[i+0]*t,r[i+1]=r[i+1]*t,r[i+2]=r[i+2]*e,r[i+3]=r[i+3]*e}apply(t,e){if("number"!=typeof t)return new m(...this.apply(t.x,t.y));const i=this.usedElemNum-6,r=this.typedArray;return[r[i+0]*t+r[i+2]*e+r[i+4],r[i+1]*t+r[i+3]*e+r[i+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,i=e[t+0],r=e[t+1],s=e[t+2],a=e[t+3],n=e[t+4],h=e[t+5],o=i*a-r*s;return new Float32Array([a/o,-r/o,-s/o,i/o,(s*h-a*n)/o,(r*n-i*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,i=this.typedArray;i[e+0]=t[0],i[e+1]=t[1],i[e+2]=t[2],i[e+3]=t[3],i[e+4]=t[4],i[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new m(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=t,r[i+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,i=this.typedArray,r=this.getGlobalScale(),s=Math.cos(t),a=Math.sin(t);i[e+0]=s*r.x,i[e+1]=a*r.x,i[e+2]=-a*r.y,i[e+3]=s*r.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,i=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),r=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new m(i,r)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const i=this.getGlobalRotation(),r=Math.cos(i),s=Math.sin(i),a=this.usedElemNum-6,n=this.typedArray;n[a+0]=r*t,n[a+1]=s*t,n[a+2]=-s*e,n[a+3]=r*e}globalToLocal(t){const e=this.getInverse();return new m(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,i=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const r=t.x||0,s=t.y||0;(r||s)&&this.translate(r,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let a=t.offsetX||0,n=t.offsetY||0;t.offset&&(a+=t.offset.x,n+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(a-=h*e,n-=h*i):(a-=h.x*e,n-=h.y*i)),{offsetX:a,offsetY:n}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class p extends c{constructor(e,i,r,s){super(e,t.ArrayType.Uint16,e.ELEMENT_ARRAY_BUFFER,e.STATIC_DRAW),this.setMaxSize(i*s);for(let t=0;t<s;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class f{constructor(t,e,i,r=255){this._r=t,this._g=e,this._b=i,this._a=r,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,i,r){this.r=t,this.g=e,this.b=i,this.a=r,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new f(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),i=parseInt(t.slice(2,4),16),r=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new f(e,i,r,s)}add(t){return new f(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new f(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof f?new f(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new f(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof f?new f(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new f(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}f.Red=new f(255,0,0,255),f.Green=new f(0,255,0,255),f.Blue=new f(0,0,255,255),f.Yellow=new f(255,255,0,255),f.Purple=new f(128,0,128,255),f.Orange=new f(255,165,0,255),f.Pink=new f(255,192,203,255),f.Gray=new f(128,128,128,255),f.Brown=new f(139,69,19,255),f.Cyan=new f(0,255,255,255),f.Magenta=new f(255,0,255,255),f.Lime=new f(192,255,0,255),f.White=new f(255,255,255,255),f.Black=new f(0,0,0,255),f.TRANSPARENT=new f(0,0,0,0);class m{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}set(t,e){return Array.isArray(t)?(this.x=t[0],this.y=t[0]):e?(this.x=t,this.y=e):(this.x=t,this.y=t),this}add(t){return new m(this.x+t.x,this.y+t.y)}subtract(t){return new m(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof m?new m(this.x*t.x,this.y*t.y):new m(this.x*t,this.y*t)}divide(t){return t instanceof m?new m(this.x/t.x,this.y/t.y):new m(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,i=this.y-t.y;return Math.sqrt(e*e+i*i)}clone(){return new m(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new m((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new m(Math.abs(this.x),Math.abs(this.y))}floor(){return new m(Math.floor(this.x),Math.floor(this.y))}ceil(){return new m(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new m(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new m(t[0],t[1])))}static fromAngle(t){return new m(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),i=this.length()*t.length(),r=Math.max(-1,Math.min(1,e/i));return Math.acos(r)}}m.ZERO=new m(0,0),m.ONE=new m(1,1),m.UP=new m(0,1),m.DOWN=new m(0,-1),m.LEFT=new m(-1,0),m.RIGHT=new m(1,0);class g{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,i,r){return new m(g.float(t,e),g.float(i,r))}static direction(t){const e=g.angle();return new m(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new f(g.float(t.r,e.r),g.float(t.g,e.g),g.float(t.b,e.b),g.float(t.a,e.a))}static pick(t){return t[g.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const i of t)e+=i[1];const i=Math.random()*e;let r=0;for(const e of t)if(r+=e[1],i<=r)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return g.float(t[0],t[1]);if(t[0]instanceof m)return g.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof f)return g.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class y{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,i){const r=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const i=t[e],s=t[(e+1)%t.length];r.push([i,s])}})),i=i||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return r.forEach((([t,r])=>{const a=new m(t.x-e.x,t.y-e.y),n=new m(r.x-e.x,r.y-e.y),h=r.subtract(t).perpendicular(),o=Math.abs(h.dot(a))/(h.length()*a.length())+.01,l=Math.abs(h.dot(n))/(h.length()*n.length())+.01,u=i/o,c=i/l,d=new m(a.x,a.y).normalize(),p=new m(n.x,n.y).normalize(),f=new m(t.x+d.x*u,t.y+d.y*u),g=new m(r.x+p.x*c,r.y+p.y*c);s.push([t,r,g,f])})),s}}const x=(t,e,i,r)=>{const s=[],a=r?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),n=Math.PI;for(let e=0;e<10;e++){const r=a+e/10*n,h=a+(e+1)/10*n,o=Math.cos(r)*i,l=Math.sin(r)*i,u=Math.cos(h)*i,c=Math.sin(h)*i;s.push(t),s.push(t.add(new m(o,l))),s.push(t.add(new m(u,c)))}return s},T=e=>{const i=e.points;if(i.length<2)return{vertices:[],uv:[]};const{normals:r,length:s}=((t,e=!1)=>{const i=[];if(t.length<2||e&&t.length<3)return{normals:i,length:0};const r=t.length;let s=0;if(e)for(let e=0;e<r;e++){const i=t[e],a=t[(e+1)%r];s+=i.distanceTo(a)}else for(let e=0;e<r-1;e++)s+=t[e].distanceTo(t[e+1]);const a=(t,e,i)=>{const r=e.subtract(t).normalize(),s=e.subtract(i).normalize(),a=s.dot(r);if(a<-.999)return{normal:r.perpendicular(),miters:1};{let t=s.add(r).normalize();r.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-a)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<r-1;e++){const s=0===e?t[r-2]:t[e-1],n=t[e],h=t[e+1];i.push(a(s,n,h))}i.push(i[0])}else for(let e=0;e<r;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();i.push({normal:e.perpendicular(),miters:1})}else if(e===r-1){const r=t[e].subtract(t[e-1]).normalize();i.push({normal:r.perpendicular(),miters:1})}else i.push(a(t[e-1],t[e],t[e+1]));return{normals:i,length:s}})(i,e.closed),a=(e.width||1)/2,n=[],h=[],o=e.roundCap||!1,l=e.textureMode||t.LineTextureMode.STRETCH;let u=0;const c=e.texture?.width||1;for(let e=0;e<i.length-1;e++){const o=i[e],d=r[e].normal,p=r[e].miters,f=o.add(d.multiply(p*a)),g=o.subtract(d.multiply(p*a)),y=i[e+1],x=r[e+1].normal,T=r[e+1].miters,w=y.add(x.multiply(T*a)),b=y.subtract(x.multiply(T*a)),R=o.distanceTo(y);let E=0,S=0;l===t.LineTextureMode.STRETCH?(E=u/s,S=(u+R)/s):(E=u/c,S=E+R/c);const v=new m(E,0),M=new m(E,1),A=new m(S,0),U=new m(S,1);n.push(f),h.push(v),n.push(g),h.push(M),n.push(w),h.push(A),n.push(w),h.push(A),n.push(b),h.push(U),n.push(g),h.push(M),u+=R}if(o&&!e.closed){const t=i[0],e=r[0].normal,s=x(t,e,a,!0);n.push(...s);const h=i[i.length-1],o=r[i.length-1].normal,l=x(h,o,a,!1);n.push(...l)}return{vertices:n,uv:h}};var w="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",b="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const R=(t,e,i)=>{const r=t.createShader(i);if(!r)throw new Error("Unable to create webgl shader");t.shaderSource(r,e),t.compileShader(r);if(!t.getShaderParameter(r,t.COMPILE_STATUS)){const i=t.getShaderInfoLog(r);throw console.error("Shader compilation failed:",i),new Error("Unable to compile shader: "+i+e)}return r};function E(t,e,i,r=!1,s=!1,a="clamp"){const n=t.createTexture();if(!n)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,n),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,i?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,i?t.LINEAR:t.NEAREST),a){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),r?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),n}const S=5126;var v="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",M="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const A=[{name:"aPosition",size:2,type:S,stride:24},{name:"aRegion",size:2,type:S,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:S,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],U=[{name:"aPosition",size:2,type:S,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:S,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class P{constructor(t,e,i,r,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const a=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let i="";for(let t=0;t<e;t++)i+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,i+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",i)}return t}(i,t.maxTextureUnits-s);this.program=((t,e,i)=>{var r=t.createProgram(),s=R(t,e,35633),a=R(t,i,35632);if(!r)throw new Error("Unable to create program shader");if(t.attachShader(r,s),t.attachShader(r,a),t.linkProgram(r),!t.getProgramParameter(r,t.LINK_STATUS)){const e=t.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+e)}return r})(t.gl,e,a),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(a),r&&this.setAttributes(r)}setUniforms(t,e){const i=this.gl;for(const r of t.getUnifromNames()){const s=this.getUniform(r);t.bind(i,r,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,i=t.match(/attribute\s+\w+\s+(\w+)/g);if(i)for(const t of i){const i=t.split(" ")[2],r=e.getAttribLocation(this.program,i);-1!=r&&(this.attributeLoc[i]=r)}const r=t.match(/uniform\s+\w+\s+(\w+)/g);if(r)for(const t of r){const i=t.split(" ")[2];this.uniformLoc[i]=e.getUniformLocation(this.program,i)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const i=this.gl;i.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),i.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(e,i,r,s,a=0){let n={[t.ShaderType.SPRITE]:v,[t.ShaderType.GRAPHIC]:w}[s],h={[t.ShaderType.SPRITE]:M,[t.ShaderType.GRAPHIC]:b}[s];const o={[t.ShaderType.SPRITE]:A,[t.ShaderType.GRAPHIC]:U}[s];return n=n.replace("void main(void) {",r+"\nvoid main(void) {"),h=h.replace("void main(void) {",i+"\nvoid main(void) {"),n=n.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new P(e,h,n,o,a)}}class C{constructor(e){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=e,this.gl=e.gl,this.webglArrayBuffer=new c(e.gl,t.ArrayType.Float32,e.gl.ARRAY_BUFFER,e.gl.STREAM_DRAW),this.maxTextureUnits=e.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...i){const[r,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,i){this.setShader("default",t,e,i)}setShader(t,e,i,r){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new P(this.rapid,e,i,r)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class F extends C{constructor(t){super(t),this.vertex=0,this.offset=m.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",b,w,U)}startRender(t,e,i,r){r&&this.currentShader?.setUniforms(r,this),this.offset=new m(t,e),this.vertex=0,this.webglArrayBuffer.clear(),i&&i.base&&(this.texture=this.useTexture(i.base.texture)[0])}addVertex(t,e,i,r,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const _=Math.floor(16384);class D extends p{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class N extends C{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",M,v,A),this.indexBuffer=new D(e,_)}addVertex(t,e,i,r,s,a){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(a)}renderSprite(t,e,i,r,s,a,n,h,o,l,u,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=_||this.isUnifromChanged(u)||this.rapid.projectionDirty)&&(this.render(),u&&this.isUnifromChanged(u)&&(this.currentShader.setUniforms(u,this),this.setCurrentUniform(u)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const g=f-this.spriteTextureUnitIndexOffset,y=c?a:r,x=c?r:a,T=d?n:s,w=d?s:n,b=h,R=h+e,E=o,S=o+i;this.addVertex(b,E,y,T,g,l),this.addVertex(R,E,x,T,g,l),this.addVertex(R,S,x,w,g,l),this.addVertex(b,S,y,w,g,l)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class B{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(e,i=this.antialias,r=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);if(!s){const t=await this.loadImage(e);s=I.fromImageSource(this.render,t,i,r),this.cache.set(e,s)}return new L(s)}textureFromFrameBufferObject(t){return new L(t)}async textureFromSource(e,i=this.antialias,r=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);return s||(s=I.fromImageSource(this.render,e,i,r),this.cache.set(e,s)),new L(s)}async loadImage(t){return new Promise((e=>{const i=new Image;i.onload=()=>{e(i)},i.src=t}))}createText(t){return new k(this.render,t)}destroy(t){t instanceof L?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,i=this.antialias){return new O(this.render,t,e,i)}removeCache(t){const e=t instanceof L?t.base?.texture:t.texture;e&&this.cache.forEach(((t,i)=>{t===e&&this.cache.delete(i)}))}}class I{constructor(e,i,r,s=t.TextureWrapMode.CLAMP){this.texture=e,this.width=i,this.height=r,this.wrapMode=s}static fromImageSource(e,i,r=!1,s=t.TextureWrapMode.CLAMP){return new I(E(e.gl,i,r,!1,!1,s),i.width,i.height)}destroy(t){t.deleteTexture(this.texture)}}class L{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,i,r){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+i/this.base.width,this.clipH=this.clipY+r/this.base.height,this.width=i*this.scale,this.height=r*this.scale,this}static fromImageSource(t,e,i=!1){return new L(I.fromImageSource(t,e,i))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const i=[],r=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let a=0;a<s;a++)for(let s=0;s<r;s++){const r=this.clone();r.setClipRegion(s*t,a*e,t,e),i.push(r)}return i}clone(){return new L(this.base)}}class k extends L{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(I.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const i=this.text.split("\n");let r=0,s=0;for(const t of i){const i=e.measureText(t);r=Math.max(r,i.width),s+=this.options.fontSize||16}t.width=2*r,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let a=0;for(const t of i)e.fillText(t,0,a),a+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class O extends I{constructor(t,e,i,r=!1){const s=t.gl,a=E(s,{width:e,height:i},r,!0,!1),n=s.createFramebuffer();if(!n)throw s.deleteTexture(a),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,n),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,a,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(n),s.deleteTexture(a),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,i),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(a,e,i),this.gl=s,this.framebuffer=n,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}class G{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof L&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class z{constructor(t){this.rapid=t}getYSortRow(t,e){const i=new Map;if(!t)return i;for(const r of t){const t=Math.floor(r.ySort/e);i.has(t)||i.set(t,[]),i.get(t).push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,i=(t.errorY??2)+1;if("number"==typeof t.error){const r=(t.error??2)+1;e=r,i=r}else t.error&&(e=t.error.x+1,i=t.error.y+1);return{errorX:e,errorY:i}}renderYSortRow(t,e){if(e&&0!==e.length)for(const i of e)i.render?i.render():i.renderSprite&&t.renderSprite(i.renderSprite)}getTileData(e,i){const r=i.shape??t.TilemapShape.SQUARE,s=e.width,a=r===t.TilemapShape.ISOMETRIC?e.height/2:e.height,n=this.rapid.matrixStack,{errorX:h,errorY:o}=this.getOffset(i),l=new m(0,0),u=new m(this.rapid.width,0),c=new m(0,this.rapid.height),d=new m(this.rapid.width,this.rapid.height),p=n.globalToLocal(l),f=n.globalToLocal(u),g=n.globalToLocal(c),y=n.globalToLocal(d),x=Math.min(p.x,f.x,g.x,y.x),T=Math.max(p.x,f.x,g.x,y.x),w=Math.min(p.y,f.y,g.y,y.y),b=Math.max(p.y,f.y,g.y,y.y),R=new m(Math.floor(x/s)-h,Math.floor(w/a)-o),E=new m(Math.ceil(T/s)+h,Math.ceil(b/a)+o);return{startTile:R,viewportWidth:E.x-R.x,viewportHeight:E.y-R.y,height:a}}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const i=e.tileSet,{startTile:r,viewportWidth:s,viewportHeight:a,height:n}=this.getTileData(i,e),h=this.getYSortRow(e.ySortCallback,n),o=e.ySortCallback&&e.ySortCallback.length>0;for(let n=0;n<a;n++){const a=n+r.y,l=h.get(a)??[];if(a<0||a>=t.length)this.renderYSortRow(this.rapid,l);else{for(let n=0;n<s;n++){const s=n+r.x;if(s<0||s>=t[a].length)continue;const h=t[a][s],o=i.getTile(h);if(!o)continue;const u=this.mapToLocal(new m(s,a),e),c=this.rapid.matrixStack.localToGlobal(u).y+(o.ySortOffset??0),d=e.eachTile&&e.eachTile(h,s,a)||{};l.push({ySort:c,renderSprite:{...o,x:u.x+(o.x||0),y:u.y+(o.y||0),...d}})}o&&l.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,l)}}this.rapid.matrixStack.applyTransformAfter(e)}localToMap(e,i){const r=i.tileSet;if(i.shape===t.TilemapShape.ISOMETRIC){let t=0,i=0;const s=r.height/2,a=r.width/2;let n=Math.floor(e.y/s);const h=n%2==0;let o=Math.floor(e.x/a);const l=o%2==0,u=e.x%a/a,c=e.y%s/s,d=c<u,p=c<1-u;return h||(n-=1),d&&!l&&h?n-=1:d||!l||h?p&&l&&h?(o-=2,n-=1):p||l||h||(n+=1):(n+=1,o-=2),t=o,i=n,t=Math.floor(o/2),new m(t,i)}return new m(Math.floor(e.x/r.width),Math.floor(e.y/r.height))}mapToLocal(e,i){const r=i.tileSet;if(i.shape===t.TilemapShape.ISOMETRIC){let t=new m(e.x*r.width,e.y*r.height/2);return e.y%2!=0&&(t.x+=r.width/2),t}return new m(e.x*r.width,e.y*r.height)}}const X=t=>"object"==typeof t&&null!==t&&Object.getPrototypeOf(t)===Object.prototype;const W=!0;class j{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof L?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=g.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=g.pick(e.texture)),this.maxLife=g.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,f.White),velocity:this.processAttribute(e.animation.velocity,m.ZERO),acceleration:this.processAttribute(e.animation.acceleration,m.ZERO)},this.position=m.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if(X(t)){const i=g.scalarOrRange(t.start,e),r=g.scalarOrRange(t.end||i,e);return{delta:t.delta??this.getDelta(i,r,this.maxLife),value:i,damping:t.damping}}return this.processAttribute({start:t},e)}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const i=e.value,r=Math.pow(e.damping,t);e.value="number"==typeof i?i*r:i.multiply(r)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const i=e.value;"number"==typeof i?e.value+=t*e.delta:e.value=i.add(e.delta.multiply(t))}e.color.value.clamp();const i=m.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(i).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,i){return"number"==typeof t&&"number"==typeof e?(e-t)/i:t instanceof m&&e instanceof m||t instanceof f&&e instanceof f?e.subtract(t).divide(i):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case t.ParticleShape.POINT:this.position=m.ZERO;break;case t.ParticleShape.CIRCLE:const e=Math.random()*Math.PI*2,i=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new m(Math.cos(e)*i,Math.sin(e)*i);break;case t.ParticleShape.RECT:this.position=new m((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class Y{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=W,this.position=m.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:W,this.position=e.position||m.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new j(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,i=Math.floor(e);i>0&&(this.emit(i),this.emitTimer-=i/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}return t.BaseTexture=I,t.Color=f,t.DynamicArrayBuffer=u,t.Entity=class{constructor(t,e){this.parent=null,this.children=[],this.transform=new d,this.rapid=t,this.tags=e.tags??[],e.position?this.position=e.position:this.position=new m(e.x??0,e.y??0),"number"==typeof e.scale?this.scale=new m(e.scale,e.scale):e.scale?this.scale=e.scale:this.scale=new m(1,1),this.rotation=e.rotation??0}getParentTransform(){return this.parent?this.parent.transform:this.rapid.matrixStack}update(t){}updateTransform(t=!1){t&&this.parent&&this.parent.updateTransform(t);const e=this.transform,i=this.getParentTransform();e.setTransform(i.getTransform()),e.translate(this.position),e.rotate(this.rotation),e.scale(this.scale)}render(){this.updateTransform()}postRender(){for(const t of this.children)t.render(),t.postRender()}postUpdate(t){for(const e of this.children)e.update(t),e.postUpdate(t)}addChild(t){t.parent&&t.parent.removeChild(t),t.parent=this,this.children.push(t)}removeChild(t){const e=this.children.indexOf(t);e>-1&&(t.parent=null,this.children.splice(e,1))}root(t){this.update(t),this.postUpdate(t),this.render(),this.postRender()}findDescendant(t,e=!0){const i=[];for(const r of this.children){if(t(r)){if(e)return r;i.push(r)}const s=r.findDescendant(t,e);if(e){if(s)return s}else i.push(...s)}return e?null:i}findDescendantByTag(t,e=!1){return this.findDescendant((e=>0!==t.length&&t.every((t=>e.tags.includes(t)))),e)}},t.FrameBufferObject=O,t.GLShader=P,t.InputManager=class{constructor(t){this.mousePosition=m.ZERO,this.keysDown=new Set,this.keysDownLastFrame=new Set,this.buttonsDown=new Set,this.buttonsDownLastFrame=new Set,this.rapid=t,this.canvas=t.canvas,this.attachEventListeners()}attachEventListeners(){const t=this.canvas.getBoundingClientRect();this.canvas.addEventListener("mousemove",(e=>{this.mousePosition=this.rapid.cssToGameCoords(e.clientX-t.left,e.clientY-t.top)})),window.addEventListener("keydown",(t=>{this.keysDown.add(t.code)})),window.addEventListener("keyup",(t=>{this.keysDown.delete(t.code)})),this.canvas.addEventListener("mousedown",(t=>{this.buttonsDown.add(t.button)})),this.canvas.addEventListener("mouseup",(t=>{this.buttonsDown.delete(t.button)}))}updateNextFrame(){this.keysDownLastFrame=new Set(this.keysDown),this.buttonsDownLastFrame=new Set(this.buttonsDown)}isKeyDown(t){return this.keysDown.has(t)}isKeyUp(t){return!this.keysDown.has(t)}wasKeyPressed(t){return this.keysDown.has(t)&&!this.keysDownLastFrame.has(t)}wasKeyReleased(t){return!this.keysDown.has(t)&&this.keysDownLastFrame.has(t)}isButtonDown(t){return this.buttonsDown.has(t)}isButtonUp(t){return!this.buttonsDown.has(t)}wasButtonPressed(t){return this.buttonsDown.has(t)&&!this.buttonsDownLastFrame.has(t)}wasButtonReleased(t){return!this.buttonsDown.has(t)&&this.buttonsDownLastFrame.has(t)}getMouseLocal(t){return t.transform.globalToLocal(this.mousePosition)}},t.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},t.MatrixStack=d,t.ParticleEmitter=Y,t.Random=g,t.Rapid=class{constructor(e){this.projectionDirty=!0,this.matrixStack=new d,this.tileMap=new z(this),this.light=new y(this),this.defaultColor=new f(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const i=(t=>{const e={stencil:!0},i=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!i)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return i})(e.canvas);this.gl=i,this.canvas=e.canvas,this.textures=new B(this,e.antialias??!1),this.maxTextureUnits=i.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=e.width||this.canvas.width,this.height=e.height||this.canvas.height,this.scaleEnable=e.scaleEnable??!1,this.scaleRadio=e.scaleRadio||t.ScaleRadio.KEEP,this.devicePixelRatio=e.devicePixelRatio??window.devicePixelRatio??1,this.backgroundColor=e.backgroundColor||new f(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(i),this.updateDisplaySize(this.width,this.height),this.projectionDirty=!1,this.canvas.style.display="block"}updateDisplaySize(e,i){const r=this.width,s=this.height;let a,n,h,o;if(this.scaleEnable){const l=r/s,u=e/i;switch(this.scaleRadio){case t.ScaleRadio.IGNORE:a=r,n=s,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.ScaleRadio.KEEP:let c,d;a=r,n=s,u>l?(d=i,c=d*l):(c=e,d=c/l),h=c*this.devicePixelRatio,o=d*this.devicePixelRatio;break;case t.ScaleRadio.EXPAND:u>l?(n=s,a=s*u):(a=r,n=r/u),h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.ScaleRadio.KEEP_W:a=r,n=r/u,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.ScaleRadio.KEEP_H:n=s,a=s*u,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;default:a=r,n=s,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio}}else a=r,n=s,h=r*this.devicePixelRatio,o=s*this.devicePixelRatio;this.logicWidth=a,this.logicHeight=n,this.physicsWidth=h,this.physicsHeight=o,this.resizeSize(a,n,h,o,!0)}resizeSize(t,e,i,r,s=!1){this.updateProjection(0,t,e,0),this.gl.viewport(0,0,i,r),this.gl.scissor(0,0,i,r),s&&(this.canvas.width=i,this.canvas.height=r,this.canvas.style.width=i/this.devicePixelRatio+"px",this.canvas.style.height=r/this.devicePixelRatio+"px")}updateProjection(t,e,i,r){this.projection=this.createOrthMatrix(t,e,i,r),this.projectionDirty=!0}initWebgl(t){t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",N),this.registerRegion("graphic",F)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const i=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=i,this.currentRegionName=t,i.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),i=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,i}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof G?{tileSet:e}:e)}applyCameraTransform(t){this.withTransform((()=>{if(t.center){const t=new m(this.logicWidth,this.logicHeight).divide(2);this.matrixStack.translate(t)}this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}))}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:i,offsetY:r}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,i,r,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:i,uv:r}=T({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:i,uv:r})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,i)=>{const r=Array.isArray(t.color)?t.color[i]:t.color,s=t.uv?.[i];this.addGraphicVertex(e.x,e.y,s,r)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:i}=this.startDraw(t);this.setRegion("graphic",t.shader);const r=this.currentRegion;r.startRender(e,i,t.texture,t.uniforms),t.drawType&&(r.drawType=t.drawType)}addGraphicVertex(t,e,i,r){this.currentRegion.addVertex(t,e,i?.x,i?.y,(r||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,i=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,i)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:i}=t,r=[new m(0,0),new m(e,0),new m(e,i),new m(0,i)];this.renderGraphic({...t,points:r,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,i=t.radius,r=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const r=t/e*Math.PI*2,a=Math.cos(r)*i,n=Math.sin(r)*i;s.push(new m(a,n))}this.renderGraphic({...t,points:s,color:r,drawType:this.gl.TRIANGLE_FAN})}clear(t){const e=this.gl,i=t||this.backgroundColor;e.clearColor(i.r/255,i.g/255,i.b/255,i.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,i,r){return new Float32Array([2/(e-t),0,0,0,0,2/(r-i),0,0,0,0,-1,0,-(e+t)/(e-t),-(r+i)/(r-i),0,1])}drawMask(e=t.MaskType.Include,i){this.startDrawMask(e),i(),this.endDrawMask()}startDrawMask(e=t.MaskType.Include){const i=this.gl;this.currentMaskType.push(e),this.setMaskType(e,!0),i.stencilOp(i.KEEP,i.KEEP,i.REPLACE),i.colorMask(!1,!1,!1,!1)}endDrawMask(){const e=this.gl;this.quitCurrentRegion(),e.stencilOp(e.KEEP,e.KEEP,e.KEEP),e.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??t.MaskType.Include,!1)}setMaskType(e,i=!1){const r=this.gl;if(this.quitCurrentRegion(),i)this.clearMask(),r.stencilFunc(r.ALWAYS,1,255);else switch(e){case t.MaskType.Include:r.stencilFunc(r.EQUAL,1,255);break;case t.MaskType.Exclude:r.stencilFunc(r.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,i,r=0){return P.createCostumShader(this,t,e,i,r)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeSize(t.width,t.height,t.width,t.height),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeSize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(e){switch(e){case t.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case t.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case t.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(e){this.startDrawMask(e.type||t.MaskType.Exclude);this.light.createLightShadowMaskPolygon(e.occlusion,e.lightSource,e.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:f.Black})})),this.endDrawMask()}createParticleEmitter(t){return new Y(this,t)}cssToGameCoords(t,e){const i=t instanceof m?t:new m(t,e),r=this.canvas.getBoundingClientRect(),s=i.x/r.width,a=i.y/r.height,n=s*this.logicWidth,h=a*this.logicHeight;return new m(n,h)}gameToCssCoords(t,e){const i=t instanceof m?t:new m(t,e??0),r=this.canvas.getBoundingClientRect(),s=i.x/this.logicWidth,a=i.y/this.logicHeight,n=s*r.width,h=a*r.height;return new m(n,h)}},t.SCALEFACTOR=2,t.Text=k,t.Texture=L,t.TextureCache=B,t.TileMapRender=z,t.TileSet=G,t.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,i,r){if(!i)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(i,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(i,s[0]):t.uniform1f(i,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(i,s):t.uniform2fv(i,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(i,s):t.uniform3fv(i,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(i,s):t.uniform4fv(i,s);break;case 9:t.uniformMatrix3fv(i,!1,s);break;case 16:t.uniformMatrix4fv(i,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(i,s?1:0);else if(s.base?.texture){const e=r.useTexture(s.base.texture)[0];t.uniform1i(i,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}},t.Vec2=m,t.WebglBufferArray=c,t.WebglElementBufferArray=p,t.graphicAttributes=U,t.isPlainObject=X,t.spriteAttributes=A,t}({});
|
package/dist/rapid.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var t,e,i,r,s,n,a,h;!function(t){t.KEEP="keep",t.KEEP_H="keep_h",t.KEEP_W="keep_w",t.IGNORE="ignore",t.EXPAND="expand"}(t||(t={})),function(t){t.STRETCH="stretch",t.REPEAT="repeat"}(e||(e={})),function(t){t.REPEAT="repeat",t.CLAMP="clamp",t.MIRROR="mirror"}(i||(i={})),function(t){t.Include="normal",t.Exclude="inverse"}(r||(r={})),function(t){t.SQUARE="square",t.ISOMETRIC="isometric"}(s||(s={})),function(t){t.SPRITE="sprite",t.GRAPHIC="graphic"}(n||(n={})),function(t){t.Additive="additive",t.Subtractive="subtractive",t.Mix="mix"}(a||(a={})),function(t){t.POINT="point",t.CIRCLE="circle",t.RECT="rect"}(h||(h={}));var o;!function(t){t[t.Float32=0]="Float32",t[t.Uint32=1]="Uint32",t[t.Uint16=2]="Uint16"}(o||(o={}));class l{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case o.Float32:return Float32Array;case o.Uint32:return Uint32Array;case o.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case o.Float32:this.typedArray=this.float32;break;case o.Uint32:this.typedArray=this.uint32;break;case o.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class u extends l{constructor(t,e,i=t.ARRAY_BUFFER,r=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=i,this.usage=r}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class c extends l{constructor(){super(o.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=r[i+0]*t+r[i+2]*e+r[i+4],r[i+5]=r[i+1]*t+r[i+3]*e+r[i+5]}rotate(t){const e=this.usedElemNum-6,i=this.typedArray,r=Math.cos(t),s=Math.sin(t),n=i[e+0],a=i[e+1],h=i[e+2],o=i[e+3];i[e+0]=n*r-a*s,i[e+1]=n*s+a*r,i[e+2]=h*r-o*s,i[e+3]=h*s+o*r}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const i=this.usedElemNum-6,r=this.typedArray;r[i+0]=r[i+0]*t,r[i+1]=r[i+1]*t,r[i+2]=r[i+2]*e,r[i+3]=r[i+3]*e}apply(t,e){if("number"!=typeof t)return new f(...this.apply(t.x,t.y));const i=this.usedElemNum-6,r=this.typedArray;return[r[i+0]*t+r[i+2]*e+r[i+4],r[i+1]*t+r[i+3]*e+r[i+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,i=e[t+0],r=e[t+1],s=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=i*n-r*s;return new Float32Array([n/o,-r/o,-s/o,i/o,(s*h-n*a)/o,(r*a-i*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,i=this.typedArray;i[e+0]=t[0],i[e+1]=t[1],i[e+2]=t[2],i[e+3]=t[3],i[e+4]=t[4],i[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new f(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=t,r[i+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,i=this.typedArray,r=this.getGlobalScale(),s=Math.cos(t),n=Math.sin(t);i[e+0]=s*r.x,i[e+1]=n*r.x,i[e+2]=-n*r.y,i[e+3]=s*r.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,i=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),r=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new f(i,r)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const i=this.getGlobalRotation(),r=Math.cos(i),s=Math.sin(i),n=this.usedElemNum-6,a=this.typedArray;a[n+0]=r*t,a[n+1]=s*t,a[n+2]=-s*e,a[n+3]=r*e}globalToLocal(t){const e=this.getInverse();return new f(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,i=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const r=t.x||0,s=t.y||0;(r||s)&&this.translate(r,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let n=t.offsetX||0,a=t.offsetY||0;t.offset&&(n+=t.offset.x,a+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(n-=h*e,a-=h*i):(n-=h.x*e,a-=h.y*i)),{offsetX:n,offsetY:a}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class d extends u{constructor(t,e,i,r){super(t,o.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*r);for(let t=0;t<r;t++)this.addObject(t*i);this.bindBuffer(),this.bufferData()}addObject(t){}}class p{constructor(t,e,i,r=255){this._r=t,this._g=e,this._b=i,this._a=r,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,i,r){this.r=t,this.g=e,this.b=i,this.a=r,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),i=parseInt(t.slice(2,4),16),r=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,i,r,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}p.Red=new p(255,0,0,255),p.Green=new p(0,255,0,255),p.Blue=new p(0,0,255,255),p.Yellow=new p(255,255,0,255),p.Purple=new p(128,0,128,255),p.Orange=new p(255,165,0,255),p.Pink=new p(255,192,203,255),p.Gray=new p(128,128,128,255),p.Brown=new p(139,69,19,255),p.Cyan=new p(0,255,255,255),p.Magenta=new p(255,0,255,255),p.Lime=new p(192,255,0,255),p.White=new p(255,255,255,255),p.Black=new p(0,0,0,255),p.TRANSPARENT=new p(0,0,0,0);class f{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}set(t,e){return Array.isArray(t)?(this.x=t[0],this.y=t[0]):e?(this.x=t,this.y=e):(this.x=t,this.y=t),this}add(t){return new f(this.x+t.x,this.y+t.y)}subtract(t){return new f(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof f?new f(this.x*t.x,this.y*t.y):new f(this.x*t,this.y*t)}divide(t){return t instanceof f?new f(this.x/t.x,this.y/t.y):new f(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,i=this.y-t.y;return Math.sqrt(e*e+i*i)}clone(){return new f(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new f((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new f(Math.abs(this.x),Math.abs(this.y))}floor(){return new f(Math.floor(this.x),Math.floor(this.y))}ceil(){return new f(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new f(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new f(t[0],t[1])))}static fromAngle(t){return new f(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),i=this.length()*t.length(),r=Math.max(-1,Math.min(1,e/i));return Math.acos(r)}}f.ZERO=new f(0,0),f.ONE=new f(1,1),f.UP=new f(0,1),f.DOWN=new f(0,-1),f.LEFT=new f(-1,0),f.RIGHT=new f(1,0);class m{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}}class g{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,i,r){return new f(g.float(t,e),g.float(i,r))}static direction(t){const e=g.angle();return new f(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new p(g.float(t.r,e.r),g.float(t.g,e.g),g.float(t.b,e.b),g.float(t.a,e.a))}static pick(t){return t[g.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const i of t)e+=i[1];const i=Math.random()*e;let r=0;for(const e of t)if(r+=e[1],i<=r)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return g.float(t[0],t[1]);if(t[0]instanceof f)return g.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof p)return g.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class x{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,i){const r=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const i=t[e],s=t[(e+1)%t.length];r.push([i,s])}})),i=i||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return r.forEach((([t,r])=>{const n=new f(t.x-e.x,t.y-e.y),a=new f(r.x-e.x,r.y-e.y),h=r.subtract(t).perpendicular(),o=Math.abs(h.dot(n))/(h.length()*n.length())+.01,l=Math.abs(h.dot(a))/(h.length()*a.length())+.01,u=i/o,c=i/l,d=new f(n.x,n.y).normalize(),p=new f(a.x,a.y).normalize(),m=new f(t.x+d.x*u,t.y+d.y*u),g=new f(r.x+p.x*c,r.y+p.y*c);s.push([t,r,g,m])})),s}}const y=(t,e,i,r)=>{const s=[],n=r?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),a=Math.PI;for(let e=0;e<10;e++){const r=n+e/10*a,h=n+(e+1)/10*a,o=Math.cos(r)*i,l=Math.sin(r)*i,u=Math.cos(h)*i,c=Math.sin(h)*i;s.push(t),s.push(t.add(new f(o,l))),s.push(t.add(new f(u,c)))}return s},T=t=>{const i=t.points;if(i.length<2)return{vertices:[],uv:[]};const{normals:r,length:s}=((t,e=!1)=>{const i=[];if(t.length<2||e&&t.length<3)return{normals:i,length:0};const r=t.length;let s=0;if(e)for(let e=0;e<r;e++){const i=t[e],n=t[(e+1)%r];s+=i.distanceTo(n)}else for(let e=0;e<r-1;e++)s+=t[e].distanceTo(t[e+1]);const n=(t,e,i)=>{const r=e.subtract(t).normalize(),s=e.subtract(i).normalize(),n=s.dot(r);if(n<-.999)return{normal:r.perpendicular(),miters:1};{let t=s.add(r).normalize();r.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-n)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<r-1;e++){const s=0===e?t[r-2]:t[e-1],a=t[e],h=t[e+1];i.push(n(s,a,h))}i.push(i[0])}else for(let e=0;e<r;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();i.push({normal:e.perpendicular(),miters:1})}else if(e===r-1){const r=t[e].subtract(t[e-1]).normalize();i.push({normal:r.perpendicular(),miters:1})}else i.push(n(t[e-1],t[e],t[e+1]));return{normals:i,length:s}})(i,t.closed),n=(t.width||1)/2,a=[],h=[],o=t.roundCap||!1,l=t.textureMode||e.STRETCH;let u=0;const c=t.texture?.width||1;for(let t=0;t<i.length-1;t++){const o=i[t],d=r[t].normal,p=r[t].miters,m=o.add(d.multiply(p*n)),g=o.subtract(d.multiply(p*n)),x=i[t+1],y=r[t+1].normal,T=r[t+1].miters,w=x.add(y.multiply(T*n)),b=x.subtract(y.multiply(T*n)),E=o.distanceTo(x);let R=0,v=0;l===e.STRETCH?(R=u/s,v=(u+E)/s):(R=u/c,v=R+E/c);const S=new f(R,0),A=new f(R,1),M=new f(v,0),U=new f(v,1);a.push(m),h.push(S),a.push(g),h.push(A),a.push(w),h.push(M),a.push(w),h.push(M),a.push(b),h.push(U),a.push(g),h.push(A),u+=E}if(o&&!t.closed){const t=i[0],e=r[0].normal,s=y(t,e,n,!0);a.push(...s);const h=i[i.length-1],o=r[i.length-1].normal,l=y(h,o,n,!1);a.push(...l)}return{vertices:a,uv:h}};var w="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",b="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const E=(t,e,i)=>{const r=t.createShader(i);if(!r)throw new Error("Unable to create webgl shader");t.shaderSource(r,e),t.compileShader(r);if(!t.getShaderParameter(r,t.COMPILE_STATUS)){const i=t.getShaderInfoLog(r);throw console.error("Shader compilation failed:",i),new Error("Unable to compile shader: "+i+e)}return r};function R(t,e,i,r=!1,s=!1,n="clamp"){const a=t.createTexture();if(!a)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,i?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,i?t.LINEAR:t.NEAREST),n){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),r?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),a}const v=5126;var S="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",A="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const M=[{name:"aPosition",size:2,type:v,stride:24},{name:"aRegion",size:2,type:v,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:v,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],U=[{name:"aPosition",size:2,type:v,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:v,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class P{constructor(t,e,i,r,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const n=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let i="";for(let t=0;t<e;t++)i+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,i+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",i)}return t}(i,t.maxTextureUnits-s);this.program=((t,e,i)=>{var r=t.createProgram(),s=E(t,e,35633),n=E(t,i,35632);if(!r)throw new Error("Unable to create program shader");if(t.attachShader(r,s),t.attachShader(r,n),t.linkProgram(r),!t.getProgramParameter(r,t.LINK_STATUS)){const e=t.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+e)}return r})(t.gl,e,n),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(n),r&&this.setAttributes(r)}setUniforms(t,e){const i=this.gl;for(const r of t.getUnifromNames()){const s=this.getUniform(r);t.bind(i,r,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,i=t.match(/attribute\s+\w+\s+(\w+)/g);if(i)for(const t of i){const i=t.split(" ")[2],r=e.getAttribLocation(this.program,i);-1!=r&&(this.attributeLoc[i]=r)}const r=t.match(/uniform\s+\w+\s+(\w+)/g);if(r)for(const t of r){const i=t.split(" ")[2];this.uniformLoc[i]=e.getUniformLocation(this.program,i)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const i=this.gl;i.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),i.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,i,r,s=0){let a={[n.SPRITE]:S,[n.GRAPHIC]:w}[r],h={[n.SPRITE]:A,[n.GRAPHIC]:b}[r];const o={[n.SPRITE]:M,[n.GRAPHIC]:U}[r];return a=a.replace("void main(void) {",i+"\nvoid main(void) {"),h=h.replace("void main(void) {",e+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new P(t,h,a,o,s)}}class C{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new u(t.gl,o.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...i){const[r,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,i){this.setShader("default",t,e,i)}setShader(t,e,i,r){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new P(this.rapid,e,i,r)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class F extends C{constructor(t){super(t),this.vertex=0,this.offset=f.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",b,w,U)}startRender(t,e,i,r){r&&this.currentShader?.setUniforms(r,this),this.offset=new f(t,e),this.vertex=0,this.webglArrayBuffer.clear(),i&&i.base&&(this.texture=this.useTexture(i.base.texture)[0])}addVertex(t,e,i,r,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const _=Math.floor(16384);class N extends d{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class D extends C{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",A,S,M),this.indexBuffer=new N(e,_)}addVertex(t,e,i,r,s,n){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(n)}renderSprite(t,e,i,r,s,n,a,h,o,l,u,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=_||this.isUnifromChanged(u)||this.rapid.projectionDirty)&&(this.render(),u&&this.isUnifromChanged(u)&&(this.currentShader.setUniforms(u,this),this.setCurrentUniform(u)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const g=f-this.spriteTextureUnitIndexOffset,x=c?n:r,y=c?r:n,T=d?a:s,w=d?s:a,b=h,E=h+e,R=o,v=o+i;this.addVertex(b,R,x,T,g,l),this.addVertex(E,R,y,T,g,l),this.addVertex(E,v,y,w,g,l),this.addVertex(b,v,x,w,g,l)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class I{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,e=this.antialias,r=i.CLAMP){let s=this.cache.get(t);if(!s){const i=await this.loadImage(t);s=L.fromImageSource(this.render,i,e,r),this.cache.set(t,s)}return new B(s)}textureFromFrameBufferObject(t){return new B(t)}async textureFromSource(t,e=this.antialias,r=i.CLAMP){let s=this.cache.get(t);return s||(s=L.fromImageSource(this.render,t,e,r),this.cache.set(t,s)),new B(s)}async loadImage(t){return new Promise((e=>{const i=new Image;i.onload=()=>{e(i)},i.src=t}))}createText(t){return new O(this.render,t)}destroy(t){t instanceof B?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,i=this.antialias){return new G(this.render,t,e,i)}removeCache(t){const e=t instanceof B?t.base?.texture:t.texture;e&&this.cache.forEach(((t,i)=>{t===e&&this.cache.delete(i)}))}}class L{constructor(t,e,r,s=i.CLAMP){this.texture=t,this.width=e,this.height=r,this.wrapMode=s}static fromImageSource(t,e,r=!1,s=i.CLAMP){return new L(R(t.gl,e,r,!1,!1,s),e.width,e.height)}destroy(t){t.deleteTexture(this.texture)}}class B{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,i,r){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+i/this.base.width,this.clipH=this.clipY+r/this.base.height,this.width=i*this.scale,this.height=r*this.scale,this}static fromImageSource(t,e,i=!1){return new B(L.fromImageSource(t,e,i))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const i=[],r=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let n=0;n<s;n++)for(let s=0;s<r;s++){const r=this.clone();r.setClipRegion(s*t,n*e,t,e),i.push(r)}return i}clone(){return new B(this.base)}}const k=2;class O extends B{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(L.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const i=this.text.split("\n");let r=0,s=0;for(const t of i){const i=e.measureText(t);r=Math.max(r,i.width),s+=this.options.fontSize||16}t.width=2*r,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of i)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class G extends L{constructor(t,e,i,r=!1){const s=t.gl,n=R(s,{width:e,height:i},r,!0,!1),a=s.createFramebuffer();if(!a)throw s.deleteTexture(n),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,a),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,n,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(a),s.deleteTexture(n),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,i),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(n,e,i),this.gl=s,this.framebuffer=a,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}class z{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof B&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class X{constructor(t){this.rapid=t}getYSortRow(t,e){const i=new Map;if(!t)return i;for(const r of t){const t=Math.floor(r.ySort/e);i.has(t)||i.set(t,[]),i.get(t).push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,i=(t.errorY??2)+1;if("number"==typeof t.error){const r=(t.error??2)+1;e=r,i=r}else t.error&&(e=t.error.x+1,i=t.error.y+1);return{errorX:e,errorY:i}}renderYSortRow(t,e){if(e&&0!==e.length)for(const i of e)i.render?i.render():i.renderSprite&&t.renderSprite(i.renderSprite)}getTileData(t,e){const i=e.shape??s.SQUARE,r=t.width,n=i===s.ISOMETRIC?t.height/2:t.height,a=this.rapid.matrixStack,{errorX:h,errorY:o}=this.getOffset(e),l=new f(0,0),u=new f(this.rapid.width,0),c=new f(0,this.rapid.height),d=new f(this.rapid.width,this.rapid.height),p=a.globalToLocal(l),m=a.globalToLocal(u),g=a.globalToLocal(c),x=a.globalToLocal(d),y=Math.min(p.x,m.x,g.x,x.x),T=Math.max(p.x,m.x,g.x,x.x),w=Math.min(p.y,m.y,g.y,x.y),b=Math.max(p.y,m.y,g.y,x.y),E=new f(Math.floor(y/r)-h,Math.floor(w/n)-o),R=new f(Math.ceil(T/r)+h,Math.ceil(b/n)+o);return{startTile:E,viewportWidth:R.x-E.x,viewportHeight:R.y-E.y,height:n}}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const i=e.tileSet,{startTile:r,viewportWidth:s,viewportHeight:n,height:a}=this.getTileData(i,e),h=this.getYSortRow(e.ySortCallback,a),o=e.ySortCallback&&e.ySortCallback.length>0;for(let a=0;a<n;a++){const n=a+r.y,l=h.get(n)??[];if(n<0||n>=t.length)this.renderYSortRow(this.rapid,l);else{for(let a=0;a<s;a++){const s=a+r.x;if(s<0||s>=t[n].length)continue;const h=t[n][s],o=i.getTile(h);if(!o)continue;const u=this.mapToLocal(new f(s,n),e),c=this.rapid.matrixStack.localToGlobal(u).y+(o.ySortOffset??0),d=e.eachTile&&e.eachTile(h,s,n)||{};l.push({ySort:c,renderSprite:{...o,x:u.x+(o.x||0),y:u.y+(o.y||0),...d}})}o&&l.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,l)}}this.rapid.matrixStack.applyTransformAfter(e)}localToMap(t,e){const i=e.tileSet;if(e.shape===s.ISOMETRIC){let e=0,r=0;const s=i.height/2,n=i.width/2;let a=Math.floor(t.y/s);const h=a%2==0;let o=Math.floor(t.x/n);const l=o%2==0,u=t.x%n/n,c=t.y%s/s,d=c<u,p=c<1-u;return h||(a-=1),d&&!l&&h?a-=1:d||!l||h?p&&l&&h?(o-=2,a-=1):p||l||h||(a+=1):(a+=1,o-=2),e=o,r=a,e=Math.floor(o/2),new f(e,r)}return new f(Math.floor(t.x/i.width),Math.floor(t.y/i.height))}mapToLocal(t,e){const i=e.tileSet;if(e.shape===s.ISOMETRIC){let e=new f(t.x*i.width,t.y*i.height/2);return t.y%2!=0&&(e.x+=i.width/2),e}return new f(t.x*i.width,t.y*i.height)}}const Y=t=>"object"==typeof t&&null!==t&&Object.getPrototypeOf(t)===Object.prototype;class j{constructor(t,e){this.parent=null,this.children=[],this.transform=new c,this.rapid=t,this.tags=e.tags??[],e.position?this.position=e.position:this.position=new f(e.x??0,e.y??0),"number"==typeof e.scale?this.scale=new f(e.scale,e.scale):e.scale?this.scale=e.scale:this.scale=new f(1,1),this.rotation=e.rotation??0}getParentTransform(){return this.parent?this.parent.transform:this.rapid.matrixStack}update(t){}updateTransform(t=!1){t&&this.parent&&this.parent.updateTransform(t);const e=this.transform,i=this.getParentTransform();e.setTransform(i.getTransform()),e.translate(this.position),e.rotate(this.rotation),e.scale(this.scale)}render(){this.updateTransform()}postRender(){for(const t of this.children)t.render(),t.postRender()}postUpdate(t){for(const e of this.children)e.update(t),e.postUpdate(t)}addChild(t){t.parent&&t.parent.removeChild(t),t.parent=this,this.children.push(t)}removeChild(t){const e=this.children.indexOf(t);e>-1&&(t.parent=null,this.children.splice(e,1))}root(t){this.update(t),this.postUpdate(t),this.render(),this.postRender()}findDescendant(t,e=!0){const i=[];for(const r of this.children){if(t(r)){if(e)return r;i.push(r)}const s=r.findDescendant(t,e);if(e){if(s)return s}else i.push(...s)}return e?null:i}findDescendantByTag(t,e=!1){return this.findDescendant((e=>0!==t.length&&t.every((t=>e.tags.includes(t)))),e)}}const H=!0;class W{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof B?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=g.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=g.pick(e.texture)),this.maxLife=g.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,p.White),velocity:this.processAttribute(e.animation.velocity,f.ZERO),acceleration:this.processAttribute(e.animation.acceleration,f.ZERO)},this.position=f.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if(Y(t)){const i=g.scalarOrRange(t.start,e),r=g.scalarOrRange(t.end||i,e);return{delta:t.delta??this.getDelta(i,r,this.maxLife),value:i,damping:t.damping}}return this.processAttribute({start:t},e)}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const i=e.value,r=Math.pow(e.damping,t);e.value="number"==typeof i?i*r:i.multiply(r)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const i=e.value;"number"==typeof i?e.value+=t*e.delta:e.value=i.add(e.delta.multiply(t))}e.color.value.clamp();const i=f.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(i).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,i){return"number"==typeof t&&"number"==typeof e?(e-t)/i:t instanceof f&&e instanceof f||t instanceof p&&e instanceof p?e.subtract(t).divide(i):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case h.POINT:this.position=f.ZERO;break;case h.CIRCLE:const t=Math.random()*Math.PI*2,e=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new f(Math.cos(t)*e,Math.sin(t)*e);break;case h.RECT:this.position=new f((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class K{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=H,this.position=f.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:H,this.position=e.position||f.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new W(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,i=Math.floor(e);i>0&&(this.emit(i),this.emitTimer-=i/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}class q{constructor(e){this.projectionDirty=!0,this.matrixStack=new c,this.tileMap=new X(this),this.light=new x(this),this.defaultColor=new p(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const i=(t=>{const e={stencil:!0},i=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!i)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return i})(e.canvas);this.gl=i,this.canvas=e.canvas,this.textures=new I(this,e.antialias??!1),this.maxTextureUnits=i.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=e.width||this.canvas.width,this.height=e.height||this.canvas.height,this.scaleEnable=e.scaleEnable??!1,this.scaleRadio=e.scaleRadio||t.KEEP,this.devicePixelRatio=e.devicePixelRatio??window.devicePixelRatio??1,this.backgroundColor=e.backgroundColor||new p(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(i),this.updateDisplaySize(this.width,this.height),this.projectionDirty=!1,this.canvas.style.display="block"}updateDisplaySize(e,i){const r=this.width,s=this.height;let n,a,h,o;if(this.scaleEnable){const l=r/s,u=e/i;switch(this.scaleRadio){case t.IGNORE:n=r,a=s,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.KEEP:let c,d;n=r,a=s,u>l?(d=i,c=d*l):(c=e,d=c/l),h=c*this.devicePixelRatio,o=d*this.devicePixelRatio;break;case t.EXPAND:u>l?(a=s,n=s*u):(n=r,a=r/u),h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.KEEP_W:n=r,a=r/u,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;case t.KEEP_H:a=s,n=s*u,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio;break;default:n=r,a=s,h=e*this.devicePixelRatio,o=i*this.devicePixelRatio}}else n=r,a=s,h=r*this.devicePixelRatio,o=s*this.devicePixelRatio;this.logicWidth=n,this.logicHeight=a,this.physicsWidth=h,this.physicsHeight=o,this.resizeSize(n,a,h,o,!0)}resizeSize(t,e,i,r,s=!1){this.updateProjection(0,t,e,0),this.gl.viewport(0,0,i,r),this.gl.scissor(0,0,i,r),s&&(this.canvas.width=i,this.canvas.height=r,this.canvas.style.width=i/this.devicePixelRatio+"px",this.canvas.style.height=r/this.devicePixelRatio+"px")}updateProjection(t,e,i,r){this.projection=this.createOrthMatrix(t,e,i,r),this.projectionDirty=!0}initWebgl(t){t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",D),this.registerRegion("graphic",F)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const i=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=i,this.currentRegionName=t,i.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),i=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,i}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof z?{tileSet:e}:e)}applyCameraTransform(t){this.withTransform((()=>{if(t.center){const t=new f(this.logicWidth,this.logicHeight).divide(2);this.matrixStack.translate(t)}this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}))}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:i,offsetY:r}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,i,r,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:i,uv:r}=T({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:i,uv:r})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,i)=>{const r=Array.isArray(t.color)?t.color[i]:t.color,s=t.uv?.[i];this.addGraphicVertex(e.x,e.y,s,r)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:i}=this.startDraw(t);this.setRegion("graphic",t.shader);const r=this.currentRegion;r.startRender(e,i,t.texture,t.uniforms),t.drawType&&(r.drawType=t.drawType)}addGraphicVertex(t,e,i,r){this.currentRegion.addVertex(t,e,i?.x,i?.y,(r||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,i=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,i)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:i}=t,r=[new f(0,0),new f(e,0),new f(e,i),new f(0,i)];this.renderGraphic({...t,points:r,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,i=t.radius,r=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const r=t/e*Math.PI*2,n=Math.cos(r)*i,a=Math.sin(r)*i;s.push(new f(n,a))}this.renderGraphic({...t,points:s,color:r,drawType:this.gl.TRIANGLE_FAN})}clear(t){const e=this.gl,i=t||this.backgroundColor;e.clearColor(i.r/255,i.g/255,i.b/255,i.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,i,r){return new Float32Array([2/(e-t),0,0,0,0,2/(r-i),0,0,0,0,-1,0,-(e+t)/(e-t),-(r+i)/(r-i),0,1])}drawMask(t=r.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=r.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??r.Include,!1)}setMaskType(t,e=!1){const i=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),i.stencilFunc(i.ALWAYS,1,255);else switch(t){case r.Include:i.stencilFunc(i.EQUAL,1,255);break;case r.Exclude:i.stencilFunc(i.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,i,r=0){return P.createCostumShader(this,t,e,i,r)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeSize(t.width,t.height,t.width,t.height),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeSize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case a.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case a.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case a.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||r.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:p.Black})})),this.endDrawMask()}createParticleEmitter(t){return new K(this,t)}cssToGameCoords(t,e){const i=t instanceof f?t:new f(t,e),r=this.canvas.getBoundingClientRect(),s=i.x/r.width,n=i.y/r.height,a=s*this.logicWidth,h=n*this.logicHeight;return new f(a,h)}gameToCssCoords(t,e){const i=t instanceof f?t:new f(t,e??0),r=this.canvas.getBoundingClientRect(),s=i.x/this.logicWidth,n=i.y/this.logicHeight,a=s*r.width,h=n*r.height;return new f(a,h)}}class ${constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,i,r){if(!i)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(i,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(i,s[0]):t.uniform1f(i,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(i,s):t.uniform2fv(i,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(i,s):t.uniform3fv(i,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(i,s):t.uniform4fv(i,s);break;case 9:t.uniformMatrix3fv(i,!1,s);break;case 16:t.uniformMatrix4fv(i,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(i,s?1:0);else if(s.base?.texture){const e=r.useTexture(s.base.texture)[0];t.uniform1i(i,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}}class V{constructor(t){this.mousePosition=f.ZERO,this.keysDown=new Set,this.keysDownLastFrame=new Set,this.buttonsDown=new Set,this.buttonsDownLastFrame=new Set,this.rapid=t,this.canvas=t.canvas,this.attachEventListeners()}attachEventListeners(){const t=this.canvas.getBoundingClientRect();this.canvas.addEventListener("mousemove",(e=>{this.mousePosition=this.rapid.cssToGameCoords(e.clientX-t.left,e.clientY-t.top)})),window.addEventListener("keydown",(t=>{this.keysDown.add(t.code)})),window.addEventListener("keyup",(t=>{this.keysDown.delete(t.code)})),this.canvas.addEventListener("mousedown",(t=>{this.buttonsDown.add(t.button)})),this.canvas.addEventListener("mouseup",(t=>{this.buttonsDown.delete(t.button)}))}updateNextFrame(){this.keysDownLastFrame=new Set(this.keysDown),this.buttonsDownLastFrame=new Set(this.buttonsDown)}isKeyDown(t){return this.keysDown.has(t)}isKeyUp(t){return!this.keysDown.has(t)}wasKeyPressed(t){return this.keysDown.has(t)&&!this.keysDownLastFrame.has(t)}wasKeyReleased(t){return!this.keysDown.has(t)&&this.keysDownLastFrame.has(t)}isButtonDown(t){return this.buttonsDown.has(t)}isButtonUp(t){return!this.buttonsDown.has(t)}wasButtonPressed(t){return this.buttonsDown.has(t)&&!this.buttonsDownLastFrame.has(t)}wasButtonReleased(t){return!this.buttonsDown.has(t)&&this.buttonsDownLastFrame.has(t)}getMouseLocal(t){return t.transform.globalToLocal(this.mousePosition)}}export{o as ArrayType,L as BaseTexture,a as BlendMode,p as Color,l as DynamicArrayBuffer,j as Entity,G as FrameBufferObject,P as GLShader,V as InputManager,e as LineTextureMode,r as MaskType,m as MathUtils,c as MatrixStack,K as ParticleEmitter,h as ParticleShape,g as Random,q as Rapid,k as SCALEFACTOR,t as ScaleRadio,n as ShaderType,O as Text,B as Texture,I as TextureCache,i as TextureWrapMode,X as TileMapRender,z as TileSet,s as TilemapShape,$ as Uniform,f as Vec2,u as WebglBufferArray,d as WebglElementBufferArray,U as graphicAttributes,Y as isPlainObject,M as spriteAttributes};
|
package/dist/rapid.umd.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var t,e,i,r,s,a,n,o;exports.ScaleRadio=void 0,(t=exports.ScaleRadio||(exports.ScaleRadio={})).KEEP="keep",t.KEEP_H="keep_h",t.KEEP_W="keep_w",t.IGNORE="ignore",t.EXPAND="expand",exports.LineTextureMode=void 0,(e=exports.LineTextureMode||(exports.LineTextureMode={})).STRETCH="stretch",e.REPEAT="repeat",exports.TextureWrapMode=void 0,(i=exports.TextureWrapMode||(exports.TextureWrapMode={})).REPEAT="repeat",i.CLAMP="clamp",i.MIRROR="mirror",exports.MaskType=void 0,(r=exports.MaskType||(exports.MaskType={})).Include="normal",r.Exclude="inverse",exports.TilemapShape=void 0,(s=exports.TilemapShape||(exports.TilemapShape={})).SQUARE="square",s.ISOMETRIC="isometric",exports.ShaderType=void 0,(a=exports.ShaderType||(exports.ShaderType={})).SPRITE="sprite",a.GRAPHIC="graphic",exports.BlendMode=void 0,(n=exports.BlendMode||(exports.BlendMode={})).Additive="additive",n.Subtractive="subtractive",n.Mix="mix",exports.ParticleShape=void 0,(o=exports.ParticleShape||(exports.ParticleShape={})).POINT="point",o.CIRCLE="circle",o.RECT="rect";var h;exports.ArrayType=void 0,(h=exports.ArrayType||(exports.ArrayType={}))[h.Float32=0]="Float32",h[h.Uint32=1]="Uint32",h[h.Uint16=2]="Uint16";class l{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case exports.ArrayType.Float32:return Float32Array;case exports.ArrayType.Uint32:return Uint32Array;case exports.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case exports.ArrayType.Float32:this.typedArray=this.float32;break;case exports.ArrayType.Uint32:this.typedArray=this.uint32;break;case exports.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class u extends l{constructor(t,e,i=t.ARRAY_BUFFER,r=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=i,this.usage=r}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class c extends l{constructor(){super(exports.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=r[i+0]*t+r[i+2]*e+r[i+4],r[i+5]=r[i+1]*t+r[i+3]*e+r[i+5]}rotate(t){const e=this.usedElemNum-6,i=this.typedArray,r=Math.cos(t),s=Math.sin(t),a=i[e+0],n=i[e+1],o=i[e+2],h=i[e+3];i[e+0]=a*r-n*s,i[e+1]=a*s+n*r,i[e+2]=o*r-h*s,i[e+3]=o*s+h*r}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const i=this.usedElemNum-6,r=this.typedArray;r[i+0]=r[i+0]*t,r[i+1]=r[i+1]*t,r[i+2]=r[i+2]*e,r[i+3]=r[i+3]*e}apply(t,e){if("number"!=typeof t)return new f(...this.apply(t.x,t.y));const i=this.usedElemNum-6,r=this.typedArray;return[r[i+0]*t+r[i+2]*e+r[i+4],r[i+1]*t+r[i+3]*e+r[i+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,i=e[t+0],r=e[t+1],s=e[t+2],a=e[t+3],n=e[t+4],o=e[t+5],h=i*a-r*s;return new Float32Array([a/h,-r/h,-s/h,i/h,(s*o-a*n)/h,(r*n-i*o)/h])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,i=this.typedArray;i[e+0]=t[0],i[e+1]=t[1],i[e+2]=t[2],i[e+3]=t[3],i[e+4]=t[4],i[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new f(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=t,r[i+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,i=this.typedArray,r=this.getGlobalScale(),s=Math.cos(t),a=Math.sin(t);i[e+0]=s*r.x,i[e+1]=a*r.x,i[e+2]=-a*r.y,i[e+3]=s*r.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,i=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),r=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new f(i,r)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const i=this.getGlobalRotation(),r=Math.cos(i),s=Math.sin(i),a=this.usedElemNum-6,n=this.typedArray;n[a+0]=r*t,n[a+1]=s*t,n[a+2]=-s*e,n[a+3]=r*e}globalToLocal(t){const e=this.getInverse();return new f(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,i=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const r=t.x||0,s=t.y||0;(r||s)&&this.translate(r,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let a=t.offsetX||0,n=t.offsetY||0;t.offset&&(a+=t.offset.x,n+=t.offset.y);const o=t.origin;return o&&("number"==typeof o?(a-=o*e,n-=o*i):(a-=o.x*e,n-=o.y*i)),{offsetX:a,offsetY:n}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class d extends u{constructor(t,e,i,r){super(t,exports.ArrayType.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*r);for(let t=0;t<r;t++)this.addObject(t*i);this.bindBuffer(),this.bufferData()}addObject(t){}}class p{constructor(t,e,i,r=255){this._r=t,this._g=e,this._b=i,this._a=r,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,i,r){this.r=t,this.g=e,this.b=i,this.a=r,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),i=parseInt(t.slice(2,4),16),r=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,i,r,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}p.Red=new p(255,0,0,255),p.Green=new p(0,255,0,255),p.Blue=new p(0,0,255,255),p.Yellow=new p(255,255,0,255),p.Purple=new p(128,0,128,255),p.Orange=new p(255,165,0,255),p.Pink=new p(255,192,203,255),p.Gray=new p(128,128,128,255),p.Brown=new p(139,69,19,255),p.Cyan=new p(0,255,255,255),p.Magenta=new p(255,0,255,255),p.Lime=new p(192,255,0,255),p.White=new p(255,255,255,255),p.Black=new p(0,0,0,255),p.TRANSPARENT=new p(0,0,0,0);class f{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}set(t,e){return Array.isArray(t)?(this.x=t[0],this.y=t[0]):e?(this.x=t,this.y=e):(this.x=t,this.y=t),this}add(t){return new f(this.x+t.x,this.y+t.y)}subtract(t){return new f(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof f?new f(this.x*t.x,this.y*t.y):new f(this.x*t,this.y*t)}divide(t){return t instanceof f?new f(this.x/t.x,this.y/t.y):new f(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,i=this.y-t.y;return Math.sqrt(e*e+i*i)}clone(){return new f(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new f((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new f(Math.abs(this.x),Math.abs(this.y))}floor(){return new f(Math.floor(this.x),Math.floor(this.y))}ceil(){return new f(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new f(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new f(t[0],t[1])))}static fromAngle(t){return new f(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),i=this.length()*t.length(),r=Math.max(-1,Math.min(1,e/i));return Math.acos(r)}}f.ZERO=new f(0,0),f.ONE=new f(1,1),f.UP=new f(0,1),f.DOWN=new f(0,-1),f.LEFT=new f(-1,0),f.RIGHT=new f(1,0);class m{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,i,r){return new f(m.float(t,e),m.float(i,r))}static direction(t){const e=m.angle();return new f(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new p(m.float(t.r,e.r),m.float(t.g,e.g),m.float(t.b,e.b),m.float(t.a,e.a))}static pick(t){return t[m.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const i of t)e+=i[1];const i=Math.random()*e;let r=0;for(const e of t)if(r+=e[1],i<=r)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return m.float(t[0],t[1]);if(t[0]instanceof f)return m.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof p)return m.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class x{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,i){const r=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const i=t[e],s=t[(e+1)%t.length];r.push([i,s])}})),i=i||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return r.forEach((([t,r])=>{const a=new f(t.x-e.x,t.y-e.y),n=new f(r.x-e.x,r.y-e.y),o=r.subtract(t).perpendicular(),h=Math.abs(o.dot(a))/(o.length()*a.length())+.01,l=Math.abs(o.dot(n))/(o.length()*n.length())+.01,u=i/h,c=i/l,d=new f(a.x,a.y).normalize(),p=new f(n.x,n.y).normalize(),m=new f(t.x+d.x*u,t.y+d.y*u),x=new f(r.x+p.x*c,r.y+p.y*c);s.push([t,r,x,m])})),s}}const g=(t,e,i,r)=>{const s=[],a=r?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),n=Math.PI;for(let e=0;e<10;e++){const r=a+e/10*n,o=a+(e+1)/10*n,h=Math.cos(r)*i,l=Math.sin(r)*i,u=Math.cos(o)*i,c=Math.sin(o)*i;s.push(t),s.push(t.add(new f(h,l))),s.push(t.add(new f(u,c)))}return s},y=t=>{const e=t.points;if(e.length<2)return{vertices:[],uv:[]};const{normals:i,length:r}=((t,e=!1)=>{const i=[];if(t.length<2||e&&t.length<3)return{normals:i,length:0};const r=t.length;let s=0;if(e)for(let e=0;e<r;e++){const i=t[e],a=t[(e+1)%r];s+=i.distanceTo(a)}else for(let e=0;e<r-1;e++)s+=t[e].distanceTo(t[e+1]);const a=(t,e,i)=>{const r=e.subtract(t).normalize(),s=e.subtract(i).normalize(),a=s.dot(r);if(a<-.999)return{normal:r.perpendicular(),miters:1};{let t=s.add(r).normalize();r.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-a)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<r-1;e++){const s=0===e?t[r-2]:t[e-1],n=t[e],o=t[e+1];i.push(a(s,n,o))}i.push(i[0])}else for(let e=0;e<r;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();i.push({normal:e.perpendicular(),miters:1})}else if(e===r-1){const r=t[e].subtract(t[e-1]).normalize();i.push({normal:r.perpendicular(),miters:1})}else i.push(a(t[e-1],t[e],t[e+1]));return{normals:i,length:s}})(e,t.closed),s=(t.width||1)/2,a=[],n=[],o=t.roundCap||!1,h=t.textureMode||exports.LineTextureMode.STRETCH;let l=0;const u=t.texture?.width||1;for(let t=0;t<e.length-1;t++){const o=e[t],c=i[t].normal,d=i[t].miters,p=o.add(c.multiply(d*s)),m=o.subtract(c.multiply(d*s)),x=e[t+1],g=i[t+1].normal,y=i[t+1].miters,T=x.add(g.multiply(y*s)),w=x.subtract(g.multiply(y*s)),b=o.distanceTo(x);let R=0,E=0;h===exports.LineTextureMode.STRETCH?(R=l/r,E=(l+b)/r):(R=l/u,E=R+b/u);const S=new f(R,0),v=new f(R,1),M=new f(E,0),A=new f(E,1);a.push(p),n.push(S),a.push(m),n.push(v),a.push(T),n.push(M),a.push(T),n.push(M),a.push(w),n.push(A),a.push(m),n.push(v),l+=b}if(o&&!t.closed){const t=e[0],r=i[0].normal,n=g(t,r,s,!0);a.push(...n);const o=e[e.length-1],h=i[e.length-1].normal,l=g(o,h,s,!1);a.push(...l)}return{vertices:a,uv:n}};var T="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",w="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const b=(t,e,i)=>{const r=t.createShader(i);if(!r)throw new Error("Unable to create webgl shader");t.shaderSource(r,e),t.compileShader(r);if(!t.getShaderParameter(r,t.COMPILE_STATUS)){const i=t.getShaderInfoLog(r);throw console.error("Shader compilation failed:",i),new Error("Unable to compile shader: "+i+e)}return r};function R(t,e,i,r=!1,s=!1,a="clamp"){const n=t.createTexture();if(!n)throw new Error("unable to create texture");let o;switch(t.bindTexture(t.TEXTURE_2D,n),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,i?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,i?t.LINEAR:t.NEAREST),a){case"repeat":o=t.REPEAT;break;case"mirror":o=t.MIRRORED_REPEAT;break;default:o=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,o),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,o),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),r?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),n}const E=5126;var S="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",v="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const M=[{name:"aPosition",size:2,type:E,stride:24},{name:"aRegion",size:2,type:E,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:E,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],A=[{name:"aPosition",size:2,type:E,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:E,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class U{constructor(t,e,i,r,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const a=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let i="";for(let t=0;t<e;t++)i+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,i+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",i)}return t}(i,t.maxTextureUnits-s);this.program=((t,e,i)=>{var r=t.createProgram(),s=b(t,e,35633),a=b(t,i,35632);if(!r)throw new Error("Unable to create program shader");if(t.attachShader(r,s),t.attachShader(r,a),t.linkProgram(r),!t.getProgramParameter(r,t.LINK_STATUS)){const e=t.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+e)}return r})(t.gl,e,a),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(a),r&&this.setAttributes(r)}setUniforms(t,e){const i=this.gl;for(const r of t.getUnifromNames()){const s=this.getUniform(r);t.bind(i,r,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,i=t.match(/attribute\s+\w+\s+(\w+)/g);if(i)for(const t of i){const i=t.split(" ")[2],r=e.getAttribLocation(this.program,i);-1!=r&&(this.attributeLoc[i]=r)}const r=t.match(/uniform\s+\w+\s+(\w+)/g);if(r)for(const t of r){const i=t.split(" ")[2];this.uniformLoc[i]=e.getUniformLocation(this.program,i)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const i=this.gl;i.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),i.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,i,r,s=0){let a={[exports.ShaderType.SPRITE]:S,[exports.ShaderType.GRAPHIC]:T}[r],n={[exports.ShaderType.SPRITE]:v,[exports.ShaderType.GRAPHIC]:w}[r];const o={[exports.ShaderType.SPRITE]:M,[exports.ShaderType.GRAPHIC]:A}[r];return a=a.replace("void main(void) {",i+"\nvoid main(void) {"),n=n.replace("void main(void) {",e+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),n=n.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new U(t,n,a,o,s)}}class P{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new u(t.gl,exports.ArrayType.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...i){const[r,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,i){this.setShader("default",t,e,i)}setShader(t,e,i,r){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new U(this.rapid,e,i,r)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class C extends P{constructor(t){super(t),this.vertex=0,this.offset=f.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",w,T,A)}startRender(t,e,i,r){r&&this.currentShader?.setUniforms(r,this),this.offset=new f(t,e),this.vertex=0,this.webglArrayBuffer.clear(),i&&i.base&&(this.texture=this.useTexture(i.base.texture)[0])}addVertex(t,e,i,r,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const F=Math.floor(16384);class _ extends d{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class D extends P{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",v,S,M),this.indexBuffer=new _(e,F)}addVertex(t,e,i,r,s,a){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(a)}renderSprite(t,e,i,r,s,a,n,o,h,l,u,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=F||this.isUnifromChanged(u)||this.rapid.projectionDirty)&&(this.render(),u&&this.isUnifromChanged(u)&&(this.currentShader.setUniforms(u,this),this.setCurrentUniform(u)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const x=f-this.spriteTextureUnitIndexOffset,g=c?a:r,y=c?r:a,T=d?n:s,w=d?s:n,b=o,R=o+e,E=h,S=h+i;this.addVertex(b,E,g,T,x,l),this.addVertex(R,E,y,T,x,l),this.addVertex(R,S,y,w,x,l),this.addVertex(b,S,g,w,x,l)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class N{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,e=this.antialias,i=exports.TextureWrapMode.CLAMP){let r=this.cache.get(t);if(!r){const s=await this.loadImage(t);r=B.fromImageSource(this.render,s,e,i),this.cache.set(t,r)}return new I(r)}textureFromFrameBufferObject(t){return new I(t)}async textureFromSource(t,e=this.antialias,i=exports.TextureWrapMode.CLAMP){let r=this.cache.get(t);return r||(r=B.fromImageSource(this.render,t,e,i),this.cache.set(t,r)),new I(r)}async loadImage(t){return new Promise((e=>{const i=new Image;i.onload=()=>{e(i)},i.src=t}))}createText(t){return new L(this.render,t)}destroy(t){t instanceof I?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,i=this.antialias){return new k(this.render,t,e,i)}removeCache(t){const e=t instanceof I?t.base?.texture:t.texture;e&&this.cache.forEach(((t,i)=>{t===e&&this.cache.delete(i)}))}}class B{constructor(t,e,i,r=exports.TextureWrapMode.CLAMP){this.texture=t,this.width=e,this.height=i,this.wrapMode=r}static fromImageSource(t,e,i=!1,r=exports.TextureWrapMode.CLAMP){return new B(R(t.gl,e,i,!1,!1,r),e.width,e.height)}destroy(t){t.deleteTexture(this.texture)}}class I{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,i,r){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+i/this.base.width,this.clipH=this.clipY+r/this.base.height,this.width=i*this.scale,this.height=r*this.scale,this}static fromImageSource(t,e,i=!1){return new I(B.fromImageSource(t,e,i))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const i=[],r=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let a=0;a<s;a++)for(let s=0;s<r;s++){const r=this.clone();r.setClipRegion(s*t,a*e,t,e),i.push(r)}return i}clone(){return new I(this.base)}}class L extends I{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(B.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const i=this.text.split("\n");let r=0,s=0;for(const t of i){const i=e.measureText(t);r=Math.max(r,i.width),s+=this.options.fontSize||16}t.width=2*r,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let a=0;for(const t of i)e.fillText(t,0,a),a+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class k extends B{constructor(t,e,i,r=!1){const s=t.gl,a=R(s,{width:e,height:i},r,!0,!1),n=s.createFramebuffer();if(!n)throw s.deleteTexture(a),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,n),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,a,0);const o=s.createRenderbuffer();if(!o)throw s.deleteFramebuffer(n),s.deleteTexture(a),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,o),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,i),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,o),super(a,e,i),this.gl=s,this.framebuffer=n,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}class O{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof I&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class G{constructor(t){this.rapid=t}getYSortRow(t,e){const i=new Map;if(!t)return i;for(const r of t){const t=Math.floor(r.ySort/e);i.has(t)||i.set(t,[]),i.get(t).push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,i=(t.errorY??2)+1;if("number"==typeof t.error){const r=(t.error??2)+1;e=r,i=r}else t.error&&(e=t.error.x+1,i=t.error.y+1);return{errorX:e,errorY:i}}renderYSortRow(t,e){if(e&&0!==e.length)for(const i of e)i.render?i.render():i.renderSprite&&t.renderSprite(i.renderSprite)}getTileData(t,e){const i=e.shape??exports.TilemapShape.SQUARE,r=t.width,s=i===exports.TilemapShape.ISOMETRIC?t.height/2:t.height,a=this.rapid.matrixStack,{errorX:n,errorY:o}=this.getOffset(e),h=new f(0,0),l=new f(this.rapid.width,0),u=new f(0,this.rapid.height),c=new f(this.rapid.width,this.rapid.height),d=a.globalToLocal(h),p=a.globalToLocal(l),m=a.globalToLocal(u),x=a.globalToLocal(c),g=Math.min(d.x,p.x,m.x,x.x),y=Math.max(d.x,p.x,m.x,x.x),T=Math.min(d.y,p.y,m.y,x.y),w=Math.max(d.y,p.y,m.y,x.y),b=new f(Math.floor(g/r)-n,Math.floor(T/s)-o),R=new f(Math.ceil(y/r)+n,Math.ceil(w/s)+o);return{startTile:b,viewportWidth:R.x-b.x,viewportHeight:R.y-b.y,height:s}}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const i=e.tileSet,{startTile:r,viewportWidth:s,viewportHeight:a,height:n}=this.getTileData(i,e),o=this.getYSortRow(e.ySortCallback,n),h=e.ySortCallback&&e.ySortCallback.length>0;for(let n=0;n<a;n++){const a=n+r.y,l=o.get(a)??[];if(a<0||a>=t.length)this.renderYSortRow(this.rapid,l);else{for(let n=0;n<s;n++){const s=n+r.x;if(s<0||s>=t[a].length)continue;const o=t[a][s],h=i.getTile(o);if(!h)continue;const u=this.mapToLocal(new f(s,a),e),c=this.rapid.matrixStack.localToGlobal(u).y+(h.ySortOffset??0),d=e.eachTile&&e.eachTile(o,s,a)||{};l.push({ySort:c,renderSprite:{...h,x:u.x+(h.x||0),y:u.y+(h.y||0),...d}})}h&&l.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,l)}}this.rapid.matrixStack.applyTransformAfter(e)}localToMap(t,e){const i=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=0,r=0;const s=i.height/2,a=i.width/2;let n=Math.floor(t.y/s);const o=n%2==0;let h=Math.floor(t.x/a);const l=h%2==0,u=t.x%a/a,c=t.y%s/s,d=c<u,p=c<1-u;return o||(n-=1),d&&!l&&o?n-=1:d||!l||o?p&&l&&o?(h-=2,n-=1):p||l||o||(n+=1):(n+=1,h-=2),e=h,r=n,e=Math.floor(h/2),new f(e,r)}return new f(Math.floor(t.x/i.width),Math.floor(t.y/i.height))}mapToLocal(t,e){const i=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=new f(t.x*i.width,t.y*i.height/2);return t.y%2!=0&&(e.x+=i.width/2),e}return new f(t.x*i.width,t.y*i.height)}}const z=t=>"object"==typeof t&&null!==t&&Object.getPrototypeOf(t)===Object.prototype;const X=!0;class W{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof I?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=m.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=m.pick(e.texture)),this.maxLife=m.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,p.White),velocity:this.processAttribute(e.animation.velocity,f.ZERO),acceleration:this.processAttribute(e.animation.acceleration,f.ZERO)},this.position=f.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if(z(t)){const i=m.scalarOrRange(t.start,e),r=m.scalarOrRange(t.end||i,e);return{delta:t.delta??this.getDelta(i,r,this.maxLife),value:i,damping:t.damping}}return this.processAttribute({start:t},e)}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const i=e.value,r=Math.pow(e.damping,t);e.value="number"==typeof i?i*r:i.multiply(r)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const i=e.value;"number"==typeof i?e.value+=t*e.delta:e.value=i.add(e.delta.multiply(t))}e.color.value.clamp();const i=f.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(i).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,i){return"number"==typeof t&&"number"==typeof e?(e-t)/i:t instanceof f&&e instanceof f||t instanceof p&&e instanceof p?e.subtract(t).divide(i):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case exports.ParticleShape.POINT:this.position=f.ZERO;break;case exports.ParticleShape.CIRCLE:const t=Math.random()*Math.PI*2,e=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new f(Math.cos(t)*e,Math.sin(t)*e);break;case exports.ParticleShape.RECT:this.position=new f((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class j{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=X,this.position=f.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:X,this.position=e.position||f.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new W(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,i=Math.floor(e);i>0&&(this.emit(i),this.emitTimer-=i/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}exports.BaseTexture=B,exports.Color=p,exports.DynamicArrayBuffer=l,exports.Entity=class{constructor(t,e){this.parent=null,this.children=[],this.transform=new c,this.rapid=t,this.tags=e.tags??[],e.position?this.position=e.position:this.position=new f(e.x??0,e.y??0),"number"==typeof e.scale?this.scale=new f(e.scale,e.scale):e.scale?this.scale=e.scale:this.scale=new f(1,1),this.rotation=e.rotation??0}getParentTransform(){return this.parent?this.parent.transform:this.rapid.matrixStack}update(t){}updateTransform(t=!1){t&&this.parent&&this.parent.updateTransform(t);const e=this.transform,i=this.getParentTransform();e.setTransform(i.getTransform()),e.translate(this.position),e.rotate(this.rotation),e.scale(this.scale)}render(){this.updateTransform()}postRender(){for(const t of this.children)t.render(),t.postRender()}postUpdate(t){for(const e of this.children)e.update(t),e.postUpdate(t)}addChild(t){t.parent&&t.parent.removeChild(t),t.parent=this,this.children.push(t)}removeChild(t){const e=this.children.indexOf(t);e>-1&&(t.parent=null,this.children.splice(e,1))}root(t){this.update(t),this.postUpdate(t),this.render(),this.postRender()}findDescendant(t,e=!0){const i=[];for(const r of this.children){if(t(r)){if(e)return r;i.push(r)}const s=r.findDescendant(t,e);if(e){if(s)return s}else i.push(...s)}return e?null:i}findDescendantByTag(t,e=!1){return this.findDescendant((e=>0!==t.length&&t.every((t=>e.tags.includes(t)))),e)}},exports.FrameBufferObject=k,exports.GLShader=U,exports.InputManager=class{constructor(t){this.mousePosition=f.ZERO,this.keysDown=new Set,this.keysDownLastFrame=new Set,this.buttonsDown=new Set,this.buttonsDownLastFrame=new Set,this.rapid=t,this.canvas=t.canvas,this.attachEventListeners()}attachEventListeners(){const t=this.canvas.getBoundingClientRect();this.canvas.addEventListener("mousemove",(e=>{this.mousePosition=this.rapid.cssToGameCoords(e.clientX-t.left,e.clientY-t.top)})),window.addEventListener("keydown",(t=>{this.keysDown.add(t.code)})),window.addEventListener("keyup",(t=>{this.keysDown.delete(t.code)})),this.canvas.addEventListener("mousedown",(t=>{this.buttonsDown.add(t.button)})),this.canvas.addEventListener("mouseup",(t=>{this.buttonsDown.delete(t.button)}))}updateNextFrame(){this.keysDownLastFrame=new Set(this.keysDown),this.buttonsDownLastFrame=new Set(this.buttonsDown)}isKeyDown(t){return this.keysDown.has(t)}isKeyUp(t){return!this.keysDown.has(t)}wasKeyPressed(t){return this.keysDown.has(t)&&!this.keysDownLastFrame.has(t)}wasKeyReleased(t){return!this.keysDown.has(t)&&this.keysDownLastFrame.has(t)}isButtonDown(t){return this.buttonsDown.has(t)}isButtonUp(t){return!this.buttonsDown.has(t)}wasButtonPressed(t){return this.buttonsDown.has(t)&&!this.buttonsDownLastFrame.has(t)}wasButtonReleased(t){return!this.buttonsDown.has(t)&&this.buttonsDownLastFrame.has(t)}getMouseLocal(t){return t.transform.globalToLocal(this.mousePosition)}},exports.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},exports.MatrixStack=c,exports.ParticleEmitter=j,exports.Random=m,exports.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new c,this.tileMap=new G(this),this.light=new x(this),this.defaultColor=new p(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const e=(t=>{const e={stencil:!0},i=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!i)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return i})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new N(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.height||this.canvas.height,this.scaleEnable=t.scaleEnable??!1,this.scaleRadio=t.scaleRadio||exports.ScaleRadio.KEEP,this.devicePixelRatio=t.devicePixelRatio??window.devicePixelRatio??1,this.backgroundColor=t.backgroundColor||new p(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.updateDisplaySize(this.width,this.height),this.projectionDirty=!1,this.canvas.style.display="block"}updateDisplaySize(t,e){const i=this.width,r=this.height;let s,a,n,o;if(this.scaleEnable){const h=i/r,l=t/e;switch(this.scaleRadio){case exports.ScaleRadio.IGNORE:s=i,a=r,n=t*this.devicePixelRatio,o=e*this.devicePixelRatio;break;case exports.ScaleRadio.KEEP:let u,c;s=i,a=r,l>h?(c=e,u=c*h):(u=t,c=u/h),n=u*this.devicePixelRatio,o=c*this.devicePixelRatio;break;case exports.ScaleRadio.EXPAND:l>h?(a=r,s=r*l):(s=i,a=i/l),n=t*this.devicePixelRatio,o=e*this.devicePixelRatio;break;case exports.ScaleRadio.KEEP_W:s=i,a=i/l,n=t*this.devicePixelRatio,o=e*this.devicePixelRatio;break;case exports.ScaleRadio.KEEP_H:a=r,s=r*l,n=t*this.devicePixelRatio,o=e*this.devicePixelRatio;break;default:s=i,a=r,n=t*this.devicePixelRatio,o=e*this.devicePixelRatio}}else s=i,a=r,n=i*this.devicePixelRatio,o=r*this.devicePixelRatio;this.logicWidth=s,this.logicHeight=a,this.physicsWidth=n,this.physicsHeight=o,this.resizeSize(s,a,n,o,!0)}resizeSize(t,e,i,r,s=!1){this.updateProjection(0,t,e,0),this.gl.viewport(0,0,i,r),this.gl.scissor(0,0,i,r),s&&(this.canvas.width=i,this.canvas.height=r,this.canvas.style.width=i/this.devicePixelRatio+"px",this.canvas.style.height=r/this.devicePixelRatio+"px")}updateProjection(t,e,i,r){this.projection=this.createOrthMatrix(t,e,i,r),this.projectionDirty=!0}initWebgl(t){t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",D),this.registerRegion("graphic",C)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const i=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=i,this.currentRegionName=t,i.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),i=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,i}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof O?{tileSet:e}:e)}applyCameraTransform(t){this.withTransform((()=>{if(t.center){const t=new f(this.logicWidth,this.logicHeight).divide(2);this.matrixStack.translate(t)}this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}))}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:i,offsetY:r}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,i,r,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:i,uv:r}=y({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:i,uv:r})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,i)=>{const r=Array.isArray(t.color)?t.color[i]:t.color,s=t.uv?.[i];this.addGraphicVertex(e.x,e.y,s,r)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:i}=this.startDraw(t);this.setRegion("graphic",t.shader);const r=this.currentRegion;r.startRender(e,i,t.texture,t.uniforms),t.drawType&&(r.drawType=t.drawType)}addGraphicVertex(t,e,i,r){this.currentRegion.addVertex(t,e,i?.x,i?.y,(r||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,i=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,i)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:i}=t,r=[new f(0,0),new f(e,0),new f(e,i),new f(0,i)];this.renderGraphic({...t,points:r,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,i=t.radius,r=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const r=t/e*Math.PI*2,a=Math.cos(r)*i,n=Math.sin(r)*i;s.push(new f(a,n))}this.renderGraphic({...t,points:s,color:r,drawType:this.gl.TRIANGLE_FAN})}clear(t){const e=this.gl,i=t||this.backgroundColor;e.clearColor(i.r/255,i.g/255,i.b/255,i.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,i,r){return new Float32Array([2/(e-t),0,0,0,0,2/(r-i),0,0,0,0,-1,0,-(e+t)/(e-t),-(r+i)/(r-i),0,1])}drawMask(t=exports.MaskType.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=exports.MaskType.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??exports.MaskType.Include,!1)}setMaskType(t,e=!1){const i=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),i.stencilFunc(i.ALWAYS,1,255);else switch(t){case exports.MaskType.Include:i.stencilFunc(i.EQUAL,1,255);break;case exports.MaskType.Exclude:i.stencilFunc(i.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,i,r=0){return U.createCostumShader(this,t,e,i,r)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeSize(t.width,t.height,t.width,t.height),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeSize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case exports.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case exports.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case exports.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||exports.MaskType.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:p.Black})})),this.endDrawMask()}createParticleEmitter(t){return new j(this,t)}cssToGameCoords(t,e){const i=t instanceof f?t:new f(t,e),r=this.canvas.getBoundingClientRect(),s=i.x/r.width,a=i.y/r.height,n=s*this.logicWidth,o=a*this.logicHeight;return new f(n,o)}gameToCssCoords(t,e){const i=t instanceof f?t:new f(t,e??0),r=this.canvas.getBoundingClientRect(),s=i.x/this.logicWidth,a=i.y/this.logicHeight,n=s*r.width,o=a*r.height;return new f(n,o)}},exports.SCALEFACTOR=2,exports.Text=L,exports.Texture=I,exports.TextureCache=N,exports.TileMapRender=G,exports.TileSet=O,exports.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,i,r){if(!i)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(i,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(i,s[0]):t.uniform1f(i,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(i,s):t.uniform2fv(i,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(i,s):t.uniform3fv(i,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(i,s):t.uniform4fv(i,s);break;case 9:t.uniformMatrix3fv(i,!1,s);break;case 16:t.uniformMatrix4fv(i,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(i,s?1:0);else if(s.base?.texture){const e=r.useTexture(s.base.texture)[0];t.uniform1i(i,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}},exports.Vec2=f,exports.WebglBufferArray=u,exports.WebglElementBufferArray=d,exports.graphicAttributes=A,exports.isPlainObject=z,exports.spriteAttributes=M;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export declare const spriteAttributes: ({
|
|
2
|
-
name: string;
|
|
3
|
-
size: number;
|
|
4
|
-
type: number;
|
|
5
|
-
stride: number;
|
|
6
|
-
offset?: undefined;
|
|
7
|
-
normalized?: undefined;
|
|
8
|
-
} | {
|
|
9
|
-
name: string;
|
|
10
|
-
size: number;
|
|
11
|
-
type: number;
|
|
12
|
-
stride: number;
|
|
13
|
-
offset: number;
|
|
14
|
-
normalized?: undefined;
|
|
15
|
-
} | {
|
|
16
|
-
name: string;
|
|
17
|
-
size: number;
|
|
18
|
-
type: number;
|
|
19
|
-
stride: number;
|
|
20
|
-
offset: number;
|
|
21
|
-
normalized: boolean;
|
|
22
|
-
})[];
|
|
23
|
-
export declare const graphicAttributes: ({
|
|
24
|
-
name: string;
|
|
25
|
-
size: number;
|
|
26
|
-
type: number;
|
|
27
|
-
stride: number;
|
|
28
|
-
offset?: undefined;
|
|
29
|
-
normalized?: undefined;
|
|
30
|
-
} | {
|
|
31
|
-
name: string;
|
|
32
|
-
size: number;
|
|
33
|
-
type: number;
|
|
34
|
-
stride: number;
|
|
35
|
-
offset: number;
|
|
36
|
-
normalized: boolean;
|
|
37
|
-
} | {
|
|
38
|
-
name: string;
|
|
39
|
-
size: number;
|
|
40
|
-
type: number;
|
|
41
|
-
stride: number;
|
|
42
|
-
offset: number;
|
|
43
|
-
normalized?: undefined;
|
|
44
|
-
})[];
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type Rapid from "../render";
|
|
2
|
-
import { Uniform } from "../webgl/uniform";
|
|
3
|
-
import RenderRegion from "./region";
|
|
4
|
-
import { Texture } from "../texture";
|
|
5
|
-
declare class GraphicRegion extends RenderRegion {
|
|
6
|
-
private vertex;
|
|
7
|
-
private texture?;
|
|
8
|
-
private offset;
|
|
9
|
-
drawType: number;
|
|
10
|
-
constructor(rapid: Rapid);
|
|
11
|
-
startRender(offsetX: number, offsetY: number, texture?: Texture, uniforms?: Uniform): void;
|
|
12
|
-
addVertex(x: number, y: number, u: number, v: number, color: number): void;
|
|
13
|
-
protected executeRender(): void;
|
|
14
|
-
}
|
|
15
|
-
export default GraphicRegion;
|
package/dist/regions/region.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { IAttribute, WebGLContext } from "../interface";
|
|
2
|
-
import { WebglBufferArray } from "../math";
|
|
3
|
-
import Rapid from "../render";
|
|
4
|
-
import GLShader from "../webgl/glshader";
|
|
5
|
-
import { Uniform } from "../webgl/uniform";
|
|
6
|
-
declare class RenderRegion {
|
|
7
|
-
currentShader?: GLShader;
|
|
8
|
-
protected webglArrayBuffer: WebglBufferArray;
|
|
9
|
-
protected rapid: Rapid;
|
|
10
|
-
protected gl: WebGLContext;
|
|
11
|
-
protected usedTextures: WebGLTexture[];
|
|
12
|
-
protected shaders: Map<string, GLShader>;
|
|
13
|
-
protected isCostumShader: boolean;
|
|
14
|
-
private costumUnifrom?;
|
|
15
|
-
private defaultShader?;
|
|
16
|
-
private maxTextureUnits;
|
|
17
|
-
protected freeTextureUnitNum: number;
|
|
18
|
-
constructor(rapid: Rapid);
|
|
19
|
-
getTextureUnitList(): number[];
|
|
20
|
-
protected addVertex(x: number, y: number, ..._: unknown[]): void;
|
|
21
|
-
/**
|
|
22
|
-
* 使用纹理,必须在渲染操作之前
|
|
23
|
-
* @param texture
|
|
24
|
-
* @returns
|
|
25
|
-
*/
|
|
26
|
-
useTexture(texture: WebGLTexture): [number, boolean];
|
|
27
|
-
enterRegion(customShader?: GLShader): void;
|
|
28
|
-
updateProjection(): void;
|
|
29
|
-
isUnifromChanged(newCurrentUniform?: Uniform): boolean;
|
|
30
|
-
setCurrentUniform(newCurrentUniform: Uniform): void;
|
|
31
|
-
exitRegion(): void;
|
|
32
|
-
protected initDefaultShader(vs: string, fs: string, attributes?: IAttribute[]): void;
|
|
33
|
-
setShader(name: string, vs: string, fs: string, attributes?: IAttribute[]): void;
|
|
34
|
-
getShader(name: string): GLShader | undefined;
|
|
35
|
-
render(): void;
|
|
36
|
-
protected executeRender(): void;
|
|
37
|
-
protected initializeForNextRender(): void;
|
|
38
|
-
hasPendingContent(): boolean;
|
|
39
|
-
isShaderChanged(shader?: GLShader): boolean;
|
|
40
|
-
}
|
|
41
|
-
export default RenderRegion;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import RenderRegion from "./region";
|
|
2
|
-
import Rapid from "../render";
|
|
3
|
-
import GLShader from "../webgl/glshader";
|
|
4
|
-
import { Uniform } from "../webgl/uniform";
|
|
5
|
-
declare class SpriteRegion extends RenderRegion {
|
|
6
|
-
private batchSprite;
|
|
7
|
-
private indexBuffer;
|
|
8
|
-
private spriteTextureUnits;
|
|
9
|
-
private spriteTextureUnitIndexOffset;
|
|
10
|
-
constructor(rapid: Rapid);
|
|
11
|
-
protected addVertex(x: number, y: number, u: number, v: number, textureUnit: number, color: number): void;
|
|
12
|
-
renderSprite(texture: WebGLTexture, width: number, height: number, u0: number, v0: number, u1: number, v1: number, offsetX: number, offsetY: number, color: number, uniforms?: Uniform, flipX?: boolean, flipY?: boolean, additionalTexturenit?: number): void;
|
|
13
|
-
protected executeRender(): void;
|
|
14
|
-
enterRegion(customShader?: GLShader | undefined): void;
|
|
15
|
-
protected initializeForNextRender(): void;
|
|
16
|
-
hasPendingContent(): boolean;
|
|
17
|
-
}
|
|
18
|
-
export default SpriteRegion;
|
package/dist/tilemap.d.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import Rapid from "./render";
|
|
2
|
-
import { IRegisterTileOptions, ILayerRenderOptions } from "./interface";
|
|
3
|
-
import { Texture } from "./texture";
|
|
4
|
-
import { Vec2 } from "./math";
|
|
5
|
-
/**
|
|
6
|
-
* Represents a tileset that manages tile textures and their properties.
|
|
7
|
-
*/
|
|
8
|
-
export declare class TileSet {
|
|
9
|
-
/** Map storing tile textures and their associated options */
|
|
10
|
-
private textures;
|
|
11
|
-
/** Width of each tile in the tileset */
|
|
12
|
-
width: number;
|
|
13
|
-
/** Height of each tile in the tileset */
|
|
14
|
-
height: number;
|
|
15
|
-
/**
|
|
16
|
-
* Creates a new TileSet instance.
|
|
17
|
-
* @param width - The width of each tile in pixels
|
|
18
|
-
* @param height - The height of each tile in pixels
|
|
19
|
-
*/
|
|
20
|
-
constructor(width: number, height: number);
|
|
21
|
-
/**
|
|
22
|
-
* Registers a new tile with the given ID and options.
|
|
23
|
-
* @param id - The unique identifier for the tile
|
|
24
|
-
* @param options - The tile options or texture to register
|
|
25
|
-
*/
|
|
26
|
-
setTile(id: string | number, options: IRegisterTileOptions | Texture): void;
|
|
27
|
-
/**
|
|
28
|
-
* Retrieves the registered tile options for the given ID.
|
|
29
|
-
* @param id - The unique identifier of the tile to retrieve
|
|
30
|
-
* @returns The registered tile options, or undefined if not found
|
|
31
|
-
*/
|
|
32
|
-
getTile(id: string | number): IRegisterTileOptions | undefined;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Represents a tilemap renderer that handles rendering of tile-based maps.
|
|
36
|
-
*/
|
|
37
|
-
export declare class TileMapRender {
|
|
38
|
-
private rapid;
|
|
39
|
-
/**
|
|
40
|
-
* Creates a new TileMapRender instance.
|
|
41
|
-
* @param rapid - The Rapid rendering instance to use.
|
|
42
|
-
*/
|
|
43
|
-
constructor(rapid: Rapid);
|
|
44
|
-
/**
|
|
45
|
-
* 将需要 y-sort 的回调根据它们的 y 坐标分组到一个 Map 中。
|
|
46
|
-
* 使用 Map 是为了避免因地图坐标过大而创建稀疏数组,从而优化内存使用。
|
|
47
|
-
*
|
|
48
|
-
* @param ySortCallbacks - Array of y-sort callbacks to process.
|
|
49
|
-
* @param height - The effective height of a tile row, used for grouping.
|
|
50
|
-
* @returns A Map where keys are row indices (y) and values are arrays of y-sort callbacks for that row.
|
|
51
|
-
* @private
|
|
52
|
-
*/
|
|
53
|
-
private getYSortRow;
|
|
54
|
-
/**
|
|
55
|
-
* Calculates the error offset values for tile rendering.
|
|
56
|
-
* @param options - Layer rendering options containing error values.
|
|
57
|
-
* @returns Object containing x and y error offsets.
|
|
58
|
-
* @private
|
|
59
|
-
*/
|
|
60
|
-
private getOffset;
|
|
61
|
-
/**
|
|
62
|
-
* Renders a row of y-sorted entities.
|
|
63
|
-
* @param rapid - The Rapid rendering instance.
|
|
64
|
-
* @param ySortRow - Array of y-sort callbacks to render.
|
|
65
|
-
* @private
|
|
66
|
-
*/
|
|
67
|
-
private renderYSortRow;
|
|
68
|
-
/**
|
|
69
|
-
* Calculates tile rendering data based on the viewport and tileset.
|
|
70
|
-
* @param tileSet - The tileset to use for rendering.
|
|
71
|
-
* @param options - Layer rendering options.
|
|
72
|
-
* @returns Object containing calculated tile rendering data.
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
private getTileData;
|
|
76
|
-
/**
|
|
77
|
-
* Renders the tilemap layer based on the provided data and options.
|
|
78
|
-
*
|
|
79
|
-
* @param data - A 2D array representing the tilemap data.
|
|
80
|
-
* @param options - The rendering options for the tilemap layer.
|
|
81
|
-
* @returns
|
|
82
|
-
*/
|
|
83
|
-
renderLayer(data: (number | string)[][], options: ILayerRenderOptions): void;
|
|
84
|
-
/**
|
|
85
|
-
* Converts local coordinates to map coordinates.
|
|
86
|
-
* @param local - The local coordinates.
|
|
87
|
-
* @param options - The rendering options.
|
|
88
|
-
* @returns The map coordinates.
|
|
89
|
-
*/
|
|
90
|
-
localToMap(local: Vec2, options: ILayerRenderOptions): Vec2;
|
|
91
|
-
/**
|
|
92
|
-
* Converts map coordinates to local coordinates.
|
|
93
|
-
* @param map - The map coordinates.
|
|
94
|
-
* @param options - The rendering options.
|
|
95
|
-
* @returns The local coordinates.
|
|
96
|
-
*/
|
|
97
|
-
mapToLocal(map: Vec2, options: ILayerRenderOptions): Vec2;
|
|
98
|
-
}
|
package/dist/webgl/uniform.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { UniformType, WebGLContext } from '../interface';
|
|
2
|
-
import RenderRegion from '../regions/region';
|
|
3
|
-
export declare class Uniform {
|
|
4
|
-
private data;
|
|
5
|
-
isDirty: boolean;
|
|
6
|
-
constructor(data: UniformType);
|
|
7
|
-
setUniform(key: string, data: UniformType[string]): void;
|
|
8
|
-
/**
|
|
9
|
-
* @ignore
|
|
10
|
-
*/
|
|
11
|
-
clearDirty(): void;
|
|
12
|
-
getUnifromNames(): string[];
|
|
13
|
-
bind(gl: WebGLContext, uniformName: string, loc: WebGLUniformLocation, region: RenderRegion): void;
|
|
14
|
-
}
|