Advertisement
BigSHinyToys

Another RPG example

Jun 5th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local function loadMap(path)
  2.     local tOut = {}
  3.     if fs.exists(path) and not fs.isDir(path) then
  4.         local file = fs.open(path,"r")
  5.         if file then
  6.             local sLine = file.readLine()
  7.             while sLine do
  8.                 tOut[#tOut + 1] = {}
  9.                 for x = 1,#sLine do
  10.                     tOut[#tOut][x] = sLine:sub(x,x)
  11.                 end
  12.                 sLine = file.readLine()
  13.             end
  14.         else
  15.             error("Unable to open file")
  16.         end
  17.     else
  18.         error("Incorrect path")
  19.     end
  20.     return tOut
  21. end
  22.  
  23. local function drawMap(tMap,offsetX,offsetY)
  24.     offsetX = offsetX or 0 -- make shore these have a value
  25.     offsetY = offsetY or 0
  26.     local termX,termY = term.getSize()
  27.     for y = 1,termY do
  28.         term.setCursorPos(1,y)
  29.         if tMap[y - offsetY] then
  30.             for x = 1,termX do
  31.                 if tMap[y - offsetY][x - offsetX] then
  32.                     term.write(tMap[y - offsetY][x - offsetX])
  33.                 else
  34.                     term.write(" ")
  35.                 end
  36.             end
  37.         end
  38.     end
  39. end
  40.  
  41. local offX,offY = 0,0 -- offset of the map to be draw from
  42. local map = loadMap("map01") -- will load a file called map01 you can change this to the name of your actual map
  43. local termX,termY = term.getSize()
  44. local playerX = math.floor(termX/2)
  45. local playerY = math.floor(termY/2)
  46. local text = ""
  47.  
  48. local function draw()
  49.     term.clear()
  50.     drawMap(map,offX,offY)
  51.     term.setCursorPos(playerX,playerY)
  52.     term.write("@")
  53.     term.setCursorPos(1,termY)
  54.     term.write("X "..tostring(offX).." Y "..tostring(offY).."  Text : "..tostring(text))
  55. end
  56.  
  57. while true do
  58.     draw()
  59.     local event = {os.pullEvent()}
  60.     if event[1] == "key" then
  61.         if event[2] == 200 then -- up key
  62.             offY = offY + 1 -- we change thses to the players moves relitive to map
  63.         elseif event[2] == 208 then -- down key
  64.             offY = offY - 1
  65.         elseif event[2] == 203 then -- left
  66.             offX = offX + 1
  67.         elseif event[2] == 205 then -- right
  68.             offX = offX - 1
  69.         elseif event[2] == 14 then -- Press [Backspace] key to exit
  70.             break
  71.         end
  72.     end
  73.     if map[playerY - offY] and map[playerY - offY][playerX - offX] then
  74.         text = map[playerY - offY][playerX - offX]
  75.     else
  76.         text = ""
  77.     end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement