Advertisement
Guest User

ray

a guest
Feb 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. dir = fs.getDir(shell.getRunningProgram()).."/"
  2. blittleEnabled = true
  3. function fillBox(x1,y1,x2,y2,col)
  4.   win.setBackgroundColor(col)
  5.   for x=x1,x2 do
  6.     for y=y1,y2 do
  7.       win.setCursorPos(x,y)
  8.       win.write(" ")
  9.     end
  10.   end
  11. end
  12. if not fs.exists(dir.."blittle") then
  13.   shell.run("pastebin","get","ujchRSnU",dir.."blittle")
  14. end
  15. os.loadAPI(dir.."blittle")
  16. level = {}
  17. local native = term.native()
  18. nX,nY = term.getSize()
  19. if blittleEnabled then
  20.   win = (blittle.createWindow(term.current(),1,1,nX,nY,true))
  21. end
  22. function math.round(nNum)
  23.   return math.floor(nNum+0.5)
  24. end
  25. function loadLevel(path)
  26.   file = {}
  27.   f = fs.open(dir..path,"r")
  28.   line = true
  29.   while line do
  30.     line = f.readLine()
  31.     file[#file+1] = line
  32.   end
  33.   f.close()
  34.   level = {}
  35.   for i=1,#file[1] do
  36.     level[i] = {}
  37.   end
  38.   for y=1,#file do
  39.     for x=1,#file[y] do
  40.       char = string.sub(file[y],x,x)
  41.       if char == "0" then
  42.         char = colors.white
  43.       elseif char == "1" then
  44.         char = colors.orange
  45.       elseif char == "2" then
  46.         char = colors.magenta
  47.       elseif char == "3" then
  48.         char = colors.lightBlue
  49.       elseif char == "4" then
  50.         char = colors.yellow
  51.       elseif char == "5" then
  52.         char = colors.lime
  53.       elseif char == "6" then
  54.         char = colors.pink
  55.       elseif char == "7" then
  56.         char = colors.gray
  57.       elseif char == "8" then
  58.         char = colors.lightGray
  59.       elseif char == "9" then
  60.         char = colors.cyan
  61.       elseif char == "a" then
  62.         char = colors.purple
  63.       elseif char == "b" then
  64.         char = colors.blue
  65.       elseif char == "c" then
  66.         char = colors.brown
  67.       elseif char == "d" then
  68.         char = colors.green
  69.       elseif char == "e" then
  70.         char = colors.red
  71.       elseif char == "f" then
  72.         char = colors.black
  73.       elseif char == " " then
  74.         char = false
  75.       end
  76.       level[x][y] = char
  77.     end
  78.   end
  79. end
  80. loadLevel("/level1")
  81. xPos = 3
  82. yPos = 3
  83. yaw = 0
  84. rayInc = 1
  85. rayDist = 100
  86. w,h = win.getSize()
  87. rayDirections = {}
  88. yawOffset = 0
  89. for i=0,90 do
  90.   rayDirections[i] = {
  91.     [1] = (0)+(i/90),
  92.     [2] = (-1)+(i/90),
  93.   }
  94. end
  95. for i=90,180 do
  96.   rayDirections[i] = {
  97.     [1] = (1)-((i-90)/90),
  98.     [2] = (0)+((i-90)/90),
  99.   }
  100. end
  101. for i=180,270 do
  102.   rayDirections[i] = {
  103.     [1] = (0)-((i-180)/90),
  104.     [2] = (1)-((i-180)/90),
  105.   }
  106. end
  107. for i=270,360 do
  108.   rayDirections[i] = {
  109.     [1] = (-1)+((i-270)/90),
  110.     [2] = (0)-((i-270)/90),
  111.   }
  112. end
  113. rayShadow = true
  114. function castRay(rayYaw)
  115.   while rayYaw > 360 do
  116.     rayYaw = rayYaw - 360
  117.   end
  118.   while rayYaw < 0 do
  119.     rayYaw = rayYaw + 360
  120.   end
  121.   rayX = xPos
  122.   rayY = yPos
  123.   rayYaw = math.round(rayYaw)
  124.   for len=1,rayDist do
  125.     rayY = rayY + (rayDirections[rayYaw][2]/10)
  126.     rayX = rayX + (rayDirections[rayYaw][1]/10)
  127.     if rayShadow then
  128.       if not rayPixels[math.round(rayX)] then
  129.         rayPixels[math.round(rayX)] = {}
  130.       end
  131.       if math.round(rayX) > 0 and math.round(rayX) <= #level and math.round(rayY) > 0 and math.round(rayY) <= #level then
  132.         if level[math.round(rayX)][math.round(rayY)] then
  133.           rayPixels[math.round(rayX)][math.round(rayY)] = level[math.round(rayX)][math.round(rayY)]
  134.         else
  135.           rayPixels[math.round(rayX)][math.round(rayY)] = colors.white
  136.         end
  137.       end
  138.     end
  139.     --rayY = rayY + (rayDirections[rayYaw][2]/10)
  140.     --rayX = rayX + (rayDirections[rayYaw][1]/10)
  141.     if math.round(rayX) > 0 and math.round(rayX) <= #level and math.round(rayY) > 0 and math.round(rayY) <= #level then
  142.       if level[math.round(rayX)][math.round(rayY)] then
  143.         return len,level[math.round(rayX)][math.round(rayY)]
  144.       end
  145.     end
  146.   end
  147.   return false
  148. end
  149. xRaw = 2
  150. yRaw = 2
  151. h2 = math.round(h/2)
  152. fovMultiplier = 1
  153. fov = w/fovMultiplier
  154. function redraw()
  155.   win.setVisible(false)
  156.   xPos = math.round(xRaw*5)/5
  157.   yPos = math.round(yRaw*5)/5
  158.   rayPixels = {}
  159.   xDraw = 0
  160.   win.setBackgroundColor(colors.white)
  161.   win.clear()
  162.   fillBox(1,h/2,w,h+1,colors.lightGray)
  163.   for i=yaw-(fov/2),yaw+(fov/2),fovMultiplier do
  164.     size,col = castRay(math.round(i))
  165.     if size then
  166.       if size > (h*2) then
  167.         size = h*2
  168.       end
  169.       size = (h/2)-(size/6)
  170.       fillBox(xDraw,(h/2)-size,xDraw+(fovMultiplier-1),(h/2)+size,col)
  171.     end
  172.     xDraw = xDraw + fovMultiplier
  173.   end
  174.   win.setVisible(true)
  175.   for x=1,#level do
  176.     for y=1,#level[x] do
  177.       if level[x][y] then
  178.         paintutils.drawPixel(x,y,level[x][y])
  179.         if rayPixels[x] then
  180.           if rayPixels[x][y] then
  181.             term.setBackgroundColor(colors.lightGray)
  182.             term.setTextColor(rayPixels[x][y])
  183.             term.setCursorPos(x,y)
  184.             write(".")
  185.           end
  186.         end
  187.       else
  188.         paintutils.drawPixel(x,y,colors.gray)
  189.         if rayPixels[x] then
  190.           if rayPixels[x][y] then
  191.             term.setTextColor(rayPixels[x][y])
  192.             term.setCursorPos(x,y)
  193.             write(".")
  194.           end
  195.         end
  196.       end
  197.     end
  198.   end
  199.   term.setBackgroundColor(colors.lightBlue)
  200.   term.setCursorPos(xPos,yPos)
  201.   term.setTextColor(colors.white)
  202.   pointYaw = yaw + 45
  203.   if pointYaw >= 0 and pointYaw < 90 then
  204.     write("^")
  205.   elseif pointYaw >= 90 and pointYaw < 180 then
  206.     write(">")
  207.   elseif pointYaw >= 180 and pointYaw < 270 then
  208.     write("v")
  209.   elseif pointYaw >= 270 and pointYaw < 450 then
  210.     write("<")
  211.   end
  212. end
  213. while true do
  214. if yaw > 360 then
  215.   yaw = 0
  216. elseif yaw < 0 then
  217.   yaw = 360
  218. end
  219. redraw()
  220. term.setCursorPos(1,h)
  221. term.setBackgroundColor(colors.lightGray)
  222. write(tostring(yaw))
  223. e,k = os.pullEvent("key")
  224. if k == keys.left then
  225.   yaw = yaw - 10
  226. elseif k == keys.right then
  227.   yaw = yaw + 10
  228. elseif k == keys.w then
  229.   yRaw = yRaw + rayDirections[yaw][2]/5
  230.   xRaw = xRaw + rayDirections[yaw][1]/5
  231. elseif k == keys.s then
  232.   yRaw = yRaw - rayDirections[yaw][2]/5
  233.   xRaw = xRaw - rayDirections[yaw][1]/5
  234. elseif k == keys.a then
  235.   if yaw > 270 then
  236.     yRaw = yRaw + rayDirections[yaw-90][2]/5
  237.     xRaw = xRaw + rayDirections[yaw-90][1]/5
  238.   else
  239.     yRaw = yRaw - rayDirections[yaw+90][2]/5
  240.     xRaw = xRaw - rayDirections[yaw+90][1]/5
  241.   end
  242. elseif k == keys.d then
  243.   if yaw > 270 then
  244.     yRaw = yRaw - rayDirections[yaw-90][2]/5
  245.     xRaw = xRaw - rayDirections[yaw-90][1]/5
  246.   else
  247.     yRaw = yRaw + rayDirections[yaw+90][2]/5
  248.     xRaw = xRaw + rayDirections[yaw+90][1]/5
  249.   end
  250. elseif k == keys.up then
  251.   yaw = yaw - 1
  252. elseif k == keys.down then
  253.   yaw = yaw + 1
  254. elseif k == keys.enter then
  255.   break
  256. end
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement