local me = LocalPlayer()

local width, height = ScrW(), ScrH()
local centerX, centerY = width / 2, height / 2

local muzzledata = false

local function GetPosition()
    local vm = me:GetViewModel()

    if not IsValid( vm ) then
        muzzle = false
        return centerX, centerY, false
    end

    local muzzle = vm:LookupAttachment( "muzzle" )

    if muzzle == 0 then
        muzzle = false
        return centerX, centerY, false
    end

    muzzledata = vm:GetAttachment( muzzle )
    return 0, 0, true
end

local traceStruct = {
    start = Vector(),
    endpos = Vector(),
    filter = me
}

hook.Add( "HUDPaint","1234", function()
    local time = RealTime() * 128
    local x, y, f = GetPosition()

    if f then
        traceStruct.start = muzzledata.Pos 
        traceStruct.endpos = muzzledata.Pos + muzzledata.Ang:Forward() * 4096

        local tr = util.TraceLine( traceStruct )
        local pos = tr.HitPos:ToScreen()

        x, y = pos.x, pos.y
    end


	for degrees = 1, 3 do 
        local deg = math.rad( degrees * 120 + time )

        local tx = math.cos( deg ) * 16 + x
        local ty = math.sin( deg ) * 16 + y

        for i = 1, 3 do 
            local deg = math.rad( ( i - 1 ) * 120 + time ) + ( i % 1 )
            
            local subx = math.cos( deg ) * 8 + tx
            local suby = math.sin( deg ) * 8 + ty
    
            local nextDeg = math.rad( i * 120 + time )
        
            local nextx = math.cos( nextDeg ) * 8 + tx
            local nexty = math.sin( nextDeg ) * 8 + ty

            surface.SetDrawColor( 255, 0, 0 )
            surface.DrawLine( subx, suby, nextx, nexty )

            if f then
                local start = muzzledata.Pos:ToScreen()
                local vx, vy = start.x, start.y
                
                surface.SetDrawColor( 255, 0, 0, 32 )
                surface.DrawLine( vx, vy, subx, suby )
                surface.DrawLine( vx, vy, nextx, nexty )
            end
        end
	end
end )