Advertisement
Guest User

Untitled

a guest
Sep 27th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. import "Scripts/Functions/ReleaseTableObjects.lua"
  2.  
  3. Script.enabled=true--bool "Enabled"
  4. Script.openstate=false--bool "Start Open"
  5. Script.distance=Vec3(1,0,0)--Vec3 "Distance"
  6. Script.movespeed=1--float "Move speed" 0,100,3
  7. Script.opensoundfile=""--path "Open Sound" "Wav File (*wav):wav|Sound"
  8. Script.closesoundfile=""--path "Close Sound" "Wav File (*wav):wav|Sound"
  9. Script.loopsoundfile=""--path "Loop Sound" "Wav File (*wav):wav|Sound"
  10. Script.closedelay=2000--int "Close delay"
  11.  
  12. function Script:Start()
  13.     self.entity:SetGravityMode(false)
  14.     if self.entity:GetMass()==0 then
  15.         Debug:Error("Entity mass must be greater than 0.")
  16.     end
  17.  
  18.     self.sound={}
  19.  
  20.     if self.opensoundfile~="" then self.sound.open = Sound:Load(self.opensoundfile) end
  21.     if self.loopsoundfile~="" then self.sound.loop = Sound:Load(self.loopsoundfile) end
  22.     if self.closesoundfile~="" then self.sound.close = Sound:Load(self.closesoundfile) end
  23.    
  24.     if self.sound.loop~=nil then
  25.         self.loopsource = Source:Create()
  26.         self.loopsource:SetSound(self.sound.loop)
  27.         self.loopsource:SetLoopMode(true)
  28.         self.loopsource:SetRange(50)
  29.     end
  30.    
  31.     self.opentime=0
  32.    
  33.     --Create a motorized slider joint
  34.     local position=self.entity:GetPosition(true)
  35.     local pin=self.distance:Normalize()
  36.     self.joint=Joint:Slider(position.x,position.y,position.z,pin.x,pin.y,pin.z,self.entity,nil)
  37.     if self.openstate then
  38.         self.openangle=0
  39.         self.closedangle=self.distance:Length()
  40.     else
  41.         self.openangle=self.distance:Length()
  42.         self.closedangle=0
  43.     end
  44.     self.joint:EnableMotor()
  45.     self.joint:SetMotorSpeed(self.movespeed)
  46. end
  47.  
  48. function Script:Toggle()--in
  49.     if self.enabled then
  50.         if self.openstate then
  51.             self:Close()
  52.         else
  53.             self:Open()
  54.         end
  55.     end
  56. end
  57.  
  58. function Script:Open()--in
  59.     if self.enabled then
  60.         self.opentime = Time:GetCurrent()
  61.         if self.openstate==false then
  62.             if self.sound.open then
  63.                 self.entity:EmitSound(self.sound.open)
  64.             end
  65.             self.joint:SetAngle(self.openangle)
  66.             self.openstate=true        
  67.             self.component:CallOutputs("Open")
  68.             if self.loopsource~=nil then
  69.                 self.loopsource:SetPosition(self.entity:GetPosition(true))
  70.                 if self.loopsource:GetState()==Source.Stopped then
  71.                     self.loopsource:Play()
  72.                 end
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. function Script:Close()--in
  79.     if self.enabled then
  80.         if self.openstate then
  81.             if self.sound.close then
  82.                 self.entity:EmitSound(self.sound.close)
  83.             end
  84.             self.joint:SetAngle(self.closedangle)
  85.             self.openstate=false
  86.             if self.loopsource~=nil then
  87.                 self.loopsource:SetPosition(self.entity:GetPosition(true))
  88.                 if self.loopsource:GetState()==Source.Stopped then
  89.                     self.loopsource:Play()
  90.                 end
  91.             end
  92.             self.component:CallOutputs("Close")
  93.         end
  94.     end
  95. end
  96.  
  97. function Script:Disable()--in
  98.     self.enabled=false
  99. end
  100.  
  101. function Script:Enable()--in
  102.     self.enabled=true
  103. end
  104.  
  105. function Script:UpdatePhysics()
  106.    
  107.     --Disable loop sound
  108.     if self.sound.loop~=nil then
  109.         local angle
  110.         if self.openstate then
  111.             angle = self.openangle
  112.         else
  113.             angle = self.closedangle
  114.         end
  115.         if math.abs(self.joint:GetAngle()-angle)<0.1 then
  116.             if self.loopsource:GetState()~=Source.Stopped then
  117.                 self.loopsource:Stop()
  118.             end
  119.         else
  120.             if self.loopsource:GetState()==Source.Stopped then
  121.                 self.loopsource:Resume()
  122.             end
  123.         end
  124.         if self.loopsource:GetState()==Source.Playing then
  125.             self.loopsource:SetPosition(self.entity:GetPosition(true))
  126.         end
  127.     end
  128.    
  129.     --Automatically close the door after a delay
  130.     if self.closedelay>0 then
  131.         if self.openstate then
  132.             local time = Time:GetCurrent()
  133.             if time-self.opentime>self.closedelay then
  134.                 self:Close()
  135.             end
  136.         end
  137.     end
  138. end
  139.  
  140. function Script:Release()
  141.     ReleaseTableObjects(self.sound)
  142.     if self.loopsource then
  143.         self.loopsource:Release()
  144.         self.loopsource=nil
  145.     end
  146.     self.sound=nil
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement