forked from Code-Bullet/NEAT-Template-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.js
More file actions
33 lines (29 loc) · 942 Bytes
/
Block.js
File metadata and controls
33 lines (29 loc) · 942 Bytes
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
function Block(x, y) {
this.x = x;
this.y = y;
this.w = 10; // width of block
this.h = 10; // height of block
this.show = function () {
image(blockImg, this.x, this.y, this.w, this.h);
};
this.intersects = function (other) {
//circle-rectangle collision (e.g. Treat or Enemy)
if (other.r !== undefined) {
let closestX = constrain(other.x, this.x, this.x + this.w);
let closestY = constrain(other.y, this.y, this.y + this.h);
let dx = other.x - closestX;
let dy = other.y - closestY;
let distanceSq = dx * dx + dy * dy;
return distanceSq < other.r * other.r;
//rectangle-rectangle collision (e.g. Player)
} else if (other.w !== undefined && other.h !== undefined) {
return !(
other.x + other.w < this.x ||
other.x > this.x + this.w ||
other.y + other.h < this.y ||
other.y > this.y + this.h
);
}
return false;
};
}