Advertisement
Guest User

Tribehouse Worm

a guest
Sep 1st, 2014
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. --------------
  2. --------------
  3. CONTROLLER = 'Firestonnne'
  4. KEY_BINDS = {UP=73,DOWN=75,LEFT=74,RIGHT=76}
  5. LENGTH = 10
  6. --------------
  7. --------------
  8. rendered = true
  9. wormHead = {x=40, y=20}
  10. dir = 1
  11.  
  12. function eventLoop(currentTime, timeRemaining)
  13.     if (dir == 1) then wormHead.y = wormHead.y - 1
  14.     elseif (dir == 2) then wormHead.x = wormHead.x + 1
  15.     elseif (dir == 3) then wormHead.y = wormHead.y + 1
  16.     elseif (dir == 4) then wormHead.x = wormHead.x - 1
  17.     end
  18.     tfm.exec.addConjuration(wormHead.x, wormHead.y, 500 * LENGTH)
  19.     rendered = true
  20. end
  21.  
  22. function turnRight()
  23.     dir = dir + 1
  24.     if (dir == 5) then dir = 1 end
  25. end
  26.  
  27. function turnLeft()
  28.     dir = dir - 1
  29.     if (dir == 0) then dir = 4 end
  30. end
  31.  
  32. function eventKeyboard(name, keyCode, down, x, y)
  33.     if (name ~= CONTROLLER or not rendered) then return end
  34.    
  35.     if (keyCode == KEY_BINDS.UP) then
  36.         if (dir == 2) then turnLeft()
  37.         elseif (dir == 4) then turnRight()
  38.         else return end
  39.     elseif (keyCode == KEY_BINDS.DOWN) then
  40.         if (dir == 2) then turnRight()
  41.         elseif (dir == 4) then turnLeft()
  42.         else return end
  43.     elseif (keyCode == KEY_BINDS.LEFT) then
  44.         if (dir == 1) then turnLeft()
  45.         elseif (dir == 3) then turnRight()
  46.         else return end
  47.     elseif (keyCode == KEY_BINDS.RIGHT) then
  48.         if (dir == 1) then turnRight()
  49.         elseif (dir == 3) then turnLeft()
  50.         else return end
  51.     end
  52.     rendered = false
  53. end
  54.  
  55. function bindKeys()
  56.     tfm.exec.bindKeyboard(CONTROLLER, KEY_BINDS.UP, true, true)
  57.     tfm.exec.bindKeyboard(CONTROLLER, KEY_BINDS.DOWN, true, true)
  58.     tfm.exec.bindKeyboard(CONTROLLER, KEY_BINDS.LEFT, true, true)
  59.     tfm.exec.bindKeyboard(CONTROLLER, KEY_BINDS.RIGHT, true, true)
  60. end
  61.  
  62. function setKeybind(keybind, key)
  63.     if (keybind == 'up') then
  64.         KEY_BINDS.UP = key
  65.     elseif (keybind == 'down') then
  66.         KEY_BINDS.DOWN = key
  67.     elseif (keybind == 'left') then
  68.         KEY_BINDS.LEFT = key
  69.     elseif (keybind == 'right') then
  70.         KEY_BINDS.RIGHT = key
  71.     end
  72.     bindKeys()
  73. end
  74.  
  75. function setController(name)
  76.     CONTROLLER = name
  77.     bindKeys()
  78. end
  79.  
  80. function eventChatCommand(name, cmd)
  81.     if (name ~= CONTROLLER) then return end
  82.     local args = {}
  83.     for i in string.gmatch(cmd, "%S+") do table.insert(args, i) end
  84.     if (table.getn(args) > 1 and args[1] == 'worm') then
  85.         if (args[2] == 'reset') then start()
  86.         elseif (table.getn(args) > 3 and args[2] == 'set') then
  87.             if (args[3] == 'controller') then CONTROLLER = args[4]
  88.             elseif (args[3] == 'length') then LENGTH = tonumber(args[4])
  89.             elseif (args[3] == 'keybind' and args[4] == 'default') then
  90.                 KEY_BINDS = {UP=73,DOWN=75,LEFT=74,RIGHT=76}
  91.                 bindKeys()
  92.             elseif (table.getn(args) > 4 and args[3] == 'keybind') then
  93.                 setKeybind(args[4], tonumber(args[5]))
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. function start()
  100.     system.disableChatCommandDisplay('worm', true)
  101.     bindKeys()
  102.     dir = 1
  103.     wormHead = {x=40, y=20}
  104. end
  105.  
  106. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement