-----------------------------------------
-- FT CONTUSION (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
local concussionActive = false
local lastConcussionSound = 0
local function ApplyShake(endTime)
    local ply = LocalPlayer()
    if not ply or not ply:IsValid() then return end
    if not ply:Alive() then return end

    local remainingTime = endTime - CurTime()
    local intensity = math.Clamp(remainingTime / 11, 0, 1)

    local randAng =
        Angle(math.Rand(-1, 1), math.Rand(-1, 1), math.Rand(-1, 1)) * intensity
end

net.Receive("StartConcussionEffect", function()
    local effectDuration = 15
    local endTime = CurTime() + effectDuration

    if concussionActive then
        hook.Remove("HUDPaint", "DarkenScreenEffect")
        hook.Remove("RenderScreenspaceEffects", "BlurScreenEffect")
        hook.Remove("CalcView", "ConcussionCameraShake")
        timer.Remove("ShakeEffectTimer")
        timer.Remove("ConcussionEnd")
    end

    concussionActive = true

    if CurTime() - lastConcussionSound > 10 then
        surface.PlaySound("explosion_contusion.mp3")
        lastConcussionSound = CurTime()
    end

    hook.Add("HUDPaint", "DarkenScreenEffect", function()
        local ply = LocalPlayer()
        if not IsValid(ply) or not ply:Alive() then return end
        local alpha = math.Clamp((endTime - CurTime()) / effectDuration * 225, 0, 225)
        draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha))
    end)

    hook.Add("RenderScreenspaceEffects", "BlurScreenEffect", function()
        local ply = LocalPlayer()
        if not IsValid(ply) or not ply:Alive() then return end
        local blur = (math.sin(CurTime() * 3) * 5 + 5) * 5 *
                     math.Clamp((endTime - CurTime()) / effectDuration, 0, 1)
        DrawMotionBlur(0.1, blur, 0.05)
    end)

    timer.Create("ShakeEffectTimer", 0.05, effectDuration * 20, function()
        ApplyShake(endTime)
    end)

    timer.Create("ConcussionEnd", effectDuration, 1, function()
        concussionActive = false

        hook.Remove("HUDPaint", "DarkenScreenEffect")
        hook.Remove("RenderScreenspaceEffects", "BlurScreenEffect")
        hook.Remove("CalcView", "ConcussionCameraShake")
        timer.Remove("ShakeEffectTimer")

        if not IsValid(LocalPlayer()) then return end

        local gasps = {
            "gasp/focus_gasp_01.wav",
            "gasp/focus_gasp_02.wav",
            "gasp/focus_gasp_03.wav",
            "gasp/focus_gasp_04.wav",
            "gasp/focus_gasp_05.wav",
            "gasp/focus_gasp_06.wav"
        }

        surface.PlaySound(table.Random(gasps))
    end)
end)

local originalWalk, originalRun = nil, nil
local slowEnd = 0

net.Receive("ConcussionSlowdown", function()
    local mult = net.ReadFloat()
    local duration = net.ReadFloat()
    local ply = LocalPlayer()

    if not IsValid(ply) then return end

    if not originalWalk then
        originalWalk = ply:GetWalkSpeed()
        originalRun = ply:GetRunSpeed()
    end

    ply:SetWalkSpeed(originalWalk * mult)
    ply:SetRunSpeed(originalRun * mult)

    slowEnd = CurTime() + duration

    timer.Create("ConcussionRestoreSpeed", duration, 1, function()
        if IsValid(ply) then
            ply:SetWalkSpeed(originalWalk)
            ply:SetRunSpeed(originalRun)
        end
    end)
end)

hook.Add("PlayerSpawn", "Concussion_ResetOnRespawn", function(ply)
    if ply ~= LocalPlayer() then return end

    concussionActive = false

    hook.Remove("HUDPaint", "DarkenScreenEffect")
    hook.Remove("RenderScreenspaceEffects", "BlurScreenEffect")
    hook.Remove("CalcView", "ConcussionCameraShake")

    timer.Remove("ShakeEffectTimer")
    timer.Remove("ConcussionEnd")

    if originalWalk and originalRun then
        ply:SetWalkSpeed(originalWalk)
        ply:SetRunSpeed(originalRun)
    end
end)

-----------------------------------------
-- FT CONTUSION (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
