function init() DOORS_1_VELOCITY = 1.0 DOORS_2_VELOCITY = 2.0 doors1 = FindJoints("door1") doors2 = FindJoints("door2") local shapes = FindShapes("dcoll") for i=1, #shapes do local shape = shapes[i] SetShapeCollisionFilter(shape, 2, 255-2) end btn_open = FindShape("open") btn_close = FindShape("close") state = 0 stateTime = 0.0 clickUp = LoadSound("clickup.ogg") clickDown = LoadSound("clickdown.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 atTarget = true for i=1, #doors1 do local joint = doors1[i] local min, _ = GetJointLimits(joint) local m = GetJointMovement(joint) if m > min + 1 and not timedOut() then atTarget = false end end for i=1, #doors2 do local joint = doors2[i] local min, _ = GetJointLimits(joint) local m = GetJointMovement(joint) if m > min + 1 and not timedOut() then atTarget = false end end if atTarget then setState(0) end elseif state == 2 then local atTarget = true for i=1, #doors1 do local joint = doors1[i] local _, max = GetJointLimits(joint) local m = GetJointMovement(joint) if m < max - 4 and not timedOut() then atTarget = false end end for i=1, #doors2 do local joint = doors2[i] local _, max = GetJointLimits(joint) local m = GetJointMovement(joint) if m < max - 4 and not timedOut() then atTarget = false end end if atTarget then setState(0) end end if state ~= 0 then for i=1, #doors1 do local joint = doors1[i] local shapes = GetJointShapes(joint) for j = 1, #shapes do local body = GetShapeBody(shapes[j]) if IsBodyDynamic(body) then SetBodyActive(body, true) end end end for i=1, #doors2 do local joint = doors2[i] local shapes = GetJointShapes(joint) for j = 1, #shapes do local body = GetShapeBody(shapes[j]) if IsBodyDynamic(body) then SetBodyActive(body, true) end end end end if GetPlayerInteractShape() == btn_close and InputPressed("interact") then setState(1) PlaySound(clickDown) for i=1, #doors1 do local joint = doors1[i] local min, _ = GetJointLimits(joint) SetJointMotorTarget(joint, min, DOORS_1_VELOCITY) end for i=1, #doors2 do local joint = doors2[i] local min, _ = GetJointLimits(joint) SetJointMotorTarget(joint, min, DOORS_2_VELOCITY) end elseif GetPlayerInteractShape() == btn_open and InputPressed("interact") then setState(2) PlaySound(clickUp) for i=1, #doors1 do local joint = doors1[i] local _, max = GetJointLimits(joint) SetJointMotorTarget(joint, max, DOORS_1_VELOCITY) end for i=1, #doors2 do local joint = doors2[i] local _, max = GetJointLimits(joint) SetJointMotorTarget(joint, max, DOORS_2_VELOCITY) end end end