-
Notifications
You must be signed in to change notification settings - Fork 1
/
collision.ts
44 lines (41 loc) · 1.33 KB
/
collision.ts
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
function checkCollisionWithMap(
x: number,
y: number,
collisionData: CollisionData,
tileMap: TileMap
) {
// Adjust the input x and y to account for the camera offset
const tileWidth = tileMap.tilewidth;
const tileHeight = tileMap.tileheight;
const offsetX =
(-camera.cameraX - (tileMap.width * tileWidth) / 2) * camera.zoomFactor;
const offsetY =
(-camera.cameraY - (tileMap.height * tileHeight) / 2) * camera.zoomFactor;
console.log(
`playerLeft: ${playerLeft}, playerRight: ${playerRight}, playerTop: ${playerTop}, playerBottom: ${playerBottom}`
);
console.log(
`tileLeft: ${tileLeft}, tileRight: ${tileRight}, tileTop: ${tileTop}, tileBottom: ${tileBottom}`
);
// Check if the x and y are within the tileMap bounds
/* if (
adjustedX < 0 ||
adjustedY < 0 ||
adjustedX >= tileMap.width ||
adjustedY >= tileMap.height
) {
return true;
} */
for (let y = 0; y < collisionData.height; y++) {
for (let x = 0; x < collisionData.width; x++) {
if (collisionData.data[y][x] !== 0) {
console.log(
x * tileWidth * camera.zoomFactor + offsetX,
y * tileHeight * camera.zoomFactor + offsetY,
tileWidth * camera.zoomFactor,
tileHeight * camera.zoomFactor
);
}
}
}
}