Optimum usage

Help with these render optimization
 avatar
unknown
lua
3 years ago
4.3 kB
17
Indexable
local opt_time = 0
local threshold = 50

function moveCrane()
    if (not isCraneActive) then
        return false
    end
    if (not isElement(crane_arm)) then
        return false
    end
    if (isChatBoxInputActive()) then
        return false
    end
    local x, y, z = getElementPosition(crane)
    local rx, ry, rz = getElementRotation(crane_arm)
    local hrx, hry, hrz = getElementRotation(crane_head)
    local cspeedx, cspeedy, cspeedz = getElementVelocity(crane)
    for i, v in pairs(container) do
        if isElement(v) then
            local posx, posy, posz = getElementPosition(v)
            local actx, acty, actz = getElementVelocity(v)
            if ((actx > 0 and acty > 0) and posz ~= 17.83) then
                model = getElementModel(v)
                containerMove = v
                break
            end
        end
    end
    if (containerMove) then
        speedx, speedy, speedz = getElementVelocity(containerMove)
        cx, cy, cz = getElementPosition(containerMove)
        crx, cry, crz = getElementRotation(containerMove)
    end
    if cameraMovement1 then
        setCameraMatrix(x, y, z+75, x, y, 0, 135)
    elseif cameraMovement2 then
        local newX, newY = getPointFromDistanceRotation(x, y, 69, -rz)
        setCameraMatrix(x, y, z+40, newX, newY, 0, 0)
    elseif cameraMovement3 then
        local newX, newY = getPointFromDistanceRotation(x, y, 10, -rz)
        local newLX, newLY = getPointFromDistanceRotation(x, y, 69, -rz)
        local rplus = rx
        if rx > 347 then
            rplus = 0
        end
        setCameraMatrix(newX, newY, z, newLX, newLY, -10 + rplus, 0)
    end
    if ((getKeyState("arrow_l") and not getKeyState("arrow_r")) and getTickCount() - time_opt > threshold) then
        local ax, ay = x + travelspeed, y + travelspeed
        if (ax >= orX and ay >= orY) then
            ax, ay = orX, orY
            limitationTravel = true
        else
            limitationTravel = false
        end
        setElementPosition(crane, ax, ay, z)
        setElementPosition(crane_head, ax, ay, 27)
        setElementPosition(crane_arm, ax, ay, 35.5)
        time_opt = getTickCount()
    elseif (getKeyState("arrow_r") and not getKeyState("arrow_l") and getTickCount() - time_opt > threshold) then
        local ax, ay = x - travelspeed, y - travelspeed
        if (ax <= limitRx and ay <= limitRy) then
            ax, ay = limitRx, limitRy
            limitationTravel = true
        else 
            limitationTravel = false
        end
        setElementPosition(crane, ax, ay, z)
        setElementPosition(crane_head, ax, ay, 27)
        setElementPosition(crane_arm, ax, ay, 35.5)
        time_opt = getTickCount()
    end
    if getKeyState("s") and getTickCount() - time_opt > threshold then --lift
        arx = rx + liftdropspeed
        if arx > 45 and arx < 348 then
            arx = 45
        end
        time_opt = getTickCount()
    elseif getKeyState("w") and getTickCount() - time_opt > threshold then --drop
        arx = rx - liftdropspeed
        if arx < 348 and arx > 45 then
            arx = 348
        end
        time_opt = getTickCount()
    end
    if getKeyState("a") and getTickCount() - time_opt > threshold then 
        arz = rz + spinspeed
        time_opt = getTickCount()
    elseif getKeyState("d") and getTickCount() - time_opt > threshold then
        arz = rz - spinspeed
        time_opt = getTickCount()
    end
    setElementRotation(crane_arm, arx or rx, ry, arz or rz)
    setElementRotation(crane_head, hrx, hry, arz or rz)
    if containerMove and getKeyState("q") and getTickCount() - time_opt > threshold then
        local axx, ayy, azz = getElementAngularVelocity(containerMove)
        local carz = crz + spinspeed * 1.1
        setElementRotation(containerMove, crx, cry, carz)
        time_opt = getTickCount()
    elseif containerMove and getKeyState("e") and getTickCount() - time_opt > threshold then
        local axx, ayy, azz = getElementAngularVelocity(containerMove)
        local carz = crz - spinspeed * 1.1
        setElementRotation(containerMove, crx, cry, carz)
        time_opt = getTickCount()
    end
end
addEventHandler("onClientRender", root, moveCrane)
Editor is loading...