AddCSLuaFile()

ENT.Type = "anim"
ENT.Base = "base_anim"
ENT.PrintName = "Barricade"
ENT.Category = "BWA"
ENT.Spawnable = true

if CLIENT then
    function ENT:Think()
        if self:GetNWBool('lamp') then
            local dlight = DynamicLight( self:EntIndex() )
            if ( dlight ) then
                dlight.pos = self:GetPos()+Vector(0,0,16)
                dlight.r = 200
                dlight.g = 200
                dlight.b = 255
                dlight.brightness = 0.01
                dlight.Decay = 1000
                dlight.Size = 64
                dlight.DieTime = CurTime() + 0.1
            end
        end
        if ( IsValid( self.lamp ) ) and self:GetNWBool('lamp') then
            self.lamp:SetPos( self:GetPos()+Vector(0,0,16) )
            self.lamp:SetAngles( self:GetAngles()+Angle(180,0,0) )
            self.lamp:Update()
        end
    end
    function ENT:OnRemove()
        if ( IsValid( self.lamp ) ) then
            self.lamp:Remove()
        end
    end
    function ENT:Initialize()
        timer.Simple(1, function()
            if self:GetNWBool('lamp') then
                local lamp = ProjectedTexture()
                self.lamp = lamp 
            
                lamp:SetTexture( "effects/flashlight001" )
                lamp:SetFarZ( 1024 )
                lamp:SetColor(Color(175,175,255,255))
                lamp:SetEnableShadows(true)
                lamp:SetPos( self:GetPos()+Vector(0,0,16)+self:GetForward()*32 )
                lamp:SetAngles( self:GetAngles()+Angle(180,0,0) )
                lamp:SetNearZ(8)
                lamp:Update()
            end
        end)
    end
    hook.Add("HUDPaint", "HPBarricadeFS", function()
        local ent = LocalPlayer():GetEyeTrace().Entity
        if IsValid(ent) then
            local bool = ent:GetClass() == "sur_barricade"
            if bool then
                local pos = ent:WorldSpaceCenter():ToScreen()
                local tab = {
                    w = 200,
                    h = 20,
                }

                surface.SetDrawColor(0,0,0,200)
                surface.DrawRect(pos.x-tab.w/2, pos.y-tab.h/2, tab.w, tab.h)
    
                surface.SetDrawColor(25,125,25)
                surface.DrawRect(pos.x-tab.w/2+4, pos.y-tab.h/2+2, (tab.w-8)*(ent:Health('health')/ent:GetNWFloat('healthmax')), tab.h-4)

                draw.SimpleText("Barricade", "SuR_SmallFont1", pos.x, pos.y-20, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
                draw.SimpleText(math.floor(ent:Health()).."/"..math.floor(ent:GetNWFloat('healthmax')), "Trebuchet18", pos.x, pos.y, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            end
        end
    end)
end