Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. --config
  2. running = true
  3. debug = false
  4. updaterate = 0.1
  5.  
  6. --var setup
  7. local w, h = term.getSize()
  8. entitys = {}
  9. entityCount = 0
  10. ticks = 0
  11.  
  12. --classes setup
  13. local Man = {}
  14. Man.default = {name = "Noname", strenght = 100, speed = 1, sprite = "", x = 1, y = 1}
  15. Man.mt = {__index = Man.default}
  16. function Man:new(t)
  17.     setmetatable(t, Man.mt)
  18.     entitys[entityCount] = t
  19.     entityCount = entityCount + 1
  20.     return t
  21. end
  22.  
  23. local Wall1 = {}
  24. Wall1.default = {sprite = "|", resistance = 200, x = 1, y = 1}
  25. Wall1.mt = {__index = Wall1.default}
  26. function Wall1:new(t)
  27.     setmetatable(t, Wall1.mt)
  28.     entitys[entityCount] = t
  29.     entityCount = entityCount + 1
  30.     return t
  31. end
  32.  
  33.  
  34. --Game
  35.  
  36. --creating objects
  37. local man = Man:new({sprite = "o", x = 2 + 1, y = 2})
  38. local wall = Wall1:new({x = 17, y = 6})
  39.  
  40. term.clear()
  41. term.setCursorPos(1,1)
  42.  
  43. function checkPosMove(x, y, dir)
  44.     local canMove = true
  45.     if dir == 17 then
  46.         for i = 0,#entitys do if y-1 == entitys[i].y and x == entitys[i].x then canMove = false end end
  47.     end
  48.     if dir == 31 then
  49.         for i = 0,#entitys do if y+1 == entitys[i].y and x == entitys[i].x then canMove = false end end
  50.     end
  51.     if dir == 30 then
  52.         for i = 0,#entitys do if x-1 == entitys[i].x and y == entitys[i].y then canMove = false end end
  53.     end
  54.     if dir == 32 then
  55.         for i = 0,#entitys do if x+1 == entitys[i].x and y == entitys[i].y then canMove = false end end
  56.     end
  57.     return canMove
  58. end
  59.  
  60. function move(Obj, key)
  61.    
  62.     if key == 17 and checkPosMove(Obj.x, Obj.y, key) then
  63.         Obj.y = Obj.y - 1
  64.     end
  65.     if key == 31 and checkPosMove(Obj.x, Obj.y, key) then
  66.         Obj.y = Obj.y + 1
  67.     end
  68.     if key == 30 and checkPosMove(Obj.x, Obj.y, key) then
  69.         Obj.x = Obj.x - 1
  70.     end
  71.     if key == 32 and checkPosMove(Obj.x, Obj.y, key) then
  72.         Obj.x = Obj.x + 1
  73.     end
  74.    
  75. end
  76.  
  77. function tick()
  78.     ticks = ticks + 1
  79.     term.clear()
  80.     term.setCursorPos(1,1)
  81. end
  82.  
  83. function draw()
  84.     term.clear()
  85.     term.setCursorPos(1,1)
  86.     for i = 0,#entitys do
  87.         term.setCursorPos(entitys[i].x, entitys[i].y)  
  88.         term.write(entitys[i].sprite)
  89.     end
  90. end
  91.  
  92. fps = os.startTimer(updaterate)
  93. while running do
  94.     local ev, p1 = os.pullEvent()
  95.  
  96.     draw()
  97.     if ev == "key" then
  98.         move(man, p1)
  99.        
  100.         if p1 == 16 then
  101.             term.clear()
  102.             term.setCursorPos(1,1)
  103.             running = false
  104.         end
  105.     end
  106.  
  107.     fps = os.startTimer(updaterate)
  108. end
Add Comment
Please, Sign In to add comment