-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
358 lines (295 loc) · 9.81 KB
/
script.js
File metadata and controls
358 lines (295 loc) · 9.81 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
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
349
350
351
352
353
354
355
356
357
358
const wallTexture = document.getElementById("wallTexture");
const textureSize = 360; // Assuming 64x64 texture
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let statBarWidth = window.innerWidth;
let statBarHeight = 0;
virtualScreenWidth = canvas.width;
virtualScreenHeight = canvas.height - statBarHeight;
const map = [
[1,1,1,1,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,1,1,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1,0,1],
[1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,0,1,0,1],
[1,0,1,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0,1],
[1,0,1,0,1,0,1,1,1,1,1,1,1,0,1,1,0,1,0,1],
[1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1],
[1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1],
[1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1],
[1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1],
[1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1],
[1,0,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,0,1],
[1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1],
[1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1],
[1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
];
const mapWidth = map[0].length;
const mapHeight = map.length;
const tileSize = 64;
let posX = 100;
let posY = tileSize * 18;
let dir = 0;
let targetDir = dir; // The desired direction player wants to face
const rotationSpeed = 0.02; // radians per frame for smooth rotation
const moveSpeed = 0.15; // units per second
const flashlightAngle = -1;//Math.PI/10000000;
const fov = Math.PI / 2;
const numRays = virtualScreenWidth;
const maxDepth = 1000;
function castRays() {
const minimapScale = Math.floor((statBarHeight)/mapHeight); // minimap tile size in pixels
const xOffset = virtualScreenWidth - mapWidth * minimapScale;
const yOffset = canvas.height - mapHeight * minimapScale;
ctx.fillStyle = "#000";
ctx.fillRect(xOffset, yOffset, minimapScale * mapWidth, minimapScale * mapHeight);
// Draw minimap tiles
for (let y = 0; y < mapHeight; y++) {
for (let x = 0; x < mapWidth; x++) {
ctx.fillStyle = map[y][x] ? "#999" : "#222";
ctx.fillRect(xOffset + x * minimapScale, yOffset + y * minimapScale, minimapScale, minimapScale);
}
}
const px = posX / tileSize;
const py = posY / tileSize;
for (let i = 0; i < numRays; i++) {
const rayAngle = dir - fov / 2 + (i / numRays) * fov;
const rayDirX = Math.cos(rayAngle);
const rayDirY = Math.sin(rayAngle);
let mapX = Math.floor(px);
let mapY = Math.floor(py);
const stepX = rayDirX < 0 ? -1 : 1;
const stepY = rayDirY < 0 ? -1 : 1;
const deltaDistX = Math.abs(1 / rayDirX);
const deltaDistY = Math.abs(1 / rayDirY);
let sideDistX = rayDirX < 0
? (px - mapX) * deltaDistX
: (mapX + 1 - px) * deltaDistX;
let sideDistY = rayDirY < 0
? (py - mapY) * deltaDistY
: (mapY + 1 - py) * deltaDistY;
let hit = false;
let side = 0;
while (!hit) {
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if (
mapX < 0 || mapX >= mapWidth ||
mapY < 0 || mapY >= mapHeight
) break;
if (map[mapY][mapX] > 0) hit = true;
}
// Perpendicular wall distance
let distance;
const rayDir = [rayDirX, rayDirY];
const playerDir = [Math.cos(dir), Math.sin(dir)];
if (side === 0) {
distance = (mapX - px + (1 - stepX) / 2) / rayDirX;
} else {
distance = (mapY - py + (1 - stepY) / 2) / rayDirY;
}
let wallX; // where exactly the wall was hit
if (side === 0) {
wallX = py + distance * rayDirY;
} else {
wallX = px + distance * rayDirX;
}
wallX -= Math.floor(wallX); // only the fractional part remains
// x coordinate on the texture
let texX = Math.floor(wallX * textureSize);
if ((side === 0 && rayDirX > 0) || (side === 1 && rayDirY < 0)) {
texX = textureSize - texX - 1; // flip texture horizontally for some directions
}
// Correct for fisheye by projecting distance onto player's direction vector:
const correctedDist = distance * Math.cos(rayAngle - dir);
// const wallHeight = Math.min(virtualScreenHeight,virtualScreenHeight / (distance || 0.0001););
const wallHeight = virtualScreenHeight / (distance || 0.0001)
const yStart = (virtualScreenHeight - wallHeight) / 2;
if(Math.abs(dir - rayAngle) <= flashlightAngle / 2){
const shade = 255;
ctx.fillStyle = side === 1
? `rgb(${shade * 0.7}, ${shade * 0.7}, ${shade * 0.7})`
: `rgb(${shade}, ${shade}, ${shade})`;
}else{
const shade = 255 - Math.min(255, distance * 15);
ctx.fillStyle = side === 1
? `rgb(${shade * 0.7}, ${shade * 0.7}, ${shade * 0.7})`
: `rgb(${shade}, ${shade}, ${shade})`;
}
ctx.drawImage(
wallTexture,
texX, 0 /*+ Math.abs(Math.max(0,textureSize * ((wallHeight/virtualScreenHeight) - 1)/2))*/, 1, textureSize, // source: 1-pixel vertical strip of texture
i, yStart, 1, wallHeight // dest: vertical column on canvas
);
// Draw this ray on minimap (reduced resolution)
/*if (i % 10 === 0) {
const hitX = (px + rayDirX * distance) * minimapScale;
const hitY = (py + rayDirY * distance) * minimapScale;
ctx.strokeStyle = (Math.abs(dir - rayAngle) <= flashlightAngle / 2) ? "rgba(255,255,255,0.3)" : "rgba(255,255,0,0.3)";
ctx.beginPath();
ctx.moveTo(xOffset + px * minimapScale, yOffset + py * minimapScale);
ctx.lineTo(xOffset + hitX, yOffset + hitY);
ctx.stroke();
}*/
}
// Draw player on minimap
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(xOffset + px * minimapScale, yOffset + py * minimapScale, 2, 0, Math.PI * 2);
ctx.fill();
// Draw direction line
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.moveTo(xOffset + px * minimapScale, yOffset + py * minimapScale);
ctx.lineTo(
xOffset + (px + Math.cos(dir) * 0.5) * minimapScale,
yOffset + (py + Math.sin(dir) * 0.5) * minimapScale
);
ctx.stroke();
}
let lastTime = performance.now();
let fps = 0;
let maxFps = 0;
let minFps = Math.pow(2, 16);
let fpsSamples = 0;
let fpsSum = 0;
function drawFPS() {
ctx.fillStyle = "white";
ctx.font = "32px monospace";
maxFps = Math.max(fps, maxFps);
minFps = Math.min(fps, minFps);
if(fpsSamples < 10){
maxFps = 0;
minFps = Math.pow(2, 16);
}
fpsSamples++;
fpsSum += fps;
ctx.fillText(`FPS: ${Math.round(fps)}`, 10, 20);
ctx.fillText(`Max: ${Math.round(maxFps)}`, 10, 50);
ctx.fillText(`Min: ${Math.round(minFps)}`, 10, 80);
ctx.fillText(`Avg: ${Math.round(fpsSum/fpsSamples)}`, 10, 110);
}
function gameLoop() {
const now = performance.now();
const delta = now - lastTime;
lastTime = now;
fps = 1000 / delta;
updatePlayerRotation();
updatePlayerPosition(delta);
ctx.clearRect(0, 0, virtualScreenWidth, virtualScreenHeight);
castRays();
drawFPS();
requestAnimationFrame(gameLoop);
}
let movingForward = false;
let movingBackward = false;
let rotatingLeft = false;
let rotatingRight = false;
// Listen for key presses
window.addEventListener('keydown', (e) => {
switch (e.key) {
case "ArrowUp":
movingForward = true;
break;
case "ArrowDown":
case "s":
case "S":
movingBackward = true;
break;
case "ArrowLeft":
rotatingLeft = true;
break;
case "ArrowRight":
rotatingRight = true;
break;
}
});
window.addEventListener('keyup', (e) => {
switch (e.key) {
case "ArrowUp":
movingForward = false;
break;
case "ArrowDown":
case "s":
case "S":
movingBackward = false;
break;
case "ArrowLeft":
rotatingLeft = false;
break;
case "ArrowRight":
rotatingRight = false;
break;
}
});
function updatePlayerRotation() {
if (rotatingLeft) {
targetDir -= rotationSpeed;
}
if (rotatingRight) {
targetDir += rotationSpeed;
}
// Normalize targetDir
while (targetDir < 0) targetDir += 2 * Math.PI;
while (targetDir >= 2 * Math.PI) targetDir -= 2 * Math.PI;
// Smoothly interpolate dir to targetDir
let diff = targetDir - dir;
while (diff < -Math.PI) diff += 2 * Math.PI;
while (diff > Math.PI) diff -= 2 * Math.PI;
if (Math.abs(diff) < rotationSpeed) {
dir = targetDir;
} else {
dir += Math.sign(diff) * rotationSpeed;
}
}
function updatePlayerPosition(deltaTime) {
let moveX = 0;
let moveY = 0;
if (movingForward) {
moveX += Math.cos(dir);
moveY += Math.sin(dir);
}
if (movingBackward) {
moveX -= Math.cos(dir);
moveY -= Math.sin(dir);
}
// Normalize
const length = Math.hypot(moveX, moveY);
if (length > 0) {
moveX /= length;
moveY /= length;
}
// Calculate new position with collision detection
const newX = posX + moveX * moveSpeed * deltaTime;
const newY = posY + moveY * moveSpeed * deltaTime;
// Check collision X axis
const tileX = Math.floor(newX / tileSize);
const tileY = Math.floor(posY / tileSize);
if (map[tileY] && map[tileY][tileX] === 0) {
posX = newX;
}
// Check collision Y axis
const tileX2 = Math.floor(posX / tileSize);
const tileY2 = Math.floor(newY / tileSize);
if (map[tileY2] && map[tileY2][tileX2] === 0) {
posY = newY;
}
}
//window.addEventListener("keydown", updatePlayerPosition);
//window.addEventListener("keydown", updatePlayerRotation);
wallTexture.onload = () => {
gameLoop();
};