-- DESCRIPTION: Rotates a non-animating door when player interacts with it. When door is initially opened, play . When the door is opening, play and when closing, play , when door is unlocked play . local Q = require "scriptbank\\quatlib" local U = require "scriptbank\\utillib" local deg = math.deg local rad = math.rad local doors = {} local names = {} local keyPressed = false g_door_rotate_lockable = {} function door_rotate_lockable_properties(e) end function door_rotate_lockable_init_name( e, name ) g_door_rotate_lockable[e] = {} Include( "quatlib.lua" ) Include( "utillib.lua" ) names[ e ] = name end local timeLastFrame = nil local timeDiff = 1 local controlEnt = nil function door_rotate_lockable_main(e) local door = doors[ e ] if door == nil then -- initialise local Ent = g_Entity[ e ] local _, _, _, ax, ay, az = GetObjectPosAng( Ent.obj ) doors[ e ] = { obj = Ent.obj, timer = math.huge, state = 'Closed', angle = 0, quat = Q.FromEuler( rad( ax ), rad( ay ), rad( az ) ), blocking = 1 } doors[ e ].originalx = -1 doors[ e ].originaly = -1 doors[ e ].originalz = -1 return end if doors[ e ].originalx == -1 then doors[ e ].originalx = g_Entity[e]['x'] doors[ e ].originaly = g_Entity[e]['y'] doors[ e ].originalz = g_Entity[e]['z'] for a = 0, 9 do local connectede = GetEntityRelationshipID(e, a) if connectede > 0 then if g_key[connectede] ~= nil then door.state = "Locked" g_door_rotate_lockable[e].key = connectede break end end end return end if controlEnt == nil then controlEnt = e end local timeThisFrame = g_Time if controlEnt == e then if timeLastFrame == nil then timeLastFrame = timeThisFrame timeDiff = 1 else timeDiff = ( timeThisFrame - timeLastFrame ) / 20 timeLastFrame = timeThisFrame end end if GetPlayerDistance(e) > 120 then if door.state == "Closed" then if g_Entity[e].activated == 1 then PlaySound( e, 0 ) --PerformLogicConnections(e) door.state = 'Knob' door.timer = timeThisFrame + 500 end elseif door.state == "Open" then if g_Entity[e].activated == 0 then door.state = 'Closing' keyPressed = true PlaySound( e, 2 ) end end end if U.PlayerLookingNear( e, 100, 140 ) then -- looking at door so prompt if door.state == "Locked" then if g_Entity[g_door_rotate_lockable[e].key].health <= 0 then Prompt("Door is locked, use key to unlock it? (E)") if g_KeyPressE == 1 then if not keyPressed then PlaySound(e, 3) --PerformLogicConnections(e) SetActivated(e,0) door.state = "Closed" keyPressed = true end else keyPressed = false end else Prompt("Door is locked, find the key to unlock it") end elseif door.state == 'Closed' then Prompt( "Press E to open door" ) if g_KeyPressE == 1 then if not keyPressed then --SetAnimationFrames( 1, 30) --PlayAnimation( e ) PlaySound( e, 0 ) --PerformLogicConnections(e) door.state = 'Knob' door.timer = timeThisFrame + 500 keyPressed = true end else keyPressed = false end elseif door.state == 'Open' then Prompt( "Press E to close door" ) if g_KeyPressE == 1 then if not keyPressed then door.state = 'Closing' keyPressed = true PlaySound( e, 2 ) end else keyPressed = false end end end if door.state == 'Knob' and timeThisFrame > door.timer then door.state = 'Opening' PlaySound( e, 1 ) door.timer = math.huge end if door.state == 'Opening' then if door.angle < 90 then door.angle = door.angle + timeDiff local rotAng = door.angle if names[ e ] == 'Right' then rotAng = -rotAng end local rotq = Q.FromEuler( 0, rad( rotAng ), 0 ) local ax, ay, az = Q.ToEuler( Q.Mul( door.quat, rotq ) ) CollisionOff( e ) ResetRotation( e, deg( ax ), deg( ay ), deg( az ) ) CollisionOn( e ) else door.state = 'Open' door.blocking = 2 end elseif door.state == 'Closing' then if door.angle > 0 then door.angle = door.angle - timeDiff local rotAng = door.angle if names[ e ] == 'Right' then rotAng = -rotAng end local rotq = Q.FromEuler( 0, rad( rotAng ), 0 ) local ax, ay, az = Q.ToEuler( Q.Mul( door.quat, rotq ) ) CollisionOff( e ) ResetRotation( e, deg( ax ), deg( ay ), deg( az ) ) CollisionOn( e ) else door.state = 'Closed' door.blocking = 1 end end -- navmesh blocker system if door.blocking ~= 0 then local blockmode = 0 if door.blocking == 1 then blockmode = 1 end if door.blocking == 2 then blockmode = 0 end RDBlockNavMesh(door.originalx,door.originaly,door.originalz,30,blockmode) door.blocking = 0 end end