LDDestroier

ComputerCraft Platformer Test (LEVEL EDITOR)

Mar 26th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.16 KB | None | 0 0
  1. local scr_x, scr_y = term.getSize()
  2. palateFile = fs.combine(shell.dir(),"palate")
  3. levelFile = nil
  4. local tArg = {...}
  5. if tArg[1] then
  6.     levelFile = fs.combine(shell.dir(),tArg[1])
  7. end
  8. if tArg[2] then
  9.     palateFile = fs.combine(shell.dir(),tArg[2])
  10. end
  11.  
  12. scrollX = 0
  13. scrollY = 0
  14.  
  15. function CTB(_color) --Color To Blit
  16.     local blitcolors = {
  17.         [colors.white] = "0",
  18.         [colors.orange] = "1",
  19.         [colors.magenta] = "2",
  20.         [colors.lightBlue] = "3",
  21.         [colors.yellow] = "4",
  22.         [colors.lime] = "5",
  23.         [colors.pink] = "6",
  24.         [colors.gray] = "7",
  25.         [colors.lightGray] = "8",
  26.         [colors.cyan] = "9",
  27.         [colors.purple] = "a",
  28.         [colors.blue] = "b",
  29.         [colors.brown] = "c",
  30.         [colors.green] = "d",
  31.         [colors.red] = "e",
  32.         [colors.black] = "f",
  33.     }
  34.     return blitcolors[_color]
  35. end
  36.  
  37. if fs.exists(palateFile) then
  38.     dofile(palateFile)
  39. end
  40.  
  41. if not palate then
  42.     palate = {                      --I say palate because it can (as should be expected to) be changed per game.
  43.         [1] = {                     --Blocks will be stored as IDs, not by name.
  44.             desc = "air",           --Brief description of what it is supposed to represent.
  45.             char = " ",             --Character used to represent it.
  46.             txtcol = colors.black,  --TEXT color. Uses colors API, not blit.
  47.             bgcol = colors.black,   --BACKGROUND color. Again, colors API.
  48.             solid = false,          --Whether or not you can go through it.
  49.             isLiquid = false,       --Whether it is a liquid or not.
  50.             touchDamage = 0,        --Damage done by TOUCHING IT. Meaning, being inside it OR a space next to it.
  51.             insideDamage = 0,       --Damage done by BEING IN THE SAME SPACE AS IT. Not being next to it.
  52.             gravity = 1,            --Gravity (let's just say in G's) when INSIDE the block.
  53.         },                          --Oh BTW, [1] will always be air. That's constant. Also, it's not really a block, but an empty space.
  54.         [2] = {
  55.             desc = "sto ne",
  56.             char = "L",
  57.             txtcol = colors.lightGray,
  58.             bgcol = colors.gray,
  59.             solid = true,
  60.             isLiquid = false,  
  61.             touchDamage = 0,
  62.             insideDamage = 0,
  63.             gravity = 1,
  64.         },
  65.         [3] = {
  66.             desc = "woodplank",
  67.             char = "=",
  68.             txtcol = colors.orange,
  69.             bgcol = colors.brown,
  70.             solid = true,
  71.             isLiquid = false,
  72.             touchDamage = 0,
  73.             insideDamage = 0,
  74.             gravity = 1,
  75.         },
  76.         [4] = {
  77.             desc = "water",
  78.             char = "/",
  79.             txtcol = colors.lightBlue,
  80.             bgcol = colors.cyan,
  81.             solid = false,
  82.             isLiquid = true,
  83.             touchDamage = 0,
  84.             insideDamage = 0,
  85.             gravity = 0.4,
  86.         },
  87.         [5] = {
  88.             desc = "lava",
  89.             char = "#",
  90.             txtcol = colors.red,
  91.             bgcol = colors.orange,
  92.             solid = false,
  93.             isLiquid = true,
  94.             touchDamage = 0,
  95.             insideDamage = 2,
  96.             gravity = 1.2,
  97.         },
  98.         [6] = {
  99.             desc = "cactus",
  100.             char = "#",
  101.             txtcol = colors.gray,
  102.             bgcol = colors.green,
  103.             solid = true,
  104.             isLiquid = false,
  105.             touchDamage = 1,
  106.             insideDamage = 0,
  107.             gravity = 1,
  108.         },
  109.     }
  110.     local file = fs.open(palateFile,"w")
  111.     file.write("palate = "..textutils.serialise(palate))
  112.     file.close()
  113. end
  114. selectedItem = 1
  115. local level
  116. if levelFile then
  117.     local file = fs.open(levelFile,"r")
  118.     level = textutils.unserialize(file.readAll())
  119.     file.close()
  120. else
  121.     level = {{}}
  122. end
  123.  
  124. local function clearDot(x,y,col)
  125.     local pX,pY = term.getCursorPos()
  126.     term.setCursorPos(x,y)
  127.     if col then
  128.         term.blit(" ",CTB(col),CTB(col))
  129.     else
  130.         term.write(" ")
  131.     end
  132.     term.setCursorPos(pX,pY)
  133. end
  134.  
  135. function palateclear()
  136.     local prevX,prevY = term.getCursorPos()
  137.     term.setBackgroundColor(palate[1].bgcol)
  138.     term.setTextColor(palate[1].txtcol)
  139.     for a = 1, scr_y do
  140.         term.setCursorPos(1,a)
  141.         write(palate[1].char:rep(scr_x))
  142.     end
  143.     term.setCursorPos(prevX,prevY)
  144. end
  145.  
  146. function renderLevel(_level,_scrollX,_scrollY)
  147.     for y = 1,scr_y do
  148.         for x = 1,scr_x do
  149.             if level[x+scrollX] then
  150.                 lobster = palate[level[x+scrollX][(scr_y-y)+scrollY+1]] --Well it wasn't a rock
  151.                 if lobster and y ~= scr_y then
  152.                     term.setCursorPos(x,y)
  153.                     term.blit(lobster.char,CTB(lobster.txtcol),CTB(lobster.bgcol))
  154.                 end
  155.             end
  156.         end
  157.     end
  158. end
  159.  
  160. function handleQuit()
  161.     term.setCursorPos(1,scr_y)
  162.     term.setBackgroundColor(colors.black)
  163.     term.setTextColor(colors.white)
  164.     term.clearLine()
  165.     sleep(0)
  166.     error() --This is probably the wrong way of going about doing this.
  167. end
  168.  
  169. function drawDock(_palate,selected)
  170.     term.setCursorPos(1,scr_y)
  171.     term.setBackgroundColor(colors.white)
  172.     term.clearLine()
  173.     term.setBackgroundColor(colors.black)
  174.     local dockpos = {}
  175.     for a = 1, #_palate do
  176.         term.setCursorPos(a,scr_y)
  177.         table.insert(dockpos,{x=a,desc=_palate[a].desc})
  178.         term.blit(_palate[a].char,CTB(_palate[a].txtcol),CTB(_palate[a].bgcol))
  179.         term.setCursorPos(scr_x-#_palate[selected].desc,scr_y)
  180.         term.setTextColor(colors.black)
  181.         term.setBackgroundColor(colors.white)
  182.         term.write(_palate[selected].desc)
  183.     end
  184.     return dockpos
  185. end
  186.  
  187. function handleKey(e)
  188.     local _,key = table.unpack(e)
  189.     oldScrollX, oldScrollY = scrollX, scrollY
  190.     if key == keys.left and scrollX > 0 then
  191.         scrollX = scrollX - 1
  192.     elseif key == keys.right then
  193.         scrollX = scrollX + 1
  194.     elseif key == keys.down and scrollY > 0 then
  195.         scrollY = scrollY - 1
  196.     elseif key == keys.up then
  197.         scrollY = scrollY + 1
  198.     elseif key == keys.q then
  199.         handleQuit()
  200.     end
  201.     if oldScrollX ~= scrollX or oldScrollY ~= scrollY then
  202.         palateclear()
  203.         drawDock(palate,selectedItem)
  204.         renderLevel(level,scrollX,scrollY)
  205.     end
  206. end
  207.  
  208. function handleMouseClick(e)
  209.     local _,button,x,y = table.unpack(e)
  210.     local dockpos = drawDock(palate,selectedItem)
  211.     if y == scr_y then  --handle dock clicking
  212.         for a = 1, #dockpos do
  213.             if dockpos[a].x == x then
  214.                 selectedItem = a
  215.                 dockpos = drawDock(palate,selectedItem)
  216.                 break
  217.             end
  218.         end
  219.     else    --handle drawing things
  220.         if not level[x+scrollX] then level[x+scrollX] = {} end
  221.         if selectedItem == 1 then --air
  222.             level[x+scrollX][(scr_y-y)+scrollY+1] = nil
  223.             clearDot(x,y,palate[1].bgcol)
  224.         else
  225.             level[x+scrollX][(scr_y-y)+scrollY+1] = selectedItem
  226.         end
  227.         renderLevel(level,scrollX,scrollY)
  228.     end
  229. end
  230.  
  231. function doit()
  232.     palateclear()
  233.     drawDock(palate,selectedItem)
  234.     renderLevel(level,scrollX,scrollY)
  235.     while true do
  236.         evt = {os.pullEvent()}
  237.         if evt[1] == "mouse_click" or evt[1] == "mouse_drag" then
  238.             handleMouseClick(evt)
  239.         elseif evt[1] == "key" then
  240.             handleKey(evt)
  241.         end
  242.     end
  243. end
  244.  
  245. doit()
Add Comment
Please, Sign In to add comment