-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
235 lines (174 loc) · 5.46 KB
/
main.js
File metadata and controls
235 lines (174 loc) · 5.46 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
// detail
const RAY_CNT = 200;
// canvas and context
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// canvas dimensions
let width = canvas.offsetWidth;
let height = canvas.offsetHeight;
// to be left alone by the user
let PPU = 10;
let MID_VERT = height/2;
let MID_HORIZ = width/2;
// view changed
let changed = true;
// Window handling
/**
* Resize the canvas element based on the css calculated values
*/
function resize() {
// redefine width and height
width = window.innerWidth; //canvas.offsetWidth;
height = window.innerHeight; //canvas.offsetHeight;
if (height > width * (3/4)) height = width * (3/4); // scale to meet the ratio of the fov
// reset the canvas element's size
canvas.width = width;
canvas.height = height;
MID_VERT = height/2;
MID_HORIZ = width/2;
changed = true;
}
// add the window event listener
window.addEventListener('resize', resize);
resize(); // resize the can vas to match the window
// Key handling, held keys are in keys array
let keys = [];
function keydownHndl(e) {
let keynum;
if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera s
keynum = e.which;
}
if (!keys.includes(keynum)) keys.push(keynum);
//console.log('down', String.fromCharCode(keynum), keynum);
}
function keyupHndl(e) {
let keynum;
if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera s
keynum = e.which;
}
let i = keys.indexOf(keynum);
if (i > -1) keys.splice(i, 1);
//console.log('up', String.fromCharCode(keynum), keynum);
}
document.onkeydown = keydownHndl;
document.onkeyup = keyupHndl;
// Mouse handling
let mouseX = 0; // mouses x within the canvas
let mouseY = 0; // mouses y within the canvas
document.addEventListener("mousemove", (ev) => {
mouseX = ev.clientX;
mouseY = ev.clientY
});
// Utitlity functs
/**
* Rotate vector counter clockwise
* @param {number[]} v a two vector
* @param {number} r an angle in radians
*/
function rotate(v, r)
{
let cos = Math.cos(r);
let sin = Math.sin(r);
return [
v[0]*cos - v[1]*sin,
v[0]*sin + v[1]*cos
];
}
/**
* Produce an array of rays
* @param {number} cnt the number of arrays
* @param {Camera} cam the camera that uses the rays
* @returns {Line[]}
*/
function makeRays(cnt, cam)
{
let theta = cam.fov/(cnt-1);
let rays = [];
let leftfov = new Line();
leftfov.dir = rotate(cam.dir, cam.fov/2);
leftfov.st = [cam.pos[0], cam.pos[1]];
rays.push(leftfov);
for (let i = 1; i < cnt; i++) {
let r = new Line();
r.dir = rotate(leftfov.dir, -i*theta);
r.st = [cam.pos[0], cam.pos[1]];
rays.push(r);
}
return rays;
}
// Ticker - provides an area for regular updates, should stay very light
ticker = setInterval(() => {
// keyboard input
keys.forEach((knum) => {
switch (knum) {
case 65: // a
//console.log('a');
player.dir = rotate(player.dir, 0.1);
changed = true;
break;
case 68: // d
//console.log('d');
player.dir = rotate(player.dir, -0.1);
changed = true;
break;
case 87: // w
player.pos[0] += player.dir[0]*0.1;
player.pos[1] += player.dir[1]*0.1;
changed = true;
break;
case 83: // s
player.pos[0] -= player.dir[0]*0.1;
player.pos[1] -= player.dir[1]*0.1;
changed = true;
break;
default:
//console.log(knum);
break;
}
});
}, 50);
// render each frame
function render()
{
// only rneder if nessisary
if (!changed) {
window.requestAnimationFrame(render);
return;
}
// clear canvas
//console.log('clear');
ctx.clearRect(0, 0, canvas.width, canvas.height);
let style = ctx.fillStyle; // save the style
// draw floor
ctx.fillStyle = "rgb(100,100,100)";
ctx.fillRect(0, MID_VERT, canvas.width, canvas.height/2);
// draw ceiling
ctx.fillStyle = "rgb(110,180,180)";
ctx.fillRect(0, 0, canvas.width, canvas.height/2);
// draw walls
let segwidth = width/RAY_CNT;
let plos = makeRays(RAY_CNT, player); // player line of sight
let pvision = scanlvlwalls(plos, lvl);
pvision.forEach((item, i) => {
let lambda = item[0]; // dir v along ray
let mu = item[1]; // dir v along wall
let wl = item[2]; // wall
if (wl != null) {
let segheight = height/lambda; // this will need ajustings
if (segheight > height) segheight = height;
let y = (height - segheight)/2;
let x = i*segwidth;
ctx.fillStyle = wl.cf(mu, lambda);
ctx.fillRect(x, y, segwidth+1, segheight);
}
});
ctx.fillStyle = style; // reset the style
changed = false;
// setup next frame
window.requestAnimationFrame(render);
}
render();