@litecanvas/utils 0.38.0 → 0.40.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 +20 -23
- package/dist/actor.min.js +1 -1
- package/dist/all.js +46 -38
- package/dist/all.min.js +2 -2
- package/dist/collision.js +1 -1
- package/dist/collision.min.js +1 -1
- package/dist/grid.js +1 -1
- package/dist/math.js +9 -1
- package/dist/math.min.js +1 -1
- package/dist/vector.js +22 -18
- package/dist/vector.min.js +1 -1
- package/package.json +2 -2
- package/src/actor/index.js +23 -17
- package/src/collision/resolverect.js +2 -2
- package/src/grid/index.js +1 -1
- package/src/math/advance.js +2 -2
- package/src/math/mean.js +2 -0
- package/src/math/median.js +4 -1
- package/src/math/sum.js +5 -0
- package/src/vector/README.md +12 -4
- package/src/vector/index.js +65 -46
package/dist/actor.js
CHANGED
|
@@ -30,17 +30,13 @@
|
|
|
30
30
|
// src/vector/index.js
|
|
31
31
|
var PI2 = 2 * Math.PI;
|
|
32
32
|
var Vector = class {
|
|
33
|
-
/** @type {number} */
|
|
34
|
-
x;
|
|
35
|
-
/** @type {number} */
|
|
36
|
-
y;
|
|
37
33
|
/**
|
|
38
34
|
* @param {number} [x=0]
|
|
39
35
|
* @param {number} [y]
|
|
40
36
|
*/
|
|
41
37
|
constructor(x = 0, y = x) {
|
|
42
|
-
this.x = x;
|
|
43
|
-
this.y = y;
|
|
38
|
+
this.x = parseFloat(x) || 0;
|
|
39
|
+
this.y = parseFloat(y) || 0;
|
|
44
40
|
}
|
|
45
41
|
/**
|
|
46
42
|
* @returns {string}
|
|
@@ -89,7 +85,7 @@
|
|
|
89
85
|
* @param {Vector} anchor
|
|
90
86
|
*/
|
|
91
87
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
92
|
-
this.sprite = sprite;
|
|
88
|
+
this.sprite = sprite || { width: 0, height: 0 };
|
|
93
89
|
this.pos = position || vec(0);
|
|
94
90
|
this._o = vec(anchor);
|
|
95
91
|
this._s = vec(1, 1);
|
|
@@ -182,35 +178,36 @@
|
|
|
182
178
|
/**
|
|
183
179
|
* Draw the actor
|
|
184
180
|
*
|
|
185
|
-
* @param {LitecanvasInstance} [
|
|
181
|
+
* @param {LitecanvasInstance} [engine]
|
|
186
182
|
*/
|
|
187
|
-
draw(
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
183
|
+
draw(engine = globalThis, saveContext = true) {
|
|
184
|
+
if (saveContext) engine.push();
|
|
185
|
+
this.transform(engine);
|
|
186
|
+
if (this.sprite.width && this.sprite.height && !this.hidden && this.opacity > 0) {
|
|
187
|
+
this.drawImage(engine);
|
|
188
|
+
}
|
|
189
|
+
if (saveContext) engine.pop();
|
|
193
190
|
}
|
|
194
191
|
/**
|
|
195
|
-
* @param {LitecanvasInstance}
|
|
192
|
+
* @param {LitecanvasInstance} engine
|
|
196
193
|
*/
|
|
197
|
-
transform(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
194
|
+
transform(engine) {
|
|
195
|
+
engine.translate(this.pos.x, this.pos.y);
|
|
196
|
+
engine.rotate(engine.deg2rad(this.angle));
|
|
197
|
+
engine.scale(
|
|
201
198
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
202
199
|
(this.flipY ? -1 : 1) * this._s.y
|
|
203
200
|
);
|
|
204
201
|
}
|
|
205
202
|
/**
|
|
206
|
-
* @param {LitecanvasInstance}
|
|
203
|
+
* @param {LitecanvasInstance} engine
|
|
207
204
|
*/
|
|
208
|
-
drawImage(
|
|
205
|
+
drawImage(engine, alpha = true) {
|
|
209
206
|
const anchor = this.anchor;
|
|
210
207
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
211
208
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
|
212
|
-
if (alpha)
|
|
213
|
-
|
|
209
|
+
if (alpha) engine.alpha(this.opacity);
|
|
210
|
+
engine.image(x, y, this.sprite);
|
|
214
211
|
}
|
|
215
212
|
};
|
|
216
213
|
|
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var x=Object.defineProperty;var
|
|
1
|
+
(()=>{var x=Object.defineProperty;var a=(o,t)=>{for(var s in t)x(o,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let t in globalThis.utils)t!=="global"&&(o||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var p={};a(p,{ANCHOR_BOT_LEFT:()=>g,ANCHOR_BOT_RIGHT:()=>d,ANCHOR_CENTER:()=>u,ANCHOR_TOP_LEFT:()=>y,ANCHOR_TOP_RIGHT:()=>f,Actor:()=>h});var T=2*Math.PI,i=class{constructor(t=0,s=t){this.x=parseFloat(t)||0,this.y=parseFloat(s)||0}toString(){return`Vector (${this.x}, ${this.y})`}},l=o=>o instanceof i,e=(o=0,t=o)=>(l(o)&&(t=o.y,o=o.x),new i(o,t));var u=e(.5,.5),y=e(0,0),f=e(1,0),g=e(0,1),d=e(1,1),h=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=y){this.sprite=t||{width:0,height:0},this.pos=s||e(0),this._o=e(r),this._s=e(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),c=this.pos.x-s*this.anchor.x,n=this.pos.y-r*this.anchor.y;return[c,n,s,r]}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){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,s=!0){let r=this.anchor,c=-this.sprite.width*(this.flipX?1-r.x:r.x),n=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(c,n,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},p);})();
|
package/dist/all.js
CHANGED
|
@@ -102,11 +102,13 @@
|
|
|
102
102
|
vecNorm: () => vecNorm,
|
|
103
103
|
vecRand: () => vecRand,
|
|
104
104
|
vecReflect: () => vecReflect,
|
|
105
|
+
vecRem: () => vecRem,
|
|
105
106
|
vecRotate: () => vecRotate,
|
|
106
107
|
vecRound: () => vecRound,
|
|
107
108
|
vecSet: () => vecSet,
|
|
108
109
|
vecSetMag: () => vecSetMag,
|
|
109
|
-
vecSub: () => vecSub
|
|
110
|
+
vecSub: () => vecSub,
|
|
111
|
+
vecToArray: () => vecToArray
|
|
110
112
|
});
|
|
111
113
|
|
|
112
114
|
// src/camera/index.js
|
|
@@ -318,7 +320,7 @@
|
|
|
318
320
|
resolveY = y2 + h2;
|
|
319
321
|
}
|
|
320
322
|
}
|
|
321
|
-
return { direction, x: resolveX, y: resolveY };
|
|
323
|
+
return { dir: direction, x: resolveX, y: resolveY };
|
|
322
324
|
};
|
|
323
325
|
|
|
324
326
|
// src/debug/assert.js
|
|
@@ -488,7 +490,7 @@
|
|
|
488
490
|
* @param {number} y
|
|
489
491
|
* @param {any} value
|
|
490
492
|
* @param {Grid} grid
|
|
491
|
-
* @returns {
|
|
493
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
492
494
|
*
|
|
493
495
|
* @param {GridForEachCallback} callback
|
|
494
496
|
* @param {boolean} [reverse=false]
|
|
@@ -585,17 +587,13 @@
|
|
|
585
587
|
var sin = Math.sin;
|
|
586
588
|
var PI2 = 2 * Math.PI;
|
|
587
589
|
var Vector = class {
|
|
588
|
-
/** @type {number} */
|
|
589
|
-
x;
|
|
590
|
-
/** @type {number} */
|
|
591
|
-
y;
|
|
592
590
|
/**
|
|
593
591
|
* @param {number} [x=0]
|
|
594
592
|
* @param {number} [y]
|
|
595
593
|
*/
|
|
596
594
|
constructor(x = 0, y = x) {
|
|
597
|
-
this.x = x;
|
|
598
|
-
this.y = y;
|
|
595
|
+
this.x = parseFloat(x) || 0;
|
|
596
|
+
this.y = parseFloat(y) || 0;
|
|
599
597
|
}
|
|
600
598
|
/**
|
|
601
599
|
* @returns {string}
|
|
@@ -612,12 +610,6 @@
|
|
|
612
610
|
}
|
|
613
611
|
return new Vector(x, y);
|
|
614
612
|
};
|
|
615
|
-
var vecEq = (v, x, y = x) => {
|
|
616
|
-
if (isVector(x)) {
|
|
617
|
-
return vecEq(v, x.x, x.y);
|
|
618
|
-
}
|
|
619
|
-
return v.x === x && v.y === y;
|
|
620
|
-
};
|
|
621
613
|
var vecSet = (v, x, y = x) => {
|
|
622
614
|
if (isVector(x)) {
|
|
623
615
|
vecSet(v, x.x, x.y);
|
|
@@ -708,11 +700,6 @@
|
|
|
708
700
|
a.y += (b.y - a.y) * t || 0;
|
|
709
701
|
return a;
|
|
710
702
|
};
|
|
711
|
-
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
712
|
-
const angle = rng() * PI2;
|
|
713
|
-
const radius = rng() * (maxlength - minlength) + minlength;
|
|
714
|
-
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
715
|
-
};
|
|
716
703
|
var vecAbs = (v) => {
|
|
717
704
|
v.x = Math.abs(v.x);
|
|
718
705
|
v.y = Math.abs(v.y);
|
|
@@ -740,8 +727,25 @@
|
|
|
740
727
|
if (v.y > max.y) v.y = max.y;
|
|
741
728
|
return v;
|
|
742
729
|
};
|
|
730
|
+
var vecRem = (v, value) => {
|
|
731
|
+
v.x %= value;
|
|
732
|
+
v.y %= value;
|
|
733
|
+
return v;
|
|
734
|
+
};
|
|
743
735
|
var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta);
|
|
736
|
+
var vecEq = (v, x, y = x) => {
|
|
737
|
+
if (isVector(x)) {
|
|
738
|
+
return vecEq(v, x.x, x.y);
|
|
739
|
+
}
|
|
740
|
+
return v.x === x && v.y === y;
|
|
741
|
+
};
|
|
744
742
|
var vecIsZero = (v) => vecEq(v, ZERO);
|
|
743
|
+
var vecToArray = (v) => [v.x, v.y];
|
|
744
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
745
|
+
const angle = rng() * PI2;
|
|
746
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
747
|
+
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
748
|
+
};
|
|
745
749
|
var ZERO = /* @__PURE__ */ vec(0, 0);
|
|
746
750
|
var ONE = /* @__PURE__ */ vec(1, 1);
|
|
747
751
|
var UP = /* @__PURE__ */ vec(0, -1);
|
|
@@ -780,7 +784,7 @@
|
|
|
780
784
|
* @param {Vector} anchor
|
|
781
785
|
*/
|
|
782
786
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
783
|
-
this.sprite = sprite;
|
|
787
|
+
this.sprite = sprite || { width: 0, height: 0 };
|
|
784
788
|
this.pos = position || vec(0);
|
|
785
789
|
this._o = vec(anchor);
|
|
786
790
|
this._s = vec(1, 1);
|
|
@@ -873,35 +877,36 @@
|
|
|
873
877
|
/**
|
|
874
878
|
* Draw the actor
|
|
875
879
|
*
|
|
876
|
-
* @param {LitecanvasInstance} [
|
|
880
|
+
* @param {LitecanvasInstance} [engine]
|
|
877
881
|
*/
|
|
878
|
-
draw(
|
|
879
|
-
if (
|
|
880
|
-
|
|
881
|
-
this.
|
|
882
|
-
|
|
883
|
-
|
|
882
|
+
draw(engine = globalThis, saveContext = true) {
|
|
883
|
+
if (saveContext) engine.push();
|
|
884
|
+
this.transform(engine);
|
|
885
|
+
if (this.sprite.width && this.sprite.height && !this.hidden && this.opacity > 0) {
|
|
886
|
+
this.drawImage(engine);
|
|
887
|
+
}
|
|
888
|
+
if (saveContext) engine.pop();
|
|
884
889
|
}
|
|
885
890
|
/**
|
|
886
|
-
* @param {LitecanvasInstance}
|
|
891
|
+
* @param {LitecanvasInstance} engine
|
|
887
892
|
*/
|
|
888
|
-
transform(
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
893
|
+
transform(engine) {
|
|
894
|
+
engine.translate(this.pos.x, this.pos.y);
|
|
895
|
+
engine.rotate(engine.deg2rad(this.angle));
|
|
896
|
+
engine.scale(
|
|
892
897
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
893
898
|
(this.flipY ? -1 : 1) * this._s.y
|
|
894
899
|
);
|
|
895
900
|
}
|
|
896
901
|
/**
|
|
897
|
-
* @param {LitecanvasInstance}
|
|
902
|
+
* @param {LitecanvasInstance} engine
|
|
898
903
|
*/
|
|
899
|
-
drawImage(
|
|
904
|
+
drawImage(engine, alpha = true) {
|
|
900
905
|
const anchor = this.anchor;
|
|
901
906
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x);
|
|
902
907
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y);
|
|
903
|
-
if (alpha)
|
|
904
|
-
|
|
908
|
+
if (alpha) engine.alpha(this.opacity);
|
|
909
|
+
engine.image(x, y, this.sprite);
|
|
905
910
|
}
|
|
906
911
|
};
|
|
907
912
|
|
|
@@ -936,6 +941,7 @@
|
|
|
936
941
|
|
|
937
942
|
// src/math/sum.js
|
|
938
943
|
var sum_default = (values) => {
|
|
944
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
939
945
|
let result = 0;
|
|
940
946
|
for (let i = 0; i < values.length; i++) {
|
|
941
947
|
result += values[i];
|
|
@@ -945,11 +951,13 @@
|
|
|
945
951
|
|
|
946
952
|
// src/math/mean.js
|
|
947
953
|
var mean_default = (values) => {
|
|
954
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
948
955
|
return sum_default(values) / values.length;
|
|
949
956
|
};
|
|
950
957
|
|
|
951
958
|
// src/math/median.js
|
|
952
|
-
var median_default = (
|
|
959
|
+
var median_default = (values) => {
|
|
960
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
953
961
|
const sorted = values.sort((a, b) => a - b);
|
|
954
962
|
const middle = Math.floor(sorted.length / 2);
|
|
955
963
|
if (sorted.length % 2 === 0) {
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var bt=Object.defineProperty;var Pt=(
|
|
2
|
-
`)}},T=class
|
|
1
|
+
(()=>{var bt=Object.defineProperty;var Pt=(s,t)=>{for(var e in t)bt(s,e,{get:t[e],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(s=!0)=>{for(let t in globalThis.utils)t!=="global"&&(s||globalThis[t]===void 0)&&(globalThis[t]=globalThis.utils[t])};var Z={};Pt(Z,{ANCHOR_BOT_LEFT:()=>vt,ANCHOR_BOT_RIGHT:()=>ts,ANCHOR_CENTER:()=>Jt,ANCHOR_TOP_LEFT:()=>v,ANCHOR_TOP_RIGHT:()=>Qt,Actor:()=>O,BACK_IN:()=>os,BACK_IN_OUT:()=>as,BACK_OUT:()=>hs,BOUNCE_IN:()=>pt,BOUNCE_IN_OUT:()=>ps,BOUNCE_OUT:()=>C,Camera:()=>g,DOWN:()=>$t,EASE_IN:()=>es,EASE_IN_OUT:()=>is,EASE_OUT:()=>rs,ELASTIC_IN:()=>ns,ELASTIC_IN_OUT:()=>cs,ELASTIC_OUT:()=>ls,Grid:()=>w,LEFT:()=>Gt,LINEAR:()=>ct,Noise:()=>Y,ONE:()=>qt,RIGHT:()=>Kt,TypedGrid:()=>T,UP:()=>zt,Vector:()=>I,ZERO:()=>Q,advance:()=>et,choose:()=>gt,colcirc:()=>X,colrect:()=>W,diff:()=>tt,dist:()=>it,flipImage:()=>ut,formatTime:()=>wt,fract:()=>st,head:()=>It,intersection:()=>M,last:()=>Et,lerpAngle:()=>nt,mag:()=>ot,makeCircle:()=>xt,makeRectangle:()=>mt,mean:()=>ht,median:()=>at,mod:()=>rt,percent:()=>lt,range:()=>dt,resolverect:()=>H,scaleImage:()=>ft,shuffle:()=>yt,sum:()=>P,tail:()=>Mt,tintImage:()=>_t,tween:()=>ss,vec:()=>h,vecAbs:()=>Bt,vecAdd:()=>A,vecAngle:()=>St,vecAngleBetween:()=>Ot,vecCeil:()=>Yt,vecClamp:()=>Ht,vecCross:()=>Ft,vecDist:()=>Lt,vecDist2:()=>kt,vecDiv:()=>L,vecDot:()=>J,vecEq:()=>S,vecFloor:()=>Zt,vecHeading:()=>G,vecIsZero:()=>Dt,vecLerp:()=>Ct,vecLimit:()=>Nt,vecMag:()=>K,vecMag2:()=>$,vecMove:()=>Xt,vecMult:()=>b,vecNorm:()=>k,vecRand:()=>jt,vecReflect:()=>At,vecRem:()=>Wt,vecRotate:()=>Tt,vecRound:()=>Ut,vecSet:()=>q,vecSetMag:()=>z,vecSub:()=>N,vecToArray:()=>Vt});var g=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(t=null,e=0,r=0,i=null,o=null){this._engine=t||globalThis,this.ox=e,this.oy=r,this.resize(i||this._engine.W-e,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,e){this.width=t,this.height=e,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 e=this.ox+this.width/2,r=this.oy+this.height/2;this._engine.translate(e,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,e){this.x=t,this.y=e}move(t,e){this.x+=t,this.y+=e}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,e,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,e=(e-this.height/2-this.oy)/this.scale,r.x=i*t-o*e+this.x,r.y=o*t+i*e+this.y,r}getCameraPoint(t,e,r={}){let i=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,e=e-this.y,t=i*t-o*e,e=o*t+i*e,r.x=t*this.scale+this.width/2+this.ox,r.y=e*this.scale+this.height/2+this.oy,r}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(t=1,e=.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),e-=r,e<=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 M=(s,t,e,r,i,o,a,n)=>{let l=Math.max(s,i),x=Math.min(s+e,i+a)-l,p=Math.max(t,o),m=Math.min(t+r,o+n)-p;return[l,p,x,m]};var H=(s,t,e,r,i,o,a,n)=>{let[l,x,p,m]=M(s,t,e,r,i,o,a,n),_="",y=s,c=t;return p<m?s<i?(_="right",y=i-e):(_="left",y=i+a):t<o?(_="bottom",c=o-r):(_="top",c=o+n),{dir:_,x:y,y:c}};var W=(s,t,e,r,i,o,a,n)=>s<i+a&&s+e>i&&t<o+n&&t+r>o;var X=(s,t,e,r,i,o)=>(r-s)*(r-s)+(i-t)*(i-t)<=(e+o)*(e+o);var w=class s{_w;_h;_c;constructor(t,e,r=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~e),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 s(this._w,this._h,this._c)}clear(){this.forEach((t,e)=>this.set(t,e,void 0))}get width(){return this._w}get height(){return this._h}set(t,e,r){this._c[this.pointToIndex(t,e)]=r}get(t,e){return this._c[this.pointToIndex(t,e)]}has(t,e){return this.get(t,e)!=null}check(t,e){return t>=0&&t<this._w&&e>=0&&e<this._h}get length(){return this._w*this._h}pointToIndex(t,e){return this.clampX(~~t)+this.clampY(~~e)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,e=!1){let r=e?this.length-1:0,i=e?-1:this.length,o=e?-1:1;for(;r!==i;){let a=this.indexToPointX(r),n=this.indexToPointY(r),l=this._c[r];if(t(a,n,l,this)===!1)break;r+=o}}fill(t){this.forEach((e,r)=>{this.set(e,r,t)})}clampX(t){return D(t,0,this._w-1)}clampY(t){return D(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",e=!0){if(!e)return this._c.join(t);let r=[];return this.forEach((i,o,a)=>{r[o]=r[o]||"",r[o]+=a+t}),r.join(`
|
|
2
|
+
`)}},T=class s extends w{constructor(t,e,r=Uint8Array){super(t,e,null),this._c=new r(this._w*this._h)}has(t,e){return this.get(t,e)!==0}clone(){let t=new s(this._w,this._h,this._c.constructor);return this.forEach((e,r,i)=>{t.set(e,r,i)}),t}};function D(s,t,e){return s<t?t:s>e?e:s}var V=Math.cos,j=Math.sin,Rt=2*Math.PI,I=class{constructor(t=0,e=t){this.x=parseFloat(t)||0,this.y=parseFloat(e)||0}toString(){return`Vector (${this.x}, ${this.y})`}},d=s=>s instanceof I,h=(s=0,t=s)=>(d(s)&&(t=s.y,s=s.x),new I(s,t)),q=(s,t,e=t)=>(d(t)?q(s,t.x,t.y):(s.x=t,s.y=e),s),A=(s,t,e=t)=>d(t)?A(s,t.x,t.y):(s.x+=t,s.y+=e,s),N=(s,t,e=t)=>d(t)?N(s,t.x,t.y):(s.x-=t,s.y-=e,s),b=(s,t,e=t)=>d(t)?b(s,t.x,t.y):(s.x*=t,s.y*=e,s),L=(s,t,e=t)=>d(t)?L(s,t.x,t.y):(s.x/=t||1,s.y/=e||1,s),Tt=(s,t)=>{let e=V(t),r=j(t);return s.x=e*s.x-r*s.y,s.y=r*s.x+e*s.y,s},At=(s,t)=>{let e=k(h(t));return N(s,b(e,2*J(s,e)))},z=(s,t)=>(k(s),b(s,t),s),K=s=>Math.hypot(s.x,s.y),$=s=>s.x*s.x+s.y*s.y,k=s=>{let t=K(s);return t>0&&L(s,t),s},Nt=(s,t=1)=>($(s)>t*t&&z(s,t),s),Lt=(s,t)=>Math.hypot(t.x-s.x,t.y-s.y),kt=(s,t)=>{let e=s.x-t.x,r=s.y-t.y;return e*e+r*r},G=s=>Math.atan2(s.y,s.x),St=s=>G(s),Ot=(s,t)=>Math.atan2(t.y-s.y,t.x-s.x),J=(s,t)=>s.x*t.x+s.y*t.y,Ft=(s,t)=>s.x*t.y-s.y*t.x,Ct=(s,t,e)=>(s.x+=(t.x-s.x)*e||0,s.y+=(t.y-s.y)*e||0,s),Bt=s=>(s.x=Math.abs(s.x),s.y=Math.abs(s.y),s),Yt=s=>(s.x=Math.ceil(s.x),s.y=Math.ceil(s.y),s),Zt=s=>(s.x=Math.floor(s.x),s.y=Math.floor(s.y),s),Ut=s=>(s.x=Math.round(s.x),s.y=Math.round(s.y),s),Ht=(s,t,e)=>(s.x<t.x&&(s.x=t.x),s.x>e.x&&(s.x=e.x),s.y<t.y&&(s.y=t.y),s.y>e.y&&(s.y=e.y),s),Wt=(s,t)=>(s.x%=t,s.y%=t,s),Xt=(s,t,e=1)=>A(s,t.x*e,t.y*e),S=(s,t,e=t)=>d(t)?S(s,t.x,t.y):s.x===t&&s.y===e,Dt=s=>S(s,Q),Vt=s=>[s.x,s.y],jt=(s=1,t=s,e=globalThis.rand||Math.random)=>{let r=e()*Rt,i=e()*(t-s)+s;return h(V(r)*i,j(r)*i)},Q=h(0,0),qt=h(1,1),zt=h(0,-1),Kt=h(1,0),$t=h(0,1),Gt=h(-1,0);var Jt=h(.5,.5),v=h(0,0),Qt=h(1,0),vt=h(0,1),ts=h(1,1),O=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,e,r=v){this.sprite=t||{width:0,height:0},this.pos=e||h(0),this._o=h(r),this._s=h(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,e=t){this._s.x=t,this._s.y=e}scaleBy(t,e=t){this._s.x*=t,this._s.y*=e}getBounds(t=!0){let e=this.sprite.width*(t?this._s.x:1),r=this.sprite.height*(t?this._s.y:1),i=this.pos.x-e*this.anchor.x,o=this.pos.y-r*this.anchor.y;return[i,o,e,r]}draw(t=globalThis,e=!0){e&&t.push(),this.transform(t),this.sprite.width&&this.sprite.height&&!this.hidden&&this.opacity>0&&this.drawImage(t),e&&t.pop()}transform(t){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,e=!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);e&&t.alpha(this.opacity),t.image(i,o,this.sprite)}};var tt=(s,t)=>Math.abs(t-s)||0;var st=s=>s%1||0;var et=(s,t,e,r=1)=>{e&&(t.x+=e.x*r,t.y+=e.y*r),s.x+=t.x*r,s.y+=t.y*r};var rt=(s,t)=>(t+s%t)%t;var it=(s,t,e,r)=>Math.hypot(e-s,r-t);var ot=(s,t)=>Math.hypot(s,t);var P=s=>{let t=0;for(let e=0;e<s.length;e++)t+=s[e];return t};var ht=s=>P(s)/s.length;var at=s=>{let t=s.sort((r,i)=>r-i),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var nt=(s,t,e)=>{let r=(t-s)%360;return r>180?r-=360:r<-180&&(r+=360),s+r*e};var lt=(s,t=0,e=1)=>e-t?(s-t)/(e-t):0;var R=Math.PI/2,ss=(s,t,e,r=1,i=ct)=>new F(s,t,e,r,i),ct=s=>s,es=s=>s*s,rs=s=>-s*(s-2),is=s=>s<.5?2*s*s:-2*s*s+4*s-1,os=s=>s*s*s-s*Math.sin(s*Math.PI),hs=s=>{let t=1-s;return 1-(t*t*t-t*Math.sin(t*Math.PI))},as=s=>{if(s<.5){let e=2*s;return .5*(e*e*e-e*Math.sin(e*Math.PI))}let t=1-(2*s-1);return .5*(1-(t*t*t-t*Math.sin(s*Math.PI)))+.5},ns=s=>Math.sin(13*R*s)*Math.pow(2,10*(s-1)),ls=s=>Math.sin(-13*R*(s+1))*Math.pow(2,-10*s)+1,cs=s=>{if(s<.5){let r=Math.sin(13*R*(2*s)),i=Math.pow(2,10*(2*s-1));return .5*r*i}let t=Math.sin(-13*R*(2*s-1+1)),e=Math.pow(2,-10*(2*s-1));return .5*(t*e+2)},pt=s=>1-C(1-s),C=s=>s<4/11?121*s*s/16:s<8/11?363/40*s*s-99/10*s+17/5:s<9/10?4356/361*s*s-35442/1805*s+16061/1805:54/5*s*s-513/25*s+268/25,ps=s=>s<.5?.5*pt(s*2):.5*C(s*2-1)+.5,F=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,e,r,i,o){this._o=t,this._p=e,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 e=this._o[this._p]||0,r=this._rel?e+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(e,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 e of this._cb)e(this._o);return this}restart(t=null,e=!1){return this.stop(e).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 B=s=>.5*(1-Math.cos(s*Math.PI)),Y=class{_p=[];_po=4;_pf=.5;_e=null;constructor(t){this._e=t||globalThis,this.noiseSeed()}noise(t,e=0,r=0){t<0&&(t=-t),e<0&&(e=-e),r<0&&(r=-r);let i=Math.floor(t),o=Math.floor(e),a=Math.floor(r),n=t-i,l=e-o,x=r-a,p,m,_=0,y=.5,c,u,E;for(let U=0;U<this._po;U++){let f=i+(o<<4)+(a<<8);p=B(n),m=B(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),E=this._p[f+16&4095],E+=p*(this._p[f+16+1&4095]-E),u+=m*(E-u),c+=B(x)*(u-c),_+=c*y,y*=this._pf,i<<=1,n*=2,o<<=1,l*=2,a<<=1,x*=2,n>=1&&(i++,n--),l>=1&&(o++,l--),x>=1&&(a++,x--)}return _}noiseDetail(t,e){t>0&&(this._po=t),e>0&&(this._pf=e)}noiseSeed(t=null){t!=null&&this._e.rseed(t);let e=this._e.rand||Math.random;for(let r=0;r<4096;r++)this._p[r]=e()}};var ut=(s,t=!0,e=!1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.scale(t?-1:1,e?-1:1),r.image(t?-s.width:0,e?-s.height:0,s),r.pop()});var ft=(s,t,e=!0,r=globalThis)=>r.paint(s.width*t,s.height*t,i=>{r.push(),i.imageSmoothingEnabled=!e,r.scale(t),r.image(0,0,s),r.pop()});var _t=(s,t,e=1,r=globalThis)=>r.paint(s.width,s.height,i=>{r.push(),r.alpha(e),r.rectfill(0,0,s.width,s.height,t),i.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,s),r.pop()});var xt=(s,t,{borderWidth:e=0,borderColor:r=0,engine:i=globalThis}={})=>{let o=s*2+e;return i.paint(o,o,()=>{i.circfill(o/2,o/2,s,t),e>0&&(i.linewidth(e),i.stroke(r))})};var mt=(s,t,e,{borderWidth:r=0,borderColor:i=0,engine:o=globalThis}={})=>{let a=s+r*2,n=t+r*2;return o.paint(a,n,()=>{let l=r>0;l&&o.cls(i),o.rectfill(l?r:0,l?r:0,s,t,e)})};var dt=(s,t=0,e=1)=>[...new Array(s).keys()].map(r=>t+e*r);var yt=(s,t=globalThis.rand||Math.random)=>{s=[...s];for(let e=s.length-1;e>0;e--){let r=Math.floor(t()*(e+1)),i=s[e];s[e]=s[r],s[r]=i}return s};var gt=(s,t=globalThis.rand||Math.random)=>s[Math.floor(t()*s.length)];var It=s=>s[0];var Et=s=>s[s.length-1];var Mt=s=>s.slice(1);var wt=s=>~~(s/60)+":"+(s%60<10?"0":"")+~~(s%60);globalThis.utils=Object.assign(globalThis.utils||{},Z);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/collision.js
CHANGED
package/dist/collision.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var x=Object.defineProperty;var w=(e,t)=>{for(var i in t)x(e,i,{get:t[i],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 h={};w(h,{colcirc:()=>T,colrect:()=>F,intersection:()=>c,resolverect:()=>g});var c=(e,t,i,a,r,s,o,l)=>{let u=Math.max(e,r),d=Math.min(e+i,r+o)-u,n=Math.max(t,s),b=Math.min(t+a,s+l)-n;return[u,n,d,b]};var g=(e,t,i,a,r,s,o,l)=>{let[u,d,n,b]=c(e,t,i,a,r,s,o,l),m="",p=e,f=t;return n<b?e<r?(m="right",p=r-i):(m="left",p=r+o):t<s?(m="bottom",f=s-a):(m="top",f=s+l),{
|
|
1
|
+
(()=>{var x=Object.defineProperty;var w=(e,t)=>{for(var i in t)x(e,i,{get:t[i],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 h={};w(h,{colcirc:()=>T,colrect:()=>F,intersection:()=>c,resolverect:()=>g});var c=(e,t,i,a,r,s,o,l)=>{let u=Math.max(e,r),d=Math.min(e+i,r+o)-u,n=Math.max(t,s),b=Math.min(t+a,s+l)-n;return[u,n,d,b]};var g=(e,t,i,a,r,s,o,l)=>{let[u,d,n,b]=c(e,t,i,a,r,s,o,l),m="",p=e,f=t;return n<b?e<r?(m="right",p=r-i):(m="left",p=r+o):t<s?(m="bottom",f=s-a):(m="top",f=s+l),{dir:m,x:p,y:f}};var F=(e,t,i,a,r,s,o,l)=>e<r+o&&e+i>r&&t<s+l&&t+a>s;var T=(e,t,i,a,r,s)=>(a-e)*(a-e)+(r-t)*(r-t)<=(i+s)*(i+s);globalThis.utils=Object.assign(globalThis.utils||{},h);})();
|
package/dist/grid.js
CHANGED
|
@@ -159,7 +159,7 @@
|
|
|
159
159
|
* @param {number} y
|
|
160
160
|
* @param {any} value
|
|
161
161
|
* @param {Grid} grid
|
|
162
|
-
* @returns {
|
|
162
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
163
163
|
*
|
|
164
164
|
* @param {GridForEachCallback} callback
|
|
165
165
|
* @param {boolean} [reverse=false]
|
package/dist/math.js
CHANGED
|
@@ -64,8 +64,14 @@
|
|
|
64
64
|
return Math.hypot(x, y);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
// src/debug/assert.js
|
|
68
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
69
|
+
if (!condition) throw new Error(message);
|
|
70
|
+
};
|
|
71
|
+
|
|
67
72
|
// src/math/sum.js
|
|
68
73
|
var sum_default = (values) => {
|
|
74
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
69
75
|
let result = 0;
|
|
70
76
|
for (let i = 0; i < values.length; i++) {
|
|
71
77
|
result += values[i];
|
|
@@ -75,11 +81,13 @@
|
|
|
75
81
|
|
|
76
82
|
// src/math/mean.js
|
|
77
83
|
var mean_default = (values) => {
|
|
84
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
78
85
|
return sum_default(values) / values.length;
|
|
79
86
|
};
|
|
80
87
|
|
|
81
88
|
// src/math/median.js
|
|
82
|
-
var median_default = (
|
|
89
|
+
var median_default = (values) => {
|
|
90
|
+
DEV: assert_default(Array.isArray(values), "1st param must be an array");
|
|
83
91
|
const sorted = values.sort((a, b) => a - b);
|
|
84
92
|
const middle = Math.floor(sorted.length / 2);
|
|
85
93
|
if (sorted.length % 2 === 0) {
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var m=Object.defineProperty;var g=(r,t)=>{for(var e in t)m(r,e,{get:t[e],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 n={};g(n,{advance:()=>x,diff:()=>a,dist:()=>u,fract:()=>c,lerpAngle:()=>i,mag:()=>l,mean:()=>f,median:()=>y,mod:()=>p,percent:()=>h,sum:()=>s});var a=(r,t)=>Math.abs(t-r)||0;var c=r=>r%1||0;var T=2*Math.PI;var x=(r,t,e,o=1)=>{e&&(t.x+=e.x*o,t.y+=e.y*o),r.x+=t.x*o,r.y+=t.y*o};var p=(r,t)=>(t+r%t)%t;var u=(r,t,e,o)=>Math.hypot(e-r,o-t);var l=(r,t)=>Math.hypot(r,t);var s=r=>{let t=0;for(let e=0;e<r.length;e++)t+=r[e];return t};var f=r=>s(r)/r.length;var y=r=>{let t=r.sort((o,d)=>o-d),e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]};var i=(r,t,e)=>{let o=(t-r)%360;return o>180?o-=360:o<-180&&(o+=360),r+o*e};var h=(r,t=0,e=1)=>e-t?(r-t)/(e-t):0;globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/vector.js
CHANGED
|
@@ -40,27 +40,25 @@
|
|
|
40
40
|
vecNorm: () => vecNorm,
|
|
41
41
|
vecRand: () => vecRand,
|
|
42
42
|
vecReflect: () => vecReflect,
|
|
43
|
+
vecRem: () => vecRem,
|
|
43
44
|
vecRotate: () => vecRotate,
|
|
44
45
|
vecRound: () => vecRound,
|
|
45
46
|
vecSet: () => vecSet,
|
|
46
47
|
vecSetMag: () => vecSetMag,
|
|
47
|
-
vecSub: () => vecSub
|
|
48
|
+
vecSub: () => vecSub,
|
|
49
|
+
vecToArray: () => vecToArray
|
|
48
50
|
});
|
|
49
51
|
var cos = Math.cos;
|
|
50
52
|
var sin = Math.sin;
|
|
51
53
|
var PI2 = 2 * Math.PI;
|
|
52
54
|
var Vector = class {
|
|
53
|
-
/** @type {number} */
|
|
54
|
-
x;
|
|
55
|
-
/** @type {number} */
|
|
56
|
-
y;
|
|
57
55
|
/**
|
|
58
56
|
* @param {number} [x=0]
|
|
59
57
|
* @param {number} [y]
|
|
60
58
|
*/
|
|
61
59
|
constructor(x = 0, y = x) {
|
|
62
|
-
this.x = x;
|
|
63
|
-
this.y = y;
|
|
60
|
+
this.x = parseFloat(x) || 0;
|
|
61
|
+
this.y = parseFloat(y) || 0;
|
|
64
62
|
}
|
|
65
63
|
/**
|
|
66
64
|
* @returns {string}
|
|
@@ -77,12 +75,6 @@
|
|
|
77
75
|
}
|
|
78
76
|
return new Vector(x, y);
|
|
79
77
|
};
|
|
80
|
-
var vecEq = (v, x, y = x) => {
|
|
81
|
-
if (isVector(x)) {
|
|
82
|
-
return vecEq(v, x.x, x.y);
|
|
83
|
-
}
|
|
84
|
-
return v.x === x && v.y === y;
|
|
85
|
-
};
|
|
86
78
|
var vecSet = (v, x, y = x) => {
|
|
87
79
|
if (isVector(x)) {
|
|
88
80
|
vecSet(v, x.x, x.y);
|
|
@@ -173,11 +165,6 @@
|
|
|
173
165
|
a.y += (b.y - a.y) * t || 0;
|
|
174
166
|
return a;
|
|
175
167
|
};
|
|
176
|
-
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
177
|
-
const angle = rng() * PI2;
|
|
178
|
-
const radius = rng() * (maxlength - minlength) + minlength;
|
|
179
|
-
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
180
|
-
};
|
|
181
168
|
var vecAbs = (v) => {
|
|
182
169
|
v.x = Math.abs(v.x);
|
|
183
170
|
v.y = Math.abs(v.y);
|
|
@@ -205,8 +192,25 @@
|
|
|
205
192
|
if (v.y > max.y) v.y = max.y;
|
|
206
193
|
return v;
|
|
207
194
|
};
|
|
195
|
+
var vecRem = (v, value) => {
|
|
196
|
+
v.x %= value;
|
|
197
|
+
v.y %= value;
|
|
198
|
+
return v;
|
|
199
|
+
};
|
|
208
200
|
var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta);
|
|
201
|
+
var vecEq = (v, x, y = x) => {
|
|
202
|
+
if (isVector(x)) {
|
|
203
|
+
return vecEq(v, x.x, x.y);
|
|
204
|
+
}
|
|
205
|
+
return v.x === x && v.y === y;
|
|
206
|
+
};
|
|
209
207
|
var vecIsZero = (v) => vecEq(v, ZERO);
|
|
208
|
+
var vecToArray = (v) => [v.x, v.y];
|
|
209
|
+
var vecRand = (minlength = 1, maxlength = minlength, rng = globalThis.rand || Math.random) => {
|
|
210
|
+
const angle = rng() * PI2;
|
|
211
|
+
const radius = rng() * (maxlength - minlength) + minlength;
|
|
212
|
+
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
213
|
+
};
|
|
210
214
|
var ZERO = /* @__PURE__ */ vec(0, 0);
|
|
211
215
|
var ONE = /* @__PURE__ */ vec(1, 1);
|
|
212
216
|
var UP = /* @__PURE__ */ vec(0, -1);
|
package/dist/vector.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var E=Object.defineProperty;var F=(t,e)=>{for(var o in e)E(t,o,{get:e[o],enumerable:!0})};var h={};F(h,{DOWN:()=>X,LEFT:()=>Y,ONE:()=>J,RIGHT:()=>Q,UP:()=>K,Vector:()=>s,ZERO:()=>C,vec:()=>r,vecAbs:()=>U,vecAdd:()=>y,vecAngle:()=>N,vecAngleBetween:()=>P,vecCeil:()=>Z,vecClamp:()=>B,vecCross:()=>w,vecDist:()=>q,vecDist2:()=>L,vecDiv:()=>u,vecDot:()=>D,vecEq:()=>a,vecFloor:()=>$,vecHeading:()=>A,vecIsZero:()=>W,vecLerp:()=>H,vecLimit:()=>b,vecMag:()=>R,vecMag2:()=>T,vecMove:()=>V,vecMult:()=>x,vecNorm:()=>i,vecRand:()=>z,vecReflect:()=>S,vecRem:()=>G,vecRotate:()=>O,vecRound:()=>j,vecSet:()=>d,vecSetMag:()=>g,vecSub:()=>p,vecToArray:()=>k});var M=Math.cos,f=Math.sin,I=2*Math.PI,s=class{constructor(e=0,o=e){this.x=parseFloat(e)||0,this.y=parseFloat(o)||0}toString(){return`Vector (${this.x}, ${this.y})`}},n=t=>t instanceof s,r=(t=0,e=t)=>(n(t)&&(e=t.y,t=t.x),new s(t,e)),d=(t,e,o=e)=>(n(e)?d(t,e.x,e.y):(t.x=e,t.y=o),t),y=(t,e,o=e)=>n(e)?y(t,e.x,e.y):(t.x+=e,t.y+=o,t),p=(t,e,o=e)=>n(e)?p(t,e.x,e.y):(t.x-=e,t.y-=o,t),x=(t,e,o=e)=>n(e)?x(t,e.x,e.y):(t.x*=e,t.y*=o,t),u=(t,e,o=e)=>n(e)?u(t,e.x,e.y):(t.x/=e||1,t.y/=o||1,t),O=(t,e)=>{let o=M(e),c=f(e);return t.x=o*t.x-c*t.y,t.y=c*t.x+o*t.y,t},S=(t,e)=>{let o=i(r(e));return p(t,x(o,2*D(t,o)))},g=(t,e)=>(i(t),x(t,e),t),R=t=>Math.hypot(t.x,t.y),T=t=>t.x*t.x+t.y*t.y,i=t=>{let e=R(t);return e>0&&u(t,e),t},b=(t,e=1)=>(T(t)>e*e&&g(t,e),t),q=(t,e)=>Math.hypot(e.x-t.x,e.y-t.y),L=(t,e)=>{let o=t.x-e.x,c=t.y-e.y;return o*o+c*c},A=t=>Math.atan2(t.y,t.x),N=t=>A(t),P=(t,e)=>Math.atan2(e.y-t.y,e.x-t.x),D=(t,e)=>t.x*e.x+t.y*e.y,w=(t,e)=>t.x*e.y-t.y*e.x,H=(t,e,o)=>(t.x+=(e.x-t.x)*o||0,t.y+=(e.y-t.y)*o||0,t),U=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),Z=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),$=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),j=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),B=(t,e,o)=>(t.x<e.x&&(t.x=e.x),t.x>o.x&&(t.x=o.x),t.y<e.y&&(t.y=e.y),t.y>o.y&&(t.y=o.y),t),G=(t,e)=>(t.x%=e,t.y%=e,t),V=(t,e,o=1)=>y(t,e.x*o,e.y*o),a=(t,e,o=e)=>n(e)?a(t,e.x,e.y):t.x===e&&t.y===o,W=t=>a(t,C),k=t=>[t.x,t.y],z=(t=1,e=t,o=globalThis.rand||Math.random)=>{let c=o()*I,l=o()*(e-t)+t;return r(M(c)*l,f(c)*l)},C=r(0,0),J=r(1,1),K=r(0,-1),Q=r(1,0),X=r(0,1),Y=r(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},h);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Utilities to help build litecanvas games",
|
|
5
5
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"litecanvas": "latest"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"esbuild": "^0.25.
|
|
40
|
+
"esbuild": "^0.25.9",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
42
|
"prettier": "^3.6.2"
|
|
43
43
|
}
|
package/src/actor/index.js
CHANGED
|
@@ -40,7 +40,7 @@ export class Actor {
|
|
|
40
40
|
* @param {Vector} anchor
|
|
41
41
|
*/
|
|
42
42
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
43
|
-
this.sprite = sprite
|
|
43
|
+
this.sprite = sprite || { width: 0, height: 0 }
|
|
44
44
|
this.pos = position || vec(0)
|
|
45
45
|
this._o = vec(anchor) // clone the anchor vector
|
|
46
46
|
this._s = vec(1, 1)
|
|
@@ -146,40 +146,46 @@ export class Actor {
|
|
|
146
146
|
/**
|
|
147
147
|
* Draw the actor
|
|
148
148
|
*
|
|
149
|
-
* @param {LitecanvasInstance} [
|
|
149
|
+
* @param {LitecanvasInstance} [engine]
|
|
150
150
|
*/
|
|
151
|
-
draw(
|
|
152
|
-
if (
|
|
151
|
+
draw(engine = globalThis, saveContext = true) {
|
|
152
|
+
if (saveContext) engine.push()
|
|
153
153
|
|
|
154
|
-
|
|
154
|
+
this.transform(engine)
|
|
155
155
|
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
if (
|
|
157
|
+
this.sprite.width &&
|
|
158
|
+
this.sprite.height &&
|
|
159
|
+
!this.hidden &&
|
|
160
|
+
this.opacity > 0
|
|
161
|
+
) {
|
|
162
|
+
this.drawImage(engine)
|
|
163
|
+
}
|
|
158
164
|
|
|
159
|
-
if (saveContext)
|
|
165
|
+
if (saveContext) engine.pop()
|
|
160
166
|
}
|
|
161
167
|
|
|
162
168
|
/**
|
|
163
|
-
* @param {LitecanvasInstance}
|
|
169
|
+
* @param {LitecanvasInstance} engine
|
|
164
170
|
*/
|
|
165
|
-
transform(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
171
|
+
transform(engine) {
|
|
172
|
+
engine.translate(this.pos.x, this.pos.y)
|
|
173
|
+
engine.rotate(engine.deg2rad(this.angle))
|
|
174
|
+
engine.scale(
|
|
169
175
|
(this.flipX ? -1 : 1) * this._s.x,
|
|
170
176
|
(this.flipY ? -1 : 1) * this._s.y
|
|
171
177
|
)
|
|
172
178
|
}
|
|
173
179
|
|
|
174
180
|
/**
|
|
175
|
-
* @param {LitecanvasInstance}
|
|
181
|
+
* @param {LitecanvasInstance} engine
|
|
176
182
|
*/
|
|
177
|
-
drawImage(
|
|
183
|
+
drawImage(engine, alpha = true) {
|
|
178
184
|
const anchor = this.anchor
|
|
179
185
|
const x = -this.sprite.width * (this.flipX ? 1 - anchor.x : anchor.x)
|
|
180
186
|
const y = -this.sprite.height * (this.flipY ? 1 - anchor.y : anchor.y)
|
|
181
187
|
|
|
182
|
-
if (alpha)
|
|
183
|
-
|
|
188
|
+
if (alpha) engine.alpha(this.opacity)
|
|
189
|
+
engine.image(x, y, this.sprite)
|
|
184
190
|
}
|
|
185
191
|
}
|
|
@@ -12,7 +12,7 @@ import intersection from "./intersection.js"
|
|
|
12
12
|
* @param {number} y2
|
|
13
13
|
* @param {number} w2
|
|
14
14
|
* @param {number} h2
|
|
15
|
-
* @returns {{
|
|
15
|
+
* @returns {{dir: string, x: number, y: number}} An object containing the direction of the collision and the new position (X and Y) of the first rectangle.
|
|
16
16
|
*/
|
|
17
17
|
export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
18
18
|
// get the intersection area
|
|
@@ -49,5 +49,5 @@ export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return { direction, x: resolveX, y: resolveY }
|
|
52
|
+
return { dir: direction, x: resolveX, y: resolveY }
|
|
53
53
|
}
|
package/src/grid/index.js
CHANGED
|
@@ -152,7 +152,7 @@ export class Grid {
|
|
|
152
152
|
* @param {number} y
|
|
153
153
|
* @param {any} value
|
|
154
154
|
* @param {Grid} grid
|
|
155
|
-
* @returns {
|
|
155
|
+
* @returns {false|void} returns `false` to stop/break the loop
|
|
156
156
|
*
|
|
157
157
|
* @param {GridForEachCallback} callback
|
|
158
158
|
* @param {boolean} [reverse=false]
|
package/src/math/advance.js
CHANGED
|
@@ -6,8 +6,8 @@ import { Vector } from "../vector/index.js"
|
|
|
6
6
|
*
|
|
7
7
|
* @param {Vector} position
|
|
8
8
|
* @param {Vector} velocity
|
|
9
|
-
* @param {Vector
|
|
10
|
-
* @param {number
|
|
9
|
+
* @param {Vector} [acceleration]
|
|
10
|
+
* @param {number} [deltaTime]
|
|
11
11
|
*/
|
|
12
12
|
export default (position, velocity, acceleration, deltaTime = 1) => {
|
|
13
13
|
if (acceleration) {
|
package/src/math/mean.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import assert from "../debug/assert.js"
|
|
1
2
|
import sum from "./sum.js"
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -7,5 +8,6 @@ import sum from "./sum.js"
|
|
|
7
8
|
* @returns {number}
|
|
8
9
|
*/
|
|
9
10
|
export default (values) => {
|
|
11
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
10
12
|
return sum(values) / values.length
|
|
11
13
|
}
|
package/src/math/median.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Compute the median of the values. The values are sorted and the middle value is returned.
|
|
3
5
|
* In case of an even number of values, the average of the two middle values is returned
|
|
@@ -5,7 +7,8 @@
|
|
|
5
7
|
* @param {number[]} values
|
|
6
8
|
* @returns {number}
|
|
7
9
|
*/
|
|
8
|
-
export default (
|
|
10
|
+
export default (values) => {
|
|
11
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
9
12
|
const sorted = values.sort((a, b) => a - b)
|
|
10
13
|
const middle = Math.floor(sorted.length / 2)
|
|
11
14
|
|
package/src/math/sum.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Computes the sum of the values in a array.
|
|
3
5
|
*
|
|
6
|
+
* usage: `sum([2,3,5]) // returns 10`
|
|
7
|
+
*
|
|
4
8
|
* @param {number[]} values list of numbers
|
|
5
9
|
* @returns {number}
|
|
6
10
|
*/
|
|
7
11
|
export default (values) => {
|
|
12
|
+
DEV: assert(Array.isArray(values), "1st param must be an array")
|
|
8
13
|
let result = 0
|
|
9
14
|
for (let i = 0; i < values.length; i++) {
|
|
10
15
|
result += values[i]
|
package/src/vector/README.md
CHANGED
|
@@ -129,17 +129,19 @@ Returns the distance between two points represented by vectors squared.
|
|
|
129
129
|
|
|
130
130
|
Syntax: `vecDist2(a: Vector, b: Vector): number`
|
|
131
131
|
|
|
132
|
-
##
|
|
132
|
+
## vecHeading
|
|
133
133
|
|
|
134
134
|
Calculates the angle a vector makes with the positive x-axis.
|
|
135
135
|
|
|
136
|
-
Syntax: `
|
|
136
|
+
Syntax: `vecHeading(v: Vector): number`
|
|
137
137
|
|
|
138
138
|
```js
|
|
139
|
-
const a =
|
|
140
|
-
const b =
|
|
139
|
+
const a = vecHeading(vec(1, 0)) // outputs 0 (zero)
|
|
140
|
+
const b = vecHeading(vec(0, 1)) // outputs ~1.571 (same as Math.PI/2 or 90°)
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
> Note: `vecAngle` is alias of `vecHeading`.
|
|
144
|
+
|
|
143
145
|
## vecDot
|
|
144
146
|
|
|
145
147
|
Calculates the dot product of two vectors.
|
|
@@ -163,6 +165,12 @@ values equal to the new vector's.
|
|
|
163
165
|
|
|
164
166
|
Syntax: `vecLerp(a: Vector, b: Vector, atm: number): Lerp`
|
|
165
167
|
|
|
168
|
+
## vecRem
|
|
169
|
+
|
|
170
|
+
Performs modulo (remainder) division with a vector's components.
|
|
171
|
+
|
|
172
|
+
Syntax: `vecRem(v: Vector, divisor: number): Vector`
|
|
173
|
+
|
|
166
174
|
## vecRand
|
|
167
175
|
|
|
168
176
|
Creates a vector with random direction and (optional) length.
|
package/src/vector/index.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
const
|
|
2
|
-
cos = Math.cos,
|
|
1
|
+
const cos = Math.cos,
|
|
3
2
|
sin = Math.sin,
|
|
4
3
|
PI2 = 2 * Math.PI
|
|
5
4
|
|
|
6
5
|
export class Vector {
|
|
7
|
-
/** @type {number} */
|
|
8
|
-
x
|
|
9
|
-
/** @type {number} */
|
|
10
|
-
y
|
|
11
|
-
|
|
12
6
|
/**
|
|
13
7
|
* @param {number} [x=0]
|
|
14
8
|
* @param {number} [y]
|
|
15
9
|
*/
|
|
16
10
|
constructor(x = 0, y = x) {
|
|
17
|
-
this.x = x
|
|
18
|
-
this.y = y
|
|
11
|
+
this.x = parseFloat(x) || 0
|
|
12
|
+
this.y = parseFloat(y) || 0
|
|
19
13
|
}
|
|
20
14
|
|
|
21
15
|
/**
|
|
@@ -47,21 +41,6 @@ export const vec = (x = 0, y = x) => {
|
|
|
47
41
|
return new Vector(x, y)
|
|
48
42
|
}
|
|
49
43
|
|
|
50
|
-
/**
|
|
51
|
-
* Checks whether two vectors are equal.
|
|
52
|
-
*
|
|
53
|
-
* @param {Vector} v
|
|
54
|
-
* @param {number|Vector} x
|
|
55
|
-
* @param {number} [y]
|
|
56
|
-
* @returns {boolean}
|
|
57
|
-
*/
|
|
58
|
-
export const vecEq = (v, x, y = x) => {
|
|
59
|
-
if (isVector(x)) {
|
|
60
|
-
return vecEq(v, x.x, x.y)
|
|
61
|
-
}
|
|
62
|
-
return v.x === x && v.y === y
|
|
63
|
-
}
|
|
64
|
-
|
|
65
44
|
/**
|
|
66
45
|
* Assigns new values to a vector.
|
|
67
46
|
*
|
|
@@ -187,7 +166,7 @@ export const vecReflect = (v, normal) => {
|
|
|
187
166
|
}
|
|
188
167
|
|
|
189
168
|
/**
|
|
190
|
-
*
|
|
169
|
+
* Sets a vector's magnitude to value.
|
|
191
170
|
*
|
|
192
171
|
* @param {Vector} v
|
|
193
172
|
* @param {Vector} normal
|
|
@@ -338,27 +317,6 @@ export const vecLerp = (a, b, t) => {
|
|
|
338
317
|
return a
|
|
339
318
|
}
|
|
340
319
|
|
|
341
|
-
/**
|
|
342
|
-
* Sample a vector with random direction and (optional) length.
|
|
343
|
-
*
|
|
344
|
-
* If the `litecanvas#rand()` not is globally explosed, uses `Math.random()`.
|
|
345
|
-
* You can set `vecconfig.random` to set your own "random" function.
|
|
346
|
-
*
|
|
347
|
-
* @param {number} [minlength]
|
|
348
|
-
* @param {number} [maxlength]
|
|
349
|
-
* @param {() => number} [randomFn]
|
|
350
|
-
* @returns {Vector}
|
|
351
|
-
*/
|
|
352
|
-
export const vecRand = (
|
|
353
|
-
minlength = 1,
|
|
354
|
-
maxlength = minlength,
|
|
355
|
-
rng = globalThis.rand || Math.random
|
|
356
|
-
) => {
|
|
357
|
-
const angle = rng() * PI2
|
|
358
|
-
const radius = rng() * (maxlength - minlength) + minlength
|
|
359
|
-
return vec(cos(angle) * radius, sin(angle) * radius)
|
|
360
|
-
}
|
|
361
|
-
|
|
362
320
|
/**
|
|
363
321
|
* @param {Vector} v
|
|
364
322
|
* @returns {Vector}
|
|
@@ -421,6 +379,19 @@ export const vecClamp = (v, min, max) => {
|
|
|
421
379
|
return v
|
|
422
380
|
}
|
|
423
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Performs modulo (remainder) division with a vector's components.
|
|
384
|
+
*
|
|
385
|
+
* @param {Vector} v
|
|
386
|
+
* @param {number} value
|
|
387
|
+
* @returns {Vector}
|
|
388
|
+
*/
|
|
389
|
+
export const vecRem = (v, value) => {
|
|
390
|
+
v.x %= value
|
|
391
|
+
v.y %= value
|
|
392
|
+
return v
|
|
393
|
+
}
|
|
394
|
+
|
|
424
395
|
/**
|
|
425
396
|
* @param {Vector} from
|
|
426
397
|
* @param {Vector} to
|
|
@@ -430,8 +401,56 @@ export const vecClamp = (v, min, max) => {
|
|
|
430
401
|
export const vecMove = (from, to, delta = 1) =>
|
|
431
402
|
vecAdd(from, to.x * delta, to.y * delta)
|
|
432
403
|
|
|
404
|
+
/**
|
|
405
|
+
* Checks whether two vectors are equal.
|
|
406
|
+
*
|
|
407
|
+
* @param {Vector} v
|
|
408
|
+
* @param {number|Vector} x
|
|
409
|
+
* @param {number} [y]
|
|
410
|
+
* @returns {boolean}
|
|
411
|
+
*/
|
|
412
|
+
export const vecEq = (v, x, y = x) => {
|
|
413
|
+
if (isVector(x)) {
|
|
414
|
+
return vecEq(v, x.x, x.y)
|
|
415
|
+
}
|
|
416
|
+
return v.x === x && v.y === y
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Checks if the vector's components are zero.
|
|
421
|
+
*
|
|
422
|
+
* @param {Vector} v
|
|
423
|
+
* @returns {boolean}
|
|
424
|
+
*/
|
|
433
425
|
export const vecIsZero = (v) => vecEq(v, ZERO)
|
|
434
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Returns the vector's components as an array of numbers.
|
|
429
|
+
*
|
|
430
|
+
* @returns {number[]}
|
|
431
|
+
*/
|
|
432
|
+
export const vecToArray = (v) => [v.x, v.y]
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Sample a vector with random direction and (optional) length.
|
|
436
|
+
*
|
|
437
|
+
* If the `litecanvas#rand()` not is globally explosed, uses `Math.random()`.
|
|
438
|
+
*
|
|
439
|
+
* @param {number} [minlength]
|
|
440
|
+
* @param {number} [maxlength]
|
|
441
|
+
* @param {() => number} [randomFn]
|
|
442
|
+
* @returns {Vector}
|
|
443
|
+
*/
|
|
444
|
+
export const vecRand = (
|
|
445
|
+
minlength = 1,
|
|
446
|
+
maxlength = minlength,
|
|
447
|
+
rng = globalThis.rand || Math.random
|
|
448
|
+
) => {
|
|
449
|
+
const angle = rng() * PI2
|
|
450
|
+
const radius = rng() * (maxlength - minlength) + minlength
|
|
451
|
+
return vec(cos(angle) * radius, sin(angle) * radius)
|
|
452
|
+
}
|
|
453
|
+
|
|
435
454
|
// constants
|
|
436
455
|
export const ZERO = /** @__PURE__ */ vec(0, 0)
|
|
437
456
|
export const ONE = /** @__PURE__ */ vec(1, 1)
|