Advertisement
LDDestroier

platformer test 1

Jan 3rd, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.21 KB | None | 0 0
  1. local map = {} --not a path, rather a paint file turned into a table
  2. local mapname = "map" --THIS is a path
  3. local mapX, mapY --X and Y sizes of map
  4. local mappaint = nil --the 'paintutils.loadImage()' of the map
  5. xscroll = 0 --changes the view X (higher moves everything left)
  6. yscroll = 0 --changes the view Y (higher moves everything down)
  7. Xmaxspeed = 4
  8. Ymaxspeed = 4
  9. gravity = 1
  10.  
  11. local controls = {
  12.     ["up"] = keys.up,
  13.     ["down"] = keys.down,
  14.     ["left"] = keys.left,
  15.     ["right"] = keys.right,
  16.     ["jump"] = keys.x,
  17.     ["action"] = keys.z,
  18. }
  19.  
  20. log = {} --debug log
  21. logadd = function(data)
  22.     local time = os.clock()
  23.     log[#log+1] = "["..time.."] "..tostring(data or "Nil")
  24. end
  25.  
  26. local keysDown = {}
  27. local miceDown = {}
  28.  
  29. getInput = function() --makes key/mouse input better
  30.     local evt
  31.     while true do
  32.         evt = {os.pullEvent()}
  33.         if evt[1] == "mouse_click" or evt[1] == "mouse_drag" then
  34.             miceDown[evt[2]] = {x = evt[3],y = evt[4]}
  35.         elseif evt[1] == "mouse_up" then
  36.             miceDown[evt[2]] = nil
  37.         end
  38.         if evt[1] == "key" then
  39.             keysDown[evt[2]] = true
  40.         elseif evt[1] == "key_up" then
  41.             keysDown[evt[2]] = false
  42.         end
  43.     end
  44. end
  45.  
  46. setNewMap = function(name)
  47.     local oldname = mapname
  48.     if fs.exists(name) then
  49.         mappaint = paintutils.loadImage(name)
  50.         local file = fs.open(name,"r")
  51.         local line = ""
  52.         mapX = 0
  53.         mapY = 0
  54.         map = {}
  55.         repeat
  56.             line = file.readLine()
  57.             if line then
  58.                 mapY = mapY + 1
  59.                 mapX = (#line > mapX) and #line or mapX
  60.                 map[#map+1] = line
  61.             end
  62.         until not line
  63.         file.close()
  64.         logadd("changed map to '"..name.."' from '"..oldname.."' (X:"..mapX.."/Y:"..mapY..")")
  65.     else
  66.         logadd("tried to change map to nonexistant '"..name.."'")
  67.     end
  68. end
  69.  
  70. makeNewPlayer = function(name,x,y,txt,bg,char,direction)
  71.     local player = {
  72.         name = name or "Player",
  73.         x = x or 0, --x position relative to map
  74.         y = y or 0, --x position relative to map
  75.         xv = 0, --x velocity, higher is more rightwards
  76.         yv = 0, --y velocity, higher is more downwards
  77.         txt = txt or colors.white,
  78.         bg = bg or colors.gray,
  79.         char = char or "o",
  80.         direction = direction or 1, -- 1 is right, -1 is left
  81.         grounded = false,
  82.         stat = { --in case I use them
  83.             hp = 100,   --Hit Points
  84.             mp = 30,    --Magic Points
  85.             str = 5,    --Attack Strength
  86.             def = 4,    --Defence
  87.         }
  88.            
  89.     }
  90.     logadd("defined new player '"..name.."'")
  91.     return player
  92. end
  93.  
  94. players = {
  95.     [1] = makeNewPlayer("P1",6,5,colors.lime,colors.green,"o",1),
  96.     [2] = makeNewPlayer("P@",8,5,colors.yellow,colors.orange,"o",-1),
  97. }
  98.  
  99. local youPlayer = 1 --you is player 1
  100.  
  101. local isOnscreen = function(x,y,scx,scy)
  102.     local sx,sy = term.getSize()
  103.     sx,sy = scx or sx, scy or sy
  104.     x,y = math.floor(x),math.floor(y)
  105.     if (x < 1) or (x > sx) then return false end
  106.     if (y < 1) or (y > sy) then return false end
  107.     return true
  108. end
  109.  
  110. getMapBit = function(x,y)
  111.     if y > #map then
  112.         return true, "#"
  113.     elseif (x < 1) or (x > mapX) then
  114.         return true, "#"
  115.     else
  116.         local bit = string.sub(map[y] or "#",x,x) or " "
  117.         if bit:gsub(" ","") == "" then
  118.             return false, bit
  119.         else
  120.             return true, bit
  121.         end
  122.     end
  123. end
  124.  
  125. checkDistance = function(player,direction,distance)
  126.     --direction: 0 is down, 1 is left, 2 is up, 3 is right
  127.     --this function checks to see if you can freely move a certain amount of steps without hitting the map, and then returns the max distance you can go
  128.     local x,y = player.x,player.y
  129.     local endMultiply = 1 --1 if not inverting, -1 if it is
  130.     distance = math.floor(distance)
  131.     if distance == 0 then
  132.         return true, 0
  133.     end
  134.     if distance < 0 then
  135.         distance = math.abs(distance)
  136.         direction = direction + 2
  137.         endMultiply = -1
  138.     end
  139.     direction = (math.rad((math.floor(direction) % 4) * 90))
  140.     for a = 1, distance do
  141.         if getMapBit(x+(math.sin(direction)*a), y+(math.cos(direction)*a)) then
  142.             return false, (a-1)*endMultiply
  143.         end
  144.     end
  145.     if getMapBit(x+(math.sin(direction)*distance), y+(math.cos(direction)*distance)) then
  146.         return false, (distance-1)*endMultiply
  147.     else
  148.         return true, distance*endMultiply
  149.     end
  150. end
  151.  
  152. doPhysics = function()
  153.     for k,v in pairs(players) do
  154.         if math.abs(v.xv) > Xmaxspeed then
  155.             if v.xv > 0 then
  156.                 players[k].xv = Xmaxspeed
  157.             else
  158.                 players[k].xv = -Xmaxspeed
  159.             end
  160.         end
  161.         if math.abs(v.yv) > Ymaxspeed then
  162.             if v.yv > 0 then
  163.                 players[k].yv = Ymaxspeed
  164.             else
  165.                 players[k].yv = -Ymaxspeed
  166.             end
  167.         end
  168.         --do Y map collision
  169.         players[k].yv = v.yv + gravity
  170.         local isClear, dist = checkDistance(v,0,v.yv)
  171.         players[k].y = v.y + dist
  172.         if not isClear then
  173.             players[k].yv = 0
  174.         end
  175.         --do X map collision
  176.         local isClear, dist = checkDistance(v,3,v.xv)
  177.         players[k].x = v.x + dist
  178.         if not isClear then
  179.             players[k].xv = 0
  180.         end
  181.         players[k].grounded = not checkDistance(v,1,0)
  182.     end
  183. end
  184.  
  185. render = function()
  186.     local cTXT,cBG = term.getTextColor(),term.getBackgroundColor()
  187.     term.setBackgroundColor(colors.black)
  188.     term.clear()
  189.     paintutils.drawImage(mappaint,xscroll or 0,yscroll or 0)
  190.     local dx,dy --drawnX, drawnY
  191.     for k,v in pairs(players) do
  192.         dx = v.x-xscroll
  193.         dy = v.y-yscroll
  194.         if isOnscreen(dx,dy) then
  195.             term.setCursorPos(dx,dy)
  196.             term.setTextColor(v.txt)
  197.             term.setBackgroundColor(v.bg)
  198.             term.write(v.char)
  199.         end
  200.     end
  201.     term.setTextColor(cTXT)
  202.     term.setBackgroundColor(cBG)
  203. end
  204.  
  205. mapPrompt = function()
  206.     term.setBackgroundColor(colors.black)
  207.     term.setTextColor(colors.white)
  208.     term.clear()
  209.     print("Select map:")
  210.     local doopmap = read()
  211.     setNewMap(doopmap)
  212. end
  213.  
  214. doEverything = function()
  215.     while true do
  216.         if keysDown[controls.right] then
  217.             players[youPlayer].xv = players[youPlayer].xv + 1
  218.         elseif keysDown[controls.left] then
  219.             players[youPlayer].xv = players[youPlayer].xv - 1
  220.         else
  221.             if players[youPlayer].xv > 0 then
  222.                 players[youPlayer].xv = players[youPlayer].xv - 1
  223.             else
  224.                 players[youPlayer].xv = players[youPlayer].xv + 1
  225.             end
  226.         end
  227.         if keysDown[controls.jump] and players[youPlayer].grounded then
  228.             players[youPlayer].yv = -4
  229.         end
  230.         doPhysics()
  231.         sleep(0) --yes I fucking know that sleep(0.05) is the same as sleep(0) stop fucking telling me for christs sake
  232.     end
  233. end
  234.  
  235. alwaysRender = function()
  236.     while true do
  237.         render()
  238.         sleep(0)
  239.     end
  240. end
  241.  
  242. while true do
  243.     mapPrompt()
  244.     parallel.waitForAny(doEverything,getInput,alwaysRender)
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement