local U = require "scriptbank\\utillib" -- controls how close the player needs to be before healthbar appears local showDistance = 2000 local Entity local healthBars = {} local charList = {} local controlEnt = nil local ctrlTimer = math.huge local deg = math.deg local atan = math.atan2 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 } Entity = g_Entity end function AttachHealthBar ( c ) local Char = charList[ c ] if Char == nil then charList[ c ] = { maxHealth = g_Entity[ 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 U.PlayerCloserThan( c, showDistance ) then if Char.red == nil and Char.green == nil and Char.body == nil then local redBar = getUnused( 'HealthBarRed' ) local greenBar = getUnused( 'HealthBarGreen' ) local hbBody = getUnused( 'HealthBarBody' ) if redBar == nil or greenBar == nil or hbBody == 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 Char.body = hbBody healthBars[ hbBody ].used = true end else -- 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 healthBars[ hbBody ].used = false Char.body = nil end if Char.green ~= nil then Hide( Char.green ) healthBars[ Char.green ].used = false Char.green = nil end if Char.body ~= nil then Hide(Char.body ) healthBars[ hbBody ].used = false Char.body = nil end end end local function repositionHealthBars( character, body, rB, gB, Px, Pz ) local Ent = Entity[ 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 ( Entity[ gB ].obj, ( 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 ( Entity[ rB ].obj, ( ( 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 ) -- position body xo, _, zo = U.Rotate3D( 0, 0, 1, 0, ang, 0 ) ScaleObject ( Entity[ body ].obj, 100, 32, 100 ) CollisionOff( body ) ResetPosition( body, Ent.x + xo, Ent.y + 86, Ent.z + zo) SetRotation( body, 0, deg( ang ), 0 ) CollisionOn( body ) Show( body ) else Hide( rB ) healthBars[ rB ].used = false Hide( gB ) healthBars[ gB ].used = false Hide( body ) healthBars[ body ].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 and v.body ~= nil then repositionHealthBars( k, v.body, v.red, v.green, PX, PZ ) end end ctrlTimer = g_Time + 50 end end