function init()
    PLATFORM_VELOCITY = 0.6

    platform_body = FindBody("platform")
    platform_joints = FindJoints("platform")
    btn_up = FindShape("up")
    btn_down = FindShape("down")

    min, max = GetJointLimits(platform_joints[1])
    state = 0
    stateTime = 0.0

    clickUp = LoadSound("clickup.ogg")
	clickDown = LoadSound("clickdown.ogg")
	motorSound = LoadLoop("vehicle/hydraulic-loop.ogg")
end

function setState(s)
    state = s
    stateTime = 0.0
end

function timedOut()
    return stateTime > 10.0
end

function tick(dt)

    stateTime = stateTime + dt

    if state == 1 then
        local m = GetJointMovement(platform_joints[1])
        if m >= (max - 0.01) or timedOut() then
            setState(0)
        end
    elseif state == 2 then
        local m = GetJointMovement(platform_joints[1])
        if m <= 0.01 or timedOut() then
            setState(0)
        end
    end

    if state ~= 0 then
        SetBodyActive(platform_body, true)
        PlayLoop(motorSound, GetBodyTransform(platform_body).pos)
    end

    if GetPlayerInteractShape() == btn_up and InputPressed("interact") then
        setState(1)
        PlaySound(clickUp)
        for i=1, #platform_joints do
            local joint = platform_joints[i]
            SetJointMotorTarget(joint, max, PLATFORM_VELOCITY)
        end
    elseif GetPlayerInteractShape() == btn_down and InputPressed("interact") then
        setState(2)
        PlaySound(clickDown)
        for i=1, #platform_joints do
            local joint = platform_joints[i]
            SetJointMotorTarget(joint, min, PLATFORM_VELOCITY)
        end
    end
end
