Piorjade

ComputerCraft Jump n' Run "Engine" (you are free to edit it)

May 19th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. --Main "Engine"
  2. --Completely Written by Piorjade
  3. --You a free to edit/extend the code for your own purposes...
  4. --GET THE LEVELEDITOR HERE: pastebin get ESPY1FUz editor
  5. --(Though I'm really shitty at programming)
  6.  
  7. --Variablen
  8. _ver = 0.1
  9. _verstr = "0.1"
  10. lvlLocation = "/game/levels/"
  11. lvlData = {}
  12. currentLevel = ""
  13. xPos = 1
  14. yPos = 1
  15.  
  16. --Funktionen
  17.  
  18. function clear(bg, fg)
  19.     term.setCursorPos(1,1)
  20.     term.setBackgroundColor(bg)
  21.     term.setTextColor(fg)
  22.     term.clear()
  23. end
  24.  
  25. function menu()
  26.     clear(colors.black, colors.white)
  27.     term.setCursorPos(24,6)
  28.     term.setBackgroundColor(colors.lime)
  29.     term.write("Start")
  30.     term.setCursorPos(24,8)
  31.     term.write("Exit")
  32.     menu = true
  33.     while menu do
  34.         local event, button, x, y = os.pullEventRaw("mouse_click")
  35.         if button == 1 and x >= 24 and x <= 28 and y == 6 then
  36.             menu = false
  37.             loadLevel("tutorial")
  38.             break
  39.         elseif button == 1 and x >= 24 and x <= 27 and y == 8 then
  40.             clear(colors.black, colors.white)
  41.             print("Thanks for playing!")
  42.             break
  43.         end
  44.     end
  45. end
  46.  
  47. function getLevelData(level)
  48.     local file = fs.open(lvlLocation..level..".lvlDat","r")
  49.     local inhalt = file.readAll()
  50.     lvlData = textutils.unserialize(inhalt)
  51.     file.close()
  52. end
  53.  
  54. function drawEntities()
  55.     term.setBackgroundColor(colors.blue)
  56.     term.setTextColor(colors.black)
  57.     xPos = tonumber(lvlData.spawnPosX)
  58.     yPos = tonumber(lvlData.spawnPosY)
  59.     term.setCursorPos(xPos, yPos)
  60.     term.write("P")
  61.  
  62.     playing = true
  63.     game()
  64. end
  65.  
  66. function game()
  67.     while playing do
  68.         local event, key = os.pullEventRaw("key")
  69.         if key == keys.left and xPos > 1 then
  70.             local blocktrue = false
  71.             for _, block in ipairs(lvlData.blocksX) do
  72.                 local x = tonumber(block)
  73.                 local y = tonumber(lvlData.blocksY[_])
  74.                 if xPos == x+1 and yPos == y then
  75.                     blocktrue = true
  76.                 end
  77.             end
  78.             if blocktrue then
  79.  
  80.             else
  81.                 xPos = xPos-1
  82.                 gravity()
  83.                 redrawLvl()
  84.             end
  85.         elseif key == keys.right and xPos < 51 then
  86.             local block = false
  87.             for _,block in ipairs(lvlData.blocksX) do
  88.                 local x = tonumber(block)
  89.                 local y = tonumber(lvlData.blocksY[_])
  90.                 if xPos == x-1 and yPos == y then
  91.                     blocktrue = true
  92.                 end
  93.             end
  94.             if blocktrue then
  95.  
  96.             else
  97.                 xPos = xPos+1
  98.                 gravity()
  99.                 redrawLvl()
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. function gravity()
  106.     for _, block in ipairs(lvlData.blocksX) do
  107.         local x = tonumber(block)
  108.         local y = tonumber(lvlData.blocksY[_])
  109.         if xPos == x and yPos+1 == y then
  110.             break
  111.         elseif xPos == x and yPos+2 <= y then
  112.             yPos = yPos+1
  113.             gravity()
  114.         end
  115.     end
  116. end
  117.  
  118. function redrawLvl()
  119.     clear(colors.black, colors.white)
  120.     lvl = paintutils.loadImage(lvlLocation..currentLevel..".lvl")
  121.     paintutils.drawImage(lvl, 1, 1)
  122.     term.setBackgroundColor(colors.blue)
  123.     term.setTextColor(colors.black)
  124.     term.setCursorPos(xPos, yPos)
  125.     term.write("P")
  126. end
  127.  
  128. function loadLevel(level)
  129.     getLevelData(level)
  130.     lvl = paintutils.loadImage(lvlLocation..level..".lvl")
  131.     clear(colors.black, colors.white)
  132.     paintutils.drawImage(lvl, 1, 1)
  133.     currentLevel = level
  134.     drawEntities()
  135. end
  136.  
  137.  
  138. --Code
  139. local args = {...}
  140.  
  141. if #args < 1 then
  142.     clear(colors.black, colors.white)
  143.     menu()
  144. elseif #args == 2 then
  145.     if fs.exists(args[1]) and fs.exists(args[1]..args[2]..".lvl") then
  146.         lvlLocation = args[1]
  147.         currentLevel = args[2]
  148.         loadLevel(args[2])
  149.     else
  150.         print("File does not exist. Please make sure")
  151.         print("that you wrote it like this:")
  152.         term.setTextColor(colors.yellow)
  153.         print("thisfile <folder/to/the> <level>")
  154.         term.setTextColor(colors.red)
  155.         print("AND WITHOUT THE .lvl ENDING!")
  156.         term.setTextColor(colors.white)
  157.     end
  158. else
  159.     print("Too many arguments. Please make sure")
  160.     print("that you wrote it like this:")
  161.     term.setTextColor(colors.yellow)
  162.     print("thisfile <folder/to/the> <level>")
  163.     term.setTextColor(colors.red)
  164.     print("AND WITHOUT THE .lvl ENDING!")
  165.     term.setTextColor(colors.white)
  166. end
Add Comment
Please, Sign In to add comment