forked from tsoding/koil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
567 lines (567 loc) · 19.6 KB
/
Copy pathgame.js
File metadata and controls
567 lines (567 loc) · 19.6 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
const EPS = 1e-6;
const NEAR_CLIPPING_PLANE = 0.1;
const FAR_CLIPPING_PLANE = 10.0;
const FOV = Math.PI * 0.5;
const COS_OF_HALF_FOV = Math.cos(FOV * 0.5);
const PLAYER_STEP_LEN = 0.5;
const PLAYER_SPEED = 2;
const MINIMAP_SPRITES = false;
const MINIMAP_PLAYER_SIZE = 0.5;
const MINIMAP_SPRITE_SIZE = 0.3;
const MINIMAP_SCALE = 0.03;
export class RGBA {
r;
g;
b;
a;
constructor(r, g, b, a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
static red() {
return new RGBA(1, 0, 0, 1);
}
static green() {
return new RGBA(0, 1, 0, 1);
}
static blue() {
return new RGBA(0, 0, 1, 1);
}
static yellow() {
return new RGBA(1, 1, 0, 1);
}
static purple() {
return new RGBA(1, 0, 1, 1);
}
static cyan() {
return new RGBA(0, 1, 1, 1);
}
toStyle() {
return `rgba(`
+ `${Math.floor(this.r * 255)}, `
+ `${Math.floor(this.g * 255)}, `
+ `${Math.floor(this.b * 255)}, `
+ `${this.a})`;
}
}
export class Vector2 {
x;
y;
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
setAngle(angle, len = 1) {
this.x = Math.cos(angle) * len;
this.y = Math.sin(angle) * len;
return this;
}
getAngle() {
return Math.atan2(this.y, this.x);
}
clone() {
return new Vector2(this.x, this.y);
}
copy(that) {
this.x = that.x;
this.y = that.y;
return this;
}
setScalar(scalar) {
this.x = scalar;
this.y = scalar;
return this;
}
add(that) {
this.x += that.x;
this.y += that.y;
return this;
}
sub(that) {
this.x -= that.x;
this.y -= that.y;
return this;
}
div(that) {
this.x /= that.x;
this.y /= that.y;
return this;
}
mul(that) {
this.x *= that.x;
this.y *= that.y;
return this;
}
sqrLength() {
return this.x * this.x + this.y * this.y;
}
length() {
return Math.sqrt(this.sqrLength());
}
scale(value) {
this.x *= value;
this.y *= value;
return this;
}
norm() {
const l = this.length();
return l === 0 ? this : this.scale(1 / l);
}
rot90() {
const oldX = this.x;
this.x = -this.y;
this.y = oldX;
return this;
}
sqrDistanceTo(that) {
const dx = that.x - this.x;
const dy = that.y - this.y;
return dx * dx + dy * dy;
}
distanceTo(that) {
return Math.sqrt(this.sqrDistanceTo(that));
}
lerp(that, t) {
this.x += (that.x - this.x) * t;
this.y += (that.y - this.y) * t;
return this;
}
dot(that) {
return this.x * that.x + this.y * that.y;
}
map(f) {
this.x = f(this.x);
this.y = f(this.y);
return this;
}
}
function strokeLine(ctx, p1, p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
function snap(x, dx) {
if (dx > 0)
return Math.ceil(x + Math.sign(dx) * EPS);
if (dx < 0)
return Math.floor(x + Math.sign(dx) * EPS);
return x;
}
function hittingCell(p1, p2) {
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
return new Vector2(Math.floor(p2.x + Math.sign(dx) * EPS), Math.floor(p2.y + Math.sign(dy) * EPS));
}
function rayStep(p1, p2) {
let p3 = p2;
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
if (dx !== 0) {
const k = dy / dx;
const c = p1.y - k * p1.x;
{
const x3 = snap(p2.x, dx);
const y3 = x3 * k + c;
p3 = new Vector2(x3, y3);
}
if (k !== 0) {
const y3 = snap(p2.y, dy);
const x3 = (y3 - c) / k;
const p3t = new Vector2(x3, y3);
if (p2.sqrDistanceTo(p3t) < p2.sqrDistanceTo(p3)) {
p3 = p3t;
}
}
}
else {
const y3 = snap(p2.y, dy);
const x3 = p2.x;
p3 = new Vector2(x3, y3);
}
return p3;
}
const SCENE_FLOOR1 = new RGBA(0.094, 0.094 + 0.07, 0.094 + 0.07, 1.0);
const SCENE_FLOOR2 = new RGBA(0.188, 0.188 + 0.07, 0.188 + 0.07, 1.0);
const SCENE_CEILING1 = new RGBA(0.094 + 0.07, 0.094, 0.094, 1.0);
const SCENE_CEILING2 = new RGBA(0.188 + 0.07, 0.188, 0.188, 1.0);
export function createScene(walls) {
const scene = {
height: walls.length,
width: Number.MIN_VALUE,
walls: [],
};
for (let row of walls) {
scene.width = Math.max(scene.width, row.length);
}
for (let row of walls) {
scene.walls = scene.walls.concat(row);
for (let i = 0; i < scene.width - row.length; ++i) {
scene.walls.push(null);
}
}
return scene;
}
export function sceneSize(scene) {
return new Vector2(scene.width, scene.height);
}
function sceneContains(scene, p) {
return 0 <= p.x && p.x < scene.width && 0 <= p.y && p.y < scene.height;
}
function sceneGetTile(scene, p) {
if (!sceneContains(scene, p))
return undefined;
return scene.walls[Math.floor(p.y) * scene.width + Math.floor(p.x)];
}
function sceneGetFloor(p) {
if ((Math.floor(p.x) + Math.floor(p.y)) % 2 == 0) {
return SCENE_FLOOR1;
}
else {
return SCENE_FLOOR2;
}
}
function sceneGetCeiling(p) {
if ((Math.floor(p.x) + Math.floor(p.y)) % 2 == 0) {
return SCENE_CEILING1;
}
else {
return SCENE_CEILING2;
}
}
function sceneIsWall(scene, p) {
const c = sceneGetTile(scene, p);
return c !== null && c !== undefined;
}
function sceneCanRectangleFitHere(scene, px, py, sx, sy) {
const x1 = Math.floor(px - sx * 0.5);
const x2 = Math.floor(px + sx * 0.5);
const y1 = Math.floor(py - sy * 0.5);
const y2 = Math.floor(py + sy * 0.5);
for (let x = x1; x <= x2; ++x) {
for (let y = y1; y <= y2; ++y) {
if (sceneIsWall(scene, new Vector2(x, y))) {
return false;
}
}
}
return true;
}
function castRay(scene, p1, p2) {
let start = p1;
while (start.sqrDistanceTo(p1) < FAR_CLIPPING_PLANE * FAR_CLIPPING_PLANE) {
const c = hittingCell(p1, p2);
if (sceneIsWall(scene, c))
break;
const p3 = rayStep(p1, p2);
p1 = p2;
p2 = p3;
}
return p2;
}
export function createPlayer(position, direction) {
return {
position: position,
velocity: new Vector2(0, 0),
direction: direction,
movingForward: false,
movingBackward: false,
movingLeft: false,
movingRight: false,
turningLeft: false,
turningRight: false,
rotationalMovement: 0,
sprinting: false,
};
}
function playerFovRange(player) {
const l = Math.tan(FOV * 0.5) * NEAR_CLIPPING_PLANE;
const p = new Vector2().setAngle(player.direction, NEAR_CLIPPING_PLANE).add(player.position);
const wing = p.clone().sub(player.position).rot90().norm().scale(l);
const p1 = p.clone().sub(wing);
const p2 = p.clone().add(wing);
return [p1, p2];
}
function renderMinimap(ctx, player, scene, sprites) {
ctx.save();
const cellSize = ctx.canvas.width * MINIMAP_SCALE;
const gridSize = sceneSize(scene);
ctx.translate(ctx.canvas.width * 0.03, ctx.canvas.height * 0.03);
ctx.scale(cellSize, cellSize);
ctx.fillStyle = "#181818";
ctx.fillRect(0, 0, gridSize.x, gridSize.y);
ctx.lineWidth = 0.05;
for (let y = 0; y < gridSize.y; ++y) {
for (let x = 0; x < gridSize.x; ++x) {
const cell = sceneGetTile(scene, new Vector2(x, y));
if (cell instanceof RGBA) {
ctx.fillStyle = cell.toStyle();
ctx.fillRect(x, y, 1, 1);
}
else if (cell instanceof ImageData) {
ctx.fillStyle = "blue";
ctx.fillRect(x, y, 1, 1);
}
}
}
ctx.strokeStyle = "#303030";
for (let x = 0; x <= gridSize.x; ++x) {
strokeLine(ctx, new Vector2(x, 0), new Vector2(x, gridSize.y));
}
for (let y = 0; y <= gridSize.y; ++y) {
strokeLine(ctx, new Vector2(0, y), new Vector2(gridSize.x, y));
}
ctx.fillStyle = "magenta";
ctx.fillRect(player.position.x - MINIMAP_PLAYER_SIZE * 0.5, player.position.y - MINIMAP_PLAYER_SIZE * 0.5, MINIMAP_PLAYER_SIZE, MINIMAP_PLAYER_SIZE);
const [p1, p2] = playerFovRange(player);
ctx.strokeStyle = "magenta";
strokeLine(ctx, p1, p2);
strokeLine(ctx, player.position, p1);
strokeLine(ctx, player.position, p2);
if (MINIMAP_SPRITES) {
ctx.fillStyle = "red";
ctx.strokeStyle = "yellow";
const sp = new Vector2();
const dir = new Vector2().setAngle(player.direction);
strokeLine(ctx, player.position, player.position.clone().add(dir));
for (let sprite of sprites) {
ctx.fillRect(sprite.position.x - MINIMAP_SPRITE_SIZE * 0.5, sprite.position.y - MINIMAP_SPRITE_SIZE * 0.5, MINIMAP_SPRITE_SIZE, MINIMAP_SPRITE_SIZE);
sp.copy(sprite.position).sub(player.position);
strokeLine(ctx, player.position, player.position.clone().add(sp));
const spl = sp.length();
if (spl <= NEAR_CLIPPING_PLANE)
continue;
if (spl >= FAR_CLIPPING_PLANE)
continue;
const dot = sp.dot(dir) / spl;
ctx.fillStyle = "white";
ctx.font = "0.5px bold";
ctx.fillText(`${dot}`, player.position.x, player.position.y);
if (!(COS_OF_HALF_FOV <= dot))
continue;
const dist = NEAR_CLIPPING_PLANE / dot;
sp.norm().scale(dist).add(player.position);
ctx.fillRect(sp.x - MINIMAP_SPRITE_SIZE * 0.5, sp.y - MINIMAP_SPRITE_SIZE * 0.5, MINIMAP_SPRITE_SIZE, MINIMAP_SPRITE_SIZE);
}
}
ctx.restore();
}
const dts = [];
function renderFPS(ctx, deltaTime) {
ctx.font = "48px bold";
ctx.fillStyle = "white";
dts.push(deltaTime);
if (dts.length > 60)
dts.shift();
const dtAvg = dts.reduce((a, b) => a + b, 0) / dts.length;
ctx.fillText(`${Math.floor(1 / dtAvg)}`, 100, 100);
}
function renderWalls(display, player, scene) {
const [r1, r2] = playerFovRange(player);
const d = new Vector2().setAngle(player.direction);
for (let x = 0; x < display.backImageData.width; ++x) {
const p = castRay(scene, player.position, r1.clone().lerp(r2, x / display.backImageData.width));
const c = hittingCell(player.position, p);
const cell = sceneGetTile(scene, c);
const v = p.clone().sub(player.position);
display.zBuffer[x] = v.dot(d);
if (cell instanceof RGBA) {
const stripHeight = display.backImageData.height / display.zBuffer[x];
const shadow = 1 / display.zBuffer[x] * 2;
for (let dy = 0; dy < Math.ceil(stripHeight); ++dy) {
const y = Math.floor((display.backImageData.height - stripHeight) * 0.5) + dy;
const destP = (y * display.backImageData.width + x) * 4;
display.backImageData.data[destP + 0] = cell.r * shadow * 255;
display.backImageData.data[destP + 1] = cell.g * shadow * 255;
display.backImageData.data[destP + 2] = cell.b * shadow * 255;
}
}
else if (cell instanceof ImageData) {
const stripHeight = display.backImageData.height / display.zBuffer[x];
let u = 0;
const t = p.clone().sub(c);
if (Math.abs(t.x) < EPS && t.y > 0) {
u = t.y;
}
else if (Math.abs(t.x - 1) < EPS && t.y > 0) {
u = 1 - t.y;
}
else if (Math.abs(t.y) < EPS && t.x > 0) {
u = 1 - t.x;
}
else {
u = t.x;
}
const y1 = Math.floor((display.backImageData.height - stripHeight) * 0.5);
const y2 = Math.floor(y1 + stripHeight);
const by1 = Math.max(0, y1);
const by2 = Math.min(display.backImageData.height - 1, y2);
const tx = Math.floor(u * cell.width);
const sh = (1 / Math.ceil(stripHeight)) * cell.height;
const shadow = Math.min(1 / display.zBuffer[x] * 4, 1);
for (let y = by1; y <= by2; ++y) {
const ty = Math.floor((y - y1) * sh);
const destP = (y * display.backImageData.width + x) * 4;
const srcP = (ty * cell.width + tx) * 4;
display.backImageData.data[destP + 0] = cell.data[srcP + 0] * shadow;
display.backImageData.data[destP + 1] = cell.data[srcP + 1] * shadow;
display.backImageData.data[destP + 2] = cell.data[srcP + 2] * shadow;
}
}
}
}
function renderCeiling(imageData, player) {
const pz = imageData.height / 2;
const [p1, p2] = playerFovRange(player);
const bp = p1.clone().sub(player.position).length();
for (let y = Math.floor(imageData.height / 2); y < imageData.height; ++y) {
const sz = imageData.height - y - 1;
const ap = pz - sz;
const b = (bp / ap) * pz / NEAR_CLIPPING_PLANE;
const t1 = player.position.clone().add(p1.clone().sub(player.position).norm().scale(b));
const t2 = player.position.clone().add(p2.clone().sub(player.position).norm().scale(b));
for (let x = 0; x < imageData.width; ++x) {
const t = t1.clone().lerp(t2, x / imageData.width);
const tile = sceneGetCeiling(t);
if (tile instanceof RGBA) {
const shadow = player.position.distanceTo(t);
const destP = (sz * imageData.width + x) * 4;
imageData.data[destP + 0] = tile.r * shadow * 255;
imageData.data[destP + 1] = tile.g * shadow * 255;
imageData.data[destP + 2] = tile.b * shadow * 255;
}
}
}
}
function renderFloor(imageData, player) {
const pz = imageData.height / 2;
const [p1, p2] = playerFovRange(player);
const bp = p1.clone().sub(player.position).length();
for (let y = Math.floor(imageData.height / 2); y < imageData.height; ++y) {
const sz = imageData.height - y - 1;
const ap = pz - sz;
const b = (bp / ap) * pz / NEAR_CLIPPING_PLANE;
const t1 = player.position.clone().add(p1.clone().sub(player.position).norm().scale(b));
const t2 = player.position.clone().add(p2.clone().sub(player.position).norm().scale(b));
for (let x = 0; x < imageData.width; ++x) {
const t = t1.clone().lerp(t2, x / imageData.width);
const tile = sceneGetFloor(t);
if (tile instanceof RGBA) {
const shadow = player.position.distanceTo(t);
const destP = (y * imageData.width + x) * 4;
imageData.data[destP + 0] = tile.r * shadow * 255;
imageData.data[destP + 1] = tile.g * shadow * 255;
imageData.data[destP + 2] = tile.b * shadow * 255;
}
}
}
}
function displaySwapBackImageData(display) {
display.backCtx.putImageData(display.backImageData, 0, 0);
display.ctx.drawImage(display.backCtx.canvas, 0, 0, display.ctx.canvas.width, display.ctx.canvas.height);
}
function renderSprites(display, player, sprites) {
const markSize = 100;
const sp = new Vector2();
const dir = new Vector2().setAngle(player.direction);
const [p1, p2] = playerFovRange(player);
for (const sprite of sprites) {
sp.copy(sprite.position).sub(player.position);
const spl = sp.length();
if (spl <= NEAR_CLIPPING_PLANE)
continue;
if (spl >= FAR_CLIPPING_PLANE)
continue;
const dot = sp.dot(dir) / spl;
if (!(COS_OF_HALF_FOV <= dot))
continue;
const dist = NEAR_CLIPPING_PLANE / dot;
sp.norm().scale(dist).add(player.position);
const t = p1.distanceTo(sp) / p1.distanceTo(p2);
const cx = display.backImageData.width * t;
const cy = display.backImageData.height * 0.5;
const pdist = sprite.position.clone().sub(player.position).dot(dir);
if (pdist < NEAR_CLIPPING_PLANE)
continue;
const spriteScale = 0.5;
const spriteSize = display.backImageData.height / pdist * spriteScale;
const x1 = Math.floor(cx - spriteSize * 0.5);
const x2 = Math.floor(x1 + spriteSize - 1);
const bx1 = Math.max(0, x1);
const bx2 = Math.min(display.backImageData.width - 1, x2);
const y1 = Math.floor(cy - spriteSize * 0.5);
const y2 = Math.floor(y1 + spriteSize - 1);
const by1 = Math.max(0, y1);
const by2 = Math.min(display.backImageData.height - 1, y2);
const src = sprite.imageData.data;
const dest = display.backImageData.data;
for (let x = bx1; x <= bx2; ++x) {
if (pdist < display.zBuffer[x]) {
for (let y = by1; y <= by2; ++y) {
const tx = Math.floor((x - x1) / spriteSize * sprite.imageData.width);
const ty = Math.floor((y - y1) / spriteSize * sprite.imageData.height);
const srcP = (ty * sprite.imageData.width + tx) * 4;
const destP = (y * display.backImageData.width + x) * 4;
const alpha = src[srcP + 3] / 255;
dest[destP + 0] = dest[destP + 0] * (1 - alpha) + src[srcP + 0] * alpha;
dest[destP + 1] = dest[destP + 1] * (1 - alpha) + src[srcP + 1] * alpha;
dest[destP + 2] = dest[destP + 2] * (1 - alpha) + src[srcP + 2] * alpha;
}
}
}
}
}
export function renderGame(display, deltaTime, player, scene, sprites) {
let angularVelocity = 0.0;
let velocityRaw = new Vector2();
if (player.movingForward) {
velocityRaw.add(new Vector2().setAngle(player.direction));
}
if (player.movingBackward) {
velocityRaw.sub(new Vector2().setAngle(player.direction));
}
if (player.movingLeft) {
velocityRaw.sub(new Vector2().setAngle(player.direction + 0.5 * Math.PI));
}
if (player.movingRight) {
velocityRaw.add(new Vector2().setAngle(player.direction + 0.5 * Math.PI));
}
if (player.turningLeft) {
angularVelocity -= Math.PI;
}
if (player.turningRight) {
angularVelocity += Math.PI;
}
if (player.rotationalMovement !== 0) {
const angularSpeedPerMouseMovementUnit = 0.1;
angularVelocity += (angularSpeedPerMouseMovementUnit * player.rotationalMovement);
player.rotationalMovement = 0;
}
player.velocity.setScalar(0);
if (velocityRaw.length() > 0) {
let speed = PLAYER_SPEED;
if (player.sprinting) {
const sprintingSpeedMultiplier = 1.75;
speed *= sprintingSpeedMultiplier;
}
player.velocity.setAngle(velocityRaw.getAngle(), speed);
}
player.direction = player.direction + angularVelocity * deltaTime;
const nx = player.position.x + player.velocity.x * deltaTime;
if (sceneCanRectangleFitHere(scene, nx, player.position.y, MINIMAP_PLAYER_SIZE, MINIMAP_PLAYER_SIZE)) {
player.position.x = nx;
}
const ny = player.position.y + player.velocity.y * deltaTime;
if (sceneCanRectangleFitHere(scene, player.position.x, ny, MINIMAP_PLAYER_SIZE, MINIMAP_PLAYER_SIZE)) {
player.position.y = ny;
}
renderFloor(display.backImageData, player);
renderCeiling(display.backImageData, player);
renderWalls(display, player, scene);
renderSprites(display, player, sprites);
displaySwapBackImageData(display);
renderFPS(display.ctx, deltaTime);
}
//# sourceMappingURL=game.js.map