-
Notifications
You must be signed in to change notification settings - Fork 0
/
raycaster.js
183 lines (151 loc) · 4.68 KB
/
raycaster.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
const canvas = document.getElementById('rendering-surface');
const ctx = canvas.getContext('2d');
const canvas2 = document.getElementById('rendering-surface2');
const ctx2 = canvas2.getContext('2d');
canvas.width = 640;
canvas.height = 640;
canvas2.width = 640;
canvas2.height = 640;
const tileWidth = canvas.width / 16;
const tileHeight = canvas.height / 16;
const keys = {};
// 16x16 map
const map = [1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,
1,2,2,0,0,0,0,0,0,0,0,0,0,0,1,1,
3,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,
3,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,
1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
let playerX = 2.5;
let playerY= 2.5;
let playerFOV = Math.PI / 3;
let playerDirectionAngle = 1.523 * 2;
function clear(ctx) {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function renderMinimap() {
// render map
for (let row = 0; row < 16; row++) {
for (let col = 0; col < 16; col++) {
if (map[col + row * 16] === 0) {
continue;
}
const colorMap = {
1: '#0000FF',
2: '#00FF00',
3: '#FF0000'
}
const wallColor = colorMap[map[col + row * 16]];
const x = col * tileWidth;
const y = row * tileHeight;
ctx.save();
ctx.fillStyle = wallColor;
ctx.fillRect(x, y, tileWidth, tileHeight);
ctx.restore();
}
}
// render player
const playerMarkerSize = 6;
ctx.fillStyle = 'red';
ctx.fillRect(
(playerX * tileWidth) - playerMarkerSize / 2,
(playerY * tileHeight) - playerMarkerSize / 2,
playerMarkerSize,
playerMarkerSize
);
// draw FOV
for (let i = 0; i < canvas.width; i++) {
const rayAngle = playerDirectionAngle - playerFOV / 2 + playerFOV * i / canvas.width;
const { x, y, distance, wall } = castRay(playerX, playerY, rayAngle);
drawLine(ctx, playerX * tileWidth, playerY * tileWidth, x, y, '#FFF');
const colorMap = {
1: '#0000FF',
2: '#00FF00',
3: '#FF0000'
}
const wallColor = colorMap[wall];
// draw 2.5d
const wallHeight = canvas.height / (distance * Math.cos(rayAngle - playerDirectionAngle));
drawLine(ctx2, i, canvas.height / 2 - wallHeight / 2, i, canvas.height / 2 - wallHeight / 2 + wallHeight, wallColor);
}
// render player direction ray
const { x, y } = castRay(playerX, playerY, playerDirectionAngle);
drawLine(ctx, playerX * tileWidth, playerY * tileHeight, x, y, 'hsl(30, 100%, 70%)');
}
function drawLine(ctx, x1, y1, x2, y2, color) {
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function castRay(fromX, fromY, direction) {
let distance;
let x, y;
let pixelX, pixelY;
let wall;
for (distance = 0; distance < 20; distance += 0.005) {
x = fromX + distance * Math.cos(direction);
y = fromY + distance * Math.sin(direction);
wall = map[Math.trunc(x) + Math.trunc(y) * 16];
if (wall !== 0) {
break;
}
pixelX = x * canvas.width / 16;
pixelY = y * canvas.width / 16;
}
return { distance: distance - 0.005, x: pixelX, y: pixelY, wall }
}
function handleInput() {
if (keys['w']) {
playerY = playerY + 0.1 * Math.sin(playerDirectionAngle);
playerX = playerX + 0.1 * Math.cos(playerDirectionAngle);
}
if (keys['s']) {
playerY = playerY - 0.1 * Math.sin(playerDirectionAngle);
playerX = playerX - 0.1 * Math.cos(playerDirectionAngle);
}
if (keys['a']) {
playerY = playerY - 0.1 * Math.sin(playerDirectionAngle + Math.PI / 2);
playerX = playerX - 0.1 * Math.cos(playerDirectionAngle + Math.PI / 2);
}
if (keys['d']) {
playerY = playerY + 0.1 * Math.sin(playerDirectionAngle + Math.PI / 2);
playerX = playerX + 0.1 * Math.cos(playerDirectionAngle + Math.PI / 2);
}
if (keys['ArrowLeft']) {
playerDirectionAngle -= 0.03;
}
if (keys['ArrowRight']) {
playerDirectionAngle += 0.03;
}
}
function loop() {
handleInput();
clear(ctx);
clear(ctx2);
renderMinimap();
window.requestAnimationFrame(loop);
}
loop();
window.addEventListener('keydown', (e) => {
keys[e.key] = true;
});
window.addEventListener('keyup', (e) => {
keys[e.key] = false;
});