Advertisement
Guest User

ManualPlatform

a guest
Oct 10th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. Script.enabled=true--bool "Enabled"
  2. Script.openstate=false--bool "Start Open"
  3. Script.distance=Vec3(0,0,0)--Vec3 "Distance"
  4. Script.movespeed=1--float "Move speed" 0,100,3
  5.  
  6. function Script:Start()
  7. self.rot =1
  8.     self.stopped = true
  9.     self.currentposition = nil
  10.     self.doorstatus = self.openstate
  11.     self.destination = Vec3(0,0,0)
  12.     self.startingposition = self.entity:GetPosition()
  13.     self.startingrotation = self.entity:GetRotation()  
  14.     self.omega = self.entity:GetOmega(true)
  15.        
  16.     self.destination.x = self.startingposition.x+self.distance.x
  17.     self.destination.y = self.startingposition.y+self.distance.y
  18.     self.destination.z = self.startingposition.z+self.distance.z
  19.     self.distance1 =  self.entity:GetPosition():DistanceToPoint(Vec3(self.destination.x, self.destination.y,self.destination.z))
  20.    
  21.     self.destinationglobal = Vec3(0,0,0)
  22.     self.startingpositionglobal = self.entity:GetPosition(true)
  23.     self.startingrotationglobal = self.entity:GetRotation(true)
  24.     self.destinationglobal.x = self.startingpositionglobal.x+self.distance.x
  25.     self.destinationglobal.y = self.startingpositionglobal.y+self.distance.y
  26.     self.destinationglobal.z = self.startingpositionglobal.z+self.distance.z
  27.     self.distanceglobal1 =  self.entity:GetPosition():DistanceToPoint(Vec3(self.destinationglobal.x, self.destinationglobal.y,self.destinationglobal.z))
  28.    
  29.     self.entity:SetKeyValue("type","sliding_door")
  30.            
  31.     if self.openstate == true then
  32.             self:Open()
  33.             self.stopped = true
  34.             self.doorposition = 100
  35.             self:MoveDoor(self.doorposition)
  36.             self.entity:SetMass(0)
  37.         else
  38.             self:Close()
  39.             self.stopped = true        
  40.             self.doorposition = 0
  41.             self:MoveDoor(self.doorposition)
  42.             self.entity:SetMass(0)
  43.     end
  44. end
  45.  
  46. function Script:Open() --in
  47.     if self.enabled then       
  48.         self.stopped = false
  49.         self.direction =1      
  50.     end
  51. end
  52.  
  53. function Script:Close() --in
  54.     if self.enabled then
  55.         self.stopped = false
  56.         self.direction = -1
  57.     end
  58. end
  59.  
  60. function Script:Toggle()--in
  61.     if self.enabled then   
  62.         if self.openstate == true then         
  63.             self:Close()               
  64.             self.openstate = false             
  65.         elseif self.openstate == false then        
  66.             self:Open()            
  67.             self.openstate = true
  68.         end
  69.         self.component:CallOutputs("[Toggled]")
  70.     end
  71. end
  72.  
  73. function Script:UpdatePhysics()
  74.     -- we check if the entity is physically there because if you usegetangle it will have rounding errors.
  75.     if self.enabled then
  76.         if self.stopped == false then
  77.             if self.direction == -1 then
  78.                 if self.doorposition > 0 then          
  79.                     self.doorposition= self.doorposition -  (self.movespeed/self.distance1)
  80.                     else
  81.                     self.component:CallOutputs("[Closed]")
  82.                     self.stopped = true
  83.                     self.entity:SetMass(0)
  84.                     self.entity:SetRotation(self.startingrotation.x,self.startingrotation.y,self.startingrotation.z)   
  85.                 end
  86.             end
  87.             if self.direction == 1 then
  88.                 if self.doorposition < 100 then        
  89.                     self.doorposition= self.doorposition + (self.movespeed/self.distance1)
  90.                 else
  91.                     self.component:CallOutputs("[Opened]")                 
  92.                     self.stopped = true        
  93.                     self.entity:SetMass(0)
  94.                     self.entity:SetRotation(self.startingrotation.x,self.startingrotation.y,self.startingrotation.z)                   
  95.                 end
  96.             end    
  97.         end
  98.     end
  99. end
  100.  
  101.  
  102. function Script:UpdateWorld()
  103.     if self.enabled then
  104.         if self.stopped == false then
  105.             self:MoveDoor(self.doorposition)
  106.         end
  107.     end
  108. end
  109.  
  110.  
  111.  
  112. function Script:MoveDoor(percent)
  113.     self.entity:SetMass(100000) -- yeah... real life values sorta....
  114.     self.entity:SetPosition(Math:Lerp(self.startingposition.x,self.destination.x,percent/100) , Math:Lerp(self.startingposition.y,self.destination.y,percent/100) , Math:Lerp(self.startingposition.z,self.destination.z,percent/100))  -- allows item to force it's way through solid objects
  115.     self.entity:PhysicsSetPosition(Math:Lerp(self.startingpositionglobal.x,self.destinationglobal.x,percent/100) , Math:Lerp(self.startingpositionglobal.y,self.destinationglobal.y,percent/100) , Math:Lerp(self.startingpositionglobal.z,self.destinationglobal.z,percent/100))   -- causes entity to update its physics so things dont fall through 
  116.     self.entity:PhysicsSetPosition(self.entity:GetPosition(true).x,self.entity:GetPosition(true).y,self.entity:GetPosition(true).z,0)      
  117.     self.entity:SetRotation(self.startingrotation.x,self.startingrotation.y,self.startingrotation.z)   
  118.     self.entity:SetOmega(self.omega)
  119. end
  120.  
  121.  
  122. --This function will be called after the world is rendered, before the screen is refreshed.
  123. --Use this to perform any 2D drawing you want the entity to display.
  124. function Script:PostRender(context)
  125.         --[[
  126.         Debug Stuff
  127.         context:SetBlendMode(Blend.Alpha)  
  128.         context:SetColor(1,1,1,14)
  129.         context:DrawText(self.entity:GetPosition().x,2,2)
  130.         context:DrawText(self.startingposition.x,2,12)
  131.         context:DrawText(self.destination.x,2,24)
  132.         context:DrawText(self.doorposition,2,36)
  133.         ]]--
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement