2011-12-05 19:27:07 +01:00
|
|
|
HealthBar = {}
|
2011-11-04 00:10:12 +01:00
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local healthManaPanel = nil
|
|
|
|
|
|
|
|
-- public functions
|
2011-12-05 19:27:07 +01:00
|
|
|
function HealthBar.create()
|
|
|
|
healthManaPanel = UI.display('healthbar.otui', { parent = Game.gameRightPanel })
|
2011-11-04 00:10:12 +01:00
|
|
|
|
2011-11-14 09:21:01 +01:00
|
|
|
local healthBar = UIProgressBar.create()
|
|
|
|
healthManaPanel:addChild(healthBar)
|
|
|
|
healthBar:setId('healthBar')
|
|
|
|
healthBar:setStyle('HealthBar')
|
|
|
|
|
2011-11-04 00:10:12 +01:00
|
|
|
local healthLabel = UILabel.create()
|
|
|
|
healthManaPanel:addChild(healthLabel)
|
|
|
|
healthLabel:setId('healthLabel')
|
|
|
|
healthLabel:setStyle('HealthLabel')
|
|
|
|
healthLabel:setText('0 / 0')
|
2011-11-14 15:37:55 +01:00
|
|
|
|
2011-11-14 09:21:01 +01:00
|
|
|
local manaBar = UIProgressBar.create()
|
|
|
|
healthManaPanel:addChild(manaBar)
|
|
|
|
manaBar:setId('manaBar')
|
|
|
|
manaBar:setStyle('ManaBar')
|
2011-11-04 00:10:12 +01:00
|
|
|
|
|
|
|
local manaLabel = UILabel.create()
|
|
|
|
healthManaPanel:addChild(manaLabel)
|
|
|
|
manaLabel:setId('manaLabel')
|
|
|
|
manaLabel:setStyle('ManaLabel')
|
|
|
|
manaLabel:setText('0 / 0')
|
|
|
|
|
2011-11-14 09:21:01 +01:00
|
|
|
healthManaPanel:setHeight(healthBar:getHeight() + manaBar:getHeight() + 4)
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2011-12-05 19:27:07 +01:00
|
|
|
function HealthBar.destroy()
|
2011-11-04 00:10:12 +01:00
|
|
|
healthManaPanel:destroy()
|
|
|
|
healthManaPanel = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hooked events
|
2011-12-22 05:06:00 +01:00
|
|
|
function HealthBar.onHealthChange(health, maxHealth)
|
2011-11-04 00:10:12 +01:00
|
|
|
local label = healthManaPanel:getChildById('healthLabel')
|
|
|
|
label:setText(health .. ' / ' .. maxHealth)
|
2011-11-14 15:37:55 +01:00
|
|
|
|
2011-11-14 09:21:01 +01:00
|
|
|
local healthBar = healthManaPanel:getChildById('healthBar')
|
|
|
|
healthBar:setPercent(health / maxHealth * 100)
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2011-12-22 05:06:00 +01:00
|
|
|
function HealthBar.onManaChange(mana, maxMana)
|
2011-11-04 00:10:12 +01:00
|
|
|
local label = healthManaPanel:getChildById('manaLabel')
|
|
|
|
label:setText(mana .. ' / ' .. maxMana)
|
2011-11-14 15:37:55 +01:00
|
|
|
|
2011-11-14 09:21:01 +01:00
|
|
|
local manaBar = healthManaPanel:getChildById('manaBar')
|
2011-11-14 15:37:55 +01:00
|
|
|
|
|
|
|
local percent
|
|
|
|
if maxMana == 0 then
|
|
|
|
percent = 100
|
|
|
|
else
|
|
|
|
percent = mana / maxMana * 100
|
|
|
|
end
|
|
|
|
manaBar:setPercent(percent)
|
2011-11-04 00:10:12 +01:00
|
|
|
end
|
|
|
|
|
2011-12-05 19:27:07 +01:00
|
|
|
connect(Game, { onLogin = HealthBar.create,
|
2011-12-22 05:06:00 +01:00
|
|
|
onLogout = HealthBar.destroy,
|
|
|
|
onHealthChange = HealthBar.onHealthChange,
|
|
|
|
onManaChange = HealthBar.onManaChange })
|