A
Three mistakes:Inside the facility, you cannot invoke yourself during initialization. In your code.const ray = {
FOV: Math.PI / 3,
HALF_FOV: this.FOV / 2,
NUM_RAYS: 80,
MAX_DEPTH: 400,
DELTA_ANGLE: this.FOV / this.NUM_RAYS,
};
this - a global facility.Such an object can be created through a designer, where this will refer to the facility created upon challenge:const ray = new Ray();
function Ray() {
this.FOV = Math.PI / 3;
this.HALF_FOV = this.FOV / 2;
this.NUM_RAYS = 80;
this.MAX_DEPTH = 400;
this.DELTA_ANGLE = this.FOV / this.NUM_RAYS;
}
Or so:const ray = {
FOV: Math.PI / 3,
NUM_RAYS: 80,
MAX_DEPTH: 400,
};
ray.HALF_FOV = ray.FOV / 2;
ray.DELTA_ANGLE = ray.FOV / ray.NUM_RAYS;
let lastX = lastY = 400; - there's no announcement here. Under "use strict" would make a mistake if lastY wasn't announced in advance. Must be. let lastX = 400, lastY = 400;You didn't give a replicated example, you had to think!let ctx = canvas.getContext("2d");
const map = { x: 0, lastX: 200, _x: [0, 200], y: 0, lastY: 100, _y: [0, 100] };
const player = { x: 50, y: 50, angle: 0, speed: 2 };
const ray = new Ray();
rayCasting(player, ray);
/***/
function rayCasting(player, ray) {
let curAngle = player.angle - ray.HALF_FOV;
let xo = player.x;
let yo = player.y;
/***/
for (let rayNum = 0; rayNum < ray.NUM_RAYS; rayNum++) {
let sin = Math.sin(curAngle);
let cos = Math.cos(curAngle);
curAngle += ray.DELTA_ANGLE;
let lastX = 400, lastY = 400;
for (let depth = 0; depth < ray.MAX_DEPTH; depth++) {
let x = (xo + depth) * cos;
let y = (yo + depth) * sin;
if (map._x.includes(x|0) || map._y.includes(y|0)) {
lastX = x;
lastY = y;
break;
}
}
/***/
ctx.strokeStyle = 'darkgray';
ctx.beginPath();
ctx.moveTo(xo, yo);
ctx.lineTo(lastX, lastY);
ctx.stroke();
}
}
function Ray() {
this.FOV = Math.PI / 3;
this.HALF_FOV = this.FOV / 2;
this.NUM_RAYS = 80;
this.MAX_DEPTH = 400;
this.DELTA_ANGLE = this.FOV / this.NUM_RAYS;
}<canvas id="canvas"></canvas>On the check chain Math.floor(x) === map.x ... If the number is positive, Math.floor(x) can be replaced by x|0 (killed or, Zero does nothing, surgery takes out the fragment).And the object itself. map Additional properties may be added, both stored: [x, lastX] and [y, lastY]to verify that there is a number in the mass, rewriting the entire verification: map._x.includes(x|0) || map._y.includes(y|0)♪ It's not what's right, it's just a long version that didn't like, and I thought maybe we could cut it down.