@litecanvas/utils 0.2.0 → 0.3.2
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/README.md +2 -1
- package/dist/all.js +24 -20
- package/dist/all.min.js +1 -1
- package/dist/camera.js +20 -16
- package/dist/camera.min.js +1 -1
- package/dist/collision.js +51 -0
- package/dist/collision.min.js +1 -0
- package/dist/math.js +1 -47
- package/dist/math.min.js +1 -1
- package/package.json +1 -1
- package/src/camera/index.js +26 -19
- package/src/collision/README.md +75 -0
- package/src/collision/_web.js +7 -0
- package/src/{math → collision}/resolve.js +3 -3
- package/src/index.js +2 -2
- package/src/math/_web.js +0 -4
- /package/src/{math → collision}/intersection.js +0 -0
package/README.md
CHANGED
|
@@ -6,7 +6,8 @@ Small collection of tools for developing games with [Litecanvas](https://github.
|
|
|
6
6
|
|
|
7
7
|
- **Camera**: Move-, zoom- and rotatable camera with shake. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/camera)
|
|
8
8
|
- **Vector**: Modular 2D vector. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/vector)
|
|
9
|
-
-
|
|
9
|
+
- **Collision** utilities. [Usage & Docs](https://github.com/litecanvas/utils/tree/main/src/collision)
|
|
10
|
+
- And [some math utilities](https://github.com/litecanvas/utils/tree/main/src/math)
|
|
10
11
|
|
|
11
12
|
## Install
|
|
12
13
|
|
package/dist/all.js
CHANGED
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
// src/math/wave.js
|
|
55
55
|
var wave_default = (lower, higher, t, fn = Math.sin) => lower + (fn(t) + 1) / 2 * (higher - lower);
|
|
56
56
|
|
|
57
|
-
// src/
|
|
57
|
+
// src/collision/intersection.js
|
|
58
58
|
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
59
59
|
const left = Math.max(x1, x2);
|
|
60
60
|
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
return [left, top, width, height];
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
// src/
|
|
66
|
+
// src/collision/resolve.js
|
|
67
67
|
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
68
68
|
const [left, top, width, height] = intersection_default(
|
|
69
69
|
x1,
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
w2,
|
|
76
76
|
h2
|
|
77
77
|
);
|
|
78
|
-
let direction =
|
|
78
|
+
let direction = "";
|
|
79
79
|
let resolveX = x1;
|
|
80
80
|
let resolveY = y1;
|
|
81
81
|
if (width < height) {
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
resolveY = y2 + h2;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
return
|
|
98
|
+
return { direction, x: resolveX, y: resolveY };
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
// src/camera/index.js
|
|
@@ -126,6 +126,26 @@
|
|
|
126
126
|
engine = engine || globalThis;
|
|
127
127
|
this._engine = engine;
|
|
128
128
|
this.size(engine.WIDTH || 0, engine.HEIGHT || 0);
|
|
129
|
+
this.x = this.width / 2;
|
|
130
|
+
this.y = this.height / 2;
|
|
131
|
+
}
|
|
132
|
+
size(width, height) {
|
|
133
|
+
this.width = width;
|
|
134
|
+
this.height = height;
|
|
135
|
+
}
|
|
136
|
+
start(clip = false) {
|
|
137
|
+
const centerX = this.width / 2, centerY = this.height / 2;
|
|
138
|
+
this._engine.push();
|
|
139
|
+
this._engine.translate(centerX, centerY);
|
|
140
|
+
this._engine.scale(this.scale);
|
|
141
|
+
this._engine.rotate(this.rotation);
|
|
142
|
+
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y);
|
|
143
|
+
if (clip) {
|
|
144
|
+
this._engine.cliprect(this.x, this.y, this.width, this.height);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
end() {
|
|
148
|
+
this._engine.pop();
|
|
129
149
|
}
|
|
130
150
|
/**
|
|
131
151
|
* @param {number} x
|
|
@@ -176,22 +196,6 @@
|
|
|
176
196
|
shaking() {
|
|
177
197
|
return this._shake.removeListener !== null;
|
|
178
198
|
}
|
|
179
|
-
size(width, height) {
|
|
180
|
-
this.width = width;
|
|
181
|
-
this.height = height;
|
|
182
|
-
}
|
|
183
|
-
start(clip = false) {
|
|
184
|
-
this._engine.push();
|
|
185
|
-
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y);
|
|
186
|
-
this._engine.scale(this.scale);
|
|
187
|
-
this._engine.rotate(this.rotation);
|
|
188
|
-
if (clip) {
|
|
189
|
-
this._engine.cliprect(this.x, this.y, this.width, this.height);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
end() {
|
|
193
|
-
this._engine.pop();
|
|
194
|
-
}
|
|
195
199
|
};
|
|
196
200
|
|
|
197
201
|
// src/vector/index.js
|
package/dist/all.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var N=Object.defineProperty;var P=(e,t)=>{for(var s in t)N(e,s,{get:t[s],enumerable:!0})};var T={};P(T,{Camera:()=>a,DOWN:()=>K,LEFT:()=>Q,ONE:()=>B,RIGHT:()=>J,UP:()=>C,Vector:()=>l,ZERO:()=>j,diff:()=>
|
|
1
|
+
(()=>{var N=Object.defineProperty;var P=(e,t)=>{for(var s in t)N(e,s,{get:t[s],enumerable:!0})};var T={};P(T,{Camera:()=>a,DOWN:()=>K,LEFT:()=>Q,ONE:()=>B,RIGHT:()=>J,UP:()=>C,Vector:()=>l,ZERO:()=>j,diff:()=>L,fract:()=>q,intersection:()=>y,isvector:()=>h,resolve:()=>z,vec:()=>i,vecadd:()=>I,vecconfig:()=>k,veccopy:()=>R,veccross:()=>S,vecdir:()=>A,vecdist:()=>$,vecdist2:()=>b,vecdiv:()=>d,vecdot:()=>F,veceq:()=>E,veclerp:()=>U,veclimit:()=>Y,vecmag:()=>D,vecmag2:()=>G,vecmult:()=>M,vecnorm:()=>X,vecrand:()=>Z,vecrot:()=>W,vecset:()=>H,vecsub:()=>O,wave:()=>v});var L=(e,t)=>Math.abs(t-e)||0;var q=e=>e%1||0;var v=(e,t,s,o=Math.sin)=>e+(o(s)+1)/2*(t-e);var y=(e,t,s,o,r,n,x,p)=>{let u=Math.max(e,r),w=Math.min(e+s,r+x)-u,f=Math.max(t,n),g=Math.min(t+o,n+p)-f;return[u,f,w,g]};var z=(e,t,s,o,r,n,x,p)=>{let[u,w,f,g]=y(e,t,s,o,r,n,x,p),c="",m=e,_=t;return f<g?e<r?(c="right",m=r-s):(c="left",m=r+x):t<n?(c="bottom",_=n-o):(c="top",_=n+p),{direction:c,x:m,y:_}};var a=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.size(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,o=this.height/2;this._engine.push(),this._engine.translate(s,o),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}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}shake(t=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",o=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),t-=o,t<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};var l=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},i=(e=0,t=e)=>new l(e,t),E=(e,t,s=t)=>h(t)?E(e,t.x,t.y):e.x===t&&e.y===(s||t),R=e=>i(e.x,e.y),H=(e,t,s=t)=>{h(t)?H(e,t.x,t.y):(e.x=t,e.y=s)},I=(e,t,s=t)=>{h(t)?I(e,t.x,t.y):(e.x+=t,e.y+=s)},O=(e,t,s=t)=>{h(t)?O(e,t.x,t.y):(e.x-=t,e.y-=s)},M=(e,t,s=t)=>{h(t)?M(e,t.x,t.y):(e.x*=t,e.y*=s)},d=(e,t,s=t)=>{h(t)?d(e,t.x,t.y):(e.x/=t,e.y/=s)},W=(e,t)=>{let s=Math.cos(t),o=Math.sin(t);e.x=s*e.x-o*e.y,e.y=o*e.x+s*e.y},D=e=>Math.sqrt(e.x*e.x+e.y*e.y),G=e=>e.x*e.x+e.y*e.y,X=e=>{let t=D(e);t>0&&d(e,t)},Y=(e,t)=>{let s=G(e);s>t*t&&(d(e,Math.sqrt(s)),M(e,t))},$=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return Math.sqrt(s*s+o*o)},b=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return s*s+o*o},A=e=>Math.atan2(e.y,e.x),F=(e,t)=>e.x*t.x+e.y*t.y,S=(e,t)=>e.x*t.y-e.y*t.x,U=(e,t,s)=>{e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0},Z=(e=1,t=e)=>{let s=k.random()*2*Math.PI,o=k.random()*(t-e)+e;return i(Math.cos(s)*o,Math.sin(s)*o)},h=e=>e instanceof l,k={random:()=>globalThis.rand?rand():Math.random()},j=i(0,0),B=i(1,1),C=i(0,-1),J=i(1,0),K=i(0,1),Q=i(-1,0);globalThis.utils=T;})();
|
package/dist/camera.js
CHANGED
|
@@ -27,6 +27,26 @@
|
|
|
27
27
|
engine = engine || globalThis;
|
|
28
28
|
this._engine = engine;
|
|
29
29
|
this.size(engine.WIDTH || 0, engine.HEIGHT || 0);
|
|
30
|
+
this.x = this.width / 2;
|
|
31
|
+
this.y = this.height / 2;
|
|
32
|
+
}
|
|
33
|
+
size(width, height) {
|
|
34
|
+
this.width = width;
|
|
35
|
+
this.height = height;
|
|
36
|
+
}
|
|
37
|
+
start(clip = false) {
|
|
38
|
+
const centerX = this.width / 2, centerY = this.height / 2;
|
|
39
|
+
this._engine.push();
|
|
40
|
+
this._engine.translate(centerX, centerY);
|
|
41
|
+
this._engine.scale(this.scale);
|
|
42
|
+
this._engine.rotate(this.rotation);
|
|
43
|
+
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y);
|
|
44
|
+
if (clip) {
|
|
45
|
+
this._engine.cliprect(this.x, this.y, this.width, this.height);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
end() {
|
|
49
|
+
this._engine.pop();
|
|
30
50
|
}
|
|
31
51
|
/**
|
|
32
52
|
* @param {number} x
|
|
@@ -77,22 +97,6 @@
|
|
|
77
97
|
shaking() {
|
|
78
98
|
return this._shake.removeListener !== null;
|
|
79
99
|
}
|
|
80
|
-
size(width, height) {
|
|
81
|
-
this.width = width;
|
|
82
|
-
this.height = height;
|
|
83
|
-
}
|
|
84
|
-
start(clip = false) {
|
|
85
|
-
this._engine.push();
|
|
86
|
-
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y);
|
|
87
|
-
this._engine.scale(this.scale);
|
|
88
|
-
this._engine.rotate(this.rotation);
|
|
89
|
-
if (clip) {
|
|
90
|
-
this._engine.cliprect(this.x, this.y, this.width, this.height);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
end() {
|
|
94
|
-
this._engine.pop();
|
|
95
|
-
}
|
|
96
100
|
};
|
|
97
101
|
|
|
98
102
|
// src/camera/_web.js
|
package/dist/camera.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var i=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(
|
|
1
|
+
(()=>{var i=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.size(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}size(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,h=this.height/2;this._engine.push(),this._engine.translate(s,h),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}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}shake(t=.3,s=1){this.shaking()||(this._shake.removeListener=this._engine.listen("update",h=>{this._shake.x=this._engine.randi(-s,s),this._shake.y=this._engine.randi(-s,s),t-=h,t<=0&&this.unshake()}))}unshake(){this.shaking()&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}shaking(){return this._shake.removeListener!==null}};globalThis.utils=Object.assign(globalThis.utils||{},{Camera:i});})();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
// src/collision/intersection.js
|
|
3
|
+
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
4
|
+
const left = Math.max(x1, x2);
|
|
5
|
+
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
6
|
+
const top = Math.max(y1, y2);
|
|
7
|
+
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
8
|
+
return [left, top, width, height];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/collision/resolve.js
|
|
12
|
+
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
13
|
+
const [left, top, width, height] = intersection_default(
|
|
14
|
+
x1,
|
|
15
|
+
y1,
|
|
16
|
+
w1,
|
|
17
|
+
h1,
|
|
18
|
+
x2,
|
|
19
|
+
y2,
|
|
20
|
+
w2,
|
|
21
|
+
h2
|
|
22
|
+
);
|
|
23
|
+
let direction = "";
|
|
24
|
+
let resolveX = x1;
|
|
25
|
+
let resolveY = y1;
|
|
26
|
+
if (width < height) {
|
|
27
|
+
if (x1 < x2) {
|
|
28
|
+
direction = "right";
|
|
29
|
+
resolveX = x2 - w1;
|
|
30
|
+
} else {
|
|
31
|
+
direction = "left";
|
|
32
|
+
resolveX = x2 + w2;
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (y1 < y2) {
|
|
36
|
+
direction = "bottom";
|
|
37
|
+
resolveY = y2 - h1;
|
|
38
|
+
} else {
|
|
39
|
+
direction = "top";
|
|
40
|
+
resolveY = y2 + h2;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { direction, x: resolveX, y: resolveY };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/collision/_web.js
|
|
47
|
+
globalThis.utils = Object.assign(globalThis.utils || {}, {
|
|
48
|
+
resolve: resolve_default,
|
|
49
|
+
intersection: intersection_default
|
|
50
|
+
});
|
|
51
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{var h=(o,i,r,s,t,e,n,a)=>{let m=Math.max(o,t),u=Math.min(o+r,t+n)-m,f=Math.max(i,e),c=Math.min(i+s,e+a)-f;return[m,f,u,c]};var d=(o,i,r,s,t,e,n,a)=>{let[m,u,f,c]=h(o,i,r,s,t,e,n,a),l="",p=o,g=i;return f<c?o<t?(l="right",p=t-r):(l="left",p=t+n):i<e?(l="bottom",g=e-s):(l="top",g=e+a),{direction:l,x:p,y:g}};globalThis.utils=Object.assign(globalThis.utils||{},{resolve:d,intersection:h});})();
|
package/dist/math.js
CHANGED
|
@@ -8,56 +8,10 @@
|
|
|
8
8
|
// src/math/fract.js
|
|
9
9
|
var fract_default = (value) => value % 1 || 0;
|
|
10
10
|
|
|
11
|
-
// src/math/intersection.js
|
|
12
|
-
var intersection_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
13
|
-
const left = Math.max(x1, x2);
|
|
14
|
-
const width = Math.min(x1 + w1, x2 + w2) - left;
|
|
15
|
-
const top = Math.max(y1, y2);
|
|
16
|
-
const height = Math.min(y1 + h1, y2 + h2) - top;
|
|
17
|
-
return [left, top, width, height];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// src/math/resolve.js
|
|
21
|
-
var resolve_default = (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
22
|
-
const [left, top, width, height] = intersection_default(
|
|
23
|
-
x1,
|
|
24
|
-
y1,
|
|
25
|
-
w1,
|
|
26
|
-
h1,
|
|
27
|
-
x2,
|
|
28
|
-
y2,
|
|
29
|
-
w2,
|
|
30
|
-
h2
|
|
31
|
-
);
|
|
32
|
-
let direction = false;
|
|
33
|
-
let resolveX = x1;
|
|
34
|
-
let resolveY = y1;
|
|
35
|
-
if (width < height) {
|
|
36
|
-
if (x1 < x2) {
|
|
37
|
-
direction = "right";
|
|
38
|
-
resolveX = x2 - w1;
|
|
39
|
-
} else {
|
|
40
|
-
direction = "left";
|
|
41
|
-
resolveX = x2 + w2;
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
if (y1 < y2) {
|
|
45
|
-
direction = "bottom";
|
|
46
|
-
resolveY = y2 - h1;
|
|
47
|
-
} else {
|
|
48
|
-
direction = "top";
|
|
49
|
-
resolveY = y2 + h2;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return [direction, resolveX, resolveY];
|
|
53
|
-
};
|
|
54
|
-
|
|
55
11
|
// src/math/_web.js
|
|
56
12
|
globalThis.utils = Object.assign(globalThis.utils || {}, {
|
|
57
13
|
diff: diff_default,
|
|
58
14
|
wave: wave_default,
|
|
59
|
-
fract: fract_default
|
|
60
|
-
resolve: resolve_default,
|
|
61
|
-
intersection: intersection_default
|
|
15
|
+
fract: fract_default
|
|
62
16
|
});
|
|
63
17
|
})();
|
package/dist/math.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var i=(t,a)=>Math.abs(a-t)||0;var o=(t,a,r,l=Math.sin)=>t+(l(r)+1)/2*(a-t);var f=t=>t%1||0;globalThis.utils=Object.assign(globalThis.utils||{},{diff:i,wave:o,fract:f});})();
|
package/package.json
CHANGED
package/src/camera/index.js
CHANGED
|
@@ -29,6 +29,32 @@ export default class Camera {
|
|
|
29
29
|
engine = engine || globalThis
|
|
30
30
|
this._engine = engine
|
|
31
31
|
this.size(engine.WIDTH || 0, engine.HEIGHT || 0)
|
|
32
|
+
this.x = this.width / 2
|
|
33
|
+
this.y = this.height / 2
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
size(width, height) {
|
|
37
|
+
this.width = width
|
|
38
|
+
this.height = height
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
start(clip = false) {
|
|
42
|
+
const centerX = this.width / 2,
|
|
43
|
+
centerY = this.height / 2
|
|
44
|
+
|
|
45
|
+
this._engine.push()
|
|
46
|
+
this._engine.translate(centerX, centerY)
|
|
47
|
+
this._engine.scale(this.scale)
|
|
48
|
+
this._engine.rotate(this.rotation)
|
|
49
|
+
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y)
|
|
50
|
+
|
|
51
|
+
if (clip) {
|
|
52
|
+
this._engine.cliprect(this.x, this.y, this.width, this.height)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
end() {
|
|
57
|
+
this._engine.pop()
|
|
32
58
|
}
|
|
33
59
|
|
|
34
60
|
/**
|
|
@@ -89,23 +115,4 @@ export default class Camera {
|
|
|
89
115
|
shaking() {
|
|
90
116
|
return this._shake.removeListener !== null
|
|
91
117
|
}
|
|
92
|
-
|
|
93
|
-
size(width, height) {
|
|
94
|
-
this.width = width
|
|
95
|
-
this.height = height
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
start(clip = false) {
|
|
99
|
-
this._engine.push()
|
|
100
|
-
this._engine.translate(-this.x + this._shake.x, -this.y + this._shake.y)
|
|
101
|
-
this._engine.scale(this.scale)
|
|
102
|
-
this._engine.rotate(this.rotation)
|
|
103
|
-
if (clip) {
|
|
104
|
-
this._engine.cliprect(this.x, this.y, this.width, this.height)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
end() {
|
|
109
|
-
this._engine.pop()
|
|
110
|
-
}
|
|
111
118
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Collision utilities
|
|
2
|
+
|
|
3
|
+
**CDN**: https://unpkg.com/@litecanvas/utils/dist/collision.js
|
|
4
|
+
|
|
5
|
+
## intersection
|
|
6
|
+
|
|
7
|
+
Returns the resulting rectangle of the intersection between two rectangles.
|
|
8
|
+
|
|
9
|
+
Syntax: `intersection(x1, y1, w1, h1, x2, y2, w2, h2): number[]`
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import litecanvas from "litecanvas"
|
|
13
|
+
import { intersection } from "@litecanvas/utils"
|
|
14
|
+
|
|
15
|
+
litecanvas({
|
|
16
|
+
loop: { init, draw },
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function init() {
|
|
20
|
+
// rect = [x, y, width, height]
|
|
21
|
+
rect1 = [0, 0, 50, 50]
|
|
22
|
+
rect2 = [25, 25, 80, 80]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function draw() {
|
|
26
|
+
cls(0)
|
|
27
|
+
rectfill(...rect1, 4) // draw the red rectangle
|
|
28
|
+
rectfill(...rect2, 6) // draw the blue rectangle
|
|
29
|
+
|
|
30
|
+
// check the collision
|
|
31
|
+
if (colrect(...rect1, ...rect2)) {
|
|
32
|
+
// intersection() returns an array with 4 numbers (x, y, width, height)
|
|
33
|
+
const rect3 = intersection(...rect1, ...rect2)
|
|
34
|
+
rectfill(...rect3, 5) // draw the yellow rectangle
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## resolve
|
|
40
|
+
|
|
41
|
+
Syntax: `resolve(x1, y1, w1, h1, x2, y2, w2, h2): { direction: string, x: number, y: number }`
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import litecanvas from "litecanvas"
|
|
45
|
+
import { resolve } from "@litecanvas/utils"
|
|
46
|
+
|
|
47
|
+
litecanvas({
|
|
48
|
+
loop: { init, draw },
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
function init() {
|
|
52
|
+
// rect = [x, y, width, height]
|
|
53
|
+
rect1 = [300, 170, 50, 50]
|
|
54
|
+
rect2 = [300, 200, 80, 80]
|
|
55
|
+
|
|
56
|
+
// check the collision
|
|
57
|
+
if (colrect(...rect1, ...rect2)) {
|
|
58
|
+
const { direction, x, y } = resolve(...rect1, ...rect2)
|
|
59
|
+
|
|
60
|
+
// which side of rect1 is colliding with rect2
|
|
61
|
+
console.log(direction) // outputs "bottom"
|
|
62
|
+
|
|
63
|
+
// fixes the position of rect1 to
|
|
64
|
+
// no longer collides with rect2
|
|
65
|
+
rect1[0] = x
|
|
66
|
+
rect1[1] = y
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function draw() {
|
|
71
|
+
cls(0)
|
|
72
|
+
rectfill(...rect2, 6) // blue rectangle
|
|
73
|
+
rectfill(...rect1, 4) // red rectangle
|
|
74
|
+
}
|
|
75
|
+
```
|
|
@@ -12,7 +12,7 @@ import intersection from "./intersection"
|
|
|
12
12
|
* @param {number} y2
|
|
13
13
|
* @param {number} w2
|
|
14
14
|
* @param {number} h2
|
|
15
|
-
* @returns {{
|
|
15
|
+
* @returns {{direction: 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
|
|
@@ -27,7 +27,7 @@ export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
|
27
27
|
h2,
|
|
28
28
|
)
|
|
29
29
|
|
|
30
|
-
let direction =
|
|
30
|
+
let direction = ""
|
|
31
31
|
let resolveX = x1
|
|
32
32
|
let resolveY = y1
|
|
33
33
|
|
|
@@ -49,5 +49,5 @@ export default (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return
|
|
52
|
+
return { direction, x: resolveX, y: resolveY }
|
|
53
53
|
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { default as diff } from "./math/diff.js"
|
|
2
2
|
export { default as fract } from "./math/fract.js"
|
|
3
3
|
export { default as wave } from "./math/wave.js"
|
|
4
|
-
export { default as resolve } from "./
|
|
5
|
-
export { default as intersection } from "./
|
|
4
|
+
export { default as resolve } from "./collision/resolve.js"
|
|
5
|
+
export { default as intersection } from "./collision/intersection.js"
|
|
6
6
|
export { default as Camera } from "./camera/index.js"
|
|
7
7
|
export * from "./vector/index.js"
|
package/src/math/_web.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import diff from "./diff.js"
|
|
2
2
|
import wave from "./wave.js"
|
|
3
3
|
import fract from "./fract.js"
|
|
4
|
-
import resolve from "./resolve.js"
|
|
5
|
-
import intersection from "./intersection.js"
|
|
6
4
|
|
|
7
5
|
globalThis.utils = Object.assign(globalThis.utils || {}, {
|
|
8
6
|
diff,
|
|
9
7
|
wave,
|
|
10
8
|
fract,
|
|
11
|
-
resolve,
|
|
12
|
-
intersection,
|
|
13
9
|
})
|
|
File without changes
|