@litecanvas/utils 0.31.0 → 0.33.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.min.js +1 -1
- package/dist/all.js +96 -91
- package/dist/all.min.js +2 -2
- package/dist/camera.js +5 -30
- package/dist/camera.min.js +1 -1
- package/dist/collection.js +4 -6
- package/dist/collection.min.js +1 -1
- package/dist/collision.js +47 -6
- package/dist/collision.min.js +1 -1
- package/dist/image.min.js +1 -1
- package/dist/math.js +11 -10
- package/dist/math.min.js +1 -1
- package/dist/noise.min.js +1 -1
- package/dist/tween.min.js +1 -1
- package/package.json +2 -2
- package/src/camera/index.js +6 -33
- package/src/collection/range.js +3 -7
- package/src/collection/shuffle.js +3 -3
- package/src/collision/README.md +17 -5
- package/src/collision/_web.js +2 -6
- package/src/collision/colcirc.js +23 -0
- package/src/collision/colrect.js +27 -0
- package/src/collision/index.js +4 -0
- package/src/collision/intersection.js +1 -1
- package/src/collision/{resolve.js → resolverect.js} +1 -1
- package/src/debug/assert.js +7 -0
- package/src/index.js +1 -2
- package/src/math/index.js +1 -1
- package/src/math/median.js +17 -0
- package/src/math/roundd.js +0 -14
package/dist/actor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var c=Object.defineProperty;var
|
|
1
|
+
(()=>{var c=Object.defineProperty;var p=(r,e)=>{for(var t in e)c(r,t,{get:e[t],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(r=!0)=>{for(let e in globalThis.utils)e!=="global"&&(r||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var m={};p(m,{ANCHOR_BOT_LEFT:()=>d,ANCHOR_BOT_RIGHT:()=>g,ANCHOR_CENTER:()=>h,ANCHOR_TOP_LEFT:()=>l,ANCHOR_TOP_RIGHT:()=>f,Actor:()=>u});var x=2*Math.PI,n=class{x;y;constructor(e=0,t=e){this.x=e,this.y=t}toString(){return`Vector (${this.x}, ${this.y})`}},b=r=>r instanceof n,a=(r=0,e=r)=>(b(r)&&(e=r.y,r=r.x),new n(r,e));var h=a(.5,.5),l=a(0,0),f=a(1,0),d=a(0,1),g=a(1,1),u=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(e,t,s=l){this.sprite=e,this.pos=t||a(0),this._o=a(s),this._s=a(1,1)}set x(e){this.pos.x=e}get x(){return this.pos.x}set y(e){this.pos.y=e}get y(){return this.pos.y}set anchor(e){this._o.x=e.x,this._o.y=e.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(e,t=e){this._s.x=e,this._s.y=t}scaleBy(e,t=e){this._s.x*=e,this._s.y*=t}getBounds(e=!0){let t=this.sprite.width*(e?this._s.x:1),s=this.sprite.height*(e?this._s.y:1),i=this.pos.x-t*this.anchor.x,o=this.pos.y-s*this.anchor.y;return[i,o,t,s]}draw(e=globalThis,t=!0){this.hidden||this.opacity<=0||(t&&e.push(),this.transform(e),this.drawImage(e),t&&e.pop())}transform(e){e.translate(this.pos.x,this.pos.y),e.rotate(e.deg2rad(this.angle)),e.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(e,t=!0){let s=this.anchor,i=-this.sprite.width*(this.flipX?1-s.x:s.x),o=-this.sprite.height*(this.flipY?1-s.y:s.y);t&&e.alpha(this.opacity),e.image(i,o,this.sprite)}};globalThis.utils=Object.assign(globalThis.utils||{},m);})();
|
package/dist/all.js
CHANGED
|
@@ -51,6 +51,8 @@
|
|
|
51
51
|
ZERO: () => ZERO,
|
|
52
52
|
advance: () => advance_default,
|
|
53
53
|
choose: () => choose_default,
|
|
54
|
+
colcirc: () => colcirc_default,
|
|
55
|
+
colrect: () => colrect_default,
|
|
54
56
|
diff: () => diff_default,
|
|
55
57
|
dist: () => dist_default,
|
|
56
58
|
flipImage: () => flip_default,
|
|
@@ -63,10 +65,10 @@
|
|
|
63
65
|
makeCircle: () => make_circle_default,
|
|
64
66
|
makeRectangle: () => make_rectangle_default,
|
|
65
67
|
mean: () => mean_default,
|
|
68
|
+
median: () => median_default,
|
|
66
69
|
mod: () => mod_default,
|
|
67
70
|
range: () => range_default,
|
|
68
|
-
|
|
69
|
-
roundd: () => roundd_default,
|
|
71
|
+
resolverect: () => resolverect_default,
|
|
70
72
|
scaleImage: () => scale_default,
|
|
71
73
|
shuffle: () => shuffle_default,
|
|
72
74
|
sum: () => sum_default,
|
|
@@ -105,50 +107,6 @@
|
|
|
105
107
|
wave: () => wave_default
|
|
106
108
|
});
|
|
107
109
|
|
|
108
|
-
// src/collision/intersection.js
|
|
109
|
-
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
110
|
-
const left = Math.max(x1, x2);
|
|
111
|
-
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
112
|
-
const top = Math.max(y1, y2);
|
|
113
|
-
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
114
|
-
return [left, top, width, height];
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// src/collision/resolve.js
|
|
118
|
-
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
119
|
-
const [left, top, width, height] = intersection_default(
|
|
120
|
-
x1,
|
|
121
|
-
y1,
|
|
122
|
-
w1,
|
|
123
|
-
h1,
|
|
124
|
-
x2,
|
|
125
|
-
y2,
|
|
126
|
-
w2,
|
|
127
|
-
h2
|
|
128
|
-
);
|
|
129
|
-
let direction = "";
|
|
130
|
-
let resolveX = x1;
|
|
131
|
-
let resolveY = y1;
|
|
132
|
-
if (width < height) {
|
|
133
|
-
if (x1 < x2) {
|
|
134
|
-
direction = "right";
|
|
135
|
-
resolveX = x2 - w1;
|
|
136
|
-
} else {
|
|
137
|
-
direction = "left";
|
|
138
|
-
resolveX = x2 + w2;
|
|
139
|
-
}
|
|
140
|
-
} else {
|
|
141
|
-
if (y1 < y2) {
|
|
142
|
-
direction = "bottom";
|
|
143
|
-
resolveY = y2 - h1;
|
|
144
|
-
} else {
|
|
145
|
-
direction = "top";
|
|
146
|
-
resolveY = y2 + h2;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return { direction, x: resolveX, y: resolveY };
|
|
150
|
-
};
|
|
151
|
-
|
|
152
110
|
// src/camera/index.js
|
|
153
111
|
var Camera = class {
|
|
154
112
|
/** @type {LitecanvasInstance} */
|
|
@@ -169,11 +127,6 @@
|
|
|
169
127
|
rotation = 0;
|
|
170
128
|
/** @type {number} */
|
|
171
129
|
scale = 1;
|
|
172
|
-
_shake = {
|
|
173
|
-
x: 0,
|
|
174
|
-
y: 0,
|
|
175
|
-
removeListener: null
|
|
176
|
-
};
|
|
177
130
|
/**
|
|
178
131
|
* @param {LitecanvasInstance} engine
|
|
179
132
|
*/
|
|
@@ -187,6 +140,11 @@
|
|
|
187
140
|
);
|
|
188
141
|
this.x = this.width / 2;
|
|
189
142
|
this.y = this.height / 2;
|
|
143
|
+
this._shake = {
|
|
144
|
+
x: 0,
|
|
145
|
+
y: 0,
|
|
146
|
+
removeListener: null
|
|
147
|
+
};
|
|
190
148
|
}
|
|
191
149
|
resize(width, height) {
|
|
192
150
|
this.width = width;
|
|
@@ -288,31 +246,6 @@
|
|
|
288
246
|
getBounds() {
|
|
289
247
|
return [this.ox, this.oy, this.width, this.height];
|
|
290
248
|
}
|
|
291
|
-
/**
|
|
292
|
-
* Check if a rect is inside of the camera.
|
|
293
|
-
*
|
|
294
|
-
* @param {number} x
|
|
295
|
-
* @param {number} y
|
|
296
|
-
* @param {number} width
|
|
297
|
-
* @param {number} height
|
|
298
|
-
* @returns {boolean}
|
|
299
|
-
*/
|
|
300
|
-
viewing(x, y, width, height) {
|
|
301
|
-
const cameraX = this.width / 2 - this.x;
|
|
302
|
-
const cameraY = this.height / 2 - this.y;
|
|
303
|
-
const cameraWidth = this.width / this.scale;
|
|
304
|
-
const cameraHeight = this.height / this.scale;
|
|
305
|
-
return this._engine.colrect(
|
|
306
|
-
x,
|
|
307
|
-
y,
|
|
308
|
-
width,
|
|
309
|
-
height,
|
|
310
|
-
cameraX,
|
|
311
|
-
cameraY,
|
|
312
|
-
cameraWidth,
|
|
313
|
-
cameraHeight
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
249
|
/**
|
|
317
250
|
* Shake the camera
|
|
318
251
|
*
|
|
@@ -345,6 +278,79 @@
|
|
|
345
278
|
}
|
|
346
279
|
};
|
|
347
280
|
|
|
281
|
+
// src/collision/intersection.js
|
|
282
|
+
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
283
|
+
const left = Math.max(x1, x2);
|
|
284
|
+
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
285
|
+
const top = Math.max(y1, y2);
|
|
286
|
+
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
287
|
+
return [left, top, width, height];
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/collision/resolverect.js
|
|
291
|
+
var resolverect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
292
|
+
const [left, top, width, height] = intersection_default(
|
|
293
|
+
x1,
|
|
294
|
+
y1,
|
|
295
|
+
w1,
|
|
296
|
+
h1,
|
|
297
|
+
x2,
|
|
298
|
+
y2,
|
|
299
|
+
w2,
|
|
300
|
+
h2
|
|
301
|
+
);
|
|
302
|
+
let direction = "";
|
|
303
|
+
let resolveX = x1;
|
|
304
|
+
let resolveY = y1;
|
|
305
|
+
if (width < height) {
|
|
306
|
+
if (x1 < x2) {
|
|
307
|
+
direction = "right";
|
|
308
|
+
resolveX = x2 - w1;
|
|
309
|
+
} else {
|
|
310
|
+
direction = "left";
|
|
311
|
+
resolveX = x2 + w2;
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
if (y1 < y2) {
|
|
315
|
+
direction = "bottom";
|
|
316
|
+
resolveY = y2 - h1;
|
|
317
|
+
} else {
|
|
318
|
+
direction = "top";
|
|
319
|
+
resolveY = y2 + h2;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return { direction, x: resolveX, y: resolveY };
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// src/debug/assert.js
|
|
326
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
327
|
+
if (!condition) throw new Error(message);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// src/collision/colrect.js
|
|
331
|
+
var colrect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
332
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
333
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
334
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
335
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
336
|
+
DEV: assert_default(isFinite(x2), "colrect: 5th param must be a number");
|
|
337
|
+
DEV: assert_default(isFinite(y2), "colrect: 6th param must be a number");
|
|
338
|
+
DEV: assert_default(isFinite(w2), "colrect: 7th param must be a number");
|
|
339
|
+
DEV: assert_default(isFinite(h2), "colrect: 8th param must be a number");
|
|
340
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
// src/collision/colcirc.js
|
|
344
|
+
var colcirc_default = (x1, y1, r1, x2, y2, r2) => {
|
|
345
|
+
DEV: assert_default(isFinite(x1), "colcirc: 1st param must be a number");
|
|
346
|
+
DEV: assert_default(isFinite(y1), "colcirc: 2nd param must be a number");
|
|
347
|
+
DEV: assert_default(isFinite(r1), "colcirc: 3rd param must be a number");
|
|
348
|
+
DEV: assert_default(isFinite(x2), "colcirc: 4th param must be a number");
|
|
349
|
+
DEV: assert_default(isFinite(y2), "colcirc: 5th param must be a number");
|
|
350
|
+
DEV: assert_default(isFinite(r2), "colcirc: 6th param must be a number");
|
|
351
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
352
|
+
};
|
|
353
|
+
|
|
348
354
|
// src/grid/index.js
|
|
349
355
|
var Grid = class _Grid {
|
|
350
356
|
/** @type {number} The grid width */
|
|
@@ -923,15 +929,6 @@
|
|
|
923
929
|
// src/math/mod.js
|
|
924
930
|
var mod_default = (a, b) => (b + a % b) % b;
|
|
925
931
|
|
|
926
|
-
// src/math/roundd.js
|
|
927
|
-
var roundd_default = (n, precision = 0) => {
|
|
928
|
-
if (!precision) {
|
|
929
|
-
return Math.round(n);
|
|
930
|
-
}
|
|
931
|
-
const multiplier = Math.pow(10, precision);
|
|
932
|
-
return Math.round(n * multiplier) / multiplier;
|
|
933
|
-
};
|
|
934
|
-
|
|
935
932
|
// src/math/dist.js
|
|
936
933
|
var dist_default = (x1, y1, x2, y2) => {
|
|
937
934
|
return Math.hypot(x2 - x1, y2 - y1);
|
|
@@ -956,6 +953,16 @@
|
|
|
956
953
|
return sum_default(values) / values.length;
|
|
957
954
|
};
|
|
958
955
|
|
|
956
|
+
// src/math/median.js
|
|
957
|
+
var median_default = (...values) => {
|
|
958
|
+
const sorted = values.sort((a, b) => a - b);
|
|
959
|
+
const middle = Math.floor(sorted.length / 2);
|
|
960
|
+
if (sorted.length % 2 === 0) {
|
|
961
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
962
|
+
}
|
|
963
|
+
return sorted[middle];
|
|
964
|
+
};
|
|
965
|
+
|
|
959
966
|
// src/math/lerp-angle.js
|
|
960
967
|
var lerp_angle_default = (start, end, amount) => {
|
|
961
968
|
let dif = (end - start) % 360;
|
|
@@ -1381,15 +1388,13 @@
|
|
|
1381
1388
|
};
|
|
1382
1389
|
|
|
1383
1390
|
// src/collection/range.js
|
|
1384
|
-
var range_default = (size, from = 0, step = 1) => Array.
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
})
|
|
1388
|
-
);
|
|
1391
|
+
var range_default = (size, from = 0, step = 1) => [...new Array(size).keys()].map((i) => {
|
|
1392
|
+
return from + step * i;
|
|
1393
|
+
});
|
|
1389
1394
|
|
|
1390
1395
|
// src/collection/shuffle.js
|
|
1391
1396
|
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
1392
|
-
values =
|
|
1397
|
+
values = [...values];
|
|
1393
1398
|
for (let i = values.length - 1; i > 0; i--) {
|
|
1394
1399
|
let j = Math.floor(rng() * (i + 1));
|
|
1395
1400
|
let temp = values[i];
|
package/dist/all.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`)}},
|
|
1
|
+
(()=>{var Mt=Object.defineProperty;var It=(e,t)=>{for(var s in t)Mt(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 z={};It(z,{ANCHOR_BOT_LEFT:()=>te,ANCHOR_BOT_RIGHT:()=>ee,ANCHOR_CENTER:()=>Qt,ANCHOR_TOP_LEFT:()=>J,ANCHOR_TOP_RIGHT:()=>Jt,Actor:()=>F,BACK_IN:()=>oe,BACK_IN_OUT:()=>ue,BACK_OUT:()=>ne,BOUNCE_IN:()=>mt,BOUNCE_IN_OUT:()=>ce,BOUNCE_OUT:()=>H,Camera:()=>y,DOWN:()=>Zt,EASE_IN:()=>re,EASE_IN_OUT:()=>ie,EASE_OUT:()=>ae,ELASTIC_IN:()=>le,ELASTIC_IN_OUT:()=>me,ELASTIC_OUT:()=>he,Grid:()=>M,LEFT:()=>Kt,LINEAR:()=>ht,Noise:()=>Y,ONE:()=>Vt,RIGHT:()=>$t,TypedGrid:()=>C,UP:()=>Wt,Vector:()=>w,ZERO:()=>Q,advance:()=>rt,choose:()=>wt,colcirc:()=>j,colrect:()=>B,diff:()=>tt,dist:()=>it,flipImage:()=>ft,fract:()=>st,head:()=>Nt,intersection:()=>E,last:()=>Tt,lerpAngle:()=>lt,mag:()=>ot,makeCircle:()=>xt,makeRectangle:()=>gt,mean:()=>nt,median:()=>ut,mod:()=>at,range:()=>_t,resolverect:()=>U,scaleImage:()=>bt,shuffle:()=>yt,sum:()=>A,tail:()=>Et,tintImage:()=>dt,tween:()=>se,vec:()=>o,vecAbs:()=>zt,vecAdd:()=>S,vecAngle:()=>Ft,vecAngleBetween:()=>Rt,vecCeil:()=>Xt,vecClamp:()=>jt,vecCross:()=>Ht,vecDist:()=>Lt,vecDist2:()=>Dt,vecDiv:()=>I,vecDot:()=>K,vecEq:()=>O,vecFloor:()=>Ut,vecIsZero:()=>Gt,vecLerp:()=>vt,vecLimit:()=>St,vecMag:()=>$,vecMag2:()=>Z,vecMove:()=>qt,vecMult:()=>N,vecNorm:()=>D,vecRand:()=>Yt,vecReflect:()=>Ct,vecRotate:()=>Pt,vecRound:()=>Bt,vecSet:()=>W,vecSetMag:()=>Ot,vecSub:()=>L,wave:()=>et});var y=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,a=null,i=null){this._engine=t||globalThis,this.ox=s,this.oy=r,this.resize(a||this._engine.WIDTH-s,i||this._engine.HEIGHT-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){if(this._engine.push(),t){let a=path();a.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(a)}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 a=Math.cos(-this.rotation),i=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=a*t-i*s+this.x,r.y=i*t+a*s+this.y,r}getCameraPoint(t,s,r={}){let a=Math.cos(-this.rotation),i=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=a*t-i*s,s=i*t+a*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 E=(e,t,s,r,a,i,n,u)=>{let h=Math.max(e,a),d=Math.min(e+s,a+n)-h,m=Math.max(t,i),x=Math.min(t+r,i+u)-m;return[h,m,d,x]};var U=(e,t,s,r,a,i,n,u)=>{let[h,d,m,x]=E(e,t,s,r,a,i,n,u),b="",_=e,l=t;return m<x?e<a?(b="right",_=a-s):(b="left",_=a+n):t<i?(b="bottom",l=i-r):(b="top",l=i+u),{direction:b,x:_,y:l}};var B=(e,t,s,r,a,i,n,u)=>e<a+n&&e+s>a&&t<i+u&&t+r>i;var j=(e,t,s,r,a,i)=>(r-e)*(r-e)+(a-t)*(a-t)<=(s+i)*(s+i);var M=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,a=s?-1:this.length,i=s?-1:1;for(;r!==a;){let n=this.indexToPointX(r),u=this.indexToPointY(r),h=this._c[r];if(t(n,u,h,this)===!1)break;r+=i}}fill(t){this.forEach((s,r)=>{this.set(s,r,t)})}clampX(t){return q(t,0,this._w-1)}clampY(t){return q(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((a,i,n)=>{r[i]=r[i]||"",r[i]+=n+t}),r.join(`
|
|
2
|
+
`)}},C=class e extends M{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,a)=>{t.set(s,r,a)}),t}};function q(e,t,s){return e<t?t:e>s?s:e}var At=Math.sqrt,G=Math.cos,V=Math.sin,kt=2*Math.PI,w=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},g=e=>e instanceof w,o=(e=0,t=e)=>(g(e)&&(t=e.y,e=e.x),new w(e,t)),O=(e,t,s=t)=>g(t)?O(e,t.x,t.y):e.x===t&&e.y===s,W=(e,t,s=t)=>(g(t)?W(e,t.x,t.y):(e.x=t,e.y=s),e),S=(e,t,s=t)=>g(t)?S(e,t.x,t.y):(e.x+=t,e.y+=s,e),L=(e,t,s=t)=>g(t)?L(e,t.x,t.y):(e.x-=t,e.y-=s,e),N=(e,t,s=t)=>g(t)?N(e,t.x,t.y):(e.x*=t,e.y*=s,e),I=(e,t,s=t)=>g(t)?I(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),Pt=(e,t)=>{let s=G(t),r=V(t);return e.x=s*e.x-r*e.y,e.y=r*e.x+s*e.y,e},Ct=(e,t)=>{let s=D(o(t));return L(e,N(s,2*K(e,s)))},Ot=(e,t)=>(D(e),N(e,t),e),$=e=>Math.hypot(e.x,e.y),Z=e=>e.x*e.x+e.y*e.y,D=e=>{let t=$(e);return t>0&&I(e,t),e},St=(e,t=1)=>{let s=Z(e);return s>t*t&&(I(e,At(s)),N(e,t)),e},Lt=(e,t)=>Math.hypot(t.x-e.x,t.y-e.y),Dt=(e,t)=>{let s=e.x-t.x,r=e.y-t.y;return s*s+r*r},Ft=e=>Math.atan2(e.y,e.x),Rt=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),K=(e,t)=>e.x*t.x+e.y*t.y,Ht=(e,t)=>e.x*t.y-e.y*t.x,vt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),Yt=(e=1,t=e,s=globalThis.rand||Math.random)=>{let r=s()*kt,a=s()*(t-e)+e;return o(G(r)*a,V(r)*a)},zt=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),Xt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),Ut=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),Bt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),jt=(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),qt=(e,t,s=1)=>S(e,t.x*s,t.y*s),Gt=e=>O(e,Q),Q=o(0,0),Vt=o(1,1),Wt=o(0,-1),$t=o(1,0),Zt=o(0,1),Kt=o(-1,0);var Qt=o(.5,.5),J=o(0,0),Jt=o(1,0),te=o(0,1),ee=o(1,1),F=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,r=J){this.sprite=t,this.pos=s||o(0),this._o=o(r),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),r=this.sprite.height*(t?this._s.y:1),a=this.pos.x-s*this.anchor.x,i=this.pos.y-r*this.anchor.y;return[a,i,s,r]}draw(t=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&t.push(),this.transform(t),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,a=-this.sprite.width*(this.flipX?1-r.x:r.x),i=-this.sprite.height*(this.flipY?1-r.y:r.y);s&&t.alpha(this.opacity),t.image(a,i,this.sprite)}};var tt=(e,t)=>Math.abs(t-e)||0;var et=(e,t,s,r=Math.sin)=>e+(r(s)+1)/2*(t-e);var st=e=>e%1||0;var rt=advance=(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 it=(e,t,s,r)=>Math.hypot(s-e,r-t);var ot=(e,t)=>Math.hypot(e,t);var A=e=>{let t=0;for(let s=0;s<e.length;s++)t+=e[s];return t};var nt=e=>A(e)/e.length;var ut=(...e)=>{let t=e.sort((r,a)=>r-a),s=Math.floor(t.length/2);return t.length%2===0?(t[s-1]+t[s])/2:t[s]};var lt=(e,t,s)=>{let r=(t-e)%360;return r>180?r-=360:r<-180&&(r+=360),e+r*s};var k=Math.PI/2,se=(e,t,s,r=1,a=ht)=>new R(e,t,s,r,a),ht=e=>e,re=e=>e*e,ae=e=>-e*(e-2),ie=e=>e<.5?2*e*e:-2*e*e+4*e-1,oe=e=>e*e*e-e*Math.sin(e*Math.PI),ne=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},ue=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},le=e=>Math.sin(13*k*e)*Math.pow(2,10*(e-1)),he=e=>Math.sin(-13*k*(e+1))*Math.pow(2,-10*e)+1,me=e=>{if(e<.5){let r=Math.sin(13*k*(2*e)),a=Math.pow(2,10*(2*e-1));return .5*r*a}let t=Math.sin(-13*k*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},mt=e=>1-H(1-e),H=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,ce=e=>e<.5?.5*mt(e*2):.5*H(e*2-1)+.5,R=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,r,a,i){this._o=t,this._p=s,this._x=r,this._d=a,this._e=i,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",a=>{if(this._t<=this._w){this._t+=a;return}let i=this._t-this._w;this._o[this._p]=this._lc.lerp(s,r,this._e(i/this._d)),this._t+=a,i>=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 ct=4,P=1<<ct,pt=8,pe=1<<pt,f=4095,v=e=>.5*(1-Math.cos(e*Math.PI)),Y=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 a=Math.floor(t),i=Math.floor(s),n=Math.floor(r),u=t-a,h=s-i,d=r-n,m,x,b=0,_=.5,l,c,T;for(let X=0;X<this._po;X++){let p=a+(i<<ct)+(n<<pt);m=v(u),x=v(h),l=this._p[p&f],l+=m*(this._p[p+1&f]-l),c=this._p[p+P&f],c+=m*(this._p[p+P+1&f]-c),l+=x*(c-l),p+=pe,c=this._p[p&f],c+=m*(this._p[p+1&f]-c),T=this._p[p+P&f],T+=m*(this._p[p+P+1&f]-T),c+=x*(T-c),l+=v(d)*(c-l),b+=l*_,_*=this._pf,a<<=1,u*=2,i<<=1,h*=2,n<<=1,d*=2,u>=1&&(a++,u--),h>=1&&(i++,h--),d>=1&&(n++,d--)}return b}noiseDetail(t,s){t>0&&(this._po=t),s>0&&(this._pf=s)}noiseSeed(t=null){t!=null&&this._e.seed(t);let s=this._e.rand||Math.random;for(let r=0;r<f+1;r++)this._p[r]=s()}};var ft=(e,t=!0,s=!1,r=globalThis)=>r.paint(e.width,e.height,a=>{r.push(),r.scale(t?-1:1,s?-1:1),r.image(t?-e.width:0,s?-e.height:0,e),r.pop()});var bt=(e,t,s=!0,r=globalThis)=>r.paint(e.width*t,e.height*t,a=>{r.push(),a.imageSmoothingEnabled=!s,r.scale(t),r.image(0,0,e),r.pop()});var dt=(e,t,s=1,r=globalThis)=>r.paint(e.width,e.height,a=>{r.push(),r.alpha(s),r.rectfill(0,0,e.width,e.height,t),a.globalCompositeOperation="destination-atop",r.alpha(1),r.image(0,0,e),r.pop()});var xt=(e,t,{borderWidth:s=0,borderColor:r=0,engine:a=globalThis}={})=>{let i=e*2+s;return a.paint(i,i,()=>{a.circfill(i/2,i/2,e,t),s>0&&(a.linewidth(s),a.stroke(r))})};var gt=(e,t,s,{borderWidth:r=0,borderColor:a=0,engine:i=globalThis}={})=>{let n=e+r*2,u=t+r*2;return i.paint(n,u,()=>{i.rectfill(r>0?r:0,r>0?r:0,e,t,s),r>0&&(i.linewidth(r),i.stroke(a))})};var _t=(e,t=0,s=1)=>[...new Array(e).keys()].map(r=>t+s*r);var yt=(e,t=globalThis.rand||Math.random)=>{e=[...e];for(let s=e.length-1;s>0;s--){let r=Math.floor(t()*(s+1)),a=e[s];e[s]=e[r],e[r]=a}return e};var wt=(e,t=globalThis.rand||Math.random)=>e[Math.floor(t()*e.length)];var Nt=e=>e[0];var Tt=e=>e[e.length-1];var Et=e=>e.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},z);})();
|
|
3
3
|
/*! @litecanvas/utils by Luiz Bills | MIT Licensed */
|
package/dist/camera.js
CHANGED
|
@@ -30,11 +30,6 @@
|
|
|
30
30
|
rotation = 0;
|
|
31
31
|
/** @type {number} */
|
|
32
32
|
scale = 1;
|
|
33
|
-
_shake = {
|
|
34
|
-
x: 0,
|
|
35
|
-
y: 0,
|
|
36
|
-
removeListener: null
|
|
37
|
-
};
|
|
38
33
|
/**
|
|
39
34
|
* @param {LitecanvasInstance} engine
|
|
40
35
|
*/
|
|
@@ -48,6 +43,11 @@
|
|
|
48
43
|
);
|
|
49
44
|
this.x = this.width / 2;
|
|
50
45
|
this.y = this.height / 2;
|
|
46
|
+
this._shake = {
|
|
47
|
+
x: 0,
|
|
48
|
+
y: 0,
|
|
49
|
+
removeListener: null
|
|
50
|
+
};
|
|
51
51
|
}
|
|
52
52
|
resize(width, height) {
|
|
53
53
|
this.width = width;
|
|
@@ -149,31 +149,6 @@
|
|
|
149
149
|
getBounds() {
|
|
150
150
|
return [this.ox, this.oy, this.width, this.height];
|
|
151
151
|
}
|
|
152
|
-
/**
|
|
153
|
-
* Check if a rect is inside of the camera.
|
|
154
|
-
*
|
|
155
|
-
* @param {number} x
|
|
156
|
-
* @param {number} y
|
|
157
|
-
* @param {number} width
|
|
158
|
-
* @param {number} height
|
|
159
|
-
* @returns {boolean}
|
|
160
|
-
*/
|
|
161
|
-
viewing(x, y, width, height) {
|
|
162
|
-
const cameraX = this.width / 2 - this.x;
|
|
163
|
-
const cameraY = this.height / 2 - this.y;
|
|
164
|
-
const cameraWidth = this.width / this.scale;
|
|
165
|
-
const cameraHeight = this.height / this.scale;
|
|
166
|
-
return this._engine.colrect(
|
|
167
|
-
x,
|
|
168
|
-
y,
|
|
169
|
-
width,
|
|
170
|
-
height,
|
|
171
|
-
cameraX,
|
|
172
|
-
cameraY,
|
|
173
|
-
cameraWidth,
|
|
174
|
-
cameraHeight
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
152
|
/**
|
|
178
153
|
* Shake the camera
|
|
179
154
|
*
|
package/dist/camera.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let i in globalThis.utils)i!=="global"&&(o||globalThis[i]===void 0)&&(globalThis[i]=globalThis.utils[i])};var n=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;
|
|
1
|
+
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let i in globalThis.utils)i!=="global"&&(o||globalThis[i]===void 0)&&(globalThis[i]=globalThis.utils[i])};var n=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;constructor(i=null,t=0,s=0,h=null,e=null){this._engine=i||globalThis,this.ox=t,this.oy=s,this.resize(h||this._engine.WIDTH-t,e||this._engine.HEIGHT-s),this.x=this.width/2,this.y=this.height/2,this._shake={x:0,y:0,removeListener:null}}resize(i,t){this.width=i,this.height=t,this._engine.emit("camera-resized",this)}start(i=!1){if(this._engine.push(),i){let h=path();h.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(h)}let t=this.ox+this.width/2,s=this.oy+this.height/2;this._engine.translate(t,s),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(i,t){this.x=i,this.y=t}move(i,t){this.x+=i,this.y+=t}zoom(i){this.scale*=i}zoomTo(i){this.scale=i}rotate(i){this.rotation+=i}rotateTo(i){this.rotation=i}getWorldPoint(i,t,s={}){let h=Math.cos(-this.rotation),e=Math.sin(-this.rotation);return i=(i-this.width/2-this.ox)/this.scale,t=(t-this.height/2-this.oy)/this.scale,s.x=h*i-e*t+this.x,s.y=e*i+h*t+this.y,s}getCameraPoint(i,t,s={}){let h=Math.cos(-this.rotation),e=Math.sin(-this.rotation);return i=i-this.x,t=t-this.y,i=h*i-e*t,t=e*i+h*t,s.x=i*this.scale+this.width/2+this.ox,s.y=t*this.scale+this.height/2+this.oy,s}getBounds(){return[this.ox,this.oy,this.width,this.height]}shake(i=1,t=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",s=>{this._shake.x=this._engine.randi(-i,i),this._shake.y=this._engine.randi(-i,i),t-=s,t<=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}};globalThis.utils=Object.assign(globalThis.utils||{},{Camera:n});})();
|
package/dist/collection.js
CHANGED
|
@@ -28,15 +28,13 @@
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
// src/collection/range.js
|
|
31
|
-
var range_default = (size, from = 0, step = 1) => Array.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
);
|
|
31
|
+
var range_default = (size, from = 0, step = 1) => [...new Array(size).keys()].map((i) => {
|
|
32
|
+
return from + step * i;
|
|
33
|
+
});
|
|
36
34
|
|
|
37
35
|
// src/collection/shuffle.js
|
|
38
36
|
var shuffle_default = (values, rng = globalThis.rand || Math.random) => {
|
|
39
|
-
values =
|
|
37
|
+
values = [...values];
|
|
40
38
|
for (let i = values.length - 1; i > 0; i--) {
|
|
41
39
|
let j = Math.floor(rng() * (i + 1));
|
|
42
40
|
let temp = values[i];
|
package/dist/collection.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var g=Object.defineProperty;var u=(t,l)=>{for(var o in l)g(t,o,{get:l[o],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let l in globalThis.utils)l!=="global"&&(t||globalThis[l]===void 0)&&(globalThis[l]=globalThis.utils[l])};var
|
|
1
|
+
(()=>{var g=Object.defineProperty;var u=(t,l)=>{for(var o in l)g(t,o,{get:l[o],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let l in globalThis.utils)l!=="global"&&(t||globalThis[l]===void 0)&&(globalThis[l]=globalThis.utils[l])};var r={};u(r,{choose:()=>f,head:()=>s,last:()=>h,range:()=>a,shuffle:()=>i,tail:()=>n});var a=(t,l=0,o=1)=>[...new Array(t).keys()].map(e=>l+o*e);var i=(t,l=globalThis.rand||Math.random)=>{t=[...t];for(let o=t.length-1;o>0;o--){let e=Math.floor(l()*(o+1)),d=t[o];t[o]=t[e],t[e]=d}return t};var f=(t,l=globalThis.rand||Math.random)=>t[Math.floor(l()*t.length)];var s=t=>t[0];var h=t=>t[t.length-1];var n=t=>t.slice(1);globalThis.utils=Object.assign(globalThis.utils||{},r);})();
|
package/dist/collision.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
(() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
|
+
};
|
|
7
|
+
|
|
2
8
|
// src/_global.js
|
|
3
9
|
globalThis.utils = globalThis.utils || {};
|
|
4
10
|
globalThis.utils.global = (overrides = true) => {
|
|
@@ -10,6 +16,15 @@
|
|
|
10
16
|
}
|
|
11
17
|
};
|
|
12
18
|
|
|
19
|
+
// src/collision/index.js
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
colcirc: () => colcirc_default,
|
|
23
|
+
colrect: () => colrect_default,
|
|
24
|
+
intersection: () => intersection_default,
|
|
25
|
+
resolverect: () => resolverect_default
|
|
26
|
+
});
|
|
27
|
+
|
|
13
28
|
// src/collision/intersection.js
|
|
14
29
|
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
15
30
|
const left = Math.max(x1, x2);
|
|
@@ -19,8 +34,8 @@
|
|
|
19
34
|
return [left, top, width, height];
|
|
20
35
|
};
|
|
21
36
|
|
|
22
|
-
// src/collision/
|
|
23
|
-
var
|
|
37
|
+
// src/collision/resolverect.js
|
|
38
|
+
var resolverect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
24
39
|
const [left, top, width, height] = intersection_default(
|
|
25
40
|
x1,
|
|
26
41
|
y1,
|
|
@@ -54,9 +69,35 @@
|
|
|
54
69
|
return { direction, x: resolveX, y: resolveY };
|
|
55
70
|
};
|
|
56
71
|
|
|
72
|
+
// src/debug/assert.js
|
|
73
|
+
var assert_default = (condition, message = "Assertion failed") => {
|
|
74
|
+
if (!condition) throw new Error(message);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// src/collision/colrect.js
|
|
78
|
+
var colrect_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
79
|
+
DEV: assert_default(isFinite(x1), "colrect: 1st param must be a number");
|
|
80
|
+
DEV: assert_default(isFinite(y1), "colrect: 2nd param must be a number");
|
|
81
|
+
DEV: assert_default(isFinite(w1), "colrect: 3rd param must be a number");
|
|
82
|
+
DEV: assert_default(isFinite(h1), "colrect: 4th param must be a number");
|
|
83
|
+
DEV: assert_default(isFinite(x2), "colrect: 5th param must be a number");
|
|
84
|
+
DEV: assert_default(isFinite(y2), "colrect: 6th param must be a number");
|
|
85
|
+
DEV: assert_default(isFinite(w2), "colrect: 7th param must be a number");
|
|
86
|
+
DEV: assert_default(isFinite(h2), "colrect: 8th param must be a number");
|
|
87
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/collision/colcirc.js
|
|
91
|
+
var colcirc_default = (x1, y1, r1, x2, y2, r2) => {
|
|
92
|
+
DEV: assert_default(isFinite(x1), "colcirc: 1st param must be a number");
|
|
93
|
+
DEV: assert_default(isFinite(y1), "colcirc: 2nd param must be a number");
|
|
94
|
+
DEV: assert_default(isFinite(r1), "colcirc: 3rd param must be a number");
|
|
95
|
+
DEV: assert_default(isFinite(x2), "colcirc: 4th param must be a number");
|
|
96
|
+
DEV: assert_default(isFinite(y2), "colcirc: 5th param must be a number");
|
|
97
|
+
DEV: assert_default(isFinite(r2), "colcirc: 6th param must be a number");
|
|
98
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
|
|
99
|
+
};
|
|
100
|
+
|
|
57
101
|
// src/collision/_web.js
|
|
58
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
59
|
-
resolve: resolve_default,
|
|
60
|
-
intersection: intersection_default
|
|
61
|
-
});
|
|
102
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, index_exports);
|
|
62
103
|
})();
|
package/dist/collision.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=(
|
|
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),{direction: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/image.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var f=Object.defineProperty;var d=(
|
|
1
|
+
(()=>{var f=Object.defineProperty;var d=(t,a)=>{for(var r in a)f(t,r,{get:a[r],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(t=!0)=>{for(let a in globalThis.utils)a!=="global"&&(t||globalThis[a]===void 0)&&(globalThis[a]=globalThis.utils[a])};var n={};d(n,{flipImage:()=>o,makeCircle:()=>l,makeRectangle:()=>p,scaleImage:()=>u,tintImage:()=>m});var o=(t,a=!0,r=!1,e=globalThis)=>e.paint(t.width,t.height,s=>{e.push(),e.scale(a?-1:1,r?-1:1),e.image(a?-t.width:0,r?-t.height:0,t),e.pop()});var u=(t,a,r=!0,e=globalThis)=>e.paint(t.width*a,t.height*a,s=>{e.push(),s.imageSmoothingEnabled=!r,e.scale(a),e.image(0,0,t),e.pop()});var m=(t,a,r=1,e=globalThis)=>e.paint(t.width,t.height,s=>{e.push(),e.alpha(r),e.rectfill(0,0,t.width,t.height,a),s.globalCompositeOperation="destination-atop",e.alpha(1),e.image(0,0,t),e.pop()});var l=(t,a,{borderWidth:r=0,borderColor:e=0,engine:s=globalThis}={})=>{let i=t*2+r;return s.paint(i,i,()=>{s.circfill(i/2,i/2,t,a),r>0&&(s.linewidth(r),s.stroke(e))})};var p=(t,a,r,{borderWidth:e=0,borderColor:s=0,engine:i=globalThis}={})=>{let b=t+e*2,c=a+e*2;return i.paint(b,c,()=>{i.rectfill(e>0?e:0,e>0?e:0,t,a,r),e>0&&(i.linewidth(e),i.stroke(s))})};globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/math.js
CHANGED
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
lerpAngle: () => lerp_angle_default,
|
|
27
27
|
mag: () => mag_default,
|
|
28
28
|
mean: () => mean_default,
|
|
29
|
+
median: () => median_default,
|
|
29
30
|
mod: () => mod_default,
|
|
30
|
-
roundd: () => roundd_default,
|
|
31
31
|
sum: () => sum_default,
|
|
32
32
|
wave: () => wave_default
|
|
33
33
|
});
|
|
@@ -57,15 +57,6 @@
|
|
|
57
57
|
// src/math/mod.js
|
|
58
58
|
var mod_default = (a, b) => (b + a % b) % b;
|
|
59
59
|
|
|
60
|
-
// src/math/roundd.js
|
|
61
|
-
var roundd_default = (n, precision = 0) => {
|
|
62
|
-
if (!precision) {
|
|
63
|
-
return Math.round(n);
|
|
64
|
-
}
|
|
65
|
-
const multiplier = Math.pow(10, precision);
|
|
66
|
-
return Math.round(n * multiplier) / multiplier;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
60
|
// src/math/dist.js
|
|
70
61
|
var dist_default = (x1, y1, x2, y2) => {
|
|
71
62
|
return Math.hypot(x2 - x1, y2 - y1);
|
|
@@ -90,6 +81,16 @@
|
|
|
90
81
|
return sum_default(values) / values.length;
|
|
91
82
|
};
|
|
92
83
|
|
|
84
|
+
// src/math/median.js
|
|
85
|
+
var median_default = (...values) => {
|
|
86
|
+
const sorted = values.sort((a, b) => a - b);
|
|
87
|
+
const middle = Math.floor(sorted.length / 2);
|
|
88
|
+
if (sorted.length % 2 === 0) {
|
|
89
|
+
return (sorted[middle - 1] + sorted[middle]) / 2;
|
|
90
|
+
}
|
|
91
|
+
return sorted[middle];
|
|
92
|
+
};
|
|
93
|
+
|
|
93
94
|
// src/math/lerp-angle.js
|
|
94
95
|
var lerp_angle_default = (start, end, amount) => {
|
|
95
96
|
let dif = (end - start) % 360;
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var M=Object.defineProperty;var g=(e,t)=>{for(var r in t)M(e,r,{get:t[r],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 n={};g(n,{advance:()=>a,diff:()=>c,dist:()=>p,fract:()=>u,lerpAngle:()=>h,mag:()=>f,mean:()=>y,median:()=>i,mod:()=>l,sum:()=>s,wave:()=>x});var c=(e,t)=>Math.abs(t-e)||0;var x=(e,t,r,o=Math.sin)=>e+(o(r)+1)/2*(t-e);var u=e=>e%1||0;var A=2*Math.PI;var a=advance=(e,t,r,o=1)=>{r&&(t.x+=r.x*o,t.y+=r.y*o),e.x+=t.x*o,e.y+=t.y*o};var l=(e,t)=>(t+e%t)%t;var p=(e,t,r,o)=>Math.hypot(r-e,o-t);var f=(e,t)=>Math.hypot(e,t);var s=e=>{let t=0;for(let r=0;r<e.length;r++)t+=e[r];return t};var y=e=>s(e)/e.length;var i=(...e)=>{let t=e.sort((o,d)=>o-d),r=Math.floor(t.length/2);return t.length%2===0?(t[r-1]+t[r])/2:t[r]};var h=(e,t,r)=>{let o=(t-e)%360;return o>180?o-=360:o<-180&&(o+=360),e+o*r};globalThis.utils=Object.assign(globalThis.utils||{},n);})();
|
package/dist/noise.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var A=Object.defineProperty;var k=(o,e)=>{for(var t in e)A(o,t,{get:e[t],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=(o=!0)=>{for(let e in globalThis.utils)e!=="global"&&(o||globalThis[e]===void 0)&&(globalThis[e]=globalThis.utils[e])};var v={};k(v,{Noise:()=>y});var E=4,h=1<<E,T=8,D=1<<T,i=4095,N=o=>.5*(1-Math.cos(o*Math.PI)),y=class{_p=[];_po=4;_pf=.5;_e=null;constructor(e){this._e=e||globalThis,this.noiseSeed()}noise(e,t=0,r=0){e<0&&(e=-e),t<0&&(t=-t),r<0&&(r=-r);let u=Math.floor(e),l=Math.floor(t),p=Math.floor(r),b=e-u,c=t-l,f=r-p,m,g,x=0,w=.5,n,a,d;for(let _=0;_<this._po;_++){let s=u+(l<<E)+(p<<T);m=N(b),g=N(c),n=this._p[s&i],n+=m*(this._p[s+1&i]-n),a=this._p[s+h&i],a+=m*(this._p[s+h+1&i]-a),n+=g*(a-n),s+=D,a=this._p[s&i],a+=m*(this._p[s+1&i]-a),d=this._p[s+h&i],d+=m*(this._p[s+h+1&i]-d),a+=g*(d-a),n+=N(f)*(a-n),x+=n*w,w*=this._pf,u<<=1,b*=2,l<<=1,c*=2,p<<=1,f*=2,b>=1&&(u++,b--),c>=1&&(l++,c--),f>=1&&(p++,f--)}return x}noiseDetail(e,t){e>0&&(this._po=e),t>0&&(this._pf=t)}noiseSeed(e=null){e!=null&&this._e.seed(e);let t=this._e.rand||Math.random;for(let r=0;r<i+1;r++)this._p[r]=t()}};globalThis.utils=Object.assign(globalThis.utils||{},v);})();
|
package/dist/tween.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var c=Object.defineProperty;var b=(t
|
|
1
|
+
(()=>{var c=Object.defineProperty;var b=(e,t)=>{for(var r in t)c(e,r,{get:t[r],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 m={};b(m,{BACK_IN:()=>_,BACK_IN_OUT:()=>y,BACK_OUT:()=>N,BOUNCE_IN:()=>p,BOUNCE_IN_OUT:()=>E,BOUNCE_OUT:()=>u,EASE_IN:()=>f,EASE_IN_OUT:()=>g,EASE_OUT:()=>d,ELASTIC_IN:()=>x,ELASTIC_IN_OUT:()=>v,ELASTIC_OUT:()=>w,LINEAR:()=>l,tween:()=>h});var n=Math.PI/2,h=(e,t,r,s=1,a=l)=>new o(e,t,r,s,a),l=e=>e,f=e=>e*e,d=e=>-e*(e-2),g=e=>e<.5?2*e*e:-2*e*e+4*e-1,_=e=>e*e*e-e*Math.sin(e*Math.PI),N=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},y=e=>{if(e<.5){let r=2*e;return .5*(r*r*r-r*Math.sin(r*Math.PI))}let t=1-(2*e-1);return .5*(1-(t*t*t-t*Math.sin(e*Math.PI)))+.5},x=e=>Math.sin(13*n*e)*Math.pow(2,10*(e-1)),w=e=>Math.sin(-13*n*(e+1))*Math.pow(2,-10*e)+1,v=e=>{if(e<.5){let s=Math.sin(13*n*(2*e)),a=Math.pow(2,10*(2*e-1));return .5*s*a}let t=Math.sin(-13*n*(2*e-1+1)),r=Math.pow(2,-10*(2*e-1));return .5*(t*r+2)},p=e=>1-u(1-e),u=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,E=e=>e<.5?.5*p(e*2):.5*u(e*2-1)+.5,o=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,r,s,a,i){this._o=t,this._p=r,this._x=s,this._d=a,this._e=i,this._w=0}start(t){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let r=this._o[this._p]||0,s=this._rel?r+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",a=>{if(this._t<=this._w){this._t+=a;return}let i=this._t-this._w;this._o[this._p]=this._lc.lerp(r,s,this._e(i/this._d)),this._t+=a,i>=this._d&&(this._o[this._p]=s,this.stop())}),this}stop(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let r of this._cb)r(this._o);return this}restart(t=null,r=!1){return this.stop(r).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}};globalThis.utils=Object.assign(globalThis.utils||{},m);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@litecanvas/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.33.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.2",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
42
|
"prettier": "^3.5.3"
|
|
43
43
|
}
|
package/src/camera/index.js
CHANGED
|
@@ -23,12 +23,6 @@ export default class Camera {
|
|
|
23
23
|
/** @type {number} */
|
|
24
24
|
scale = 1
|
|
25
25
|
|
|
26
|
-
_shake = {
|
|
27
|
-
x: 0,
|
|
28
|
-
y: 0,
|
|
29
|
-
removeListener: null,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
26
|
/**
|
|
33
27
|
* @param {LitecanvasInstance} engine
|
|
34
28
|
*/
|
|
@@ -45,6 +39,12 @@ export default class Camera {
|
|
|
45
39
|
|
|
46
40
|
this.x = this.width / 2
|
|
47
41
|
this.y = this.height / 2
|
|
42
|
+
|
|
43
|
+
this._shake = {
|
|
44
|
+
x: 0,
|
|
45
|
+
y: 0,
|
|
46
|
+
removeListener: null,
|
|
47
|
+
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
resize(width, height) {
|
|
@@ -171,33 +171,6 @@ export default class Camera {
|
|
|
171
171
|
return [this.ox, this.oy, this.width, this.height]
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
/**
|
|
175
|
-
* Check if a rect is inside of the camera.
|
|
176
|
-
*
|
|
177
|
-
* @param {number} x
|
|
178
|
-
* @param {number} y
|
|
179
|
-
* @param {number} width
|
|
180
|
-
* @param {number} height
|
|
181
|
-
* @returns {boolean}
|
|
182
|
-
*/
|
|
183
|
-
viewing(x, y, width, height) {
|
|
184
|
-
const cameraX = this.width / 2 - this.x
|
|
185
|
-
const cameraY = this.height / 2 - this.y
|
|
186
|
-
const cameraWidth = this.width / this.scale
|
|
187
|
-
const cameraHeight = this.height / this.scale
|
|
188
|
-
|
|
189
|
-
return this._engine.colrect(
|
|
190
|
-
x,
|
|
191
|
-
y,
|
|
192
|
-
width,
|
|
193
|
-
height,
|
|
194
|
-
cameraX,
|
|
195
|
-
cameraY,
|
|
196
|
-
cameraWidth,
|
|
197
|
-
cameraHeight
|
|
198
|
-
)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
174
|
/**
|
|
202
175
|
* Shake the camera
|
|
203
176
|
*
|
package/src/collection/range.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an new array of shuffled values
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
4
|
+
* @param {T[]} values
|
|
5
5
|
* @param {()=>number} [rng]
|
|
6
|
-
* @returns {
|
|
6
|
+
* @returns {T[]} the shuffled array
|
|
7
7
|
*/
|
|
8
8
|
export default (values, rng = globalThis.rand || Math.random) => {
|
|
9
|
-
values =
|
|
9
|
+
values = [...values] // clone the values
|
|
10
10
|
for (let i = values.length - 1; i > 0; i--) {
|
|
11
11
|
let j = Math.floor(rng() * (i + 1))
|
|
12
12
|
let temp = values[i]
|
package/src/collision/README.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
**CDN**: https://unpkg.com/@litecanvas/utils/dist/collision.js
|
|
4
4
|
|
|
5
|
+
## colrect
|
|
6
|
+
|
|
7
|
+
Check a collision between two rectangles.
|
|
8
|
+
|
|
9
|
+
Syntax: `colrect(x1, y1, w1, h1, x2, y2, w2, h2): boolean`
|
|
10
|
+
|
|
11
|
+
## colcirc
|
|
12
|
+
|
|
13
|
+
Check a collision between two circles.
|
|
14
|
+
|
|
15
|
+
Syntax: `colcirc(x1, y1, r1, x2, y2, r2): boolean`
|
|
16
|
+
|
|
5
17
|
## intersection
|
|
6
18
|
|
|
7
19
|
Returns the resulting rectangle of the intersection between two rectangles.
|
|
@@ -10,7 +22,7 @@ Syntax: `intersection(x1, y1, w1, h1, x2, y2, w2, h2): number[]`
|
|
|
10
22
|
|
|
11
23
|
```js
|
|
12
24
|
import litecanvas from "litecanvas"
|
|
13
|
-
import { intersection } from "@litecanvas/utils"
|
|
25
|
+
import { intersection, colrect } from "@litecanvas/utils"
|
|
14
26
|
|
|
15
27
|
litecanvas({
|
|
16
28
|
loop: { init, draw },
|
|
@@ -36,15 +48,15 @@ function draw() {
|
|
|
36
48
|
}
|
|
37
49
|
```
|
|
38
50
|
|
|
39
|
-
##
|
|
51
|
+
## resolverect
|
|
40
52
|
|
|
41
|
-
Syntax: `
|
|
53
|
+
Syntax: `resolverect(x1, y1, w1, h1, x2, y2, w2, h2): { direction: string, x: number, y: number }`
|
|
42
54
|
|
|
43
55
|
> Note: possible values for `direction` is `"top"`, `"botton"`, `"left"`, `"right"` or `""` (empty string, if no collision).
|
|
44
56
|
|
|
45
57
|
```js
|
|
46
58
|
import litecanvas from "litecanvas"
|
|
47
|
-
import {
|
|
59
|
+
import { resolverect, colrect } from "@litecanvas/utils"
|
|
48
60
|
|
|
49
61
|
litecanvas({
|
|
50
62
|
loop: { init, draw },
|
|
@@ -57,7 +69,7 @@ function init() {
|
|
|
57
69
|
|
|
58
70
|
// check the collision
|
|
59
71
|
if (colrect(...rect1, ...rect2)) {
|
|
60
|
-
const { direction, x, y } =
|
|
72
|
+
const { direction, x, y } = resolverect(...rect1, ...rect2)
|
|
61
73
|
|
|
62
74
|
// which side of rect1 is colliding with rect2
|
|
63
75
|
console.log(direction) // outputs "bottom"
|
package/src/collision/_web.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import "../_global.js"
|
|
2
|
-
import
|
|
3
|
-
import intersection from "./intersection.js"
|
|
2
|
+
import * as collisionUtils from "./index.js"
|
|
4
3
|
|
|
5
|
-
globalThis.utils = Object.assign(globalThis.utils || {},
|
|
6
|
-
resolve,
|
|
7
|
-
intersection,
|
|
8
|
-
})
|
|
4
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, collisionUtils)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check a collision between two circles
|
|
5
|
+
*
|
|
6
|
+
* @param {number} x1 first circle position X
|
|
7
|
+
* @param {number} y1 first circle position Y
|
|
8
|
+
* @param {number} r1 first circle position radius
|
|
9
|
+
* @param {number} x2 second circle position X
|
|
10
|
+
* @param {number} y2 second circle position Y
|
|
11
|
+
* @param {number} r2 second circle position radius
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export default (x1, y1, r1, x2, y2, r2) => {
|
|
15
|
+
DEV: assert(isFinite(x1), "colcirc: 1st param must be a number")
|
|
16
|
+
DEV: assert(isFinite(y1), "colcirc: 2nd param must be a number")
|
|
17
|
+
DEV: assert(isFinite(r1), "colcirc: 3rd param must be a number")
|
|
18
|
+
DEV: assert(isFinite(x2), "colcirc: 4th param must be a number")
|
|
19
|
+
DEV: assert(isFinite(y2), "colcirc: 5th param must be a number")
|
|
20
|
+
DEV: assert(isFinite(r2), "colcirc: 6th param must be a number")
|
|
21
|
+
|
|
22
|
+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2)
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from "../debug/assert"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check a collision between two rectangles
|
|
5
|
+
*
|
|
6
|
+
* @param {number} x1 first rectangle position X
|
|
7
|
+
* @param {number} y1 first rectangle position Y
|
|
8
|
+
* @param {number} w1 first rectangle width
|
|
9
|
+
* @param {number} h1 first rectangle height
|
|
10
|
+
* @param {number} x2 second rectangle position X
|
|
11
|
+
* @param {number} y2 second rectangle position Y
|
|
12
|
+
* @param {number} w2 second rectangle width
|
|
13
|
+
* @param {number} h2 second rectangle height
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
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), "colrect: 5th param must be a number")
|
|
22
|
+
DEV: assert(isFinite(y2), "colrect: 6th param must be a number")
|
|
23
|
+
DEV: assert(isFinite(w2), "colrect: 7th param must be a number")
|
|
24
|
+
DEV: assert(isFinite(h2), "colrect: 8th param must be a number")
|
|
25
|
+
|
|
26
|
+
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
|
|
27
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns the resulting rectangle of the intersection between two rectangles.
|
|
3
|
-
* Note: this function should be called with or after `
|
|
3
|
+
* Note: this function should be called with or after `colrect`.
|
|
4
4
|
*
|
|
5
5
|
* @param {number} x1
|
|
6
6
|
* @param {number} y1
|
|
@@ -2,7 +2,7 @@ import intersection from "./intersection"
|
|
|
2
2
|
/**
|
|
3
3
|
* Determines the direction of the collision between two rectangles and
|
|
4
4
|
* the new position of the first rectangle to be adjusted.
|
|
5
|
-
* Note: this function should be called with or after `
|
|
5
|
+
* Note: this function should be called with or after `colrect`.
|
|
6
6
|
*
|
|
7
7
|
* @param {number} x1
|
|
8
8
|
* @param {number} y1
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { default as resolve } from "./collision/resolve.js"
|
|
2
|
-
export { default as intersection } from "./collision/intersection.js"
|
|
3
1
|
export { default as Camera } from "./camera/index.js"
|
|
2
|
+
export * from "./collision/index.js"
|
|
4
3
|
export * from "./grid/index.js"
|
|
5
4
|
export * from "./vector/index.js"
|
|
6
5
|
export * from "./actor/index.js"
|
package/src/math/index.js
CHANGED
|
@@ -3,9 +3,9 @@ export { default as wave } from "./wave.js"
|
|
|
3
3
|
export { default as fract } from "./fract.js"
|
|
4
4
|
export { default as advance } from "./advance.js"
|
|
5
5
|
export { default as mod } from "./mod.js"
|
|
6
|
-
export { default as roundd } from "./roundd.js"
|
|
7
6
|
export { default as dist } from "./dist.js"
|
|
8
7
|
export { default as mag } from "./mag.js"
|
|
9
8
|
export { default as mean } from "./mean.js"
|
|
9
|
+
export { default as median } from "./median.js"
|
|
10
10
|
export { default as sum } from "./sum.js"
|
|
11
11
|
export { default as lerpAngle } from "./lerp-angle.js"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute the median of the values. The values are sorted and the middle value is returned.
|
|
3
|
+
* In case of an even number of values, the average of the two middle values is returned
|
|
4
|
+
*
|
|
5
|
+
* @param {number[]} values
|
|
6
|
+
* @returns {number}
|
|
7
|
+
*/
|
|
8
|
+
export default (...values) => {
|
|
9
|
+
const sorted = values.sort((a, b) => a - b)
|
|
10
|
+
const middle = Math.floor(sorted.length / 2)
|
|
11
|
+
|
|
12
|
+
if (sorted.length % 2 === 0) {
|
|
13
|
+
return (sorted[middle - 1] + sorted[middle]) / 2
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return sorted[middle]
|
|
17
|
+
}
|
package/src/math/roundd.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calculates the integer closest to a number and optional precision.
|
|
3
|
-
*
|
|
4
|
-
* @param {number} n number to round.
|
|
5
|
-
* @param {number} [precision] number of decimal digits to round to, default is 0.
|
|
6
|
-
* @returns {number} rounded number.
|
|
7
|
-
*/
|
|
8
|
-
export default (n, precision = 0) => {
|
|
9
|
-
if (!precision) {
|
|
10
|
-
return Math.round(n)
|
|
11
|
-
}
|
|
12
|
-
const multiplier = Math.pow(10, precision)
|
|
13
|
-
return Math.round(n * multiplier) / multiplier
|
|
14
|
-
}
|