2011-12-05 19:27:07 +01:00
HealthBar = { }
2011-11-04 00:10:12 +01:00
2012-03-29 23:46:21 +02:00
-- constants
local Icons = { }
2012-04-26 21:54:16 +02:00
Icons [ 1 ] = { tooltip = tr ( ' You are poisoned ' ) , path = ' /game_healthbar/icons/poisoned.png ' , id = ' condition_poisoned ' }
Icons [ 2 ] = { tooltip = tr ( ' You are burning ' ) , path = ' /game_healthbar/icons/burning.png ' , id = ' condition_burning ' }
Icons [ 4 ] = { tooltip = tr ( ' You are electrified ' ) , path = ' /game_healthbar/icons/electrified.png ' , id = ' condition_electrified ' }
Icons [ 8 ] = { tooltip = tr ( ' You are freezing ' ) , path = ' /game_healthbar/icons/drunk.png ' , id = ' condition_drunk ' }
Icons [ 16 ] = { tooltip = tr ( ' You are protected by a magic shield ' ) , path = ' /game_healthbar/icons/magic_shield.png ' , id = ' condition_magic_shield ' }
Icons [ 32 ] = { tooltip = tr ( ' You are paralysed ' ) , path = ' /game_healthbar/icons/slowed.png ' , id = ' condition_slowed ' }
Icons [ 64 ] = { tooltip = tr ( ' You are hasted ' ) , path = ' /game_healthbar/icons/haste.png ' , id = ' condition_haste ' }
Icons [ 128 ] = { tooltip = tr ( ' You may not logout during a fight ' ) , path = ' /game_healthbar/icons/logout_block.png ' , id = ' condition_logout_block ' }
Icons [ 256 ] = { tooltip = tr ( ' You are drowing ' ) , path = ' /game_healthbar/icons/drowning.png ' , id = ' condition_drowning ' }
Icons [ 512 ] = { tooltip = tr ( ' You are freezing ' ) , path = ' /game_healthbar/icons/freezing.png ' , id = ' condition_freezing ' }
Icons [ 1024 ] = { tooltip = tr ( ' You are dazzled ' ) , path = ' /game_healthbar/icons/dazzled.png ' , id = ' condition_dazzled ' }
Icons [ 2048 ] = { tooltip = tr ( ' You are cursed ' ) , path = ' /game_healthbar/icons/cursed.png ' , id = ' condition_cursed ' }
Icons [ 4096 ] = { tooltip = tr ( ' You are strengthened ' ) , path = ' /game_healthbar/icons/strengthened.png ' , id = ' condition_strengthened ' }
Icons [ 8192 ] = { tooltip = tr ( ' You may not logout or enter a protection zone ' ) , path = ' /game_healthbar/icons/protection_zone_block.png ' , id = ' condition_protection_zone_block ' }
Icons [ 16384 ] = { tooltip = tr ( ' You are within a protection zone ' ) , path = ' /game_healthbar/icons/protection_zone.png ' , id = ' condition_protection_zone ' }
2012-05-12 03:44:13 +02:00
Icons [ 32768 ] = { tooltip = tr ( ' You are bleeding ' ) , path = ' /game_healthbar/icons/bleeding.png ' , id = ' condition_bleeding ' }
Icons [ 65536 ] = { tooltip = tr ( ' You are hungry ' ) , path = ' /game_healthbar/icons/hungry.png ' , id = ' condition_hungry ' }
2012-03-29 23:46:21 +02:00
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
2012-07-11 05:47:53 +02:00
local soulLabel
local soulPoints
2011-11-04 00:10:12 +01:00
-- public functions
2012-03-23 14:48:05 +01:00
function HealthBar . init ( )
connect ( LocalPlayer , { onHealthChange = HealthBar.onHealthChange ,
2012-03-29 23:46:21 +02:00
onManaChange = HealthBar.onManaChange ,
2012-07-11 05:47:53 +02:00
onStatesChange = HealthBar.onStatesChange ,
onSoulChange = HealthBar.onSoulChange } )
2012-03-30 12:06:33 +02:00
2012-03-29 23:46:21 +02:00
connect ( g_game , { onGameEnd = HealthBar.offline } )
2012-03-23 14:48:05 +01:00
2012-06-26 00:13:30 +02:00
healthBarWindow = g_ui.loadUI ( ' healthbar.otui ' , GameInterface.getRightPanel ( ) )
2012-04-26 21:54:16 +02:00
healthBarButton = TopMenu.addGameToggleButton ( ' healthBarButton ' , tr ( ' Health Bar ' ) , ' healthbar.png ' , HealthBar.toggle )
2012-01-24 19:39:16 +01:00
healthBarButton : setOn ( true )
2012-03-28 16:10:21 +02:00
healthBar = healthBarWindow : recursiveGetChildById ( ' healthBar ' )
manaBar = healthBarWindow : recursiveGetChildById ( ' manaBar ' )
healthLabel = healthBarWindow : recursiveGetChildById ( ' healthLabel ' )
manaLabel = healthBarWindow : recursiveGetChildById ( ' manaLabel ' )
2012-03-23 14:48:05 +01:00
2012-07-11 05:47:53 +02:00
soulLabel = healthBarWindow : recursiveGetChildById ( ' soulLabel ' )
2012-03-23 14:48:05 +01:00
if g_game.isOnline ( ) then
local localPlayer = g_game.getLocalPlayer ( )
HealthBar.onHealthChange ( localPlayer , localPlayer : getHealth ( ) , localPlayer : getMaxHealth ( ) )
HealthBar.onManaChange ( localPlayer , localPlayer : getMana ( ) , localPlayer : getMaxMana ( ) )
2012-06-07 04:16:18 +02:00
HealthBar.onStatesChange ( localPlayer , localPlayer : getStates ( ) , 0 )
2012-07-11 05:47:53 +02:00
HealthBar.onSoulChange ( localPlayer , localPlayer : getSoul ( ) )
2012-03-23 14:48:05 +01:00
end
2011-11-04 00:10:12 +01:00
end
2012-03-23 14:48:05 +01:00
function HealthBar . terminate ( )
disconnect ( LocalPlayer , { onHealthChange = HealthBar.onHealthChange ,
2012-03-29 23:46:21 +02:00
onManaChange = HealthBar.onManaChange ,
2012-07-11 05:47:53 +02:00
onStatesChange = HealthBar.onStatesChange ,
onSoulChange = HealthBar.onSoulChange } )
2012-03-30 12:06:33 +02:00
2012-03-29 23:46:21 +02:00
disconnect ( g_game , { onGameEnd = HealthBar.offline } )
2012-03-23 14:48:05 +01:00
2012-01-24 19:39:16 +01:00
healthBarWindow : destroy ( )
healthBarButton : destroy ( )
2012-03-28 16:10:21 +02:00
healthBarWindow = nil
2012-01-24 19:39:16 +01:00
healthBarButton = nil
2012-01-03 16:17:52 +01:00
healthBar = nil
manaBar = nil
healthLabel = nil
manaLabel = nil
2012-07-11 05:47:53 +02:00
soulLabel = nil
2012-04-27 06:54:14 +02:00
HealthBar = nil
2011-11-04 00:10:12 +01:00
end
2012-01-24 19:39:16 +01:00
function HealthBar . toggle ( )
2012-06-21 21:31:22 +02:00
if healthBarButton : isOn ( ) then
healthBarWindow : close ( )
healthBarButton : setOn ( false )
else
healthBarWindow : open ( )
healthBarButton : setOn ( true )
end
end
function HealthBar . onMiniWindowClose ( )
healthBarButton : setOn ( false )
2012-01-24 19:39:16 +01:00
end
2012-03-30 12:06:33 +02:00
function HealthBar . offline ( )
2012-06-22 07:26:22 +02:00
healthBarWindow : recursiveGetChildById ( ' conditionPanel ' ) : destroyChildren ( )
2012-03-30 12:06:33 +02:00
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-03-29 23:46:21 +02:00
function HealthBar . onStatesChange ( localPlayer , now , old )
2012-06-07 04:16:18 +02:00
if now == old then return end
2012-06-26 00:13:30 +02:00
2012-03-29 23:46:21 +02:00
local bitsChanged = bit32.bxor ( now , old )
for i = 1 , 32 do
2012-03-30 00:36:55 +02:00
local pow = math.pow ( 2 , i - 1 )
if pow > bitsChanged then break end
local bitChanged = bit32.band ( bitsChanged , pow )
2012-03-29 23:46:21 +02:00
if bitChanged ~= 0 then
HealthBar.toggleIcon ( bitChanged )
end
end
end
2012-07-11 05:47:53 +02:00
function HealthBar . onSoulChange ( localPlayer , soul )
soulLabel : setText ( ' Soul: ' .. soul )
end
2012-03-29 23:46:21 +02:00
function HealthBar . toggleIcon ( bitChanged )
2012-06-22 07:26:22 +02:00
local content = healthBarWindow : recursiveGetChildById ( ' conditionPanel ' )
2012-06-26 00:13:30 +02:00
2012-03-29 23:46:21 +02:00
local icon = content : getChildById ( Icons [ bitChanged ] . id )
if icon then
icon : destroy ( )
else
2012-06-26 00:13:30 +02:00
icon = g_ui.createWidget ( ' ConditionWidget ' , content )
2012-03-29 23:46:21 +02:00
icon : setId ( Icons [ bitChanged ] . id )
icon : setImageSource ( Icons [ bitChanged ] . path )
icon : setTooltip ( Icons [ bitChanged ] . tooltip )
end
end