-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
194 lines (167 loc) · 4.93 KB
/
script.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
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
let main = document.querySelector('main');
let lamp = document.querySelector('#lamp');
let boardSize = window.innerWidth > 400 ? 400 : 300;
let elSize = boardSize/10;
let interval;
const canvas = document.querySelector('canvas');
canvas.setAttribute('width',`${boardSize}`);
canvas.setAttribute('height',`${boardSize}`);
const ctx = canvas.getContext('2d');
ctx.strokeStyle = '#ffd262';
let paragraph = document.querySelector('#mission-counter');
let firstDraw = true;
let player = {
x: 0,
y: 0
}
let exit = {
x: 0,
y: 0
}
let actions = [];
let coords = [];
let currentLevel = 1;
const section = document.querySelector('section');
function drawNest() {
if(firstDraw) {
for(let i=elSize; i<boardSize; i+= elSize)
{
ctx.moveTo(0, i);
ctx.lineTo(boardSize,i);
ctx.moveTo(i,0);
ctx.lineTo(i, boardSize);
}
firstDraw = false;
}
ctx.stroke();
}
function clear() {
ctx.clearRect(0,0, boardSize, boardSize);
actions = [];
coords = [];
section.innerText = "Actions:";
clearInterval(interval);
}
function drawPlayer(x,y) {
ctx.fillStyle = 'green';
ctx.fillRect(x*elSize, y*elSize, elSize, elSize)
}
function drawExit(x,y) {
ctx.fillStyle = 'blue';
ctx.fillRect(x*elSize, y*elSize, elSize, elSize)
}
function drawWall(x,y) {
ctx.fillStyle = 'grey';
ctx.fillRect(x*elSize, y*elSize, elSize, elSize)
}
function move(x,y) {
if(x>0) actions.push('R');
if(x<0) actions.push('L');
if(y>0) actions.push('D');
if(y<0) actions.push('U');
coords.push([y,x])
section.innerText = `Actions: ${actions.join(" ")}`
}
function back() {
actions.pop();
coords.pop();
section.innerText = `Actions: ${actions.join(" ")}`
}
function drawMessage(color, text) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.50)';
ctx.fillRect(0,0, boardSize, boardSize);
ctx.textAlign = 'center';
ctx.fillStyle = color;
ctx.font = '40px arial';
ctx.fillText(text.toUpperCase(), boardSize/2, boardSize/2);
section.innerText = `Actions: ${actions.join(" ")} ${text}!`;
}
function drawTrack() {
let pos = [player.y, player.x];
if(coords.length == 0) {
drawMessage('red', 'lost');
setTimeout(getLevelData, 800);
return;
}
coords.forEach(el => {
pos[0] += el[0];
pos[1] += el[1];
ctx.fillStyle = '#0D0';
if(boardSize==400) ctx.fillRect(pos[1]*elSize+15, pos[0]*elSize+15, 10, 10)
if(boardSize==300) ctx.fillRect(pos[1]*elSize+10, pos[0]*elSize+10, 10, 10)
if(!levels[currentLevel][pos[0]]) {
drawMessage('red', 'Lost');
setTimeout(getLevelData, 800);
return;
}
if(levels[currentLevel][pos[0]][pos[1]]==1) {
drawMessage('red', 'Collision');
setTimeout(getLevelData, 800);
throw new Error('collision!')
}
})
if(levels[currentLevel][pos[0]][pos[1]]==3) {
drawMessage('#0D0', 'Completed');
lamp.style.background = 'lime';
++currentLevel;
if(currentLevel > Object.keys(levels).length) {
section.innerText = `Actions: ${actions.join(" ")} Completed! It was the last level. Thanks for playing!`;
return;
}
setTimeout(getLevelData, 800);
return;
}
if(!levels[currentLevel][pos[0]][pos[1]] || levels[currentLevel][pos[0]][pos[1]]==0) {
drawMessage('red', 'Lost')
setTimeout(getLevelData, 800);
}
}
function getLevelData() {
clear();
activateButtons();
lamp.style.background = 'darkred';
interval = setInterval(()=> {
if(lamp.style.background == 'darkred') {lamp.style.background = 'red'}
else {lamp.style.background = 'darkred'}
},1000)
paragraph.innerText = `Mission ${currentLevel} / ${Object.keys(levels).length}`
for(let y=0; y<10; ++y) {
for(let x=0; x<10; ++x)
{
if(levels[currentLevel][y][x]==1) {
drawWall(x,y);
}
if(levels[currentLevel][y][x]==2) {
player.x = x;
player.y = y;
drawPlayer(x,y)
}
if(levels[currentLevel][y][x]==3) {
exit.x = x;
exit.y = y;
drawExit(x,y)
}
}
}
drawNest();
}
function disableButtons() {
let buttons = document.querySelectorAll('button');
buttons.forEach(el => {
el.setAttribute('disabled', true);
})
}
function activateButtons() {
let buttons = document.querySelectorAll('button');
buttons.forEach(el => {
el.removeAttribute('disabled');
})
}
function start() {
disableButtons();
drawTrack();
}
function startGame() {
main.style.visibility = 'hidden';
getLevelData();
}