No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

healthbar.lua 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. HealthBar = {}
  2. -- private variables
  3. local healthBarWindow
  4. local healthBar
  5. local manaBar
  6. local healthLabel
  7. local manaLabel
  8. -- public functions
  9. function HealthBar.create()
  10. healthBarWindow = displayUI('healthbar.otui', { parent = Game.gameRightPanel })
  11. healthBarButton = TopMenu.addGameButton('healthBarButton', 'Healh Bar', 'healthbar.png', HealthBar.toggle)
  12. healthBarButton:setOn(true)
  13. healthBar = healthBarWindow:getChildById('healthBar')
  14. manaBar = healthBarWindow:getChildById('manaBar')
  15. healthLabel = healthBarWindow:getChildById('healthLabel')
  16. manaLabel = healthBarWindow:getChildById('manaLabel')
  17. end
  18. function HealthBar.destroy()
  19. healthBarWindow:destroy()
  20. healthBarWindow = nil
  21. healthBarButton:destroy()
  22. healthBarButton = nil
  23. healthBar = nil
  24. manaBar = nil
  25. healthLabel = nil
  26. manaLabel = nil
  27. end
  28. function HealthBar.toggle()
  29. local visible = not healthBarWindow:isExplicitlyVisible()
  30. healthBarWindow:setVisible(visible)
  31. healthBarButton:setOn(visible)
  32. end
  33. -- hooked events
  34. function HealthBar.onHealthChange(health, maxHealth)
  35. healthLabel:setText(health .. ' / ' .. maxHealth)
  36. healthBar:setPercent(health / maxHealth * 100)
  37. end
  38. function HealthBar.onManaChange(mana, maxMana)
  39. manaLabel:setText(mana .. ' / ' .. maxMana)
  40. local percent
  41. if maxMana == 0 then
  42. percent = 100
  43. else
  44. percent = (mana * 100)/maxMana
  45. end
  46. manaBar:setPercent(percent)
  47. end
  48. connect(Game, { onGameStart = HealthBar.create,
  49. onGameEnd = HealthBar.destroy,
  50. onHealthChange = HealthBar.onHealthChange,
  51. onManaChange = HealthBar.onManaChange })