@litecanvas/utils 0.44.1 → 0.45.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/dist/actor.js +45 -3
- package/dist/actor.min.js +1 -1
- package/dist/all.js +41 -3
- package/dist/all.min.js +2 -2
- package/dist/collision.js +15 -0
- package/dist/collision.min.js +1 -1
- package/package.json +2 -2
- package/src/actor/index.js +29 -3
- package/src/collision/colrectcirc.js +29 -0
- package/src/collision/index.js +1 -0
package/dist/actor.js
CHANGED
|
@@ -54,6 +54,25 @@
|
|
|
54
54
|
return new Vector(x, y);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
+
// src/debug/assert.js
|
|
58
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
59
|
+
if (!condition) throw new Error(message);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// src/collision/colrectcirc.js
|
|
63
|
+
var colrectcirc_default = (x1, y1, w1, h1, x2, y2, r2) => {
|
|
64
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
65
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
66
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
67
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
68
|
+
DEV: assert_default(isFinite(x2), "colcirc: 5th param must be a number");
|
|
69
|
+
DEV: assert_default(isFinite(y2), "colcirc: 6th param must be a number");
|
|
70
|
+
DEV: assert_default(isFinite(r2), "colcirc: 7th param must be a number");
|
|
71
|
+
let xx = x2 - Math.max(x1, Math.min(x2, x1 + w1));
|
|
72
|
+
let yy = y2 - Math.max(y1, Math.min(y2, y1 + h1));
|
|
73
|
+
return xx * xx + yy * yy <= r2 * r2;
|
|
74
|
+
};
|
|
75
|
+
|
|
57
76
|
// src/actor/index.js
|
|
58
77
|
var ANCHOR_CENTER = /* @__PURE__ */ vec(0.5, 0.5);
|
|
59
78
|
var ANCHOR_TOP_LEFT = /* @__PURE__ */ vec(0, 0);
|
|
@@ -80,7 +99,7 @@
|
|
|
80
99
|
/** @type {boolean} If `true` the actor will not be drawn. */
|
|
81
100
|
hidden = false;
|
|
82
101
|
/**
|
|
83
|
-
* @param {Image|HTMLCanvasElement|OffscreenCanvas} sprite
|
|
102
|
+
* @param {Image|HTMLCanvasElement|OffscreenCanvas|ImageBitmap} sprite
|
|
84
103
|
* @param {Vector} position
|
|
85
104
|
* @param {Vector} anchor
|
|
86
105
|
*/
|
|
@@ -166,6 +185,7 @@
|
|
|
166
185
|
this._s.y *= y;
|
|
167
186
|
}
|
|
168
187
|
/**
|
|
188
|
+
* @param {boolean} scaled
|
|
169
189
|
* @returns {number[]}
|
|
170
190
|
*/
|
|
171
191
|
getBounds(scaled = true) {
|
|
@@ -175,6 +195,27 @@
|
|
|
175
195
|
const y = this.pos.y - h * this.anchor.y;
|
|
176
196
|
return [x, y, w, h];
|
|
177
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Checks if the actor is colliding with a point (with optional radius).
|
|
200
|
+
* Useful to check clicks or touchs.
|
|
201
|
+
*
|
|
202
|
+
* @param {number} x
|
|
203
|
+
* @param {number} y
|
|
204
|
+
* @param {number} [radius=1]
|
|
205
|
+
* @returns {boolean}
|
|
206
|
+
*/
|
|
207
|
+
in(x, y, radius = 1) {
|
|
208
|
+
return colrectcirc_default(...this.getBounds(), x, y, radius);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Checks if the actor is colliding with another actor.
|
|
212
|
+
*
|
|
213
|
+
* @param {Actor} other
|
|
214
|
+
* @returns {boolean}
|
|
215
|
+
*/
|
|
216
|
+
col(other) {
|
|
217
|
+
return colrect(...this.getBounds(), ...other.getBounds());
|
|
218
|
+
}
|
|
178
219
|
/**
|
|
179
220
|
* Draw the actor
|
|
180
221
|
*
|
|
@@ -191,7 +232,7 @@
|
|
|
191
232
|
/**
|
|
192
233
|
* @param {LitecanvasInstance} engine
|
|
193
234
|
*/
|
|
194
|
-
transform(engine) {
|
|
235
|
+
transform(engine = globalThis) {
|
|
195
236
|
engine.translate(this.pos.x, this.pos.y);
|
|
196
237
|
engine.rotate(engine.deg2rad(this.angle));
|
|
197
238
|
engine.scale(
|
|
@@ -201,8 +242,9 @@
|
|
|
201
242
|
}
|
|
202
243
|
/**
|
|
203
244
|
* @param {LitecanvasInstance} engine
|
|
245
|
+
* @param {boolean} alpha
|
|
204
246
|
*/
|
|
205
|
-
drawImage(engine, alpha = true) {
|
|
247
|
+
drawImage(engine = globalThis, alpha = true) {
|
|
206
248
|
const anchor = this.anchor;
|
|
207
249
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
208
250
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var f=Object.defineProperty;var g=(r,t)=>{for(var s in t)f(r,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(r=!0)=>{for(let t in globalThis.utils)t!=="global"&&(r||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var a={};g(a,{ANCHOR_BOT_LEFT:()=>_,ANCHOR_BOT_RIGHT:()=>T,ANCHOR_CENTER:()=>m,ANCHOR_TOP_LEFT:()=>u,ANCHOR_TOP_RIGHT:()=>b,Actor:()=>h});var w=2*Math.PI,n=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=r=>r instanceof n,o=(r=0,t=r)=>(d(r)&&(t=r.y,r=r.x),new n(r,t));var y=(r,t,s,e,i,c,p)=>{let x=i-Math.max(r,Math.min(i,r+s)),l=c-Math.max(t,Math.min(c,t+e));return x*x+l*l<=p*p};var m=o(.5,.5),u=o(0,0),b=o(1,0),_=o(0,1),T=o(1,1),h=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,e=u){this.sprite=t||{width:0,height:0},this.pos=s||o(0),this._o=o(e),this._s=o(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 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),e=this.sprite.height*(t?this._s.y:1),i=this.pos.x-s*this.anchor.x,c=this.pos.y-e*this.anchor.y;return[i,c,s,e]}in(t,s,e=1){return y(...this.getBounds(),t,s,e)}col(t){return colrect(...this.getBounds(),...t.getBounds())}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 e=this.anchor,i=-this.sprite.width*(this.flipX?1-e.x:e.x),c=-this.sprite.height*(this.flipY?1-e.y:e.y);s&&t.alpha(this.opacity),t.image(i,c,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},a);})();
|
package/dist/all.js
CHANGED
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
choose: () => choose_default,
|
|
55
55
|
colcirc: () => colcirc_default,
|
|
56
56
|
colrect: () => colrect_default,
|
|
57
|
+
colrectcirc: () => colrectcirc_default,
|
|
57
58
|
dd: () => dd_default,
|
|
58
59
|
diff: () => diff_default,
|
|
59
60
|
dist: () => dist_default,
|
|
@@ -358,6 +359,20 @@
|
|
|
358
359
|
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
359
360
|
};
|
|
360
361
|
|
|
362
|
+
// src/collision/colrectcirc.js
|
|
363
|
+
var colrectcirc_default = (x1, y1, w1, h1, x2, y2, r2) => {
|
|
364
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
365
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
366
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
367
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
368
|
+
DEV: assert_default(isFinite(x2), "colcirc: 5th param must be a number");
|
|
369
|
+
DEV: assert_default(isFinite(y2), "colcirc: 6th param must be a number");
|
|
370
|
+
DEV: assert_default(isFinite(r2), "colcirc: 7th param must be a number");
|
|
371
|
+
let xx = x2 - Math.max(x1, Math.min(x2, x1 + w1));
|
|
372
|
+
let yy = y2 - Math.max(y1, Math.min(y2, y1 + h1));
|
|
373
|
+
return xx * xx + yy * yy <= r2 * r2;
|
|
374
|
+
};
|
|
375
|
+
|
|
361
376
|
// src/grid/index.js
|
|
362
377
|
var Grid = class _Grid {
|
|
363
378
|
/** @type {number} The grid width */
|
|
@@ -785,7 +800,7 @@
|
|
|
785
800
|
/** @type {boolean} If `true` the actor will not be drawn. */
|
|
786
801
|
hidden = false;
|
|
787
802
|
/**
|
|
788
|
-
* @param {Image|HTMLCanvasElement|OffscreenCanvas} sprite
|
|
803
|
+
* @param {Image|HTMLCanvasElement|OffscreenCanvas|ImageBitmap} sprite
|
|
789
804
|
* @param {Vector} position
|
|
790
805
|
* @param {Vector} anchor
|
|
791
806
|
*/
|
|
@@ -871,6 +886,7 @@
|
|
|
871
886
|
this._s.y *= y;
|
|
872
887
|
}
|
|
873
888
|
/**
|
|
889
|
+
* @param {boolean} scaled
|
|
874
890
|
* @returns {number[]}
|
|
875
891
|
*/
|
|
876
892
|
getBounds(scaled = true) {
|
|
@@ -880,6 +896,27 @@
|
|
|
880
896
|
const y = this.pos.y - h * this.anchor.y;
|
|
881
897
|
return [x, y, w, h];
|
|
882
898
|
}
|
|
899
|
+
/**
|
|
900
|
+
* Checks if the actor is colliding with a point (with optional radius).
|
|
901
|
+
* Useful to check clicks or touchs.
|
|
902
|
+
*
|
|
903
|
+
* @param {number} x
|
|
904
|
+
* @param {number} y
|
|
905
|
+
* @param {number} [radius=1]
|
|
906
|
+
* @returns {boolean}
|
|
907
|
+
*/
|
|
908
|
+
in(x, y, radius = 1) {
|
|
909
|
+
return colrectcirc_default(...this.getBounds(), x, y, radius);
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Checks if the actor is colliding with another actor.
|
|
913
|
+
*
|
|
914
|
+
* @param {Actor} other
|
|
915
|
+
* @returns {boolean}
|
|
916
|
+
*/
|
|
917
|
+
col(other) {
|
|
918
|
+
return colrect(...this.getBounds(), ...other.getBounds());
|
|
919
|
+
}
|
|
883
920
|
/**
|
|
884
921
|
* Draw the actor
|
|
885
922
|
*
|
|
@@ -896,7 +933,7 @@
|
|
|
896
933
|
/**
|
|
897
934
|
* @param {LitecanvasInstance} engine
|
|
898
935
|
*/
|
|
899
|
-
transform(engine) {
|
|
936
|
+
transform(engine = globalThis) {
|
|
900
937
|
engine.translate(this.pos.x, this.pos.y);
|
|
901
938
|
engine.rotate(engine.deg2rad(this.angle));
|
|
902
939
|
engine.scale(
|
|
@@ -906,8 +943,9 @@
|
|
|
906
943
|
}
|
|
907
944
|
/**
|
|
908
945
|
* @param {LitecanvasInstance} engine
|
|
946
|
+
* @param {boolean} alpha
|
|
909
947
|
*/
|
|
910
|
-
drawImage(engine, alpha = true) {
|
|
948
|
+
drawImage(engine = globalThis, alpha = true) {
|
|
911
949
|
const anchor = this.anchor;
|
|
912
950
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
913
951
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Ft=Object.defineProperty;var St=(e,t)=>{for(var s in t)Ft(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var W={};St(W,{ANCHOR_BOT_LEFT:()=>he,ANCHOR_BOT_RIGHT:()=>ae,ANCHOR_CENTER:()=>ie,ANCHOR_TOP_LEFT:()=>rt,ANCHOR_TOP_RIGHT:()=>oe,Actor:()=>C,BACK_IN:()=>ue,BACK_IN_OUT:()=>xe,BACK_OUT:()=>fe,BOUNCE_IN:()=>_t,BOUNCE_IN_OUT:()=>ye,BOUNCE_OUT:()=>Z,Camera:()=>g,DOWN:()=>se,EASE_IN:()=>le,EASE_IN_OUT:()=>pe,EASE_OUT:()=>ce,ELASTIC_IN:()=>_e,ELASTIC_IN_OUT:()=>de,ELASTIC_OUT:()=>me,Grid:()=>w,LEFT:()=>re,LINEAR:()=>xt,Noise:()=>H,ONE:()=>vt,RIGHT:()=>ee,TypedGrid:()=>L,UP:()=>te,Vector:()=>b,ZERO:()=>st,advance:()=>ht,assert:()=>V,choose:()=>Et,colcirc:()=>q,colrect:()=>j,colrectcirc:()=>E,dd:()=>Lt,diff:()=>it,dist:()=>nt,flipImage:()=>mt,formatTime:()=>Rt,fract:()=>ot,head:()=>wt,intersection:()=>I,is:()=>A,last:()=>Tt,lerpAngle:()=>ut,log:()=>N,lpad:()=>Nt,mag:()=>lt,makeCircle:()=>gt,makeRectangle:()=>bt,mean:()=>ct,median:()=>pt,mod:()=>at,percent:()=>ft,range:()=>Mt,resolverect:()=>D,rpad:()=>At,scaleImage:()=>dt,shuffle:()=>It,sum:()=>P,tail:()=>Pt,tintImage:()=>yt,tween:()=>ne,vec:()=>n,vecAbs:()=>Dt,vecAdd:()=>F,vecAngle:()=>Ut,vecAngleBetween:()=>Ht,vecCeil:()=>Vt,vecClamp:()=>zt,vecCross:()=>Wt,vecDist:()=>Yt,vecDist2:()=>Zt,vecDiv:()=>k,vecDot:()=>et,vecEq:()=>B,vecFloor:()=>jt,vecHeading:()=>tt,vecIsZero:()=>Gt,vecLerp:()=>Xt,vecLimit:()=>Ct,vecMag:()=>Q,vecMag2:()=>v,vecMove:()=>$t,vecMult:()=>T,vecNorm:()=>O,vecRand:()=>Qt,vecReflect:()=>Bt,vecRem:()=>Kt,vecRotate:()=>Ot,vecRound:()=>qt,vecSet:()=>G,vecSetMag:()=>J,vecSub:()=>S,vecToArray:()=>Jt});var g=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,s=0,r=0,i=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(i||this._engine.W-s,o||this._engine.H-r),this.x=this.width/2,this.y=this.height/2,this._shake={x:0,y:0,removeListener:null}}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){this._engine.push(),t&&this._engine.clip(i=>{i.rect(this.ox,this.oy,this.width,this.height)});let s=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(s,r),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}end(){this._engine.pop()}lookAt(t,s){this.x=t,this.y=s}move(t,s){this.x+=t,this.y+=s}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,s=(s-this.height/2-this.oy)/this.scale,r.x=i*t-o*s+this.x,r.y=o*t+i*s+this.y,r}getCameraPoint(t,s,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=i*t-o*s,s=o*t+i*s,r.x=t*this.scale+this.width/2+this.ox,r.y=s*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",r=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=r,s<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};var I=(e,t,s,r,i,o,h,a)=>{let l=Math.max(e,i),_=Math.min(e+s,i+h)-l,p=Math.max(t,o),m=Math.min(t+r,o+a)-p;return[l,p,_,m]};var D=(e,t,s,r,i,o,h,a)=>{let[l,_,p,m]=I(e,t,s,r,i,o,h,a),x="",y=e,c=t;return p<m?e<i?(x="right",y=i-s):(x="left",y=i+h):t<o?(x="bottom",c=o-r):(x="top",c=o+a),{dir:x,x:y,y:c}};var V=(e,t="Assertion failed")=>{if(!e)throw new Error(t)};var j=(e,t,s,r,i,o,h,a)=>e<i+h&&e+s>i&&t<o+a&&t+r>o;var q=(e,t,s,r,i,o)=>(r-e)*(r-e)+(i-t)*(i-t)<=(s+o)*(s+o);var E=(e,t,s,r,i,o,h)=>{let a=i-Math.max(e,Math.min(i,e+s)),l=o-Math.max(t,Math.min(o,t+r));return a*a+l*l<=h*h};var w=class e{_w;_h;_c;constructor(t,s,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=r}[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 e(this._w,this._h,this._c)}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,r){this._c[this.pointToIndex(t,s)]=r}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}check(t,s){return t>=0&&t<this._w&&s>=0&&s<this._h}get length(){return this._w*this._h}pointToIndex(t,s){return this.clampX(~~t)+this.clampY(~~s)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,s=!1){let r=s?this.length-1:0,i=s?-1:this.length,o=s?-1:1;for(;r!==i;){let h=this.indexToPointX(r),a=this.indexToPointY(r),l=this._c[r];if(t(h,a,l,this)===!1)break;r+=o}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return z(t,0,this._w-1)}clampY(t){return z(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let r=[];return this.forEach((i,o,h)=>{r[o]=r[o]||"",r[o]+=h+t}),r.join(`
|
|
2
|
+
`)}},L=class e extends w{constructor(t,s,r=Uint8Array){super(t,s,null),this._c=new r(this._w*this._h)}has(t,s){return this.get(t,s)!==0}clone(){let t=new e(this._w,this._h,this._c.constructor);return this.forEach((s,r,i)=>{t.set(s,r,i)}),t}};function z(e,t,s){return e<t?t:e>s?s:e}var K=Math.cos,$=Math.sin,kt=2*Math.PI,b=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=e=>e instanceof b,n=(e=0,t=e)=>(d(e)&&(t=e.y,e=e.x),new b(e,t)),G=(e,t,s=t)=>(d(t)?G(e,t.x,t.y):(e.x=t,e.y=s),e),F=(e,t,s=t)=>d(t)?F(e,t.x,t.y):(e.x+=t,e.y+=s,e),S=(e,t,s=t)=>d(t)?S(e,t.x,t.y):(e.x-=t,e.y-=s,e),T=(e,t,s=t)=>d(t)?T(e,t.x,t.y):(e.x*=t,e.y*=s,e),k=(e,t,s=t)=>d(t)?k(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Ot=(e,t)=>{let s=K(t),r=$(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Bt=(e,t)=>{let s=O(n(t));return S(e,T(s,2*et(e,s)))},J=(e,t)=>(O(e),T(e,t),e),Q=e=>Math.hypot(e.x,e.y),v=e=>e.x*e.x+e.y*e.y,O=e=>{let t=Q(e);return t>0&&k(e,t),e},Ct=(e,t=1)=>(v(e)>t*t&&J(e,t),e),Yt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Zt=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},tt=e=>Math.atan2(e.y,e.x),Ut=e=>tt(e),Ht=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),et=(e,t)=>e.x*t.x+e.y*t.y,Wt=(e,t)=>e.x*t.y-e.y*t.x,Xt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Dt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Vt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),jt=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),qt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),zt=(e,t,s)=>(e.x<t.x&&(e.x=t.x),e.x>s.x&&(e.x=s.x),e.y<t.y&&(e.y=t.y),e.y>s.y&&(e.y=s.y),e),Kt=(e,t)=>(e.x%=t,e.y%=t,e),$t=(e,t,s=1)=>F(e,t.x*s,t.y*s),B=(e,t,s=t)=>d(t)?B(e,t.x,t.y):e.x===t&&e.y===s,Gt=e=>B(e,st),Jt=e=>[e.x,e.y],Qt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,i=s()*(t-e)+e;return n(K(r)*i,$(r)*i)},st=n(0,0),vt=n(1,1),te=n(0,-1),ee=n(1,0),se=n(0,1),re=n(-1,0);var ie=n(.5,.5),rt=n(0,0),oe=n(1,0),he=n(0,1),ae=n(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=rt){this.sprite=t||{width:0,height:0},this.pos=s||n(0),this._o=n(r),this._s=n(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 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),i=this.pos.x-s*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[i,o,s,r]}in(t,s,r=1){return E(...this.getBounds(),t,s,r)}col(t){return colrect(...this.getBounds(),...t.getBounds())}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,i=-this.sprite.width*(this.flipX?1-r.x:r.x),o=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(i,o,this.sprite)}};var it=(e,t)=>Math.abs(t-e)||0;var ot=e=>e%1||0;var ht=(e,t,s,r=1)=>{s&&(t.x+=s.x*r,t.y+=s.y*r),e.x+=t.x*r,e.y+=t.y*r};var at=(e,t)=>(t+e%t)%t;var nt=(e,t,s,r)=>Math.hypot(s-e,r-t);var lt=(e,t)=>Math.hypot(e,t);var P=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var ct=e=>P(e)/e.length;var pt=e=>{let t=e.sort((r,i)=>r-i),s=Math.floor(t.length/2);return t.length%2===0?(t[s-1]+t[s])/2:t[s]};var ut=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var ft=(e,t=0,s=1)=>s-t?(e-t)/(s-t):0;var R=Math.PI/2,ne=(e,t,s,r=1,i=xt)=>new Y(e,t,s,r,i),xt=e=>e,le=e=>e*e,ce=e=>-e*(e-2),pe=e=>e<.5?2*e*e:-2*e*e+4*e-1,ue=e=>e*e*e-e*Math.sin(e*Math.PI),fe=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},xe=e=>{if(e<.5){let s=2*e;return .5*(s*s*s-s*Math.sin(s*Math.PI))}let t=1-(2*e-1);return .5*(1-(t*t*t-t*Math.sin(e*Math.PI)))+.5},_e=e=>Math.sin(13*R*e)*Math.pow(2,10*(e-1)),me=e=>Math.sin(-13*R*(e+1))*Math.pow(2,-10*e)+1,de=e=>{if(e<.5){let r=Math.sin(13*R*(2*e)),i=Math.pow(2,10*(2*e-1));return .5*r*i}let t=Math.sin(-13*R*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},_t=e=>1-Z(1-e),Z=e=>e<4/11?121*e*e/16:e<8/11?363/40*e*e-99/10*e+17/5:e<9/10?4356/361*e*e-35442/1805*e+16061/1805:54/5*e*e-513/25*e+268/25,ye=e=>e<.5?.5*_t(e*2):.5*Z(e*2-1)+.5,Y=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,r,i,o){this._o=t,this._p=s,this._x=r,this._d=i,this._e=o,this._w=0}start(t){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let s=this._o[this._p]||0,r=this._rel?s+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",i=>{if(this._t<=this._w){this._t+=i;return}let o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(o/this._d)),this._t+=i,o>=this._d&&(this._o[this._p]=r,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let s of this._cb)s(this._o);return this}restart(t=null,s=!1){return this.stop(s).restart(t)}onEnd(t){return this._cb.push(t),this}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}delay(t){return this._w=t,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};var U=e=>.5*(1-Math.cos(e*Math.PI)),H=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,s=0,r=0){t<0&&(t=-t),s<0&&(s=-s),r<0&&(r=-r);let i=Math.floor(t),o=Math.floor(s),h=Math.floor(r),a=t-i,l=s-o,_=r-h,p,m,x=0,y=.5,c,u,M;for(let X=0;X<this._po;X++){let f=i+(o<<4)+(h<<8);p=U(a),m=U(l),c=this._p[f&4095],c+=p*(this._p[f+1&4095]-c),u=this._p[f+16&4095],u+=p*(this._p[f+16+1&4095]-u),c+=m*(u-c),f+=256,u=this._p[f&4095],u+=p*(this._p[f+1&4095]-u),M=this._p[f+16&4095],M+=p*(this._p[f+16+1&4095]-M),u+=m*(M-u),c+=U(_)*(u-c),x+=c*y,y*=this._pf,i<<=1,a*=2,o<<=1,l*=2,h<<=1,_*=2,a>=1&&(i++,a--),l>=1&&(o++,l--),_>=1&&(h++,_--)}return x}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let s=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=s()}};var mt=(e,t=!0,s=!1,r=globalThis)=>r.paint(e.width,e.height,i=>{r.push(),r.scale(t?-1:1,s?-1:1),r.image(t?-e.width:0,s?-e.height:0,e),r.pop()});var dt=(e,t,s=!0,r=globalThis)=>r.paint(e.width*t,e.height*t,i=>{r.push(),i.imageSmoothingEnabled=!s,r.scale(t),r.image(0,0,e),r.pop()});var yt=(e,t,s=1,r=globalThis)=>r.paint(e.width,e.height,i=>{r.push(),r.alpha(s),r.rectfill(0,0,e.width,e.height,t),i.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,e),r.pop()});var gt=(e,t,{borderWidth:s=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=e*2+s;return i.paint(o,o,()=>{i.circfill(o/2,o/2,e,t),s>0&&(i.linewidth(s),i.stroke(r))})};var bt=(e,t,s,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let h=e+r*2,a=t+r*2;return o.paint(h,a,()=>{let l=r>0;l&&o.cls(i),o.rectfill(l?r:0,l?r:0,e,t,s)})};var Mt=(e,t=0,s=1)=>[...Array(e|0).keys()].map(r=>t+s*r);var It=(e,t=globalThis.rand||Math.random)=>{e=[...e];for(let s=e.length-1;s>0;s--){let r=Math.floor(t()*(s+1)),i=e[s];e[s]=e[r],e[r]=i}return e};var Et=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var wt=e=>e[0];var Tt=e=>e[e.length-1];var Pt=e=>e.slice(1);var Rt=e=>~~(e/60)+":"+(e%60<10?"0":"")+~~(e%60);var At=(e,t,s="0")=>(e+"").padEnd(t,s);var Nt=(e,t,s="0")=>(e+"").padStart(t,s);var A=(e,t)=>typeof t=="function"?e instanceof t:typeof e===t;var N=(e,t,s=globalThis)=>s.text(16,16,(t?`${t}: `:"")+(A(e,"object")?JSON.stringify(e,null,4):e));var Lt=(e,t,s=globalThis)=>{s.pal(["blue","#fff"]),s.cls(0),s.ctx().resetTransform(),s.textfont("monospace"),s.textsize(16),s.textalign("start","top"),N(e,t||"dd() output",s),s.quit()};globalThis.utils=Object.assign(globalThis.utils||{},W);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/collision.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
__export(index_exports, {
|
|
22
22
|
colcirc: () => colcirc_default,
|
|
23
23
|
colrect: () => colrect_default,
|
|
24
|
+
colrectcirc: () => colrectcirc_default,
|
|
24
25
|
intersection: () => intersection_default,
|
|
25
26
|
resolverect: () => resolverect_default
|
|
26
27
|
});
|
|
@@ -98,6 +99,20 @@
|
|
|
98
99
|
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
99
100
|
};
|
|
100
101
|
|
|
102
|
+
// src/collision/colrectcirc.js
|
|
103
|
+
var colrectcirc_default = (x1, y1, w1, h1, x2, y2, r2) => {
|
|
104
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
105
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
106
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
107
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
108
|
+
DEV: assert_default(isFinite(x2), "colcirc: 5th param must be a number");
|
|
109
|
+
DEV: assert_default(isFinite(y2), "colcirc: 6th param must be a number");
|
|
110
|
+
DEV: assert_default(isFinite(r2), "colcirc: 7th param must be a number");
|
|
111
|
+
let xx = x2 - Math.max(x1, Math.min(x2, x1 + w1));
|
|
112
|
+
let yy = y2 - Math.max(y1, Math.min(y2, y1 + h1));
|
|
113
|
+
return xx * xx + yy * yy <= r2 * r2;
|
|
114
|
+
};
|
|
115
|
+
|
|
101
116
|
// src/collision/_web.js
|
|
102
117
|
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
103
118
|
})();
|
package/dist/collision.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var M=Object.defineProperty;var w=(e,t)=>{for(var a in t)M(e,a,{get:t[a],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(e=!0)=>{for(let t in globalThis.utils)t!=="global"&&(e||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var f={};w(f,{colcirc:()=>x,colrect:()=>g,colrectcirc:()=>T,intersection:()=>u,resolverect:()=>F});var u=(e,t,a,i,r,s,m,o)=>{let l=Math.max(e,r),d=Math.min(e+a,r+m)-l,c=Math.max(t,s),b=Math.min(t+i,s+o)-c;return[l,c,d,b]};var F=(e,t,a,i,r,s,m,o)=>{let[l,d,c,b]=u(e,t,a,i,r,s,m,o),n="",p=e,h=t;return c<b?e<r?(n="right",p=r-a):(n="left",p=r+m):t<s?(n="bottom",h=s-i):(n="top",h=s+o),{dir:n,x:p,y:h}};var g=(e,t,a,i,r,s,m,o)=>e<r+m&&e+a>r&&t<s+o&&t+i>s;var x=(e,t,a,i,r,s)=>(i-e)*(i-e)+(r-t)*(r-t)<=(a+s)*(a+s);var T=(e,t,a,i,r,s,m)=>{let o=r-Math.max(e,Math.min(r,e+a)),l=s-Math.max(t,Math.min(s,t+i));return o*o+l*l<=m*m};globalThis.utils=Object.assign(globalThis.utils||{},f);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.45.0",
|
|
4
4
|
"description": "Utilities to help build litecanvas games",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"esbuild": "^0.27.2",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
|
-
"prettier": "^3.8.
|
|
42
|
+
"prettier": "^3.8.1"
|
|
43
43
|
}
|
|
44
44
|
}
|
package/src/actor/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Vector, vec } from "../vector/index.js"
|
|
2
|
+
import colrectcirc from "../collision/colrectcirc.js"
|
|
2
3
|
|
|
3
4
|
export const ANCHOR_CENTER = /** @__PURE__ */ vec(0.5, 0.5)
|
|
4
5
|
export const ANCHOR_TOP_LEFT = /** @__PURE__ */ vec(0, 0)
|
|
@@ -35,7 +36,7 @@ export class Actor {
|
|
|
35
36
|
hidden = false
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
|
-
* @param {Image|HTMLCanvasElement|OffscreenCanvas} sprite
|
|
39
|
+
* @param {Image|HTMLCanvasElement|OffscreenCanvas|ImageBitmap} sprite
|
|
39
40
|
* @param {Vector} position
|
|
40
41
|
* @param {Vector} anchor
|
|
41
42
|
*/
|
|
@@ -133,6 +134,7 @@ export class Actor {
|
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
/**
|
|
137
|
+
* @param {boolean} scaled
|
|
136
138
|
* @returns {number[]}
|
|
137
139
|
*/
|
|
138
140
|
getBounds(scaled = true) {
|
|
@@ -143,6 +145,29 @@ export class Actor {
|
|
|
143
145
|
return [x, y, w, h]
|
|
144
146
|
}
|
|
145
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Checks if the actor is colliding with a point (with optional radius).
|
|
150
|
+
* Useful to check clicks or touchs.
|
|
151
|
+
*
|
|
152
|
+
* @param {number} x
|
|
153
|
+
* @param {number} y
|
|
154
|
+
* @param {number} [radius=1]
|
|
155
|
+
* @returns {boolean}
|
|
156
|
+
*/
|
|
157
|
+
in(x, y, radius = 1) {
|
|
158
|
+
return colrectcirc(...this.getBounds(), x, y, radius)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Checks if the actor is colliding with another actor.
|
|
163
|
+
*
|
|
164
|
+
* @param {Actor} other
|
|
165
|
+
* @returns {boolean}
|
|
166
|
+
*/
|
|
167
|
+
col(other) {
|
|
168
|
+
return colrect(...this.getBounds(), ...other.getBounds())
|
|
169
|
+
}
|
|
170
|
+
|
|
146
171
|
/**
|
|
147
172
|
* Draw the actor
|
|
148
173
|
*
|
|
@@ -168,7 +193,7 @@ export class Actor {
|
|
|
168
193
|
/**
|
|
169
194
|
* @param {LitecanvasInstance} engine
|
|
170
195
|
*/
|
|
171
|
-
transform(engine) {
|
|
196
|
+
transform(engine = globalThis) {
|
|
172
197
|
engine.translate(this.pos.x, this.pos.y)
|
|
173
198
|
engine.rotate(engine.deg2rad(this.angle))
|
|
174
199
|
engine.scale(
|
|
@@ -179,8 +204,9 @@ export class Actor {
|
|
|
179
204
|
|
|
180
205
|
/**
|
|
181
206
|
* @param {LitecanvasInstance} engine
|
|
207
|
+
* @param {boolean} alpha
|
|
182
208
|
*/
|
|
183
|
-
drawImage(engine, alpha = true) {
|
|
209
|
+
drawImage(engine = globalThis, alpha = true) {
|
|
184
210
|
const anchor = this.anchor
|
|
185
211
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x)
|
|
186
212
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check a collision between a rectangle and a circle
|
|
5
|
+
*
|
|
6
|
+
* @param {number} x1 rectangle position X
|
|
7
|
+
* @param {number} y1 rectangle position Y
|
|
8
|
+
* @param {number} w1 rectangle width
|
|
9
|
+
* @param {number} h1 rectangle height
|
|
10
|
+
* @param {number} x2 circle position X
|
|
11
|
+
* @param {number} y2 circle position Y
|
|
12
|
+
* @param {number} r2 circle radius
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export default (x1, y1, w1, h1, x2, y2, r2) => {
|
|
17
|
+
DEV: assert(isFinite(x1), "colrect: 1st param must be a number")
|
|
18
|
+
DEV: assert(isFinite(y1), "colrect: 2nd param must be a number")
|
|
19
|
+
DEV: assert(isFinite(w1), "colrect: 3rd param must be a number")
|
|
20
|
+
DEV: assert(isFinite(h1), "colrect: 4th param must be a number")
|
|
21
|
+
DEV: assert(isFinite(x2), "colcirc: 5th param must be a number")
|
|
22
|
+
DEV: assert(isFinite(y2), "colcirc: 6th param must be a number")
|
|
23
|
+
DEV: assert(isFinite(r2), "colcirc: 7th param must be a number")
|
|
24
|
+
|
|
25
|
+
let xx = x2 - Math.max(x1, Math.min(x2, x1 + w1))
|
|
26
|
+
let yy = y2 - Math.max(y1, Math.min(y2, y1 + h1))
|
|
27
|
+
|
|
28
|
+
return xx * xx + yy * yy <= r2 * r2
|
|
29
|
+
}
|
package/src/collision/index.js
CHANGED
|
@@ -2,3 +2,4 @@ export { default as resolverect } from "./resolverect.js"
|
|
|
2
2
|
export { default as intersection } from "./intersection.js"
|
|
3
3
|
export { default as colrect } from "./colrect.js"
|
|
4
4
|
export { default as colcirc } from "./colcirc.js"
|
|
5
|
+
export { default as colrectcirc } from "./colrectcirc.js"
|