Skip to content

Commit

Permalink
Merge pull request #79 from LMcAlpine/redo-rendering
Browse files Browse the repository at this point in the history
code cleanup
  • Loading branch information
LMcAlpine authored Mar 24, 2024
2 parents ac788af + e7dbe76 commit 7f6c3e6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 970 deletions.
39 changes: 39 additions & 0 deletions canvasutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,42 @@ function getXToAngle(x) {
function areCloseEnough(a, b, epsilon = 1e-6) {
return Math.abs(a - b) < epsilon;
}
function playerDistToScreen(screenWidth) {
return screenWidth / 2.0 / Math.tan(degreesToRadians(45));
}

function screenToXView(x, screenWidth) {
return Math.atan((screenWidth / 2.0 - x) / playerDistToScreen(screenWidth));
}

function scaleFromViewAngle(visangle, realWallNormalAngle, realWallDistance, viewangle, screenwidth) {


let anglea = new Angle(RIGHT_ANGLE_DEGREES + (visangle - viewangle));
let angleb = new Angle(RIGHT_ANGLE_DEGREES + (visangle - realWallNormalAngle));

let sinea = Math.sin(degreesToRadians(anglea.angle));
let sineb = Math.sin(degreesToRadians(angleb.angle));

let p = screenwidth / 2.0;
let num = p * sineb;
let den = realWallDistance * sinea;
return num / den;

}


function isPowerOfTwo(number) {
return (number & (number - 1)) === 0;
}

function adjustColorComponent(component, lightLevel) {
return Math.min(255, Math.max(0, Math.floor(component * lightLevel)));

}


// flats
// function adjustColorComponent(color, lightLevel) {
// return Math.min(255, Math.floor(color * lightLevel));
// }
6 changes: 3 additions & 3 deletions player.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class Player {
* Method to update the state of the player for each frame.
*/
update() {
console.log("x: " + this.x);
console.log("y ;" + this.y);
console.log("angle: " + this.direction.angle);
// console.log("x: " + this.x);
// console.log("y ;" + this.y);
// console.log("angle: " + this.direction.angle);
const multiplier = 550;
const magRotation = 0.1875 * multiplier;

Expand Down
39 changes: 1 addition & 38 deletions render/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Canvas {
this.offScreenWidth = 640;
this.offScreenHeight = 400;
this.offScreenCtx = this.offScreenCanvas.getContext("2d");
this.offScreenCtx = this.ctx;
// this.offScreenCtx = this.ctx;

// this.offScreenBuffer = this.offscreenCtx.getImageData(0, 0, this.offscreenWidth, this.offScreenHeight);

Expand All @@ -39,43 +39,6 @@ class Canvas {

}

drawWallCol(
offscreenCtx,
entireTextureData,
textureColumn,
x,
y1,
y2,
textureAlt,
invScale,
lightLevel, textureWidth, textureHeight, wallWidth, largeImageData, startX
) {
if (y1 < y2) {


textureColumn = Math.trunc(textureColumn) % textureWidth;



let textureY = textureAlt + (y1 - HALFHEIGHT) * invScale;

for (let i = 0; i < y2; i++) {

const texY = Math.trunc(textureY) % textureHeight;
const texPos = (texY * textureWidth + textureColumn) * 4;

let index = (i * wallWidth + (x - startX)) * 4;
largeImageData.data[index] = entireTextureData[texPos] * lightLevel;
largeImageData.data[index + 1] = entireTextureData[texPos + 1] * lightLevel;
largeImageData.data[index + 2] = entireTextureData[texPos + 2] * lightLevel;
largeImageData.data[index + 3] = 255;

textureY += invScale;
}

}
}


updateCanvas() {

Expand Down
Loading

0 comments on commit 7f6c3e6

Please sign in to comment.