Advertisement
Xelostar

3D Rendering Demo v0.3

Jun 17th, 2016
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. os.loadAPI("ThreeD")
  2.  
  3. objects = {
  4.   {oType = "frame", x = 4, y = 0, z = 0, width = 1, height = 1, length = 1, color = colors.blue},
  5.   {oType = "flip", x = 4, y = 0, z = 4, width = 1, height = 1, color = colors.blue},
  6. }
  7.  
  8. playerX = -10
  9. playerY = 0
  10. playerZ = 0
  11. playerSpeed = 0.5
  12. playerDirectionHor = 0
  13. playerDirectionVer = 0
  14.  
  15. screenWidth = term.getSize()
  16. screenHeight = 19
  17. groundColor = colors.lime
  18. FoV = 140
  19.  
  20. ThreeDFrame = ThreeD.newFrame(1, 1, screenWidth, screenHeight, FoV, playerX, playerY, playerZ, playerDirectionVer, playerDirectionHor, colors.lime)
  21.  
  22. function degreeToRadian(degree)
  23.   return degree * math.pi / 180
  24. end
  25.  
  26. function drawObjects()
  27.   local objectsToSort = textutils.unserialize(textutils.serialize(objects))
  28.   local sortedObjects = {}
  29.  
  30.   while (table.getn(objectsToSort) > 0) do
  31.     local biggestDistance = 0
  32.     local biggestDistanceNr = 0
  33.  
  34.     for objNr, object in pairs(objectsToSort) do
  35.       local distance = ((math.abs(playerX - object.x)) ^ 2 + (math.abs(playerZ - object.z)) ^ 2) ^ 0.5
  36.  
  37.       if (distance > biggestDistance) then
  38.         biggestDistance = distance
  39.         biggestDistanceNr = objNr
  40.       end
  41.     end
  42.  
  43.     table.insert(sortedObjects, objectsToSort[biggestDistanceNr])
  44.     table.remove(objectsToSort, biggestDistanceNr)
  45.   end
  46.  
  47.   term.setBackgroundColor(colors.white)
  48.   term.clear()
  49.   ThreeDFrame:drawGround()
  50.  
  51.   for objNr, object in pairs(sortedObjects) do
  52.     ThreeDFrame:drawObject(object)
  53.   end
  54. end
  55.  
  56. function drawScreen()
  57.   drawObjects()
  58. end
  59.  
  60. function inputPlayer()
  61.   local sEvent, key = os.pullEventRaw()
  62.   if (sEvent == "key") then
  63.     local dX = 0
  64.     local dY = 0
  65.     local dZ = 0
  66.  
  67.     if (key == keys.left) then
  68.       playerDirectionHor = (playerDirectionHor - 10)
  69.       if (playerDirectionHor <= -180) then
  70.         playerDirectionHor = playerDirectionHor + 360
  71.       end
  72.     elseif (key == keys.right) then
  73.       playerDirectionHor = (playerDirectionHor + 10)
  74.       if (playerDirectionHor >= 180) then
  75.         playerDirectionHor = playerDirectionHor - 360
  76.       end
  77.     elseif (key == keys.down) then
  78.       playerDirectionVer = (playerDirectionVer - 10)
  79.       if (playerDirectionVer <= -180) then
  80.         playerDirectionVer = playerDirectionVer + 360
  81.       end
  82.     elseif (key == keys.up) then
  83.       playerDirectionVer = (playerDirectionVer + 10)
  84.       if (playerDirectionVer >= 180) then
  85.         playerDirectionVer = playerDirectionVer - 360
  86.       end
  87.     elseif (key == keys.space) then
  88.       dY = playerSpeed
  89.     elseif (key == keys.leftShift) then
  90.       dY = -playerSpeed
  91.     elseif (key == keys.w) then
  92.       dX = playerSpeed * math.cos(degreeToRadian(playerDirectionHor))
  93.       dZ = playerSpeed * math.sin(degreeToRadian(playerDirectionHor))
  94.     elseif (key == keys.s) then
  95.       dX = -playerSpeed * math.cos(degreeToRadian(playerDirectionHor))
  96.       dZ = -playerSpeed * math.sin(degreeToRadian(playerDirectionHor))
  97.     elseif (key == keys.a) then
  98.       dX = playerSpeed * math.cos(degreeToRadian(playerDirectionHor - 90))
  99.       dZ = playerSpeed * math.sin(degreeToRadian(playerDirectionHor - 90))
  100.     elseif (key == keys.d) then
  101.       dX = playerSpeed * math.cos(degreeToRadian(playerDirectionHor + 90))
  102.       dZ = playerSpeed * math.sin(degreeToRadian(playerDirectionHor + 90))
  103.     end
  104.  
  105.     if (dX ~= nil) then
  106.       playerX = playerX + dX
  107.     end
  108.     if (dY ~= nil) then
  109.       playerY = playerY + dY
  110.     end
  111.     if (dZ ~= nil) then
  112.       playerZ = playerZ + dZ
  113.     end
  114.  
  115.       ThreeDFrame.playerX = playerX
  116.       ThreeDFrame.playerY = playerY
  117.       ThreeDFrame.playerZ = playerZ
  118.  
  119.       ThreeDFrame.playerDirectionHor = playerDirectionHor
  120.       ThreeDFrame.playerDirectionVer = playerDirectionVer
  121.   end
  122. end
  123.  
  124. while true do
  125.   inputPlayer()
  126.   drawScreen()
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement