Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function drawLine(x, y, width)
- local blank = ""
- for i = 0, width do
- blank = blank.." "
- end
- term.setCursorPos(x, y)
- write(blank)
- end
- function drawSquare(x1, y1, x2, y2, color)
- term.setBackgroundColor(color)
- for i = y1, y2 do
- drawLine(x1, i, x2 - x1)
- end
- end
- function radianToDegree(radian)
- return 180 * radian / math.pi
- end
- function newFrame(x1, y1, x2, y2, FoV, playerX, playerY, playerZ, playerDirectionVer, playerDirectionHor, groundColor)
- frame = {}
- frame.frameX1 = x1
- frame.frameY1 = y1
- frame.frameX2 = x2
- frame.frameY2 = y2
- frame.groundColor = groundColor
- frame.FoV = FoV
- frame.playerDirectionVer = playerDirectionVer
- frame.playerDirectionHor = playerDirectionHor
- frame.playerX = playerX
- frame.playerY = playerY
- frame.playerZ = playerZ
- function frame:findPointOnScreen(oX, oY, oZ)
- local dX = math.abs(oX - frame.playerX + 0.01)
- local dY = math.abs(oY - frame.playerY)
- local dZ = math.abs(oZ - frame.playerZ + 0.01)
- -----------------------------------------------------Horizontal rotation
- local way = radianToDegree(math.atan(dZ / dX))
- if (oZ - frame.playerZ + 0.01 < 0) then
- if (oX - frame.playerX + 0.01 < 0) then
- way = way - 180
- else
- way = -way
- end
- else
- if (oX - frame.playerX + 0.01 < 0) then
- way = -way + 180
- else
- way = way
- end
- end
- local direction = way - frame.playerDirectionHor
- if (direction > 180) then
- direction = direction - 360
- elseif (direction < -180) then
- direction = direction + 360
- end
- -----------------------------------------------------Vertical rotation
- local distance = ((dZ) ^ 2 + (dX) ^ 2) ^ 0.5
- local way2 = radianToDegree(math.atan(dY / distance))
- if (distance < 0) then
- if (oY - frame.playerY + 0.01 < 0) then
- way2 = way2 - 180
- else
- way2 = -way2 + 180
- end
- else
- if (oY - frame.playerY + 0.01 < 0) then
- way2 = -way2
- else
- way2 = way2
- end
- end
- local direction2 = way2 - frame.playerDirectionVer
- if (direction2 > 180) then
- direction2 = direction2 - 360
- elseif (direction < -180) then
- direction2 = direction2 + 360
- end
- -----------------------------------------------------Calculating and returning coordinates
- local x = direction * (frame.frameX2 / 2) / (FoV / 2) + math.floor(frame.frameX2 / 2) + 2
- local y = -direction2 * (frame.frameY2 / 2) / (FoV * frame.frameY2 / 2 / frame.frameX2) + math.floor(frame.frameY2 / 2) --+ 2
- return {x = x, y = y}
- end
- function frame:drawObject(object)
- if (object.oType == "frame") then
- local points = {
- frame:findPointOnScreen(object.x, object.y + 0.5, object.z),
- frame:findPointOnScreen(object.x, object.y + 0.5, object.z - object.width),
- frame:findPointOnScreen(object.x, object.y + 0.5 - object.height, object.z),
- frame:findPointOnScreen(object.x, object.y + 0.5 - object.height, object.z - object.width),
- frame:findPointOnScreen(object.x - object.length, object.y + 0.5, object.z),
- frame:findPointOnScreen(object.x - object.length, object.y + 0.5, object.z - object.width),
- frame:findPointOnScreen(object.x - object.length, object.y + 0.5 - object.height, object.z),
- frame:findPointOnScreen(object.x - object.length, object.y + 0.5 - object.height, object.z - object.width),}
- local linesToDraw = {
- {x = points[1].x, y = points[1].y, x2 = points[5].x, y2 = points[5].y},
- {x = points[2].x, y = points[2].y, x2 = points[6].x, y2 = points[6].y},
- {x = points[3].x, y = points[3].y, x2 = points[7].x, y2 = points[7].y},
- {x = points[4].x, y = points[4].y, x2 = points[8].x, y2 = points[8].y},
- {x = points[1].x, y = points[1].y, x2 = points[3].x, y2 = points[3].y},
- {x = points[2].x, y = points[2].y, x2 = points[4].x, y2 = points[4].y},
- {x = points[5].x, y = points[5].y, x2 = points[7].x, y2 = points[7].y},
- {x = points[6].x, y = points[6].y, x2 = points[8].x, y2 = points[8].y},
- {x = points[1].x, y = points[1].y, x2 = points[2].x, y2 = points[2].y},
- {x = points[3].x, y = points[3].y, x2 = points[4].x, y2 = points[4].y},
- {x = points[5].x, y = points[5].y, x2 = points[6].x, y2 = points[6].y},
- {x = points[7].x, y = points[7].y, x2 = points[8].x, y2 = points[8].y},
- }
- for lineNr, lineCoordinates in pairs(linesToDraw) do
- if ((lineCoordinates.x > 0 and lineCoordinates.x <= frame.frameX2) or
- (lineCoordinates.x2 > 0 and
- lineCoordinates.x2 <= frame.frameX2)) then
- paintutils.drawLine(lineCoordinates.x, lineCoordinates.y, lineCoordinates.x2, lineCoordinates.y2, object.color)
- end
- end
- elseif (object.oType == "flip") then
- coordinates1 = frame:findPointOnScreen(object.x - 0.5, object.y + 0.5, object.z - 0.5)
- coordinates2 = frame:findPointOnScreen(object.x - 0.5, object.y + 0.5 - object.height, object.z - 0.5)
- local height = math.abs(coordinates1.y - coordinates2.y)
- local width = height * object.width / object.height
- local color = object.color
- drawSquare(coordinates2.x - (0.5 * width), coordinates1.y, coordinates1.x + (0.5 * width), coordinates2.y, color)
- else
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.setCursorPos(20, 10)
- print("ERROR: Unknown object type")
- sleep(3)
- exit()
- end
- end
- function frame:drawGround()
- drawSquare(1, math.floor(frame.playerDirectionVer * (frame.frameY2 / 2) / (frame.FoV * frame.frameY2 / 2 / frame.frameX2) + math.floor(frame.frameY2 / 2)), frame.frameX2, frame.frameY2, frame.groundColor)
- end
- return frame
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement