-- Controls an elevator on a prismatic joint with two buttons -- The elevator continues at given speed until reaching the joint limit or the timer runs out pTimer = GetFloatParam("timer", 0.5) pSpeed = GetFloatParam("speed", 2.0) motor = nil limitMin = nil limitMax = nil down = nil up = nil clickUp = nil chime = nil motorSound = nil cabin = nil downPressed = nil downTimer = nil upPressed = nil upTimer = nil currentClicked = nil motorTimer = nil function init() motor = FindJoint("motor") limitMin, limitMax = GetJointLimits(motor) down = FindShapes("down",false) up = FindShapes("up",false) clickUp = LoadSound("clickup.ogg") chime = LoadSound("elevator-chime.ogg") motorSound = LoadLoop("vehicle/hydraulic-loop.ogg") cabin = FindBody("cabin") for i=1,#down do SetTag(down[i], "interact", "loc@DOWN") SetShapeEmissiveScale(down[i], 0) end for i=1,#up do SetTag(up[i], "interact", "loc@UP") SetShapeEmissiveScale(up[i], 0) end downPressed = false downTimer = 0 upPressed = false upTimer = 0 currentClicked = nil motorTimer = 0.1 end function press(shape) local t = GetShapeLocalTransform(shape) PlaySound(clickUp, TransformToParentPoint(GetBodyTransform(GetShapeBody(shape)), t.pos)) t.pos[1] = t.pos[1] + 0.05 SetShapeLocalTransform(shape, t) motorTimer = 5 if downPressed then for i=1,#down do SetShapeEmissiveScale(down[i], 1) end end if upPressed then for i=1,#up do SetShapeEmissiveScale(up[i], 1) end end end function unpress(shape) local t = GetShapeLocalTransform(shape) PlaySound(clickUp, TransformToParentPoint(GetBodyTransform(GetShapeBody(shape)), t.pos)) t.pos[1] = t.pos[1] - 0.05 SetShapeLocalTransform(shape, t) if downPressed then for i=1,#down do SetShapeEmissiveScale(down[i], 0) end end if upPressed then for i=1,#up do SetShapeEmissiveScale(up[i], 0) end end end function tick(dt) downTimer = downTimer - dt upTimer = upTimer - dt local motorPos = GetBodyTransform(cabin).pos for i=1,#down do if GetPlayerInteractShape() == down[i] and InputPressed("interact") and not upPressed then upTimer = 0 downTimer = pTimer if not downPressed then downPressed = true press(down[i]) currentClicked = down[i] end end end for i=1,#up do if GetPlayerInteractShape() == up[i] and InputPressed("interact") and not downPressed then downTimer = 0 upTimer = pTimer if not upPressed then upPressed = true press(up[i]) currentClicked = up[i] end end end if downTimer <= 0 then downTimer = 0 if downPressed then PlaySound(chime,motorPos) unpress(currentClicked) end downPressed = false end if upTimer <= 0 then upTimer = 0 if upPressed then PlaySound(chime,motorPos) unpress(currentClicked) end upPressed = false end local eps = 0.01 if motorTimer > 0 then if downPressed and motorTimer > 0.1 then SetJointMotor(motor, pSpeed) PlayLoop(motorSound,motorPos) if GetJointMovement(motor) < limitMin+eps then downTimer = 0.0 end elseif upPressed and motorTimer > 0.1 then SetJointMotor(motor, -pSpeed) PlayLoop(motorSound,motorPos) if GetJointMovement(motor) > limitMax-eps then upTimer = 0.0 end else SetJointMotor(motor, 0.0) motorTimer = 0.0 end motorTimer = motorTimer - dt end end