motorSpeed = 1.0
motorStrength = 1000
epsilon = 0.01
function init()	
	up = FindShape("up")
	down = FindShape("down")
	joint = FindJoint("joint")
	elevator = FindBody("elevator")
	limitDown, limitUp = GetJointLimits(joint)
	clickSound = LoadSound("clickdown.ogg")
	motorSound = LoadLoop("heavy_motor")
	stuckTimer = 0.0
	motor = 0
	avgSpeed = 0
end
function tick(dt)
	if GetPlayerInteractShape() == up and InputPressed("interact") then
		PlaySound(clickSound)
		if motor == 1 then
			motor = 0
		else
			motor = 1
		end
	end
	if GetPlayerInteractShape() == down and InputPressed("interact") then
		PlaySound(clickSound)
		if motor == -1 then
			motor = 0
		else
			motor = -1
		end
	end
	avgSpeed = avgSpeed*0.9 + math.abs(GetBodyVelocity(elevator)[2])*0.1
	if motor ~= 0 and avgSpeed < motorSpeed*0.5 then
		stuckTimer = stuckTimer + dt
		if stuckTimer > 1.0 then
			stop()
		end
	else
		stuckTimer = 0
	end
	if motor == 1 then
		SetJointMotorTarget(joint, limitUp, motorSpeed, motorStrength)
		PlayLoop(motorSound, GetBodyTransform(elevator).pos)
		if GetJointMovement(joint) > limitUp-epsilon then
			stop()
		end
	elseif motor == -1 then
		SetJointMotorTarget(joint, limitDown, motorSpeed, motorStrength)
		PlayLoop(motorSound, GetBodyTransform(elevator).pos)
		if GetJointMovement(joint) < limitDown+epsilon then
			stop()
		end
	else
		SetJointMotor(joint, 0, motorStrength)
	end
	if motor == 1 then SetShapeEmissiveScale(up, 1) else SetShapeEmissiveScale(up, 0) end
	if motor == -1 then SetShapeEmissiveScale(down, 1) else SetShapeEmissiveScale(down, 0) end
end
function stop()
	PlaySound(clickSound)
	motor = 0
end