Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LabDemo2Js (Beta) :: "Render Floor Textures"
- // Function to map screen pixels in the floor to board coordinates and texture pixels
- function renderFloorTextures(canvas) {
- if (!canvas || !(canvas instanceof HTMLCanvasElement)) {
- console.error('Invalid canvas element.');
- return;
- }
- const context = canvas.getContext('2d');
- const cosang = Math.cos(player.angle);
- const sinang = Math.sin(player.angle);
- const rayDirX0 = cosang - sinang;
- const rayDirY0 = sinang + cosang;
- const rayDirX1 = cosang + sinang;
- const rayDirY1 = sinang - cosang;
- for (let sy = YDIM / 2; sy < YDIM; sy++) {
- for (let sx = 0; sx < XDIM; sx++) {
- const correctedY = sy - YDIM / 2;
- const dist = player.z / correctedY;
- const screenX = 2 * sx / XDIM - 1;
- const rayDirX = rayDirX0 + screenX * (rayDirX1 - rayDirX0);
- const rayDirY = rayDirY0 + screenX * (rayDirY1 - rayDirY0);
- const floorX = player.x + dist * rayDirX;
- const floorY = player.y + dist * rayDirY;
- const boardX = Math.floor(floorX);
- const boardY = Math.floor(floorY);
- context.fillStyle = checkerBoardTile(boardX,boardY);
- context.fillRect(sx, sy, 1, 1);
- /*
- if (board[boardX] && board[boardX][boardY] && board[boardX][boardY][1]) {
- const mockKey = board[boardX][boardY][1];
- if (mockData[mockKey] && mockData[mockKey].variants[YDIM]) {
- const mock = mockData[mockKey].variants[YDIM];
- if (mock.complete) {
- const textureX = Math.floor((floorX - boardX) * mock.width) % mock.width;
- const textureY = Math.floor((floorY - boardY) * mock.height) % mock.height;
- context.drawImage(mock, textureX, textureY, 1, 1, sx, sy, 1, 1);
- } else {
- context.fillStyle = 'rgba(0, 0, 0, 0.5)';
- context.fillRect(sx, sy, 1, 1);
- }
- }
- }
- */
- }
- }
- }
- function checkerBoardTile ( x, y ) {
- switch ((x ^ y ^ 1) & 1) {
- case 0:
- return `rgba(0, 0, 0, 1.0)`; // Black
- default:
- return `rgba(255, 255, 255, 1.0)`; // White
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement