JustDoesGames

2d Platformer test

Jan 7th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.09 KB | None | 0 0
  1.  
  2. -- Platformer --
  3.  
  4. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  5. -- FUNCTIONS --
  6. local w,h = term.getSize()
  7. function valid(value,expected) if type(value) == expected then return true else return false end end
  8. function clr() term.clear() end
  9. function cp(x,y) if not valid(x,"number") then return error("Expected x to be a number, got "..type(x)) end if not valid(y,"number") then return error("Expected y to be a number, got "..type(y)) end term.setCursorPos(x,y) end
  10. function setText(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setTextColor(colors[col]) end end
  11. function setBack(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setBackgroundColor(colors[col]) end end
  12. function notnil(vari) return vari ~= nil end
  13. function lt(tab) if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end local lt = 0 for i=1, #tab do if #tab[i] > lt then lt = #tab end end return lt end
  14. function pullEvents(...) -- for pulling multiple events at a time, first event get priority
  15.     local args = {...}
  16.     --if #args == 0 then return os.pullEvent() end
  17.     while true do
  18.         event, arg1, arg2, arg3 = os.pullEvent()
  19.         for i=1, #args do
  20.             if args[i] == event then
  21.                 return event, arg1, arg2, arg3
  22.             end
  23.         end
  24.     end
  25. end
  26. -- FUNCTIONS --
  27. -- COLLISION --
  28. col = {}
  29. function createCol(name,x,y,x2,y2,status)
  30.     name = name or "name_"..#col table.insert(col,{}) status = status or true
  31.     col[#col].x, col[#col].y, col[#col].x2, col[#col].y2, col[#col].name, col[#col].status = x, y, x2, y2, name, status
  32. end
  33. function colExists(name)
  34.     for i=1, #col do
  35.         if col[i].name == name then return i end
  36.     end
  37.     return false
  38. end
  39. function removeCol(name)
  40.     if colExists(name) ~= false then table.remove(col, colExists(name)) end
  41. end
  42. function moveCol(name,newx,newy)
  43.     col[name].x2,col[name].y2 = col[name].x2+newx-col[name].x, col[name].y2+newy-col[name].y
  44.     col[name].x,col[name].y = newx,newy
  45. end
  46. function drawCol(name, color)
  47.     if not notnil(color) then if col[name].status then color = colors.green else color = colors.red end end
  48.     for i=1, #col do
  49.         if name == col[i].name then
  50.             return paintutils.drawFilledBox(col[i].x,col[i].y,col[i].x2,col[i].y2,color)
  51.         end
  52.     end
  53.     return false
  54. end
  55. function checkCol(x,y)
  56.     for i=1, #col do
  57.         if x >= col[i].x and x <= col[i].x2 and y >= col[i].y and y <= col[i].y2 then
  58.             return col[i].name
  59.         end
  60.     end
  61.     return false
  62. end
  63. -- COLLISION --
  64. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  65. local player = {}
  66. local run = true
  67. player.x = 1
  68. player.y = 1
  69. player.isJumping = false
  70. player.isFalling = false
  71. player.jumpDisabled = false
  72. local settings = {}
  73. settings.jumpHeight = 4 -- 3 being 3 px
  74. settings.jumpGravity = .9 -- effect gravity for player
  75. settings.fallSpeed = 2
  76. settings.bounceHeight = 4
  77.  
  78. local function moveCharacter(x,y)
  79.     for i=1, #col do
  80.         drawCol(col[i].name,colors.white)
  81.     end
  82.     local valid = true
  83.     if x < player.x then
  84.         if checkCol(player.x-1,player.y) ~= false then
  85.             valid = false
  86.         end
  87.     elseif x > player.x then
  88.         if checkCol(player.x+1,player.y) ~= false then
  89.             valid = false
  90.         end
  91.     end
  92.     if y < player.y then
  93.         if checkCol(player.x,player.y-1) ~= false then
  94.             valid = false
  95.         end
  96.     elseif y > player.y then
  97.         if checkCol(player.x,player.y+1) ~= false then
  98.             valid = false
  99.         end
  100.     end
  101.     if valid then
  102.         cp(player.x,player.y) setBack("black") write(" ")
  103.         cp(x,y) setBack("yellow") write(" ") setBack("black")
  104.         player.x, player.y = x, y
  105.     end
  106. end
  107. clr()
  108. moveCharacter(math.floor(w/2),1)
  109.  
  110. local function gravity()
  111.     while run do
  112.         if player.isJumping and not player.isFalling and not player.jumpDisabled then
  113.             local cnt = 0
  114.             while cnt <= settings.jumpHeight do
  115.                 if checkCol(player.x,player.y-1) ~= false then break end moveCharacter(player.x,player.y-1) sleep((cnt/settings.jumpGravity)*.06) cnt = cnt + 1
  116.             end
  117.             player.jumpDisabled = true
  118.         else
  119.             if checkCol(player.x,player.y+1) == false then
  120.                 player.isFalling = true
  121.                 local loop = settings.fallSpeed
  122.                 local force_quit = false
  123.                 repeat
  124.                     moveCharacter(player.x,player.y+1) sleep(loop*.09)
  125.                     if loop ~= 0 then loop = loop - 0.1 end
  126.                     if player.y == h then force_quit = true end
  127.                 until checkCol(player.x,player.y+1) ~= false or force_quit == true
  128.                 if force_quit == true then
  129.                     local cnt = 0
  130.                     while cnt <= settings.bounceHeight do
  131.                         if checkCol(player.x,player.y-1) ~= false then break end moveCharacter(player.x,player.y-1) sleep((cnt/settings.jumpGravity)*.06) cnt = cnt + 1
  132.                     end
  133.                 end
  134.                 player.isFalling = false
  135.                 player.isJumping = false
  136.                 player.jumpDisabled = false
  137.             end
  138.         end
  139.         sleep(.0001)
  140.     end
  141. end
  142.  
  143. local function playerController()
  144.     while run do
  145.         a,i,held = pullEvents("key")
  146.         if i == keys.d or i == keys.right then
  147.             moveCharacter(player.x+1,player.y)
  148.         elseif i == keys.a or i == keys.left then
  149.             moveCharacter(player.x-1,player.y)
  150.         elseif i == keys.s or i == keys.down then
  151.             moveCharacter(player.x,player.y+1)
  152.         elseif i == keys.q then
  153.             run = false
  154.         elseif i == keys.space then
  155.             if not player.jumpDisabled then player.isJumping = true else if checkCol(player.x,player.y+1) ~= false then player.jumpDisabled = false player.isJumping = true end end
  156.         end
  157.     end
  158. end
  159.  
  160. local function createFloor(x,y)
  161.     if checkCol(x,y) == false then
  162.         if x == player.x and y == player.y then
  163.             -- for later things maby
  164.         else
  165.             createCol("floor_"..x.."_"..y,x,y,x,y,true)
  166.             drawCol("floor_"..x.."_"..y,colors.white)
  167.         end
  168.     end
  169. end
  170. local function removeFloor(x,y)
  171.     removeCol("floor_"..x.."_"..y)
  172.     cp(x,y) setBack("black") write(" ")
  173. end
  174.  
  175. local function editController()
  176.     while run do
  177.         a,i,x,y = pullEvents("mouse_click","mouse_drag")
  178.         if i == 1 then
  179.             createFloor(x,y)
  180.         elseif i == 2 then
  181.             removeFloor(x,y)
  182.         end
  183.     end
  184. end
  185.  
  186. local function runtime()
  187.     parallel.waitForAny(playerController,editController,gravity)
  188. end
  189.  
  190. runtime()
  191. cp(1,1) setBack("black") setText("white") clr() print("Thnks for playing...") sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment