-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (218 loc) · 6.51 KB
/
index.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
const map = [
[1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1],
];
const SCREEN_WIDTH = window.innerWidth;
const SCREEN_HEIGHT = window.innerHeight;
const TICK = 20;
const CELL_SIZE = 32;
const FOV = toRadians(60);
const COLORS = {
floor: "#d52b1e", // "#ff6361"
ceiling: "#ffffff", // "#012975",
wall: "#013aa6", // "#58508d"
wallDark: "#012975", // "#003f5c"
rays: "#ffa600",
};
const player = {
x: CELL_SIZE * 1.5,
y: CELL_SIZE * 2,
angle: toRadians(0),
speed: 0,
};
const canvas = document.createElement("canvas");
canvas.setAttribute("width", SCREEN_WIDTH);
canvas.setAttribute("height", SCREEN_HEIGHT);
document.body.appendChild(canvas);
const context = canvas.getContext("2d");
function clearScreen() {
context.fillStyle = "red";
context.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
function renderMinimap(posX = 0, posY = 0, scale, rays) {
const cellSize = scale * CELL_SIZE;
map.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell) {
context.fillStyle = "grey";
context.fillRect(
posX + x * cellSize,
posY + y * cellSize,
cellSize,
cellSize
);
}
});
});
context.fillStyle = "blue";
context.fillRect(
posX + player.x * scale - 10 / 2,
posY + player.y * scale - 10 / 2,
10,
10
);
context.strokeStyle = "blue";
context.beginPath();
context.moveTo(player.x * scale, player.y * scale);
context.lineTo(
(player.x + Math.cos(player.angle) * 20) * scale,
(player.y + Math.sin(player.angle) * 20) * scale
);
context.closePath();
context.stroke();
context.strokeStyle = COLORS.rays;
rays.forEach((ray) => {
context.beginPath();
context.moveTo(player.x * scale, player.y * scale);
context.lineTo(
(player.x + Math.cos(ray.angle) * ray.distance) * scale,
(player.y + Math.sin(ray.angle) * ray.distance) * scale
);
context.closePath();
context.stroke();
});
}
function toRadians(deg) {
return (deg * Math.PI) / 180;
}
function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function outOfMapBounds(x, y) {
return x < 0 || x >= map[0].length || y < 0 || y >= map.length;
}
function getVCollision(angle) {
const right = Math.abs(Math.floor((angle - Math.PI / 2) / Math.PI) % 2);
const firstX = right
? Math.floor(player.x / CELL_SIZE) * CELL_SIZE + CELL_SIZE
: Math.floor(player.x / CELL_SIZE) * CELL_SIZE;
const firstY = player.y + (firstX - player.x) * Math.tan(angle);
const xA = right ? CELL_SIZE : -CELL_SIZE;
const yA = xA * Math.tan(angle);
let wall;
let nextX = firstX;
let nextY = firstY;
while (!wall) {
const cellX = right
? Math.floor(nextX / CELL_SIZE)
: Math.floor(nextX / CELL_SIZE) - 1;
const cellY = Math.floor(nextY / CELL_SIZE);
if (outOfMapBounds(cellX, cellY)) {
break;
}
wall = map[cellY][cellX];
if (!wall) {
nextX += xA;
nextY += yA;
} else {
}
}
return {
angle,
distance: distance(player.x, player.y, nextX, nextY),
vertical: true,
};
}
function getHCollision(angle) {
const up = Math.abs(Math.floor(angle / Math.PI) % 2);
const firstY = up
? Math.floor(player.y / CELL_SIZE) * CELL_SIZE
: Math.floor(player.y / CELL_SIZE) * CELL_SIZE + CELL_SIZE;
const firstX = player.x + (firstY - player.y) / Math.tan(angle);
const yA = up ? -CELL_SIZE : CELL_SIZE;
const xA = yA / Math.tan(angle);
let wall;
let nextX = firstX;
let nextY = firstY;
while (!wall) {
const cellX = Math.floor(nextX / CELL_SIZE);
const cellY = up
? Math.floor(nextY / CELL_SIZE) - 1
: Math.floor(nextY / CELL_SIZE);
if (outOfMapBounds(cellX, cellY)) {
break;
}
wall = map[cellY][cellX];
if (!wall) {
nextX += xA;
nextY += yA;
}
}
return {
angle,
distance: distance(player.x, player.y, nextX, nextY),
vertical: false,
};
}
function castRay(angle) {
const vCollision = getVCollision(angle);
const hCollision = getHCollision(angle);
return hCollision.distance >= vCollision.distance ? vCollision : hCollision;
}
function fixFishEye(distance, angle, playerAngle) {
const diff = angle - playerAngle;
return distance * Math.cos(diff);
}
function getRays() {
const initialAngle = player.angle - FOV / 2;
const numberOfRays = SCREEN_WIDTH;
const angleStep = FOV / numberOfRays;
return Array.from({ length: numberOfRays }, (_, i) => {
const angle = initialAngle + i * angleStep;
const ray = castRay(angle);
return ray;
});
}
function movePlayer() {
player.x += Math.cos(player.angle) * player.speed;
player.y += Math.sin(player.angle) * player.speed;
}
function renderScene(rays) {
rays.forEach((ray, i) => {
const distance = fixFishEye(ray.distance, ray.angle, player.angle);
const wallHeight = ((CELL_SIZE * 5) / distance) * 277;
context.fillStyle = ray.vertical ? COLORS.wallDark : COLORS.wall;
context.fillRect(i, SCREEN_HEIGHT / 2 - wallHeight / 2, 1, wallHeight);
context.fillStyle = COLORS.floor;
context.fillRect(
i,
SCREEN_HEIGHT / 2 + wallHeight / 2,
1,
SCREEN_HEIGHT / 2 - wallHeight / 2
);
context.fillStyle = COLORS.ceiling;
context.fillRect(i, 0, 1, SCREEN_HEIGHT / 2 - wallHeight / 2);
});
}
function gameLoop() {
clearScreen();
movePlayer();
const rays = getRays();
renderScene(rays);
renderMinimap(0, 0, 0.75, rays);
}
setInterval(gameLoop, TICK);
canvas.addEventListener("click", () => {
canvas.requestPointerLock();
});
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowUp") {
player.speed = 2;
}
if (e.key === "ArrowDown") {
player.speed = -2;
}
});
document.addEventListener("keyup", (e) => {
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
player.speed = 0;
}
});
document.addEventListener("mousemove", function (event) {
player.angle += toRadians(event.movementX);
});