Advertisement
jargon

// LabDemo2Js (Beta 3 A) :: "Raytrace/Calculate Floors 2.js"

Oct 5th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LabDemo2Js (Beta 3 A) :: "Raytrace/Calculate Floors 2.js"
  2.  
  3. var plotRaw = [];
  4. var plotTransform = [];
  5.  
  6. /*
  7. // Initialize the previous state, player, XDIM, YDIM, and board if not done elsewhere
  8. var previous = [null, 0, 0]; // Example initialization, adjust as necessary
  9. var player = { angle: 0 }; // Example player object, adjust as necessary
  10. var XDIM = 800; // Example width, adjust as necessary
  11. var YDIM = 600; // Example height, adjust as necessary
  12. var board = []; // Example board initialization, fill with your data
  13. */
  14.  
  15. function floorMain(canvas) {
  16.    
  17.     if ( plotRaw === [] ) calculateStaticFloorMap();
  18.    
  19.     if (previous !== [ player, XDIM, YDIM ]) {
  20.         if (RAYCAST_DEBUG) console.log('rotateFloor');
  21.         rotateFloor();
  22.     }
  23.     if (RAYCAST_DEBUG) console.log('renderFloor');
  24.     renderFloor(canvas);
  25. }
  26.  
  27. function calculateStaticFloorMap() {
  28.    
  29.     plotRaw = [];
  30.  
  31.     // Check for valid YDIM value
  32.     if (YDIM <= 0) {
  33.         console.error('Invalid YDIM value:', YDIM);
  34.         return; // Exit early if YDIM is not valid
  35.     }
  36.  
  37.     for (let sx = 0; sx < XDIM; sx++) {
  38.         plotRaw[sx] = [];
  39.  
  40.         // Iterate from the horizon (YDIM / 2) to the bottom of the window (YDIM)
  41.         for (let sy = YDIM / 2; sy < YDIM; sy++) {
  42.             plotRaw[sx][sy] = {};
  43.  
  44.             // Calculate distance from the camera plane
  45.             let dist = YDIM - sy; // Distance below the camera plane
  46.             let angle = (XDIM / 2 - sx) * (30 / 360) * (Math.PI * 2);
  47.  
  48.             plotRaw[sx][sy] = { x: angle, y: dist };
  49.         }
  50.     }
  51. }
  52.  
  53. function rotateFloor() {
  54.     let radians = player.angle;
  55.     let cos = Math.cos(radians);
  56.     let sin = Math.sin(radians);
  57.  
  58.     plotTransform = [];
  59.  
  60.     for (let sx = 0; sx < XDIM; sx++) {
  61.         plotTransform[sx] = [];
  62.  
  63.         for (let sy = YDIM / 2; sy < YDIM; sy++) {
  64.             plotTransform[sx][sy] = {};
  65.             let point = plotRaw[sx][sy];
  66.  
  67.             let xNew = point.x * cos - point.y * sin;
  68.             let yNew = point.x * sin + point.y * cos;
  69.  
  70.             plotTransform[sx][sy] = { x: xNew, y: yNew };
  71.         }
  72.     }
  73. }
  74.  
  75. function renderFloor(canvas) {
  76.     if (!canvas) {
  77.         console.error(`Missing canvas element (${arguments.callee.name})`);
  78.         return;
  79.     }
  80.     if (!(canvas instanceof HTMLCanvasElement)) {
  81.         console.error(`Invalid canvas element passed to ${arguments.callee.name}:`, canvas);
  82.         return;
  83.     }
  84.  
  85.     let mock;
  86.     let context = canvas.getContext('2d');
  87.     context.clearRect(0, 0, canvas.width, canvas.height); // Clear previous frame
  88.  
  89.     for (let sx = 0; sx < XDIM; sx++) {
  90.         for (let sy = YDIM / 2; sy < YDIM; sy++) {
  91.             let [x, y] = [plotTransform[sx][sy].x, plotTransform[sx][sy].y];
  92.             let wallCheck = isWall(x, y);
  93.  
  94.             switch (wallCheck) {
  95.                 case null: // out of bounds
  96.                     mock = isMock(`/GFX/stonwall`);
  97.                     break;
  98.                 case true: // wall or barrier
  99.                     mock = isMock(board[px][py][1]);
  100.                     break;
  101.                 case false: // empty floor
  102.                     mock = isMock(board[px][py][1]);
  103.                     break;
  104.             }
  105.  
  106.             if (isMock(mock) !== false) {
  107.                 context.drawImage(
  108.                     mock,
  109.                     (x % 1) * mock.width,
  110.                     (y % 1) * mock.height,
  111.                     1,
  112.                     1,
  113.                     sx,
  114.                     sy,
  115.                     1,
  116.                     1
  117.                 );
  118.             }
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement