-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo1b.ts
More file actions
205 lines (191 loc) · 6.03 KB
/
Copy pathdemo1b.ts
File metadata and controls
205 lines (191 loc) · 6.03 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
import { Cell } from './demo1';
import { Vec2, add2, mul2, sleep } from './util';
export const map: Cell[][] = [
'XXXXXXXXXX',
'X X',
'X X',
'X X X',
'X XX X',
'X X',
'X XXXX',
].map(row => row.split('').map(cell => {
return {
solid: cell === 'X',
};
}));
export const mapSize: Vec2 = {
x: map[0].length,
y: map.length,
};
export function initDemo1b() {
const playerPos: Vec2 = {x: 2, y: 3};
const playerDir: Vec2 = {x: 1, y: 0};
const canvas = document.getElementById('canvas1b') as HTMLCanvasElement;
if (canvas.parentElement) {
const width = canvas.parentElement.clientWidth;
canvas.width = canvas.parentElement.clientWidth * devicePixelRatio;
canvas.style.width = `${width}px`;
}
const cellSize = canvas.width / mapSize.x;
canvas.height = mapSize.y * cellSize;
//canvas.style.imageRendering = 'pixelated';
const ctx = canvas.getContext('2d')!;
//ctx.imageSmoothingEnabled = false;
renderMapGrid(ctx, map, mapSize, cellSize);
renderPlayerPos(ctx, playerPos, cellSize);
const aspectRatio = 320 / 200;
renderEnv(ctx, aspectRatio, playerPos, playerDir, cellSize);
}
export function renderPlayerPos(
ctx: CanvasRenderingContext2D,
playerPos: Vec2,
cellSize: number,
) {
ctx.fillStyle = '#F60';
ctx.beginPath();
ctx.arc(
playerPos.x * cellSize,
playerPos.y * cellSize,
cellSize / 4,
0,
Math.PI * 2,
);
ctx.fill();
ctx.strokeStyle = '#F60';
}
export function renderMapGrid(
ctx: CanvasRenderingContext2D,
map: Cell[][],
mapSize: Vec2,
cellSize: number,
) {
ctx.strokeStyle = '#333';
for (let y = 0; y < mapSize.y; y++) {
for (let x = 0; x < mapSize.x; x++) {
const cell = map[y][x];
if (cell.solid) {
ctx.fillStyle = '#056';
} else {
ctx.fillStyle = '#666';
}
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
ctx.strokeRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
}
export async function renderEnv(
ctx: CanvasRenderingContext2D,
aspectRatio: number,
playerPos: Vec2,
playerDir: Vec2,
cellSize: number,
) {
while (true) {
const sideDist = {x: 0, y: 0};
let perpWallDist = 0;
let step = {x: 0, y: 0};
const cameraPlane = {
x: -playerDir.y,
y: playerDir.x,
};
const points: Vec2[] = [];
for (let x = 0; x < 32; x++) {
const cameraX = aspectRatio * x / 32 - aspectRatio / 2;
const rayDir = add2(playerDir, mul2(cameraX, cameraPlane));
const mapPos = {x: Math.floor(playerPos.x), y: Math.floor(playerPos.y)};
const deltaDist = {
x: Math.abs(1 / rayDir.x),
y: Math.abs(1 / rayDir.y),
};
if (rayDir.x < 0) {
step.x = -1;
sideDist.x = (playerPos.x - mapPos.x) * deltaDist.x;
} else {
step.x = 1;
sideDist.x = (1 - playerPos.x + mapPos.x) * deltaDist.x;
}
if (rayDir.y < 0) {
step.y = -1;
sideDist.y = (playerPos.y - mapPos.y) * deltaDist.y;
} else {
step.y = 1;
sideDist.y = (1 - playerPos.y + mapPos.y) * deltaDist.y;
}
ctx.strokeStyle = '#fff';
let previous = {
x: playerPos.x * cellSize,
y: playerPos.y * cellSize,
};
while (true) {
let side = 0;
if (sideDist.x < sideDist.y) {
perpWallDist = sideDist.x;
sideDist.x += deltaDist.x;
mapPos.x += step.x;
} else {
perpWallDist = sideDist.y;
sideDist.y += deltaDist.y;
mapPos.y += step.y;
side = 1;
}
if (mapPos.x < 0 || mapPos.x >= mapSize.x || mapPos.y < 0 || mapPos.y >= mapSize.y) {
break;
}
if (perpWallDist === 0) {
continue;
}
const next = {
x: (playerPos.x + rayDir.x * perpWallDist) * cellSize,
y: (playerPos.y + rayDir.y * perpWallDist) * cellSize,
};
ctx.beginPath();
ctx.moveTo(previous.x, previous.y);
ctx.lineTo(next.x, next.y);
ctx.stroke();
previous = next;
await sleep(100);
const cell = map[mapPos.y][mapPos.x];
if (cell.solid) {
ctx.fillStyle = '#F00';
ctx.beginPath();
ctx.arc(
next.x,
next.y,
3,
0,
Math.PI * 2,
);
ctx.fill();
points.push(next);
break;
} else {
ctx.fillStyle = side ? '#0F0' : '#00F';
ctx.beginPath();
ctx.arc(
next.x,
next.y,
3,
0,
Math.PI * 2,
);
ctx.fill();
}
}
await sleep(200);
renderMapGrid(ctx, map, mapSize, cellSize);
renderPlayerPos(ctx, playerPos, cellSize);
for (const point of points) {
ctx.fillStyle = '#F00';
ctx.beginPath();
ctx.arc(
point.x,
point.y,
3,
0,
Math.PI * 2,
);
ctx.fill();
}
}
}
}