Jump to content

Alternative Sliding Door Script


reepblue
 Share

Recommended Posts

I noticed that the SlidingDoor.lua script from the Leadwerks 3.0 demo was a tad different from the current stock one. After fixing the Joint:Slider call, I've noticed that doors reset to the exact position it was when it is closed.

 

altvsstock.png

 

You can try it for yourself, I've modified the script to have the loop sound, and release everything properly. There might be a good reason why it was changed with 3.1, but I've found it to be more reliable. The door will also not collapse if a lot of heavy objects are on top of it if you're using it as a lift.

 

import "Scripts/Functions/ReleaseTableObjects.lua"

Script.enabled=true--bool "Enabled"
Script.openstate=false--bool "Start Open"
Script.offset=Vec3(0)--Vec3 "Offset"
Script.movespeed=1--float "Move speed" 0,100,3
Script.opensoundfile=""--path "Open Sound" "Wav File (*wav):wav|Sound"
Script.closesoundfile=""--path "Close Sound" "Wav File (*wav):wav|Sound"
Script.loopsoundfile=""--path "Loop Sound" "Wav File (*wav):wav|Sound"
Script.manualactivation=false--bool "Manual activate"
Script.closedelay=2000--int "Close delay"

function Script:Start()
self.entity:SetGravityMode(false)
if self.entity:GetMass()==0 then
Debug:Error("Entity mass must be greater than 0.")
end

self.sound={}

if self.opensoundfile~="" then self.sound.open = Sound:Load(self.opensoundfile) end
if self.loopsoundfile~="" then self.sound.loop = Sound:Load(self.loopsoundfile) end
if self.closesoundfile~="" then self.sound.close = Sound:Load(self.closesoundfile) end

if self.sound.loop~=nil then
self.loopsource = Source:Create()
self.loopsource:SetSound(self.sound.loop)
self.loopsource:SetLoopMode(true)
self.loopsource:SetRange(50)
end

if self.manualactivation==false then self.Use=nil end

self.opentime=0
if self.openstate then
self.openposition=self.entity:GetPosition(true)
self.closedposition=self.openposition+self.offset
self.desiredposition=Vec3(self.openposition.x,self.openposition.y,self.openposition.z)
else
self.closedposition=self.entity:GetPosition(true)
self.openposition=self.closedposition+self.offset
self.desiredposition=Vec3(self.closedposition.x,self.closedposition.y,self.closedposition.z)
end

if self.openstate then
self.openangle=0
self.closedangle=self.offset:Length()
else
self.openangle=self.offset:Length()
self.closedangle=0
end

local pin=self.offset:Normalize()
self.base=Pivot:Create()
self.base:SetPosition(self.closedposition)
self.joint = Joint:Slider(self.closedposition.x,self.closedposition.y,self.closedposition.z,pin.x,pin.y,pin.z, self.entity,self.base)
self.joint:EnableLimits()
self.joint:SetLimits(0,self.offset:Length())
end

function Script:Use()
self:Toggle()
end

function Script:Toggle()--in
if self.enabled then
if self.openstate then
self:Close()
else
self:Open()
end
end
end

function Script:Open()--in
if self.enabled then
self.opentime = Time:GetCurrent()
if self.openstate==false then
self.openstate=true
if self.opensound then
self.entity:EmitSound(self.opensound)
end
self.component:CallOutputs("Open")
if self.loopsource~=nil then
self.loopsource:SetPosition(self.entity:GetPosition(true))
if self.loopsource:GetState()==Source.Stopped then
self.loopsource:Play()
end
end
end
end
end

function Script:Close()--in
if self.enabled then
if self.openstate then
if self.closesound then
self.entity:EmitSound(self.closesound)
end
self.openstate=false
self.component:CallOutputs("Close")
if self.loopsource~=nil then
self.loopsource:SetPosition(self.entity:GetPosition(true))
if self.loopsource:GetState()==Source.Stopped then
self.loopsource:Play()
end
end
end
end
end

function Script:Disable()--in
self.enabled=false
end

function Script:Enable()--in
self.enabled=true
end

function Script:UpdatePhysics()

--Disable loop sound
if self.sound.loop~=nil then
local angle
if self.openstate then
angle = self.openangle
else
angle = self.closedangle
end
if math.abs(self.joint:GetAngle()-angle)<0.1 then
if self.loopsource:GetState()~=Source.Stopped then
self.loopsource:Stop()
end
else
if self.loopsource:GetState()==Source.Stopped then
self.loopsource:Resume()
end
end
if self.loopsource:GetState()==Source.Playing then
self.loopsource:SetPosition(self.entity:GetPosition(true))
end
end

if self.openstate then
if self.closedelay>0 then
local time = Time:GetCurrent()
if time-self.opentime>self.closedelay then
self:Close()
end
end
end

--Figure out where the door should be
local diff
if self.openstate then
diff = self.openposition - self.desiredposition
else
diff = self.closedposition - self.desiredposition
end
local l = diff:Length()
if l>self.movespeed/60 then
diff = diff:Normalize() * self.movespeed/60
end
self.desiredposition = self.desiredposition + diff

--Find the difference between where we want the door to be, and where it is
diff = self.desiredposition - self.entity:GetPosition()

--If the difference is more than a tiny bit, use physics to add forces to make it go where we want
if diff:Length()>0.01 then
self.entity:PhysicsSetPosition(self.desiredposition.x,self.desiredposition.y,self.desiredposition.z)
return
end
end

function Script:Release()
ReleaseTableObjects(self.sound)
if self.loopsource then
self.loopsource:Release()
self.loopsource=nil
end
self.sound=nil

self.base:Release()
self.base=nil
end

  • Upvote 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...