View Single Post
02-28-14, 05:14 AM   #1
sirann
A Flamescale Wyrmkin
Join Date: Mar 2007
Posts: 142
Health Text Code Optimization

Good morning,

the following segment of code is working exactly as intended, it just feels rather awkward, particularly the hcur = UnitHealth('player') or UnitHealth('vehicle') in the UNIT_HEALTH event. I imagine there's a better way to do this, any help would be greatly appreciated:

Lua Code:
  1. local ht = CreateFrame('Button', 'PlayerHealthTextFrame', self)
  2.             ht:SetSize(50,10)
  3.             ht:SetPoint('LEFT', self, 'RIGHT', 7, 3)
  4.             ht:EnableMouse(false)
  5.             ht:RegisterEvent('PLAYER_ENTERING_WORLD')
  6.             ht:RegisterEvent('PLAYER_DEAD')
  7.             ht:RegisterEvent('PLAYER_ALIVE')
  8.             ht:RegisterUnitEvent('UNIT_HEALTH', 'player', 'vehicle')
  9.             ht:RegisterUnitEvent('UNIT_ENTERING_VEHICLE', 'player')
  10.             ht:RegisterUnitEvent('UNIT_ENTERED_VEHICLE', 'player')
  11.             ht:RegisterUnitEvent('UNIT_EXITING_VEHICLE', 'player')
  12.             ht:RegisterUnitEvent('UNIT_EXITED_VEHICLE', 'player')
  13.    
  14.         local htt = ht:CreateFontString('PlayerHealthText', 'OVERLAY')
  15.             htt:SetAllPoints(true)
  16.             htt:SetFont(cfg.font, 11)
  17.             htt:SetShadowColor(0,0,0,1)
  18.             htt:SetShadowOffset(-1,-1)
  19.             htt:SetJustifyH('LEFT')
  20.    
  21.         ht:SetScript('OnEvent', function(self, event, unit)
  22.             if event == 'PLAYER_DEAD' or event == 'PLAYER_ENTERING_WORLD' and UnitIsDead('player') then
  23.                 htt:SetFormattedText('|cff%02x%02x%02xDead', 180, 0, 0)
  24.             elseif event == 'PLAYER_ALIVE' and UnitIsGhost('player') or event == 'PLAYER_ENTERING_WORLD' and UnitIsGhost('player') then
  25.                 htt:SetFormattedText('|cff%02x%02x%02xGhost', 180, 0, 0)
  26.             else
  27.                 local r, g, l, hcur, hmax
  28.                 if event == 'PLAYER_ENTERING_WORLD' and UnitInVehicle('player') or event == 'UNIT_ENTERED_VEHICLE' or event == 'UNIT_ENTERING_VEHICLE' then
  29.                     hcur, hmax = UnitHealth('vehicle'), UnitHealthMax('vehicle')
  30.                 elseif event == 'PLAYER_ENTERING_WORLD' or 'UNIT_EXITING_VEHICLE' or 'UNIT_EXITED_VEHICLE' then
  31.                     hcur, hmax = UnitHealth('player'), UnitHealthMax('player')
  32.                 elseif event == 'UNIT_HEALTH' then
  33.                     hcur = UnitHealth('player') or UnitHealth('vehicle')
  34.                     hmax = UnitHealthMax('player') or UnitHealthMax('vehicle')
  35.                 end
  36.                 if hcur < hmax then
  37.                     r, g = 180, 0
  38.                 else
  39.                     r, g = 0, 180
  40.                 end
  41.                
  42.                 if hcur > 1000000 then
  43.                     l = 'm'
  44.                     hcur = round(hcur/1000000,1)
  45.                 elseif hcur > 1000 then
  46.                     l = 'k'
  47.                     hcur = round(hcur/1000)
  48.                 else
  49.                     l = ''
  50.                 end
  51.                 htt:SetFormattedText('|cff%02x%02x%02x%s'..l, r, g, 0, hcur)
  52.             end
  53.         end)
  Reply With Quote