Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LabDemo2Js (Beta 3 A) :: "Raytrace/Calculate Floors 2.js"
- var plotRaw = [];
- var plotTransform = [];
- /*
- // Initialize the previous state, player, XDIM, YDIM, and board if not done elsewhere
- var previous = [null, 0, 0]; // Example initialization, adjust as necessary
- var player = { angle: 0 }; // Example player object, adjust as necessary
- var XDIM = 800; // Example width, adjust as necessary
- var YDIM = 600; // Example height, adjust as necessary
- var board = []; // Example board initialization, fill with your data
- */
- function floorMain(canvas) {
- if ( plotRaw === [] ) calculateStaticFloorMap();
- if (previous !== [ player, XDIM, YDIM ]) {
- if (RAYCAST_DEBUG) console.log('rotateFloor');
- rotateFloor();
- }
- if (RAYCAST_DEBUG) console.log('renderFloor');
- renderFloor(canvas);
- }
- function calculateStaticFloorMap() {
- plotRaw = [];
- // Check for valid YDIM value
- if (YDIM <= 0) {
- console.error('Invalid YDIM value:', YDIM);
- return; // Exit early if YDIM is not valid
- }
- for (let sx = 0; sx < XDIM; sx++) {
- plotRaw[sx] = [];
- // Iterate from the horizon (YDIM / 2) to the bottom of the window (YDIM)
- for (let sy = YDIM / 2; sy < YDIM; sy++) {
- plotRaw[sx][sy] = {};
- // Calculate distance from the camera plane
- let dist = YDIM - sy; // Distance below the camera plane
- let angle = (XDIM / 2 - sx) * (30 / 360) * (Math.PI * 2);
- plotRaw[sx][sy] = { x: angle, y: dist };
- }
- }
- }
- function rotateFloor() {
- let radians = player.angle;
- let cos = Math.cos(radians);
- let sin = Math.sin(radians);
- plotTransform = [];
- for (let sx = 0; sx < XDIM; sx++) {
- plotTransform[sx] = [];
- for (let sy = YDIM / 2; sy < YDIM; sy++) {
- plotTransform[sx][sy] = {};
- let point = plotRaw[sx][sy];
- let xNew = point.x * cos - point.y * sin;
- let yNew = point.x * sin + point.y * cos;
- plotTransform[sx][sy] = { x: xNew, y: yNew };
- }
- }
- }
- function renderFloor(canvas) {
- if (!canvas) {
- console.error(`Missing canvas element (${arguments.callee.name})`);
- return;
- }
- if (!(canvas instanceof HTMLCanvasElement)) {
- console.error(`Invalid canvas element passed to ${arguments.callee.name}:`, canvas);
- return;
- }
- let mock;
- let context = canvas.getContext('2d');
- context.clearRect(0, 0, canvas.width, canvas.height); // Clear previous frame
- for (let sx = 0; sx < XDIM; sx++) {
- for (let sy = YDIM / 2; sy < YDIM; sy++) {
- let [x, y] = [plotTransform[sx][sy].x, plotTransform[sx][sy].y];
- let wallCheck = isWall(x, y);
- switch (wallCheck) {
- case null: // out of bounds
- mock = isMock(`/GFX/stonwall`);
- break;
- case true: // wall or barrier
- mock = isMock(board[px][py][1]);
- break;
- case false: // empty floor
- mock = isMock(board[px][py][1]);
- break;
- }
- if (isMock(mock) !== false) {
- context.drawImage(
- mock,
- (x % 1) * mock.width,
- (y % 1) * mock.height,
- 1,
- 1,
- sx,
- sy,
- 1,
- 1
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement