2011-12-05 19:27:07 +01:00
|
|
|
HealthBar = {}
|
2011-11-04 00:10:12 +01:00
|
|
|
|
|
|
|
-- private variables
|
2012-01-24 19:39:16 +01:00
|
|
|
local healthBarWindow
|
2012-01-03 16:17:52 +01:00
|
|
|
local healthBar
|
|
|
|
local manaBar
|
|
|
|
local healthLabel
|
|
|
|
local manaLabel
|
2011-11-04 00:10:12 +01:00
|
|
|
|
|
|
|
-- public functions
|
2011-12-05 19:27:07 +01:00
|
|
|
function HealthBar.create()
|
2012-03-22 22:47:52 +01:00
|
|
|
healthBarWindow = displayUI('healthbar.otui', GameInterface.getRightPanel())
|
2012-01-24 19:39:16 +01:00
|
|
|
healthBarButton = TopMenu.addGameButton('healthBarButton', 'Healh Bar', 'healthbar.png', HealthBar.toggle)
|
|
|
|
healthBarButton:setOn(true)
|
|
|
|
healthBar = healthBarWindow:getChildById('healthBar')
|
|
|
|
manaBar = healthBarWindow:getChildById('manaBar')
|
|
|
|
healthLabel = healthBarWindow:getChildById('healthLabel')
|
|
|
|
manaLabel = healthBarWindow:getChildById('manaLabel')
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2011-12-05 19:27:07 +01:00
|
|
|
function HealthBar.destroy()
|
2012-01-24 19:39:16 +01:00
|
|
|
healthBarWindow:destroy()
|
|
|
|
healthBarWindow = nil
|
|
|
|
healthBarButton:destroy()
|
|
|
|
healthBarButton = nil
|
2012-01-03 16:17:52 +01:00
|
|
|
healthBar = nil
|
|
|
|
manaBar = nil
|
|
|
|
healthLabel = nil
|
|
|
|
manaLabel = nil
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2012-01-24 19:39:16 +01:00
|
|
|
function HealthBar.toggle()
|
|
|
|
local visible = not healthBarWindow:isExplicitlyVisible()
|
|
|
|
healthBarWindow:setVisible(visible)
|
|
|
|
healthBarButton:setOn(visible)
|
|
|
|
end
|
|
|
|
|
2011-11-04 00:10:12 +01:00
|
|
|
-- hooked events
|
2012-02-09 04:45:19 +01:00
|
|
|
function HealthBar.onHealthChange(localPlayer, health, maxHealth)
|
2012-01-03 16:17:52 +01:00
|
|
|
healthLabel:setText(health .. ' / ' .. maxHealth)
|
2011-11-14 09:21:01 +01:00
|
|
|
healthBar:setPercent(health / maxHealth * 100)
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2012-02-09 04:45:19 +01:00
|
|
|
function HealthBar.onManaChange(localPlayer, mana, maxMana)
|
2012-01-03 16:17:52 +01:00
|
|
|
manaLabel:setText(mana .. ' / ' .. maxMana)
|
2011-11-14 15:37:55 +01:00
|
|
|
|
|
|
|
local percent
|
|
|
|
if maxMana == 0 then
|
|
|
|
percent = 100
|
|
|
|
else
|
2012-01-03 16:17:52 +01:00
|
|
|
percent = (mana * 100)/maxMana
|
2011-11-14 15:37:55 +01:00
|
|
|
end
|
|
|
|
manaBar:setPercent(percent)
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2012-02-08 22:23:15 +01:00
|
|
|
connect(g_game, { onGameStart = HealthBar.create,
|
2012-02-09 04:45:19 +01:00
|
|
|
onGameEnd = HealthBar.destroy })
|
|
|
|
connect(LocalPlayer, { onHealthChange = HealthBar.onHealthChange,
|
|
|
|
onManaChange = HealthBar.onManaChange })
|