-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
205 lines (155 loc) · 4.74 KB
/
script.js
File metadata and controls
205 lines (155 loc) · 4.74 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var current_level = 0;
fetch('./levels.json')
.then(res => res.json())
.then(json => {
const levels = json;
var map = levels[current_level];
document.getElementById('last_level').innerHTML = levels.length-1;
const cell_types = {
"+": '<div class="cell player switch"></div>',
"*": '<div class="cell switch box"></div>',
"#": '<div class="cell wall"></div>',
"P": '<div class="cell player"></div>',
"B": '<div class="cell box"></div>',
"S": '<div class="cell switch"></div>',
"0": '<div class="cell"></div>',
};
var player = null; // denoted with P
var boxes = []; // position of all the boxes on the map. denoted with B
var switches = []; // position of all the switches on the map. denoted with S
var END = true;
// NOTE: no of boxes and no of switches should be equal on the map.
var grid = document.getElementById("grid");
function draw_level() {
player = null;
boxes = [];
switches = [];
let html = '';
map.forEach((row, y) => {
html += '<div class="row">';
[...row].forEach((cell, x) => {
html += cell_types[cell] || cell_types['0'];
if (cell === "P" || cell === "+") {
player = [y,x];
}
if (cell === "B" || cell === "*") {
boxes.push([y,x]);
}
if (cell === "S" || cell === "*" || cell === "+") {
switches.push([y,x]);
}
});
html += '</div>'
});
grid.innerHTML = html;
document.getElementById('message').innerHTML = `
<img src="./keys.png" alt="" height="100px">
<br>
<b>Use arrow keys to move & Space to reload level</b>
`;
// change current level count on screen
document.getElementById('current_level').innerHTML = current_level;
END = false;
}
// recursive check of objects to given position
function prepare_move(pos, type="player") {
let el = (grid.children[pos[0]]).children[pos[1]];
if (has_class(el, "wall"))
return false;
if (has_class(el, "box"))
if (type === "box")
return false;
else if (type === "player")
if (!push_box(pos, matx_diff(pos, player)))
return false;
return true;
}
function push_box(pos, v) {
let pushed = false
for (i in boxes) {
let box = boxes[i];
if (box[0] === pos[0] && box[1] === pos[1]) {
let next_cell = add_matx([...box], v);
let can_move = prepare_move(next_cell, "box");
if (!can_move) break;
let box_cell = (grid.children[box[0]]).children[box[1]];
remove_class(box_cell, 'box');
add_matx(box, v);
box_cell = (grid.children[box[0]]).children[box[1]];
add_class(box_cell, 'box');
pushed = true
break;
}
}
return pushed;
}
function check_end() {
let end = true;
for(i in switches) {
let s = switches[i];
let pressed = false;
for (j in boxes) {
let box = boxes[j];
if (compare_matx(s, box)) {
pressed = true;
break;
}
}
end = end && pressed;
if (!end) break;
}
return end;
}
function load_level () {
map = levels[current_level];
draw_level();
}
function move_player(v) { // move item on pos using velocity v
let next_cell = add_matx([...player], v);
let can_move = prepare_move(next_cell);
if (!can_move) return;
let player_cell = (grid.children[player[0]]).children[player[1]];
remove_class(player_cell, 'player');
add_matx(player, v);
player_cell = (grid.children[player[0]]).children[player[1]];
add_class(player_cell, 'player');
// check if all the switches are pushed
END = check_end()
if (END) {
if (current_level < levels.length - 1) {
document.getElementById("message").innerHTML = "<h3>Puzzle Solved!</h3><br/><p>Loading Next level in 2 seconds</p>";
current_level++;
setTimeout(load_level, 2000);
} else {
document.getElementById("message").innerHTML = "<h3>Congrats! You solved all the puzzles!</h3>";
}
}
}
function keypress (key) {
if (END) return;
let v = [0,0];
if (key === "ArrowUp") {
v = [-1, 0];
}
if (key === "ArrowDown") {
v = [1, 0];
}
if (key === "ArrowLeft") {
v = [0, -1];
}
if (key === "ArrowRight") {
v = [0, 1];
}
if (key === " ") {
// reload level
END = true;
load_level();
}
move_player(v);
}
function Init() {
draw_level();
window.addEventListener('keydown', e => keypress(e.key));
}
Init();
})