UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

3 lines (2 loc) 1.87 kB
(()=>{var g=Object.defineProperty;var f=(h,t)=>{for(var i in t)g(h,i,{get:t[i],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(h=!0)=>{for(let t in globalThis.utils)t!=="global"&&(h||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var c={};f(c,{Grid:()=>r,TypedGrid:()=>l});var r=class h{_w;_h;_c;constructor(t,i,s=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~i),this._c=s}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new h(this._w,this._h,this._c)}clear(){this.forEach((t,i)=>this.set(t,i,void 0))}get width(){return this._w}get height(){return this._h}set(t,i,s){this._c[this.pointToIndex(t,i)]=s}get(t,i){return this._c[this.pointToIndex(t,i)]}has(t,i){return this.get(t,i)!=null}check(t,i){return t>=0&&t<this._w&&i>=0&&i<this._h}get length(){return this._w*this._h}pointToIndex(t,i){return this.clampX(~~t)+this.clampY(~~i)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,i=!1){let s=i?this.length-1:0,e=i?-1:this.length,n=i?-1:1;for(;s!==e;){let o=this.indexToPointX(s),a=this.indexToPointY(s),_=this._c[s];if(t(o,a,_,this)===!1)break;s+=n}}fill(t){this.forEach((i,s)=>{this.set(i,s,t)})}clampX(t){return u(t,0,this._w-1)}clampY(t){return u(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",i=!0){if(!i)return this._c.join(t);let s=[];return this.forEach((e,n,o)=>{s[n]=s[n]||"",s[n]+=o+t}),s.join(` `)}},l=class h extends r{constructor(t,i,s=Uint8Array){super(t,i,null),this._c=new s(this._w*this._h)}has(t,i){return this.get(t,i)!==0}clone(){let t=new h(this._w,this._h,this._c.constructor);return this.forEach((i,s,e)=>{t.set(i,s,e)}),t}};function u(h,t,i){return h<t?t:h>i?i:h}globalThis.utils=Object.assign(globalThis.utils||{},c);})();