
local entityColors = {
    ["ent_lootbox_low"] = Color(255, 255, 0, 255),
    ["ent_lootbox_medium"] = Color(0, 255, 0, 255),
    ["ent_lootbox_granade"] = Color(255, 0, 0, 255),
    ["ent_lootbox_weapons"] = Color(0, 0, 255, 255),
}

hook.Add("HUDPaint", "ESP_DrawLootbox", function()
    for _, ent in ipairs(ents.GetAll()) do
        if string.StartWith(ent:GetClass(), "ent_lootbox") then
            local color = entityColors[ent:GetClass()] or Color(255, 255, 255, 255)

            local mins, maxs = ent:OBBMins(), ent:OBBMaxs()
            local corners = {
                ent:LocalToWorld(Vector(mins.x, mins.y, mins.z)),
                ent:LocalToWorld(Vector(mins.x, mins.y, maxs.z)),
                ent:LocalToWorld(Vector(mins.x, maxs.y, mins.z)),
                ent:LocalToWorld(Vector(mins.x, maxs.y, maxs.z)),
                ent:LocalToWorld(Vector(maxs.x, mins.y, mins.z)),
                ent:LocalToWorld(Vector(maxs.x, mins.y, maxs.z)),
                ent:LocalToWorld(Vector(maxs.x, maxs.y, mins.z)),
                ent:LocalToWorld(Vector(maxs.x, maxs.y, maxs.z))
            }

            local screenCorners = {}
            for _, corner in ipairs(corners) do
                local screenPos = corner:ToScreen()
                if screenPos.visible then
                    table.insert(screenCorners, screenPos)
                end
            end

            if #screenCorners == 8 then
                surface.SetDrawColor(color)

                local edges = {
                    {1, 2}, {2, 4}, {4, 3}, {3, 1},
                    {5, 6}, {6, 8}, {8, 7}, {7, 5},
                    {1, 5}, {2, 6}, {3, 7}, {4, 8}
                }

                for _, edge in ipairs(edges) do
                    local p1, p2 = screenCorners[edge[1]], screenCorners[edge[2]]
                    surface.DrawLine(p1.x, p1.y, p2.x, p2.y)
                end
            end
        end
    end
end)
