Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scr_x, scr_y = term.getSize()
- palateFile = fs.combine(shell.dir(),"palate")
- levelFile = nil
- local tArg = {...}
- if tArg[1] then
- levelFile = fs.combine(shell.dir(),tArg[1])
- end
- if tArg[2] then
- palateFile = fs.combine(shell.dir(),tArg[2])
- end
- scrollX = 0
- scrollY = 0
- function CTB(_color) --Color To Blit
- local blitcolors = {
- [colors.white] = "0",
- [colors.orange] = "1",
- [colors.magenta] = "2",
- [colors.lightBlue] = "3",
- [colors.yellow] = "4",
- [colors.lime] = "5",
- [colors.pink] = "6",
- [colors.gray] = "7",
- [colors.lightGray] = "8",
- [colors.cyan] = "9",
- [colors.purple] = "a",
- [colors.blue] = "b",
- [colors.brown] = "c",
- [colors.green] = "d",
- [colors.red] = "e",
- [colors.black] = "f",
- }
- return blitcolors[_color]
- end
- if fs.exists(palateFile) then
- dofile(palateFile)
- end
- if not palate then
- palate = { --I say palate because it can (as should be expected to) be changed per game.
- [1] = { --Blocks will be stored as IDs, not by name.
- desc = "air", --Brief description of what it is supposed to represent.
- char = " ", --Character used to represent it.
- txtcol = colors.black, --TEXT color. Uses colors API, not blit.
- bgcol = colors.black, --BACKGROUND color. Again, colors API.
- solid = false, --Whether or not you can go through it.
- isLiquid = false, --Whether it is a liquid or not.
- touchDamage = 0, --Damage done by TOUCHING IT. Meaning, being inside it OR a space next to it.
- insideDamage = 0, --Damage done by BEING IN THE SAME SPACE AS IT. Not being next to it.
- gravity = 1, --Gravity (let's just say in G's) when INSIDE the block.
- }, --Oh BTW, [1] will always be air. That's constant. Also, it's not really a block, but an empty space.
- [2] = {
- desc = "sto ne",
- char = "L",
- txtcol = colors.lightGray,
- bgcol = colors.gray,
- solid = true,
- isLiquid = false,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 1,
- },
- [3] = {
- desc = "woodplank",
- char = "=",
- txtcol = colors.orange,
- bgcol = colors.brown,
- solid = true,
- isLiquid = false,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 1,
- },
- [4] = {
- desc = "water",
- char = "/",
- txtcol = colors.lightBlue,
- bgcol = colors.cyan,
- solid = false,
- isLiquid = true,
- touchDamage = 0,
- insideDamage = 0,
- gravity = 0.4,
- },
- [5] = {
- desc = "lava",
- char = "#",
- txtcol = colors.red,
- bgcol = colors.orange,
- solid = false,
- isLiquid = true,
- touchDamage = 0,
- insideDamage = 2,
- gravity = 1.2,
- },
- [6] = {
- desc = "cactus",
- char = "#",
- txtcol = colors.gray,
- bgcol = colors.green,
- solid = true,
- isLiquid = false,
- touchDamage = 1,
- insideDamage = 0,
- gravity = 1,
- },
- }
- local file = fs.open(palateFile,"w")
- file.write("palate = "..textutils.serialise(palate))
- file.close()
- end
- selectedItem = 1
- local level
- if levelFile then
- local file = fs.open(levelFile,"r")
- level = textutils.unserialize(file.readAll())
- file.close()
- else
- level = {{}}
- end
- local function clearDot(x,y,col)
- local pX,pY = term.getCursorPos()
- term.setCursorPos(x,y)
- if col then
- term.blit(" ",CTB(col),CTB(col))
- else
- term.write(" ")
- end
- term.setCursorPos(pX,pY)
- end
- function palateclear()
- local prevX,prevY = term.getCursorPos()
- term.setBackgroundColor(palate[1].bgcol)
- term.setTextColor(palate[1].txtcol)
- for a = 1, scr_y do
- term.setCursorPos(1,a)
- write(palate[1].char:rep(scr_x))
- end
- term.setCursorPos(prevX,prevY)
- end
- function renderLevel(_level,_scrollX,_scrollY)
- for y = 1,scr_y do
- for x = 1,scr_x do
- if level[x+scrollX] then
- lobster = palate[level[x+scrollX][(scr_y-y)+scrollY+1]] --Well it wasn't a rock
- if lobster and y ~= scr_y then
- term.setCursorPos(x,y)
- term.blit(lobster.char,CTB(lobster.txtcol),CTB(lobster.bgcol))
- end
- end
- end
- end
- end
- function handleQuit()
- term.setCursorPos(1,scr_y)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- sleep(0)
- error() --This is probably the wrong way of going about doing this.
- end
- function drawDock(_palate,selected)
- term.setCursorPos(1,scr_y)
- term.setBackgroundColor(colors.white)
- term.clearLine()
- term.setBackgroundColor(colors.black)
- local dockpos = {}
- for a = 1, #_palate do
- term.setCursorPos(a,scr_y)
- table.insert(dockpos,{x=a,desc=_palate[a].desc})
- term.blit(_palate[a].char,CTB(_palate[a].txtcol),CTB(_palate[a].bgcol))
- term.setCursorPos(scr_x-#_palate[selected].desc,scr_y)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.write(_palate[selected].desc)
- end
- return dockpos
- end
- function handleKey(e)
- local _,key = table.unpack(e)
- oldScrollX, oldScrollY = scrollX, scrollY
- if key == keys.left and scrollX > 0 then
- scrollX = scrollX - 1
- elseif key == keys.right then
- scrollX = scrollX + 1
- elseif key == keys.down and scrollY > 0 then
- scrollY = scrollY - 1
- elseif key == keys.up then
- scrollY = scrollY + 1
- elseif key == keys.q then
- handleQuit()
- end
- if oldScrollX ~= scrollX or oldScrollY ~= scrollY then
- palateclear()
- drawDock(palate,selectedItem)
- renderLevel(level,scrollX,scrollY)
- end
- end
- function handleMouseClick(e)
- local _,button,x,y = table.unpack(e)
- local dockpos = drawDock(palate,selectedItem)
- if y == scr_y then --handle dock clicking
- for a = 1, #dockpos do
- if dockpos[a].x == x then
- selectedItem = a
- dockpos = drawDock(palate,selectedItem)
- break
- end
- end
- else --handle drawing things
- if not level[x+scrollX] then level[x+scrollX] = {} end
- if selectedItem == 1 then --air
- level[x+scrollX][(scr_y-y)+scrollY+1] = nil
- clearDot(x,y,palate[1].bgcol)
- else
- level[x+scrollX][(scr_y-y)+scrollY+1] = selectedItem
- end
- renderLevel(level,scrollX,scrollY)
- end
- end
- function doit()
- palateclear()
- drawDock(palate,selectedItem)
- renderLevel(level,scrollX,scrollY)
- while true do
- evt = {os.pullEvent()}
- if evt[1] == "mouse_click" or evt[1] == "mouse_drag" then
- handleMouseClick(evt)
- elseif evt[1] == "key" then
- handleKey(evt)
- end
- end
- end
- doit()
Add Comment
Please, Sign In to add comment