@""""""""""""""""""""""""""""""""ààààààààÿÿÿÿÿÿÿÿîîîîîîîîÌÌÌÌÌÌÌÌro >ü;""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""» """"""""»» """"""""»»»»""""""""°»»»»""""""""°»»»»» """"""""°»»»»»» """"""""°»»»»»»»»""""""""»»»»»»»»»""""""""»»»»»»»»»» """"""""""""""""""""""""""""""""""""""""»»»»»»»»»»» """"""""""""""""""""""""""""""""""""""""»»»»»»»»»»»»»""""""""""""""""""""""""""""""""""""""""°»»»»»»»»»»»»» """"""""""""""""""""""""""""""""""""""""°»»»»»»»»»»»»»» """"""""""""""""""""""""""""""""""""""""°»»»»»»»»»»»»»»»»""""""""""""""""""""""""""""""""""""""""»»»»»»»»»»»»»»»»»""""""""""""""""""""""""""""""""""""""""»»»»»»»»»»»»»»»»»» """"""""""""""""""""""""""""""""""""""""»»»»»»»»»»»»»»»»»»» """"""""""""»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»» """""""""""²»° »»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»» """"""""""""»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»°»»»»»»»»»»»»»»»»»»»»»» """""""""""² »»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²°»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""" »»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»°»»»»»»»»»»»»»»»»»»»»»»»» """"""""""""°»»»»»»»»»»»»»»»»»»»»»»»»»» """"""""""""»»»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²» »»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»» »»»»»»»»»»»»»»»»»»»»»»»»» """""""""""" »»»»»»»»»»»»»»»»»»»»»»»»»»»»""""""""""""»»»°»»»»»»»»»»»»»»»»»»»»»»»» """""""""""² »»»»»»»»»»»»»»»»»»»»»»»»»»»» """""""""""²»»»°»»»»»»»»»»»»»»»»»»»»»»»» »""""""""""""»°»»»»»»»»»»»»»»»»»»»»»»»»»»»»"""""""""""²»» »»»»»»»»»»»»»»»»»»»»»»»»»»° """"""""""""»°»»» »»»»»»»»»»»»»»»»»»»»°»»» """"""""""""»» »»»»»»»»»»»»»»»»»»»»»»»»»»°»"""""""""""² »»»°»»»»»»»»»»»»»»»»»»»»»» »»»""""""""""""»»°»»»°»»»»»»»»»»»»»»»»»» »»» » """""""""""²»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""-- title: raycaster -- author: MESYETI -- desc: short description -- site: https://mesyeti.uk -- license: GPL-3.0-or-later -- version: 0.1 -- script: lua local pi = math.pi; local sin = math.sin; local cos = math.cos; local ceil = math.ceil; local maxRayDistance = 300; local FOV = 60; local enable2D = false; --[[ -- game data ]] local player; local rays = {}; --[[ -- util functions ]] function newEntity(p_x, p_y, p_direction) return { x = p_x, y = p_y, direction = p_direction }; end function newRay(p_parent, p_offset) if (p_parent == nil) then trace("WARNING: nil passed as parent to newRay"); return; end return { parent = p_parent, offset = p_offset, distance = 0 }; end function Vec2(p_x, p_y) return { x = p_x, y = p_y }; end function degreesToRadians(degrees) return degrees * (pi / 180); end function moveInDirection(distance, entity) return Vec2( (cos(degreesToRadians(entity.direction)) * distance) + entity.x, (sin(degreesToRadians(entity.direction)) * distance) + entity.y ); end function moveInDirection2(distance, direction) return Vec2( (cos(degreesToRadians(direction)) * distance), (sin(degreesToRadians(direction)) * distance) ); end function collide(x, y) return mget(x // 8, y // 8) == 1; end function raycast(ray, index, i) local distance = 0; ray.x = ray.parent.x; ray.y = ray.parent.y; repeat distance = distance + 1; local update = moveInDirection2(1, ray.parent.direction + index); ray.x = ray.x + update.x; ray.y = ray.y + update.y; until collide(ray.x, ray.y) or (distance > maxRayDistance); --[[until ((distance > maxRayDistance) or (mget(ray.x // 8, ray.y // 8) ~= 0) or (mget(ceil(ray.x / 8), ceil(ray.y / 8)) ~= 0))]] --ray.distance = distance; ray.distance = distance * cos(degreesToRadians(i - player.direction)); return ray; end --[[ -- game functions ]] function updatePlayerPosition(update) player.x = update.x; player.y = update.y; end function updatePlayer() local BUTTON_UP = 0; local BUTTON_DOWN = 1; local BUTTON_LEFT = 2; local BUTTON_RIGHT = 3; local BUTTON_A = 4; if (btn(BUTTON_UP)) then updatePlayerPosition( moveInDirection(1, player) ); end if (btn(BUTTON_DOWN)) then updatePlayerPosition( moveInDirection(-1, player) ); end if (btn(BUTTON_RIGHT)) then player.direction = player.direction + 5; end if (btn(BUTTON_LEFT)) then player.direction = player.direction - 5; end enable2D = btn(BUTTON_A); end function render() cls(0); if (enable2D) then map(); -- draw player pointer thingy local ahead = moveInDirection(10, player); line(player.x, player.y, ahead.x, ahead.y, 13); end -- poggers raycasting for i = 1, #rays do rays[i].parent = player; rays[i] = raycast(rays[i], i - (FOV / 2), player.direction + (i - FOV/2)); if (enable2D) then local rayAhead = moveInDirection2(rays[i].distance, player.direction + (i - FOV/2)); line(player.x, player.y, player.x + rayAhead.x, player.y + rayAhead.y, 11); else local x = (ceil(240 / #rays)) * i; local w = ceil(240 / #rays); local y = 136 / 2; y = y - 136 / rays[i].distance * 10; local colour = 12; if (rays[i].distance > 100) then colour = 15; elseif (rays[i].distance > 50) then colour = 14; elseif (rays[i].distance > 25) then colour = 13; elseif (rays[i].distance > 5) then colour = 12; else end local h = 136 - y * 2 rect(x, y, w, h, colour); if (i ~= #rays) then -- tfw your attempt at smoothening out walls turns into floor shading local y2 = 136 / 2; y2 = y2 - 136 / rays[i + 1].distance * 10; tri(x, y, x + w, y + y2, x + w, y, colour); --tri(x, y + h, x + w, 136 - (y + y2), x + w, 136 - y, colour); end --clip(x, y, w, h); --map(180, 103, 30, 17, 0, 0, -1, h / 20); --clip(); end end --pix(player.x, player.y, 4); -- render player end --[[ -- TIC functions ]] local wasInit = false; function BOOT() player = newEntity(48, 64, 90); for i = 0, 90 do rays[i] = newRay(player, i); end wasInit = true; end function TIC() local startTime = time(); if (not wasInit) then return; end updatePlayer(); render(); local frameTime = time() - startTime; print("FPS: " .. (1 // (frameTime / 1000)), 2, 2, 4); end