ArchTyler

CaosRPG -[CODE]-

Oct 25th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local ScreenWidth, ScreenHeight = term.getSize()
  2. local char = ">"
  3. local playerX = math.floor(ScreenWidth / 2)
  4. local playerY = math.floor(ScreenHeight / 2)
  5. local offsetX, offsetY = 0, 0
  6. local map = {}
  7.  
  8. function drawCharacter()
  9.   term.setCursorPos(playerX - offsetX, playerY - offsetY)
  10.   write(char)
  11. end
  12.  
  13. function newObject(x, y, obj)
  14.   map[y] = map[y] or {}; map[y][x] = {obj}
  15. end
  16.  
  17. function newSolidObject(x, y, obj)
  18.   map[x] = map[y] or {}; map[y][x] = {obj, true}
  19. end
  20.  
  21. function checkForCollision(x, y)
  22.   return map[y] and map[y][x] and map[y][x][2]
  23. end
  24.  
  25. function drawMap()
  26.   term.clear()
  27.   for y = 1, ScreenHeight do
  28.     if map[y + offsetY] then
  29.       for x = 1, ScreenWidth do
  30.         if map[y + offsetY][x + offsetX] then
  31.           term.setCursorPos(x, y)
  32.           term.write(map[y + offsetY][x + offsetX][1])
  33.         end
  34.       end
  35.     end
  36.   end
  37.   drawCharacter()
  38. end
  39.  
  40. newSolidObject(4, 5, "x")
  41. newSolidObject(6, 6, "x")
  42. newSolidObject(5, 5, "x")
  43.  
  44.  
  45. newObject(7, 7, "w")
  46. newObject(8, 8, "w")
  47. newObject(8, 7, "w")
  48.  
  49.  
  50. while true do
  51.   drawMap()
  52.   local e, key = os.pullEvent("key")
  53.   if (key == 17 or key == 200) then
  54.     if not checkForCollision(playerX, playerY - 1) then
  55.       offsetY = offsetY - 1; playerY = playerY - 1
  56.     end; char = "^"
  57.   elseif (key == 31 or key == 208) then
  58.     if not checkForCollision(playerX, playerY + 1) then
  59.       offsetY = offsetY + 1; playerY = playerY + 1
  60.     end; char = "v"
  61.   elseif (key == 203 or key == 30) then
  62.     if not checkForCollision(playerX - 1, playerY) then
  63.       offsetX = offsetX - 1; playerX = playerX - 1
  64.     end; char = "<"
  65.   elseif (key == 205 or key == 32) then
  66.     if not checkForCollision(playerX + 1, playerY) then
  67.       offsetX = offsetX + 1; playerX = playerX + 1
  68.     end; char = ">"
  69.   elseif key == 15 then
  70.     break
  71.   end
  72. end
Advertisement
Add Comment
Please, Sign In to add comment