Advertisement
Guest User

rope.lua rope tool

a guest
Jan 2nd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1.  
  2. TOOL.Category = "Constraints"
  3. TOOL.Name = "#tool.rope.name"
  4. TOOL.Command = nil
  5. TOOL.ConfigName = nil
  6.  
  7.  
  8. TOOL.ClientConVar[ "forcelimit" ] = "0"
  9. TOOL.ClientConVar[ "addlength" ] = "0"
  10. TOOL.ClientConVar[ "material" ] = "cable/rope"
  11. TOOL.ClientConVar[ "width" ] = "2"
  12. TOOL.ClientConVar[ "rigid" ] = "0"
  13.  
  14. function TOOL:LeftClick( trace )
  15.  
  16. if ( trace.Entity:IsValid() && trace.Entity:IsPlayer() ) then return end
  17.  
  18. -- If there's no physics object then we can't constraint it!
  19. if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
  20.  
  21. local iNum = self:NumObjects()
  22.  
  23. local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
  24. self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )
  25.  
  26. if ( iNum > 0 ) then
  27.  
  28. if ( CLIENT ) then
  29.  
  30. self:ClearObjects()
  31. return true
  32.  
  33. end
  34.  
  35. -- Get client's CVars
  36. local forcelimit = self:GetClientNumber( "forcelimit" )
  37. local addlength = self:GetClientNumber( "addlength" )
  38. local material = self:GetClientInfo( "material" )
  39. local width = self:GetClientNumber( "width" )
  40. local rigid = self:GetClientNumber( "rigid" ) == 1
  41.  
  42. -- Get information we're about to use
  43. local Ent1, Ent2 = self:GetEnt(1), self:GetEnt(2)
  44. local Bone1, Bone2 = self:GetBone(1), self:GetBone(2)
  45. local WPos1, WPos2 = self:GetPos(1), self:GetPos(2)
  46. local LPos1, LPos2 = self:GetLocalPos(1),self:GetLocalPos(2)
  47. local length = ( WPos1 - WPos2):Length()
  48.  
  49. local constraint, rope = constraint.Rope( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, length, addlength, forcelimit, width, material, rigid )
  50.  
  51. -- Clear the objects so we're ready to go again
  52. self:ClearObjects()
  53.  
  54. -- Add The constraint to the players undo table
  55.  
  56. undo.Create("Rope")
  57. undo.AddEntity( constraint )
  58. undo.AddEntity( rope )
  59. undo.SetPlayer( self:GetOwner() )
  60. undo.Finish()
  61.  
  62. self:GetOwner():AddCleanup( "ropeconstraints", constraint )
  63. self:GetOwner():AddCleanup( "ropeconstraints", rope )
  64.  
  65. else
  66.  
  67. self:SetStage( iNum+1 )
  68.  
  69. end
  70.  
  71. return true
  72.  
  73. end
  74.  
  75. function TOOL:RightClick( trace )
  76.  
  77. if ( trace.Entity:IsValid() && trace.Entity:IsPlayer() ) then return end
  78.  
  79. local iNum = self:NumObjects()
  80.  
  81. local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
  82. self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )
  83.  
  84. if ( iNum > 0 ) then
  85.  
  86. if ( CLIENT ) then
  87.  
  88. self:ClearObjects()
  89. return true
  90.  
  91. end
  92.  
  93. -- Get client's CVars
  94. local forcelimit = self:GetClientNumber( "forcelimit" )
  95. local addlength = self:GetClientNumber( "addlength" )
  96. local material = self:GetClientInfo( "material" )
  97. local width = self:GetClientNumber( "width" )
  98. local rigid = self:GetClientNumber( "rigid" ) == 1
  99.  
  100. -- Get information we're about to use
  101. local Ent1, Ent2 = self:GetEnt(1), self:GetEnt(2)
  102. local Bone1, Bone2 = self:GetBone(1), self:GetBone(2)
  103. local WPos1, WPos2 = self:GetPos(1),self:GetPos(2)
  104. local LPos1, LPos2 = self:GetLocalPos(1),self:GetLocalPos(2)
  105. local length = ( WPos1 - WPos2 ):Length()
  106.  
  107. local constraint, rope = constraint.Rope( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, length, addlength, forcelimit, width, material, rigid )
  108.  
  109. -- Clear the objects and set the last object as object 1
  110. self:ClearObjects()
  111. iNum = self:NumObjects()
  112. self:SetObject( iNum + 1, Ent2, trace.HitPos, Phys, Bone2, trace.HitNormal )
  113. self:SetStage( iNum+1 )
  114.  
  115. -- Add The constraint to the players undo table
  116.  
  117. undo.Create("Rope")
  118. undo.AddEntity( constraint )
  119. if rope then undo.AddEntity( rope ) end
  120. undo.SetPlayer( self:GetOwner() )
  121. undo.Finish()
  122.  
  123. self:GetOwner():AddCleanup( "ropeconstraints", constraint )
  124. self:GetOwner():AddCleanup( "ropeconstraints", rope )
  125.  
  126. else
  127.  
  128. self:SetStage( iNum+1 )
  129.  
  130. end
  131.  
  132. return true
  133.  
  134. end
  135.  
  136. function TOOL:Reload( trace )
  137.  
  138. if (!trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end
  139. if ( CLIENT ) then return true end
  140.  
  141. local bool = constraint.RemoveConstraints( trace.Entity, "Rope" )
  142. return bool
  143.  
  144.  
  145. end
  146.  
  147. function TOOL.BuildCPanel( CPanel )
  148.  
  149. CPanel:AddControl( "Header", { Text = "#tool.rope.name", Description = "#tool.rope.help" } )
  150.  
  151. CPanel:AddControl( "ComboBox",
  152. {
  153. Label = "#tool.presets",
  154. MenuButton = 1,
  155. Folder = "rope",
  156. Options = { Default = { rope_forcelimit = '0', rope_addlength='0', rope_width='1', rope_material='cable/rope', rope_rigid='0' } },
  157. CVars = { "rope_forcelimit", "rope_addlength", "rope_width", "rope_material", "rope_rigid" }
  158. })
  159.  
  160. CPanel:AddControl( "Slider", { Label = "#tool.forcelimit", Type = "Float", Command = "rope_forcelimit", Min = "0", Max = "1000", Help=true } )
  161. CPanel:AddControl( "Slider", { Label = "#tool.rope.addlength", Type = "Float", Command = "rope_addlength", Min = "-500", Max = "500", Help=true } )
  162.  
  163. CPanel:AddControl( "CheckBox", { Label = "#tool.rope.rigid", Command = "rope_rigid", Help=true } )
  164.  
  165. CPanel:AddControl( "Slider", { Label = "#tool.rope.width", Type = "Float", Command = "rope_width", Min = "0", Max = "10" } )
  166. CPanel:AddControl( "RopeMaterial", { Label = "#tool.rope.material", convar = "rope_material" } )
  167.  
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement