AddCSLuaFile()

ENT.PrintName = "Gas Zone"
ENT.Base = "base_anim"

if SERVER then
    
    function ENT:Initialize()
        self:SetModel("models/hunter/blocks/cube025x025x025.mdl")
        self:SetNoDraw(true)
        self:SetNWFloat('radius', 1)
        self.DelayGasDamage = 0
        self.Dist = self.Dist or 5000
    end

    function ENT:Think()
        local newnum = self:GetNWFloat('radius')+(self.Dist/120/10)
        self:SetNWFloat('radius', newnum)
        if self.DelayGasDamage < CurTime() then
            self.DelayGasDamage = CurTime()+1
            local tab = player.GetAll()
            for i=1,#tab do
                local ply = tab[i]
                if ply:GetPos():DistToSqr(self:GetPos()) > self:GetNWFloat('radius')^2 or !ply:Alive() then continue end
                if ply:Health() > 5 then
                    ply:TakeDamage(5, self)
                else
                    ply:SuR_MakeZombie()
                end
                ply:PlayVoiceLine("infected", 20)
            end
        end
        self:NextThink(CurTime()+0.1)
        return true
    end

else

    local function create_particle(self)
        local size = math.Clamp(math.random(self:GetNWFloat('radius')/2,self:GetNWFloat('radius')), 10, self:GetNWFloat('radius')/2)
        local rad = self:GetNWFloat('radius')-size/2

        if not self.gasEmitter then
            self.gasEmitter = ParticleEmitter(self:GetPos())
        end

        local particles = self.gasEmitter:Add("particle/particle_smokegrenade", self:GetPos() + VectorRand(-rad, rad))
        particles:SetVelocity(Vector(0, 0, 0))
        particles:SetDieTime(10)
        particles:SetStartAlpha(200)
        particles:SetEndAlpha(0)
        particles:SetStartSize(size)
        particles:SetEndSize(size)
        particles:SetRoll(math.Rand(-180, 180))
        particles:SetRollDelta(math.Rand(-0.5, 0.5))
        particles:SetColor(0,50,0)
    end

    local alpha = 0
    local draw_gas_effect = false
    hook.Add("PostDrawTranslucentRenderables", "SuR_Zone_Draw", function()
        local tab = ents.FindByClass("sur_gas_zone")
        for _, ent in ipairs(tab) do
            render.SetColorMaterial()
            render.CullMode(1)
            render.DrawSphere(ent:GetPos(), ent:GetNWFloat('radius'), 50 , 50 , Color(0,120,0,20))
            render.CullMode(0)
            render.DrawSphere(ent:GetPos(), ent:GetNWFloat('radius'), 50 , 50 , Color(0,120,0,20))
            render.BorderSphereUnit(Color(0,120,0,200), ent:GetPos(), ent:GetNWFloat('radius'), 64, 16)
            if not ent.Gas or ent.Gas < CurTime() then
                ent.Gas = CurTime()+0.01
                create_particle(ent)
            end
            draw_gas_effect = ent:GetPos():DistToSqr(LocalPlayer():GetPos()) <= ent:GetNWFloat('radius')^2 and LocalPlayer():Alive()
        end
        if #tab == 0 then
            draw_gas_effect = false
            alpha = 0
        end
    end)

    hook.Add("HUDPaint", "SuR_Zone_Draw", function()
        if draw_gas_effect then       
            alpha = math.min(alpha+FrameTime()/0.002, 200)
        else
            alpha = math.max(alpha-FrameTime()/0.002, 0)
        end
        surface.SetDrawColor(0,80,0,alpha)
        surface.DrawRect(0,0,ScrW(),ScrH())
    end)

end