-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionResolution.js
More file actions
69 lines (60 loc) · 2.72 KB
/
Copy pathCollisionResolution.js
File metadata and controls
69 lines (60 loc) · 2.72 KB
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
//function for assigning different collision technique
function resolveCollision(tile_value, row, column) {
switch (tile_value) {//distributing collision according to tile - type
case 1://right - wall tiles
resolveLeftCollision(row, column);
break;
case 2://bricks
if (resolveTopCollision(row, column)) return;
if (resolveBottomCollision(row, column)) return;
if (resolveLeftCollision(row, column)) return;
if (resolveRightCollision(row, column))
break;
}
}
//Resolving top collision of tile
function resolveTopCollision(row, column) {//row and column tile is in grid
if (player.y_cordinate > player.old_y_cordinate) {//for top-collision player should be moving down
let y_value_of_tile_top = row * Drawing.size_of_tile;
if (player.y_cordinate + player.height > y_value_of_tile_top && player.old_y_cordinate + player.height <= y_value_of_tile_top) {
player.speed_y = 0;
player.in_air = false;
player.old_y_cordinate = player.y_cordinate = y_value_of_tile_top - player.height - 0.01;
return true;
}
}
return false;
}
function resolveBottomCollision(row, column) {
if (player.y_cordinate < player.old_y_cordinate) { //player should be moving up
let y_value_of_tile_bottom = (row + 1) * Drawing.size_of_tile;
if (player.y_cordinate < y_value_of_tile_bottom && player.old_y_cordinate >= y_value_of_tile_bottom) {
player.speed_y = 0;
player.y_cordinate = player.old_y_cordinate = y_value_of_tile_bottom;
return true;
}
}
return false;
}
function resolveRightCollision(row, column) {
if (player.x_cordinate < player.old_x_cordinate) {//for right collision to the tile player should move from left
let x_value_of_tile_right = (column + 1) * Drawing.size_of_tile;
if (player.x_cordinate < x_value_of_tile_right && player.old_x_cordinate >= x_value_of_tile_right) {
player.speed_x = 0;
player.x_cordinate = player.old_x_cordinate = x_value_of_tile_right;
return true;
}
}
return false;
}
function resolveLeftCollision(row, column) {//to collide at left with tile ,player should be moving right
if (player.x_cordinate > player.old_x_cordinate) {
let x_value_of_tile_left = (column) * Drawing.size_of_tile;
if (player.x_cordinate + player.width > x_value_of_tile_left && player.old_x_cordinate + player.width <= x_value_of_tile_left) {
player.speed_x = 0;
player.x_cordinate = player.old_x_cordinate = x_value_of_tile_left - player.width - 0.01;
return true;
}
}
return false;
}