-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo3.ts
More file actions
121 lines (108 loc) · 4.25 KB
/
demo3.ts
File metadata and controls
121 lines (108 loc) · 4.25 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
import { PlayerInputs, Ray, advanceRay, attachKeyboard, attachMouse, attachTouch, createRay, getCameraPlane, getMapCell, getWallHeight, map, mapSize, setPlayerPos, updatePosition } from './demo1';
import { createSky, getBrightness, renderBackground } from './demo2';
import { Vec2, attachRenderFunction, initCanvas, loadTextureData } from './util';
export const textureSize = {x: 64, y: 64};
export async function initDemo3() {
const playerPos: Vec2 = {x: 2, y: 3};
const playerDir: Vec2 = {x: 1, y: 0};
const playerInputs: PlayerInputs = {
moveForward: false,
moveBackward: false,
turnLeft: false,
turnRight: false,
rotationSpeed: 0,
};
const setPos = (dest: Vec2) => setPlayerPos(playerPos, dest, map, mapSize);
const wallTexture: ImageData = await loadTextureData('wall.png');
const [canvas, ctx] = initCanvas('canvas3');
const aspectRatio = canvas.width / canvas.height;
const sky = createSky(canvas, ctx);
const repaint = attachRenderFunction(canvas, dt => {
updatePosition(dt, playerInputs, playerPos, playerDir, setPos);
renderBackground(canvas, ctx, sky);
renderEnv(canvas, ctx, aspectRatio, playerPos, playerDir, wallTexture);
});
attachKeyboard(canvas, playerInputs);
attachMouse(canvas, repaint, playerPos, playerDir, setPos);
attachTouch(canvas, repaint, playerPos, playerDir, setPos);
}
export function renderEnv(
canvas: HTMLCanvasElement,
ctx: CanvasRenderingContext2D,
aspectRatio: number,
playerPos: Vec2,
playerDir: Vec2,
wallTexture: ImageData,
) {
const cameraPlane = getCameraPlane(playerDir);
for (let x = 0; x < canvas.width; x++) {
const ray = createRay(canvas, aspectRatio, playerPos, playerDir, x, cameraPlane);
const stripe = ctx.getImageData(x, 0, 1, canvas.height);
while (true) {
advanceRay(ray);
const cell = getMapCell(map, ray.mapPos, mapSize)
if (!cell) {
break;
} else if (cell.solid) {
const wall = getWallMeasurements(ray, canvas.height, playerPos);
renderWall(canvas, stripe, ray, wall, wallTexture);
break;
}
}
ctx.putImageData(stripe, x, 0);
}
}
export interface WallMeasurements {
wallHeight: number;
wallX: number;
wallY: number;
}
export function getWallMeasurements(ray: Ray, canvasHeight: number, playerPos: Vec2): WallMeasurements {
const {wallHeight, wallY} = getWallHeight(canvasHeight, ray.perpWallDist);
let wallX: number;
if (ray.side === 0) {
wallX = playerPos.y + ray.perpWallDist * ray.rayDir.y - ray.mapPos.y;
} else {
wallX = playerPos.x + ray.perpWallDist * ray.rayDir.x - ray.mapPos.x;
}
return {wallHeight, wallX, wallY};
}
export function renderWall(
canvas: HTMLCanvasElement,
stripe: ImageData,
ray: Ray,
wall: WallMeasurements,
wallTexture?: ImageData,
) {
const brightness = getBrightness(ray.perpWallDist, ray.side);
const texX = (wall.wallX * textureSize.x) & (textureSize.x - 1);
/*
if (ray.side === 0 && ray.rayDir.x < 0) {
texX = textureSize.x - texX - 1;
}
if (ray.side === 1 && ray.rayDir.y > 0) {
texX = textureSize.x - texX - 1;
}
*/
const yStart = Math.max(wall.wallY, 0);
const yEnd = Math.min(wall.wallY + wall.wallHeight, canvas.height);
const step = textureSize.y / wall.wallHeight;
let texPos = wall.wallY < yStart ? (yStart - wall.wallY) * step : 0;
for (let y = yStart; y < yEnd; y++) {
const offset = y * 4;
const texY = texPos & (textureSize.y - 1);
texPos += step;
if (wallTexture) {
const texOffset = (texY * textureSize.x + texX) * 4;
stripe.data[offset] = wallTexture.data[texOffset] * brightness;
stripe.data[offset + 1] = wallTexture.data[texOffset + 1] * brightness;
stripe.data[offset + 2] = wallTexture.data[texOffset + 2] * brightness;
stripe.data[offset + 3] = 255;
} else {
stripe.data[offset] = 0;
stripe.data[offset + 1] = 85 * brightness;
stripe.data[offset + 2] = 102 * brightness;
stripe.data[offset + 3] = 255;
}
}
}