UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

3 lines (2 loc) 1.84 kB
(()=>{var a=Object.defineProperty;var d=(n,t)=>{for(var i in t)a(n,i,{get:t[i],enumerable:!0})};window.utils=window.utils||{};window.utils.global=(n=!0)=>{for(let t in window.utils)t!=="global"&&(n||globalThis[t]===void 0)&&(globalThis[t]=window.utils[t])};var c={};d(c,{Grid:()=>r,TypedGrid:()=>l});var r=class n{_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 n(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,h=i?-1:1;for(;s!==e;){let o=this.indexToPointX(s),w=this.indexToPointY(s),_=this._c[s];if(t(o,w,_,this)===!1)break;s+=h}}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,h,o)=>{s[h]=s[h]||"",s[h]+=o+t}),s.join(` `)}},l=class n 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 n(this._w,this._h,this._c.constructor);return this.forEach((i,s,e)=>{t.set(i,s,e)}),t}};function u(n,t,i){return n<t?t:n>i?i:n}window.utils=Object.assign(window.utils||{},c);})();