local U = require "scriptbank\\utillib" -- controls how close the player needs to be before healthbar appears local showDistance = 1000 local gEnt = g_Entity local healthBars = {} local charList = {} local controlEnt = nil local ctrlTimer = math.huge local deg = math.deg local atan = math.atan2 local modf = math.modf local function getUnused( name ) for k, v in pairs( healthBars ) do if not v.used and v.name == name then return k end end end function health_bar_init_name( e, name ) Include( "utillib.lua" ) Hide( e ) healthBars[ e ] = { name = name, used = false } end function AttachHealthBar ( c ) local Char = charList[ c ] if Char == nil then charList[ c ] = { maxHealth = gEnt[ c ].health, red = nil, green = nil, timer = g_Time + 200 } return end if g_Time < Char.timer then return end Char.timer = g_Time + 200 if gEnt[ c ].health < Char.maxHealth or U.PlayerCloserThan( c, showDistance ) then if Char.red == nil and Char.green == nil then local redBar = getUnused( 'HealthBarRed' ) local greenBar = getUnused( 'HealthBarGreen' ) if redBar == nil or greenBar == nil then return end -- we've found our health bars so attach to our character Char.red = redBar healthBars[ redBar ].used = true Char.green = greenBar healthBars[ greenBar ].used = true end elseif gEnt[ c ].health <= 0 or not U.PlayerCloserThan( c, showDistance ) then -- too far away from player so free up the bars if Char.red ~= nil then Hide( Char.red ) healthBars[ Char.red ].used = false Char.red = nil end if Char.green ~= nil then Hide( Char.green ) healthBars[ Char.green ].used = false Char.green = nil end end end local function repositionHealthBars( character, rB, gB, Px, Pz ) local Ent = gEnt[ character ] local ang = atan( Px - Ent.x, Pz - Ent.z ) local xo, zo = 0, 0 local mH = charList[ character ].maxHealth local tweakFactor = 3.1 * mH / 100 if Ent.health > 0 then -- position green bar xo = ( ( mH / 2 ) - ( Ent.health / 2 ) ) / tweakFactor ScaleObject( gEnt[ gB ].obj, modf( Ent.health / mH * 100 ), 19, 100 ) xo, _, zo = U.Rotate3D( xo, 0, 0, 0, ang, 0 ) CollisionOff( gB ) SetPosition( gB, Ent.x - xo, Ent.y + 87.5, Ent.z - zo ) SetRotation( gB, 0, deg( ang ), 0 ) CollisionOn( gB ) Show( gB ) -- position red bar xo = ( -( mH / 2 ) + ( mH - Ent.health) / 2 ) / tweakFactor ScaleObject( gEnt[ rB ].obj, modf( ( mH - Ent.health ) / mH * 100 ), 19, 100 ) xo, _, zo = U.Rotate3D( xo, 0, 0, 0, ang, 0 ) CollisionOff( rB ) SetPosition( rB, Ent.x - xo, Ent.y + 87.5, Ent.z - zo ) SetRotation( rB, 0, deg( ang ), 0 ) CollisionOn( rB ) Show( rB ) else Hide( rB ) healthBars[ rB ].used = false Hide( gB ) healthBars[ gB ].used = false charList[ character ] = nil end end function health_bar_main(e) if controlEnt == nil then controlEnt = e ctrlTimer = g_Time + 50 end if e ~= controlEnt then return end -- we've got this far so this entity is controlling all the action if g_Time > ctrlTimer then local PX, PZ = g_PlayerPosX, g_PlayerPosZ for k, v in pairs( charList ) do if v.red ~= nil and v.green ~= nil then repositionHealthBars( k, v.red, v.green, PX, PZ ) end end ctrlTimer = g_Time + 50 end end