Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.3 kB
3
Indexable
Never
function createLeafObject(rack)
    local distance = 0.7
    local initialOffset = -1.0
    local rackPositions = {
        {x = initialOffset, y = 0, z = rack.coords.z + 1.7},
        {x = initialOffset + distance, y = 0, z = rack.coords.z + 1.7},
        {x = initialOffset + 2 * distance, y = 0, z = rack.coords.z + 1.7},
        {x = initialOffset + 3 * distance, y = 0, z = rack.coords.z + 1.7},
    }

    rack.leafObjects = {}

    for _, leafData in ipairs(rack.dryingData) do
        if not leafData.netId then
            local weedTypeData = getWeedTypeByLeaf(leafData.leafType)

            if not weedTypeData then
                print("Error: Could not find weed type for leafType: " .. tostring(leafData.leafType))
                return
            end

            local leafObject = weedTypeData.leafRackObject
            if not leafObject then
                print("Error: leafRackObject not found for leafType: " .. tostring(leafData.leafType))
                return
            end

            local baseCoords = rackPositions[leafData.positionIndex]
            local rotatedX, rotatedY = rotateAroundPoint(baseCoords.x, baseCoords.y, 0, 0, rack.currentHeading)

            local finalCoords = {
                x = rack.coords.x + rotatedX,
                y = rack.coords.y + rotatedY,
                z = baseCoords.z
            }

            local obj = CreateObjectNoOffset(leafObject, finalCoords.x, finalCoords.y, finalCoords.z, true, false, false)
            if DoesEntityExist(obj) then
                FreezeEntityPosition(obj, true)

                SetEntityRotation(obj, 0.0, 0.0, rack.currentHeading + 90.0, 2, true)

                local netId = NetworkGetNetworkIdFromEntity(obj)

                leafData.netId = netId
                leafData.object = leafObject 
            else
                print("Error: Failed to create leaf object for leafType: " .. tostring(leafData.leafType))
            end
        end
        table.insert(rack.leafObjects, leafData.netId)
    end

    if #rack.dryingData ~= #rack.leafObjects then
        print("Warning: dryingData and leafObjects length mismatch for rack with netId " .. rack.netId)
    end

    saveRackData()
end
Leave a Comment