-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimap.html
328 lines (266 loc) · 8.19 KB
/
minimap.html
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
<!DOCTYPE html>
<html>
<head>
<title>Raycaster</title>
</head>
<body>
<div>
<canvas id="canvas">
</div>
<script type="text/javascript">
window.onload = function() {
const app = new App
lastTime = Date.now()
setInterval(app.gameLoop, 1000/30)
}
class App {
constructor() {
this.canvas = new Canvas
this.player = this.canvas.player
this.frameTime
this.lastTime = Date.now()
document.addEventListener('keyup', this.handleUp.bind(this))
document.addEventListener('keydown', this.handleDown.bind(this))
this.gameLoop = this.gameLoop.bind(this)
}
handleDown(event) {
event.preventDefault()
switch (event.keyCode) {
case 87:
this.player.speed = 1
break
case 83:
this.player.speed = -1
break
case 65:
this.player.dir = -1
break
case 68:
this.player.dir = 1
break
case 82:
this.player.toggleRotate()
case 16:
this.player.running()
break
}
}
handleUp(event) {
event.preventDefault()
if (event.keyCode === 38 || event.keyCode === 40 || event.keyCode === 87 || event.keyCode === 83) {
this.player.speed = 0
} else if (event.keyCode === 37 || event.keyCode === 39 || event.keyCode === 65 || event.keyCode === 68) {
this.player.dir = 0
} else if (event.keyCode === 16) {
this.player.walking()
}
}
gameLoop() {
this.canvas.render()
this.screen.render(this.player, this.canvas)
this.logTime()
this.renderInfo()
}
logTime() {
let currTime = Date.now()
this.frameTime = (currTime - this.lastTime)
this.lastTime = currTime
}
}
class Canvas {
constructor() {
this.canvas = document.getElementById('canvas')
this.ctx = this.canvas.getContext('2d')
this.matrix = ([
[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, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
[1, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 2, 0, 0, 2, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1],
[1, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 2, 2, 2, 2, 2, 2, 2, 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, 0, 2, 0, 0, 1],
[1, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 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]
])
this.mapDim = this.matrix[0].length
this.scale = 30
this.pixelSize = this.mapDim * this.scale
this.canvas.width = this.pixelSize
this.canvas.height = this.pixelSize
this.player = new Player(this)
}
drawMap() {
for (let y = 0; y < this.mapDim; y++) {
for (let x = 0; x < this.mapDim; x++) {
switch (this.matrix[y][x]) {
case 1:
this.ctx.fillStyle = "black"
this.ctx.fillRect(x * this.scale, y * this.scale, this.scale, this.scale)
break
case 2:
this.ctx.fillStyle = "grey"
this.ctx.fillRect(x * this.scale, y * this.scale, this.scale, this.scale)
break
}
}
}
}
render() {
this.ctx.clearRect(0, 0, this.pixelSize, this.pixelSize)
this.drawMap()
this.player.render()
}
}
class Player {
constructor(canvas) {
this.canvas = canvas
this.scale = canvas.scale
this.pixelSize = canvas.pixelSize
this.ctx = canvas.ctx
this.matrix = canvas.matrix
this.mapDim = canvas.mapDim
this.x = 12.5
this.y = 9.5
this.dir = 0
this.rot = 0
this.rotate = false
this.size = this.scale / 4
this.speed = 0
this.moveSpeed = 0.07
this.rotSpeed = 4 * Math.PI / 180
;
this.stripWidth = 15
this.fov = 80 * (Math.PI / 180)
this.numRays = Math.ceil(this.pixelSize / this.stripWidth)
this.viewDist = (this.pixelSize / 2) / Math.tan(this.fov / 2)
this.rays = []
}
running() {
this.moveSpeed = 0.12
}
walking() {
this.moveSpeed = 0.05
}
toggleRotate() {
this.rotate = !this.rotate
}
checkCollision(xx, yy){
if (xx < 0 || xx > this.mapDim || yy < 0 || yy > this.mapDim) {
return
}
if (this.matrix[Math.floor(yy + this.size/this.scale)][Math.floor(xx + this.size/this.scale)] !== 0 || this.matrix[Math.floor(yy)][Math.floor(xx)] !== 0) {
return
}
if(this.rotate) { this.rot += 1 * .005 }
this.x = xx
this.y = yy
}
castRays() {
this.rays = []
for (let i = 0; i < this.numRays; i++) {
this.rays.push(new Ray(this, i))
}
}
render() {
this.ctx.fillStyle = "red"
this.ctx.fillRect(this.x * this.scale, this.y * this.scale, this.size, this.size)
let step = this.speed * this.moveSpeed
this.rot += (this.dir * this.rotSpeed)
let xx = this.x + Math.cos(this.rot) * step
let yy = this.y + Math.sin(this.rot) * step
this.checkCollision(xx, yy)
this.castRays()
}
}
class Ray {
constructor(player, idx) {
this.player = player
this.scale = player.scale
this.size = player.size
this.ctx = player.ctx
this.mapDim = player.mapDim
this.matrix = player.matrix
this.screenPos = (-this.player.numRays / 2 + idx) * this.player.stripWidth
this.viewDist = Math.sqrt(Math.pow(this.screenPos, 2) + Math.pow(this.player.viewDist, 2))
this.angle = Math.asin(this.screenPos / this.viewDist)
this.xHit = 0
this.yHit = 0
this.dist = 0
this.block = null
this.cast(this.player.rot + this.angle)
}
cast(ang) {
let angMod = ang % (Math.PI * 2)
const angle = angMod < 0 ? angMod + Math.PI * 2 : angMod
const right = angle > Math.PI * 2 * 0.75 || angle < Math.PI * 2 * 0.25
const up = angle > Math.PI
this.checkHoriz(angle, right)
this.checkVert(angle, up)
this.draw(this.xHit, this.yHit)
}
checkHoriz(angle, right) {
const slope = Math.sin(angle) / Math.cos(angle)
const dx = right ? 1 : -1
const dy = dx * slope
let x = right ? Math.ceil(this.player.x) : Math.floor(this.player.x)
let y = this.player.y + (x - this.player.x) * slope
while (x >= 0 && x < this.mapDim && y >= 0 && y < this.mapDim) {
let mapX = x + (right ? 0 : -1)
let mapY = Math.floor(y)
if (this.matrix[mapY][mapX] !== 0) {
this.block = this.matrix[mapY][mapX]
this.dist = (Math.pow(x - this.player.x, 2) + Math.pow(y - this.player.y, 2))
this.xHit = x
this.yHit = y
break
}
x += dx
y += dy
}
}
checkVert(angle, up) {
const slope = Math.cos(angle) / Math.sin(angle)
const dy = up ? -1 : 1
const dx = dy * slope
let y = up ? Math.floor(this.player.y) : Math.ceil(this.player.y)
let x = this.player.x + (y - this.player.y) * slope
while (x >= 0 && x < this.mapDim && y >= 0 && y < this.mapDim) {
let mapY = y + (up ? -1 : 0)
let mapX = Math.floor(x)
if (this.matrix[mapY][mapX] !== 0) {
let dist = Math.pow(x - this.player.x, 2) + Math.pow(y - this.player.y, 2)
if (!this.dist || dist < this.dist) {
this.block = this.matrix[mapY][mapX]
this.dist = dist
this.xHit = x
this.yHit = y
}
break
}
x += dx
y += dy
}
}
draw(x, y) {
this.ctx.strokeStyle = 'green'
this.ctx.beginPath()
this.ctx.moveTo(this.player.x * this.scale + this.size / 2 , this.player.y * this.scale + this.size/2)
this.ctx.lineTo(x * this.scale, y * this.scale)
this.ctx.closePath()
this.ctx.stroke()
}
}
</script>
</body>
</html>