-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (91 loc) · 2.79 KB
/
Copy pathscript.js
File metadata and controls
102 lines (91 loc) · 2.79 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
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
96
97
98
99
100
101
102
import ITEMS_POSITIONS from "./objects.js";
// Player movement logic
const gameContainer = document.getElementById("game-container");
const state = {
collides: "random",
};
const TOP_Y_LIMIT = -194;
const BOTTOM_Y_LIMIT = -54;
const LEFT_X_LIMIT = -152;
const RIGHT_X_LIMIT = 105;
function parseMovement(str, curr) {
// format ex: lt12 or gt14
const sign = str.slice(0, 2);
const num = parseInt(str.slice(2));
if (sign.localeCompare("lt") === 0) {
return curr < num;
} else if (sign.localeCompare("gt") === 0) {
return curr > num;
}
return false;
}
function collidesWithObjects(x, y) {
for (const item in ITEMS_POSITIONS) {
const itemPos = ITEMS_POSITIONS[item];
if (parseMovement(itemPos.x, x) && parseMovement(itemPos.y, y)) {
state.collides = item;
console.log(`Collision with ${item} at x=${x}, y=${y}`);
return true;
}
}
state.collides = "random";
return false;
}
function canMoveTo(x, y) {
console.log(`Checking position: x=${x}, y=${y}`);
return (
y > TOP_Y_LIMIT &&
y < BOTTOM_Y_LIMIT &&
x > LEFT_X_LIMIT &&
x < RIGHT_X_LIMIT &&
!collidesWithObjects(x, y)
);
}
const player = document.getElementsByClassName("player")[0];
document.addEventListener("keydown", (event) => {
event.preventDefault();
const key = event.key;
player.classList = "player"; // Reset player classes
switch (key) {
case "ArrowUp":
player.classList.add("moving-up");
if (canMoveTo(player.offsetLeft, player.offsetTop - 2)) {
player.style.top = `${player.offsetTop - 2}px`;
}
break;
case "ArrowDown":
player.classList.add("moving-down");
if (canMoveTo(player.offsetLeft, player.offsetTop + 2)) {
player.style.top = `${player.offsetTop + 2}px`;
}
break;
case "ArrowLeft":
player.classList.add("moving-left");
if (canMoveTo(player.offsetLeft - 2, player.offsetTop)) {
player.style.left = `${player.offsetLeft - 2}px`;
}
break;
case "ArrowRight":
player.classList.add("moving-right");
if (canMoveTo(player.offsetLeft + 2, player.offsetTop)) {
player.style.left = `${player.offsetLeft + 2}px`;
}
break;
}
player.addEventListener("animationend", (e) => {
if (e.animationName === "moveDown") {
player.classList.remove("moving-down");
player.classList.add("idle-down");
} else if (e.animationName === "moveUp") {
player.classList.remove("moving-up");
player.classList.add("idle-up");
} else if (e.animationName === "moveLeft") {
player.classList.remove("moving-left");
player.classList.add("idle-left");
} else if (e.animationName === "moveRight") {
player.classList.remove("moving-right");
player.classList.add("idle-right");
}
});
});
export default state;