Piorjade

ComputerCraft Jump n' Run "Engine" LEVELEDITOR

May 19th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. --Editor (yaay make your own levels based on this "engine")
  2. --Again, written by me c:
  3. --(I know, I'm shit at programming but hey why not C:)
  4.  
  5. --Variablen
  6. _ver = 0.1
  7. _verstr = "0.1"
  8. pfad = ""
  9.  
  10. lvlData = {
  11.     spawnPosX="1",
  12.     spawnPosY="1",
  13.     blocksX = {
  14.  
  15.     },
  16.     blocksY = {
  17.  
  18.     }
  19. }
  20.  
  21. --Funktionen
  22. function clear(bg, fg)
  23.     term.setCursorPos(1,1)
  24.     term.setBackgroundColor(bg)
  25.     term.setTextColor(fg)
  26.     term.clear()
  27. end
  28.  
  29. function menu()
  30.     clear(colors.black, colors.white)
  31.     term.setBackgroundColor(colors.lime)
  32.     term.setCursorPos(25, 6)
  33.     term.write("NEW")
  34.     term.setCursorPos(24, 8)
  35.     term.write("LOAD")
  36.     term.setCursorPos(24, 10)
  37.     term.write("EXIT")
  38.     menu = true
  39.     while menu do
  40.         local event, button, x, y = os.pullEventRaw("mouse_click")
  41.         if button == 1 and x >= 25 and x <= 27 and y == 6 then
  42.             menu = false
  43.             menu2()
  44.         end
  45.     end
  46. end
  47.  
  48. function menu2()
  49.     clear(colors.black, colors.white)
  50.     oldTerm = term.native()
  51.     local graywindow = window.create(oldTerm, 15, 5, 20, 5)
  52.     term.redirect(graywindow)
  53.     term.setBackgroundColor(colors.lightGray)
  54.     term.setCursorPos(1,1)
  55.     term.clear()
  56.     local textBox = window.create(term.current(), 2, 2, 18, 1)
  57.     textBox.setBackgroundColor(colors.gray)
  58.     textBox.setTextColor(colors.lime)
  59.     textBox.clear()
  60.     textBox.write("Enter Name/Desti..")
  61.     term.setBackgroundColor(colors.gray)
  62.     term.setTextColor(colors.white)
  63.     term.setCursorPos(2, 4)
  64.     term.write("New")
  65.     term.setCursorPos(16, 4)
  66.     term.write("Load")
  67.     menu = true
  68.     while menu do
  69.         local event, button, x, y = os.pullEvent("mouse_click")
  70.         if button == 1 and x >= 16 and x <= 34 and y == 6 then
  71.             term.redirect(textBox)
  72.             term.clear()
  73.             term.setCursorPos(1,1)
  74.             local eingabe = read()
  75.             pfad = eingabe
  76.             term.setCursorPos(1,1)
  77.             term.clear()
  78.             term.write(eingabe)
  79.             term.redirect(graywindow)
  80.         elseif button == 1 and x >= 16 and x <= 18 and y == 8 then
  81.             if fs.exists(pfad..".lvl") then
  82.                 textBox.setCursorPos(1,1)
  83.                 textBox.clear()
  84.                 textBox.setTextColor(colors.red)
  85.                 textBox.write("Already exists.")
  86.                 textBox.setTextColor(colors.lime)
  87.             elseif pfad == "" or pfad == nil then
  88.                 textBox.setCursorPos(1,1)
  89.                 textBox.clear()
  90.                 textBox.setTextColor(colors.red)
  91.                 textBox.write("Please fill in.")
  92.                 textBox.setTextColor(colors.lime)
  93.             else
  94.                 term.redirect(oldTerm)
  95.                 term.setCursorPos(1,1)
  96.                 clear(colors.black, colors.white)
  97.                 menu = false
  98.                 print("Please draw your LevelDESIGN.")
  99.                 print("This will be that, what you see when playing the level.")
  100.                 sleep(3)
  101.                 shell.run("paint "..pfad..".lvl")
  102.                
  103.                 editor()
  104.             end
  105.         elseif button == 1 and x >= 30 and x <= 34 and y == 8 then
  106.             if fs.exists(pfad..".lvl") and fs.exists(pfad..".lvlDat") then
  107.                 term.redirect(oldTerm)
  108.                 menu = false
  109.                 editor("true")
  110.             elseif fs.exists(pfad..".lvl") then
  111.                 term.redirect(oldTerm)
  112.                 menu = false
  113.                 editor("false")
  114.             else
  115.                 textBox.setCursorPos(1,1)
  116.                 textBox.clear()
  117.                 textBox.setTextColor(colors.red)
  118.                 textBox.write("Doesn't exist.")
  119.                 textBox.setTextColor(colors.lime)
  120.             end
  121.         end
  122.     end
  123.  
  124. end
  125.  
  126. function loadDat()
  127.     local file = fs.open(pfad..".lvlDat","r")
  128.     local inhalt = file.readAll()
  129.     lvlData = textutils.unserialize(inhalt)
  130.     file.close()
  131. end
  132.  
  133. function editor(loadData)
  134.     clear(colors.black, colors.white)
  135.     lvl = paintutils.loadImage(pfad..".lvl")
  136.     paintutils.drawImage(lvl, 1, 1)
  137.     if loadData == "true" then
  138.         loadDat()
  139.         reloadMap()
  140.     end
  141.     term.setCursorPos(1,19)
  142.     term.setBackgroundColor(colors.lightGray)
  143.     term.clearLine()
  144.     term.setBackgroundColor(colors.lime)
  145.     term.write(" Block ")
  146.     term.setBackgroundColor(colors.lightGray)
  147.     term.write(" ")
  148.     term.setBackgroundColor(colors.lime)
  149.     term.write(" Spawn ")
  150.     term.setBackgroundColor(colors.lightGray)
  151.     term.write(" ")
  152.     term.setBackgroundColor(colors.lime)
  153.     term.write(" Remove ")
  154.     editing = true
  155.     while editing do
  156.         local event, button, x, y = os.pullEventRaw()
  157.         if event == "mouse_click" and button == 1 and x >= 1 and x <= 7 and y == 19 then
  158.             currentBlock = "block"
  159.         elseif event == "mouse_click" and button == 1 and x >= 9 and x <= 15 and y == 19 then
  160.             currentBlock = "spawn"
  161.         elseif event == "mouse_click" and button == 1 and x >= 17 and x <= 24 and y == 19 then
  162.             currentBlock = "remove"
  163.         elseif event == "mouse_click" and button == 1 and x >= 1 and x <= 51 and y >= 1 and y <= 18 then
  164.             if currentBlock == "block" then
  165.                 term.setCursorPos(x, y)
  166.                 term.setBackgroundColor(colors.brown)
  167.                 term.setTextColor(colors.white)
  168.                 term.write("B")
  169.                 local x = tostring(x)
  170.                 local y = tostring(y)
  171.                 table.insert(lvlData.blocksX, x)
  172.                 table.insert(lvlData.blocksY, y)
  173.                 reloadMap()
  174.             elseif currentBlock == "spawn" then
  175.                 term.setCursorPos(x, y)
  176.                 term.setBackgroundColor(colors.blue)
  177.                 term.setTextColor(colors.white)
  178.                 term.write("S")
  179.                 local x = tostring(x)
  180.                 local y = tostring(y)
  181.                 lvlData.spawnPosX = x
  182.                 lvlData.spawnPosY = y
  183.                 reloadMap()
  184.             elseif currentBlock == "remove" then
  185.                 for _, block in ipairs(lvlData.blocksX) do
  186.                     local blockY = tonumber(lvlData.blocksY[_])
  187.                     local block = tonumber(block)
  188.                     if x == block and y == blockY then
  189.                         lvlData.blocksX[_] = nil
  190.                         lvlData.blocksY[_] = nil
  191.                     end
  192.                 end
  193.                 if x == lvlData.spawnPosX and y == lvlData.spawnPosY then
  194.                     lvlData.spawnPosX = "1"
  195.                     lvlData.spawnPosY = "1"
  196.                 end
  197.                 reloadMap()
  198.             end
  199.         elseif event == "key" and button == keys.s then
  200.             local file = fs.open(pfad..".lvlDat","w")
  201.             local lvlData = textutils.serialize(lvlData)
  202.             file.write(lvlData)
  203.             file.close()
  204.             clear(colors.black, colors.white)
  205.  
  206.             editing = false
  207.             break
  208.         end
  209.  
  210.     end
  211. end
  212.  
  213. function reloadMap()
  214.     clear(colors.black, colors.white)
  215.     term.setCursorPos(1,19)
  216.     term.setBackgroundColor(colors.lightGray)
  217.     term.clearLine()
  218.     term.setBackgroundColor(colors.lime)
  219.     term.write(" Block ")
  220.     term.setBackgroundColor(colors.lightGray)
  221.     term.write(" ")
  222.     term.setBackgroundColor(colors.lime)
  223.     term.write(" Spawn ")
  224.     term.setBackgroundColor(colors.lightGray)
  225.     term.write(" ")
  226.     term.setBackgroundColor(colors.lime)
  227.     term.write(" Remove ")
  228.     term.setCursorPos(1,1)
  229.     paintutils.drawImage(lvl, 1, 1)
  230.     for _, block in ipairs(lvlData.blocksX) do
  231.         local blockY = tonumber(lvlData.blocksY[_])
  232.         local block = tonumber(block)
  233.         term.setCursorPos(block, blockY)
  234.         term.setBackgroundColor(colors.brown)
  235.         term.setTextColor(colors.white)
  236.         term.write("B")
  237.     end
  238.     local sX = tonumber(lvlData.spawnPosX)
  239.     local sY = tonumber(lvlData.spawnPosY)
  240.     term.setCursorPos(sX, sY)
  241.     term.setBackgroundColor(colors.blue)
  242.     term.setTextColor(colors.white)
  243.     term.write("S")
  244. end
  245.  
  246.  
  247.  
  248. --Code
  249. menu2()
Advertisement
Add Comment
Please, Sign In to add comment