-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollider.js
95 lines (85 loc) · 2.46 KB
/
Collider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import Component from './Component';
import Transform from './Transform';
import Vector from '../Vector';
import { minBy } from '../util';
class Collider extends Component {
constructor(layer, size, bottom = false, oneDirection = false) {
super();
this.layer = layer;
this.size = size;
this.bottom = bottom;
this.oneDirection = oneDirection;
if (Collider.all[layer] === undefined) Collider.all[layer] = new Set();
Collider.all[layer].add(this);
}
start() {
this.transform = this.requireComponent(Transform);
}
checkAllCollisions(layers, oneDirection = false) {
const colliders = Collider.getColliders(layers);
for (let i = 0; i < colliders.length; i++) {
const collider = colliders[i];
if (collider.gameObject === this.gameObject
|| (collider.oneDirection && !oneDirection)) continue;
const depth = this.checkCollision(collider);
if (depth) return depth;
}
return null;
}
checkCollision(collider) {
const rect1 = this.rect;
const rect2 = collider.rect;
if (
rect1.x1 < rect2.x2 &&
rect1.x2 > rect2.x1 &&
rect1.y1 < rect2.y2 &&
rect1.y2 > rect2.y1
) {
const depth = Vector.zero;
depth.x = minBy(
[rect1.x2 - rect2.x1, rect1.x1 - rect2.x2],
el => Math.abs(el)
);
depth.y = minBy(
[rect1.y2 - rect2.y1, rect1.y1 - rect2.y2],
el => Math.abs(el)
);
if (collider.oneDirection) {
if (depth.y > 0 && depth.y < this.size.y / 4) {
return { collider, depth: new Vector(0, depth.y) };
}
} else {
return { collider, depth };
}
}
return null;
}
get rect() {
let y1Offset = this.size.y;
let y2Offset = 0;
if (!this.bottom) y1Offset /= 2;
if (!this.bottom) y2Offset = this.size.y / 2;
return {
x1: this.transform.position.x - this.size.x / 2,
y1: this.transform.position.y - y1Offset,
x2: this.transform.position.x + this.size.x / 2,
y2: this.transform.position.y + y2Offset
};
}
onDestroy() {
Collider.all[this.layer].delete(this);
}
static getColliders(layers) {
let colliders = [];
layers.forEach(layer => {
if (Collider.all[layer] === undefined) return;
Collider.all[layer].forEach(collider => {
if (collider.isStarted()) colliders.push(collider);
});
});
return colliders;
}
}
Collider.all = {};
window.Collider = Collider;
export default Collider;