-- 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 #include "script/common.lua" pTimer = GetFloatParam("timer", 15) pSpeed = GetFloatParam("speed", 0.1) startUp = GetIntParam("startup", 0) clickUp = 0 clickDown = 0 motor = 0 motorSound = 0 up = 0 down = 0 upTimer = 0 downTimer = 0 upPressed = 0 downPressed = 0 limitMax = 0 limitMin = 0 function init() motor = FindJoint("motor") limitMin, limitMax = GetJointLimits(motor) down = FindShape("down") up = FindShape("up") clickUp = LoadSound("clickup.ogg") clickDown = LoadSound("clickdown.ogg") motorSound = LoadLoop("heavy_motor.ogg") SetShapeEmissiveScale(down, 0) SetShapeEmissiveScale(up, 0) SetTag(up, "interact", "loc@RAISE_BRIDGE") SetTag(down, "interact", "loc@LOWER_BRIDGE") downPressed = false downTimer = 0 upPressed = false upTimer = 0 --raise the bridge at the start if startUp == 1 then downTimer = 0 upTimer = pTimer upPressed = true press(up) end end function press(shape) PlaySound(clickDown) local s = GetShapeLocalTransform(shape) local d = TransformToParentVec(s, Vec(0,-.05,0)) s.pos = VecAdd(s.pos, d) SetShapeLocalTransform(shape, s) SetShapeEmissiveScale(shape, 1) end function unpress(shape) PlaySound(clickUp) local s = GetShapeLocalTransform(shape) local d = TransformToParentVec(s, Vec(0,.05,0)) s.pos = VecAdd(s.pos, d) SetShapeLocalTransform(shape, s) SetShapeEmissiveScale(shape, 0) SetJointMotor(motor, 0.0) end function tick(dt) downTimer = downTimer - dt upTimer = upTimer - dt local eps = 1 if InputPressed("interact") then if GetPlayerInteractShape() == down then if downPressed then unpress(down) downPressed = false else press(down) downPressed = true if upPressed then upPressed = false unpress(up) end end end if GetPlayerInteractShape() == up then if upPressed then unpress(up) upPressed = false else press(up) upPressed = true if downPressed then downPressed = false unpress(down) end end end end local sound = false if downPressed then SetJointMotor(motor, pSpeed) sound = true if GetJointMovement(motor) < limitMin+eps then unpress(down) downPressed = false end end if upPressed then SetJointMotor(motor, -pSpeed) sound = true if GetJointMovement(motor) > limitMax-eps then unpress(up) upPressed = false end end if sound then local p1 = TransformToParentPoint(GetBodyTransform(GetShapeBody(up)), GetShapeLocalTransform(up).pos) PlayLoop(motorSound, p1, 0.5) end end