UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

2 lines (1 loc) • 2.48 kB
(()=>{var m=Object.defineProperty;var b=(e,t)=>{for(var s in t)m(e,s,{get:t[s],enumerable:!0})};window.utils=window.utils||{};window.utils.global=(e=!0)=>{for(let t in window.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=window.utils[t])};var h={};b(h,{ANCHOR_BOT_LEFT:()=>M,ANCHOR_BOT_RIGHT:()=>_,ANCHOR_CENTER:()=>g,ANCHOR_TOP_LEFT:()=>d,ANCHOR_TOP_RIGHT:()=>w,Actor:()=>a});var E=2*Math.PI,l=parseFloat,n=class{constructor(t=0,s=t){this.x=l(t)||0,this.y=l(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}};var f=e=>e instanceof n,i=(e=0,t=e)=>(f(e)&&(t=e.y,e=e.x),new n(e,t));var y=(e,t,s,r,o,c,p)=>{let u=o-Math.max(e,Math.min(o,e+s)),x=c-Math.max(t,Math.min(c,t+r));return u*u+x*x<=p*p};var g=i(.5,.5),d=i(0,0),w=i(1,0),M=i(0,1),_=i(1,1),a=class{sprite;pos;spd;acc;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=d){this.sprite=t||{width:0,height:0},this.pos=s||i(0),this.spd=i(0),this.acc=i(0),this._o=i(r),this._s=i(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set dx(t){this.spd.x=t}get dx(){return this.spd.x}set dy(t){this.spd.y=t}get dy(){return this.spd.y}set ddx(t){this.acc.x=t}get ddx(){return this.acc.x}set ddy(t){this.acc.y=t}get ddy(){return this.acc.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),o=this.pos.x-s*this.anchor.x,c=this.pos.y-r*this.anchor.y;return[o,c,s,r]}in(t,s,r=1){return y(...this.getBounds(),t,s,r)}col(t){return colrect(...this.getBounds(),...t.getBounds())}update(t=1){this.spd.x+=this.acc.x*t,this.spd.y+=this.acc.y*t,this.pos.x+=this.spd.x*t,this.pos.y+=this.spd.y*t}draw(t=globalThis,s=!0){s&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),s&&t.pop()}transform(t=globalThis){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t=globalThis,s=!0){let r=this.anchor,o=-this.sprite.width*(this.flipX?1-r.x:r.x),c=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(o,c,this.sprite)}};window.utils=Object.assign(window.utils||{},h);})();