Skip to content

Instantly share code, notes, and snippets.

@vega0
Created November 4, 2019 17:06
Show Gist options
  • Save vega0/d181e7e05fd3fae19bd4c11fb1fbc535 to your computer and use it in GitHub Desktop.
Save vega0/d181e7e05fd3fae19bd4c11fb1fbc535 to your computer and use it in GitHub Desktop.
- breakable props
if SERVER then
--for k, v in pairs(ents.GetAll()) do v.health = nil end
local function color_multiple(c, value)
return Color(c.r * value, c.g * value, c.b * value)
end
local function on_entity_destroy(ent) ent:EmitSound("doors/vent_open3.wav") end
hook.Add ("EntityTakeDamage", "HandleEntitiesHealth", function(target, dmg )
if target:IsPlayer() then return end
local phys = target:GetPhysicsObject()
if not IsValid(phys) then return end
if not target.health then
target.initial_health = phys:GetVolume()
target:SetNWInt("2", target.initial_health)
target.health = target.initial_health
target.initial_color = target:GetColor()
end
local ed = EffectData()
ed:SetOrigin( target:GetPos() )
ed:SetEntity( target )
util.Effect( "entity_remove", ed, true, true )
target:SetColor(color_multiple(target.initial_color, (target.health / target.initial_health)))
if target.health < 0
then on_entity_destroy(target) target:Remove()
else target.health = target.health - dmg:GetDamage() target:SetNWInt("1", target.health)
end
end)
return
end
local function draw_progress(percent, tw, th)
surface.SetDrawColor(100, 100, 100)
surface.DrawRect(ScrW() / 2 - 150, ScrH() / 2 - 25 / 2 + 100, 300, 25)
surface.SetDrawColor(0,255,0)
surface.DrawRect(ScrW() / 2 - (150 * percent), ScrH() / 2 - 25 / 2 + 100, 300 * percent, 25)
end
hook.Add("HUDPaint", "DrawDamageInfo", function()
local ent = LocalPlayer():GetEyeTrace().Entity
if not IsValid(ent) then return end
local health, initial = ent:GetNWInt("1"), ent:GetNWInt("2")
if health == 0 then return end
if ent:GetPos():Distance(LocalPlayer():GetPos()) > 250 then return end
surface.SetFont("DermaLarge")
surface.SetTextColor(255,0,0)
local percent = health / initial
local text = tostring(percent * 100) .. "%"
local w, h = surface.GetTextSize(text)
surface.SetTextPos(ScrW() / 2 - w / 2, ScrH() / 2 - h / 2 + 100)
draw_progress(percent, w, h)
surface.DrawText(text)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment