-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
32 lines (28 loc) · 1015 Bytes
/
utils.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
const objectSize = 30;
const collisionWallMargin = 5;
function detectWallCollisionOnDirection(x, y, direction) {
switch (direction) {
case 1:
return detectObjectWallCollision(x, y - ghost_speed);
case 2:
return detectObjectWallCollision(x + ghost_speed, y);
case 3:
return detectObjectWallCollision(x, y + ghost_speed);
case 4:
return detectObjectWallCollision(x - ghost_speed, y);
}
}
function detectObjectWallCollision(x, y) {
for (var i = 0; i < walls.length; i++) {
if (detectObjectsCollision(x, y, objectSize, objectSize, walls[i][0][0], walls[i][0][1], walls[i][1], walls[i][2], collisionWallMargin)) {
return true;
}
}
return false;
}
function detectObjectsCollision(x1, y1, w1, h1, x2, y2, w2, h2, collisionMargin) {
if (x1 < x2 + w2 - collisionMargin && x1 + w1 - collisionMargin > x2 && y1 < y2 + h2 - collisionMargin && h1 + y1 -collisionMargin > y2) {
return true;
}
return false;
}