/
app.js
351 lines (296 loc) · 12.6 KB
/
app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
'use strict';
import Viewport from "../shared/engine/Viewport.js";
import Vector from "../shared/engine/Vector.js";
class Map {
constructor() {
this.width = 16;
this.height = 16;
this.layout = [];
this.init();
}
init() {
this.layout = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ];
}
}
class Player {
constructor() {
this.speed = 0;
this.position = new Vector(8, 8);
this.heading = new Vector(0, -1); // up
this.perpendicular = this.heading.perpendicularCounterClockwise(); // left
this.rotate(Math.PI/4)
}
rotate(value) {
const x = this.heading.x;
this.heading.x = x * Math.cos(value) - this.heading.y * Math.sin(value);
this.heading.y = x * Math.sin(value) + this.heading.y * Math.cos(value);
this.perpendicular = this.heading.perpendicularCounterClockwise();
}
update() {
this.position.x += this.heading.x * this.speed;
this.position.y += this.heading.y * this.speed;
if(map.layout[Math.floor(this.position.x) + Math.floor(this.position.y) * map.width] === 1) {
this.position.x -= this.heading.x * this.speed;
this.position.y -= this.heading.y * this.speed;
}
}
// MOVE!!!
render(renderer) {
const size = 5;
renderer.fillStyle = "red";
renderer.beginPath();
renderer.arc(this.position.x * size, this.position.y * size, size/2, 0, Math.PI * 2);
renderer.fill();
renderer.beginPath();
renderer.strokeStyle = "black";
renderer.moveTo(this.position.x* size, this.position.y* size);
renderer.lineTo(this.position.x* size + this.heading.x * 6, this.position.y* size + this.heading.y * 6)
renderer.stroke();
}
}
class Cube {
constructor(point1, point2, point3, size1, size2, size3, mapIndex) {
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
this.size1 = size1;
this.size2 = size2;
this.size3 = size3;
this.index = mapIndex;
}
}
const ray = () => {
// MOVE OUT OF HERE!!!
const screenWidth = 640;
const fieldOfView = Math.PI / 2;
const movementVector = player.heading.cloned();
movementVector.rotate(-fieldOfView / 2);
let rotation = fieldOfView / screenWidth;
const alreadyInQueue = {};
let cubes = [];
for(let x = 0; x < screenWidth; x++) {
// Variables for running the loop
let distance = 0;
let maxDistance = 50;
let found = false;
// Start from player position
const currentPosition = player.position.cloned();
while(!found && distance++ < maxDistance) {
// convert to index values
const indexX = Math.floor(currentPosition.x);
const indexY = Math.floor(currentPosition.y);
// check if out of bounds
if(indexX < 0 || indexX >= map.width || indexY < 0 || indexY >= map.height) {
console.log("ERROR")
found = true;
}
else if(map.layout[indexY*map.width + indexX] === 1) {
// ignore if already handled IMPLEMENT
// Make distance more precise
// let tries = 0;
// let dist = 1;
// while(tries++ < 10) {
// if(Math.abs(currentPosition.x) === indexX && Math.abs(currentPosition.y) === indexY) {
// currentPosition.subtract(movementVector.scaled(dist));
// distance -= dist;
// }
// else {
// currentPosition.add(movementVector.scaled(dist));
// distance += dist;
// }
// dist /= 2;
// }
found = true;
if(alreadyInQueue[indexX + indexY * 16]) {
}
else {
let point1 = false;
let point2 = false;
let point3 = false;
const yDirection = player.position.y - indexY;
const xDirection = player.position.x - indexX;
if(yDirection > 0) {
if(xDirection > 0) {
// FACING NORTHWEST
if(map.layout[(indexX) + (indexY + 1) * map.width] !== 1) {
point1 = getLocalCoords(new Vector(indexX, indexY));
}
if(map.layout[(indexX + 1) + (indexY) * map.width] !== 1) {
point1
? point3 = getLocalCoords(new Vector(indexX + 1, indexY - 1))
: point1 = getLocalCoords(new Vector(indexX + 1, indexY - 1))
}
if(point1) point2 = getLocalCoords(new Vector(indexX + 1, indexY));
}
else {
// NORTHEAST
if(map.layout[(indexX) + (indexY + 1) * map.width] !== 1) {
point1 = getLocalCoords(new Vector(indexX + 1, indexY));
}
if(map.layout[(indexX - 1) + (indexY) * map.width] !== 1) {
point1
? point3 = getLocalCoords(new Vector(indexX, indexY - 1))
: point1 = getLocalCoords(new Vector(indexX, indexY - 1))
}
if(point1) point2 = getLocalCoords(new Vector(indexX, indexY));
}
}
else {
if(xDirection < 0) {
// SOUTHEAST
if(map.layout[(indexX) + (indexY - 1) * map.width] !== 1) {
point1 = getLocalCoords(new Vector(indexX + 1, indexY - 1));
}
if(map.layout[(indexX - 1) + (indexY) * map.width] !== 1) {
point1
? point3 = getLocalCoords(new Vector(indexX, indexY))
: point1 = getLocalCoords(new Vector(indexX, indexY))
}
if(point1) point2 = getLocalCoords(new Vector(indexX, indexY - 1));
}
else {
// SOUTHWEST
if(map.layout[(indexX) + (indexY - 1) * map.width] !== 1) {
point1 = getLocalCoords(new Vector(indexX, indexY - 1));
}
if(map.layout[(indexX + 1) + (indexY) * map.width] !== 1) {
point1
? point3 = getLocalCoords(new Vector(indexX + 1, indexY))
: point1 = getLocalCoords(new Vector(indexX + 1, indexY))
}
if(point1) point2 = getLocalCoords(new Vector(indexX + 1, indexY - 1));
}
}
const scale1 = 320 / -point1.y;
const scale2 = 320 / -point2.y;
const scale3 = 320 / -point3.y;
if(point1) {
cubes.push(new Cube(point1, point2, point3,
scale1, scale2, scale3,
new Vector(indexX, indexY)));
}
alreadyInQueue[indexX + indexY * 16] = true;
}
}
else {
currentPosition.add(movementVector);
// ADD TO Line of sight QUEUE
renderer.beginPath();
renderer.strokeStyle = "pink";
renderer.rect(indexX * 5, indexY * 5, 1, 1)
renderer.stroke();
}
}
movementVector.rotate(rotation);
}
// RENDER LOGIC MOVE OUT OF HERE!!!
const center = 160;
renderer.beginPath();
renderer.strokeStyle = "black";
renderer.fillStyle = "red";
renderer.save();
cubes.sort((a, b) => (a.point1.x - a.point2.x) < (b.point1.x - b.point2.x));
renderer.translate(center + 160, 160);
cubes.forEach(cube => {
renderer.beginPath();
const zoom = 1;
cube.point1.y = zoom - cube.size1;
cube.point2.y = zoom - cube.size2;
renderer.moveTo(cube.point1.x * cube.size1, cube.point1.y);
renderer.lineTo(cube.point1.x * cube.size1, -cube.point1.y);
renderer.lineTo(cube.point2.x * cube.size2, -cube.point2.y);
renderer.lineTo(cube.point2.x* cube.size2, cube.point2.y);
renderer.lineTo(cube.point1.x * cube.size1, cube.point1.y)
if(cube.point3) {
cube.point3.y = zoom - cube.size3;
renderer.moveTo(cube.point2.x * cube.size2, cube.point2.y);
renderer.lineTo(cube.point2.x * cube.size2, -cube.point2.y);
renderer.lineTo(cube.point3.x* cube.size3, -cube.point3.y);
renderer.lineTo(cube.point3.x* cube.size3, cube.point3.y);
renderer.lineTo(cube.point2.x * cube.size2, cube.point2.y);
}
renderer.fill();
renderer.stroke();
})
renderer.restore();
}
const getLocalCoords = (data) => {
const newCoords = data.subtracted(player.position);
newCoords.rotate(-player.perpendicular.angle());
return newCoords;
}
const move = (e) => {
switch(e.key) {
case "ArrowUp": {
player.speed+= .01;
break;
}
case "ArrowDown": {
player.speed = 0;
break;
}
case "ArrowLeft": {
player.rotate(-.05);
break;
}
case "ArrowRight": {
player.rotate(.05);
break;
}
}
}
window.addEventListener("keydown", (e) => move(e));
const renderMap = () => {
const width = map.length / 16;
const size = 5;
map.layout.forEach((square, index) => {
square===1 ? renderer.fillStyle = "green" : renderer.fillStyle = "white";
renderer.beginPath();
const x = index % width;
const y = Math.floor(index / width);
renderer.rect(x * size, y * size, size, size);
renderer.fill();
square===1 ? renderer.stroke() : false;
})
}
const viewport = new Viewport(640, 400);
const renderer = viewport.context;
const map = new Map();
const player = new Player();
const loop = () => {
viewport.clear();
const gradient = renderer.createLinearGradient(0,0, 0, 200);
gradient.addColorStop(0, "white");
gradient.addColorStop(1, "blue");
renderer.fillStyle = gradient;
renderer.fillRect(0,0, 640, 160);
const gradient2 = renderer.createLinearGradient(0,180, 0, 400);
gradient2.addColorStop(0, "green");
gradient2.addColorStop(1, "white");
renderer.fillStyle = gradient2;
renderer.fillRect(0, 160, 640, 400)
player.update();
renderMap();
player.render(renderer);
ray();
renderer.fillStyle = "orange";
renderer.fillRect(0,0, 80, 400);
renderer.fillRect(560,0, 80, 400);
window.requestAnimationFrame(loop);
};
window.requestAnimationFrame(loop);