Mousetat

Snowball fight health bar

Feb 17th, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. local defaultMaxHealth = 100
  2. local defaultObjectDamage = 2
  3.  
  4. local keys = {space = 32, left = 37, right = 39, a = 65, d = 68, e = 69, q = 81}
  5. local players = {}
  6. local objects = {}
  7.  
  8. local settings = {
  9. throwKeys = {[keys.e] = true},
  10. leftKeys = {[keys.left] = true, [keys.a] = true, [keys.q] = true},
  11. rightKeys = {[keys.right] = true, [keys.d] = true},
  12. }
  13.  
  14. function main()
  15. for n in pairs(tfm.get.room.playerList) do
  16. eventNewPlayer(n)
  17. end
  18. end
  19.  
  20. function pythag(x1, y1, x2, y2, r)
  21. local x = x2 - x1
  22. local y = y2 - y1
  23. local r = r + r
  24. return x * x + y * y < r * r
  25. end
  26.  
  27. function setKeys(n)
  28. for i, key in pairs(keys) do
  29. tfm.exec.bindKeyboard(n, key, true, true)
  30. end
  31. end
  32.  
  33. function showHealthBar(n)
  34. local player = players[n]
  35.  
  36. local healthLen = math.floor(tonumber(player.health) * (250 / defaultMaxHealth))
  37. if healthLen < 80 then healthLen = 80 elseif healthLen > 250 then healthLen = 250 end
  38.  
  39. ui.addTextArea(0, "", n, 10, 25, 250, 20, 0x324650, 0x89a7f5, 0.7, true)
  40. ui.addTextArea(1, "<p align='center'>"..player.health.." Health", n, 10, 25, healthLen, 20, 0x171918, 0x171918, 0.5, true)
  41. end
  42.  
  43. function eventNewGame()
  44. for n in pairs(tfm.get.room.playerList) do
  45. eventPlayerRespawn(n)
  46. end
  47. ui.setShamanName("Get ready..")
  48. ui.setMapName("pow's wars")
  49. end
  50.  
  51. function eventLoop()
  52. for i, object in pairs(tfm.get.room.objectList) do
  53. local id = object.id
  54. if objects[id] and not objects[id].didDamage then
  55. for n, data in pairs(tfm.get.room.playerList) do
  56. if objects[id].spawner ~= n and pythag(object.x, object.y, data.x, data.y, 50) and (math.abs(object.vx) > 2 or math.abs(object.vy) > 2) then
  57. local player = players[n]
  58. if player then
  59. player.health = player.health - defaultObjectDamage
  60. -- player.health = player.health - math.abs(object.vx) - math.abs(object.vy) - defaultObjectDamage
  61.  
  62. if player.health <= 0 then tfm.exec.killPlayer(n) end
  63. showHealthBar(n)
  64. objects[id].didDamage = true
  65. end
  66. end
  67. end
  68. end
  69. end
  70. end
  71.  
  72. function eventKeyboard(n, key, down, x, y)
  73. local data = tfm.get.room.playerList[n]
  74. local player = players[n]
  75. if not data.isDead then
  76. if settings.rightKeys[key] then
  77. player.direction = 1
  78. elseif settings.leftKeys[key] then
  79. player.direction = -1
  80. elseif settings.throwKeys[key] then
  81. local id = tfm.exec.addShamanObject(34, x + (20 * player.direction), y - 10, 0, math.random(8, 12) * player.direction, 0, false)
  82. objects[id] = {spawner = n, didDamage = false}
  83. end
  84. end
  85. end
  86.  
  87. function eventNewPlayer(n)
  88. if not players[n] then
  89. players[n] = {health = defaultMaxHealth, direction = 1}
  90. end
  91. setKeys(n)
  92. showHealthBar(n)
  93. end
  94.  
  95. function eventPlayerDied(n)
  96. players[n].health = 0
  97. showHealthBar(n)
  98.  
  99. local num, pn = 0, ""
  100. for n, data in pairs(tfm.get.room.playerList) do
  101. if not data.isDead then
  102. pn = n
  103. num = num + 1
  104. end
  105. end
  106. if num == 1 then
  107. tfm.exec.setGameTime(5)
  108. tfm.exec.giveCheese(pn)
  109. tfm.exec.playerVictory(pn)
  110. elseif num == 0 then
  111. tfm.exec.newGame(maps[math.random(#maps)])
  112. end
  113. end
  114.  
  115. function eventPlayerRespawn(n)
  116. players[n].health = defaultMaxHealth
  117. showHealthBar(n)
  118. end
  119.  
  120. main()
Advertisement
Add Comment
Please, Sign In to add comment