CapsAdmin

Untitled

Apr 17th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.81 KB | None | 0 0
  1. --------------------------------------------------------------------------
  2. -- Physgun v2
  3. --
  4. -- Author: CPA
  5. --------------------------------------------------------------------------
  6. Physgun2 = nil
  7.  
  8. Physgun2 = {}
  9. Physgun2.__index = Physgun2
  10.  
  11. Physgun2.Primary = {}
  12. Physgun2.Secondary = {}
  13.  
  14. Physgun2.Base                   = "weapon_base"
  15.  
  16. Physgun2.Spawnable              = true
  17. Physgun2.AdminSpawnable         = true
  18. Physgun2.PrintName              = "Physgun 2"          
  19. Physgun2.Slot                   = 1
  20. Physgun2.SlotPos                = 1
  21. Physgun2.DrawAmmo               = false
  22. Physgun2.DrawCrosshair          = true
  23. Physgun2.Primary.ClipSize       = -1
  24. Physgun2.Primary.DefaultClip    = -1
  25. Physgun2.Primary.Automatic      = true
  26. Physgun2.Primary.Ammo           = "none"
  27. Physgun2.Secondary.ClipSize     = -1
  28. Physgun2.Secondary.DefaultClip  = -1
  29. Physgun2.Secondary.Automatic        = false
  30. Physgun2.Secondary.Ammo         = "none"
  31.  
  32. Physgun2.ViewModel              = "models/weapons/v_pistol.mdl"
  33. Physgun2.WorldModel             = "models/weapons/w_pistol.mdl"
  34.  
  35. -- Object Info
  36. Physgun2.Attached               = nil
  37. Physgun2.Distance               = 0
  38. Physgun2.LocalPos               = nil
  39. Physgun2.OldCollide             = nil
  40. Physgun2.LastAngle              = Angle()
  41. Physgun2.RealMass               = 0
  42. Physgun2.RealDampingLin         = 0
  43. Physgun2.RealDampingAng         = 0
  44. Physgun2.Force                  = 10
  45. Physgun2.RotationForce          = 2
  46. Physgun2.IsRotating             = false
  47. Physgun2.IsPhysgun2             = true
  48. Physgun2.InitialViewAngle       = Angle()
  49. Physgun2.RotationAngle          = Angle()
  50. Physgun2.BeamStartPos           = Vector()
  51. Physgun2.BeamEndPos             = Vector()
  52. Physgun2.ShouldDrawBeam         = false
  53. Physgun2.BeamColor              = Color(255, 255, 255, 255)
  54. Physgun2.RealMoveType           = nil
  55.  
  56. function Physgun2:Initialize()  
  57. end
  58.  
  59. function Physgun2:Think()
  60.  
  61.     if CLIENT then
  62.    
  63.         self.IsRotating = self:GetNWBool("IsRotating")
  64.         --self:SetNWAngle("RotationAngle", self.RotationAngle)
  65.         self.InitialViewAngle = self:GetNWAngle("InitialViewAngle")
  66.         --self.ShouldDrawBeam = self:GetNWBool("DrawBeam")
  67.         --self.BeamStartPos = self:GetNWVector("BeamStartPos")
  68.         --self.BeamEndPos = self:GetNWVector("BeamEndPos")
  69.        
  70.     elseif SERVER then
  71.    
  72.         self:SetNWBool("IsRotating", self.IsRotating)
  73.         --self.RotationAngle = self:GetNWAngle("RotationAngle");
  74.         self:SetNWAngle("InitialViewAngle", self.InitialViewAngle)
  75.         --self:SetNWBool("DrawBeam", self.ShouldDrawBeam)
  76.         --self:SetNWVector("BeamStartPos", self.BeamStartPos)
  77.         --self:SetNWVector("BeamEndPos", self.BeamEndPos)
  78.        
  79.     end
  80.    
  81.     if( !self.Owner:KeyDown(IN_ATTACK) ) then
  82.    
  83.         self:DrawBeam(false, 0)
  84.        
  85.         if( self.Attached ) then
  86.        
  87.             self:DetachObject()
  88.             return
  89.            
  90.         end
  91.        
  92.     end
  93.    
  94.     if( self.Attached ) then
  95.    
  96.         local Obj = self.Attached
  97.        
  98.         if( !Obj:IsValid() ) then
  99.        
  100.             self:DetachObject()
  101.             return
  102.            
  103.         end
  104.                
  105.         local NewPos = self.Owner:GetShootPos() + (self.Owner:EyeAngles():Forward() * self.Distance) + self.LocalPos
  106.         local OldPos = Obj:GetPos()
  107.        
  108.         self:DrawBeam(true, NewPos)
  109.        
  110.         local Vel = (NewPos - OldPos) * self.Force; -- Delta has to be added here.
  111.         local phys = Obj:GetPhysicsObject()
  112.        
  113.         if( Obj:IsNPC() || Obj:IsPlayer() ) then
  114.        
  115.             Obj:SetPos(NewPos)
  116.            
  117.         else
  118.            
  119.             if( phys and phys:IsValid() ) then
  120.                            
  121.                 phys:SetVelocity( Vel )
  122.                
  123.             else
  124.                            
  125.                 Obj:SetVelocity( Vel * 10 )
  126.                
  127.             end
  128.            
  129.         end
  130.        
  131.         if( self.Owner:KeyDown(IN_USE) and !self.IsRotating ) then
  132.        
  133.             -- Freeze  
  134.             self.IsRotating = true
  135.             self.InitialViewAngle = self:EyeAngles()
  136.             -- self.Owner:Freeze(true)
  137.            
  138.             print("Started Rotation")
  139.            
  140.         elseif ( !self.Owner:KeyDown(IN_USE) and self.IsRotating ) then
  141.        
  142.             -- Unfreeze
  143.             self.IsRotating = false
  144.             self.InitialViewAngle = Angle()
  145.             -- self.Owner:Freeze(false)
  146.            
  147.             print("Stopped Rotation")
  148.            
  149.         end
  150.        
  151.         if( self.IsRotating ) then
  152.                
  153.             if( Obj:IsNPC() or Obj:IsPlayer() ) then
  154.            
  155.                 Obj:SetAngles( (Obj:GetAngles() + self.RotationAngle) )
  156.                 self.LastAngle = Obj:GetAngles()
  157.                    
  158.             else
  159.                                
  160.                 if( phys and phys:IsValid() ) then
  161.                
  162.                     -- phys:AddAngleVelocity( AngleDiff )
  163.                     phys:SetAngle( (phys:GetAngles() + self.RotationAngle) )
  164.                     self.LastAngle = phys:GetAngles()
  165.                    
  166.                 end
  167.            
  168.             end
  169.        
  170.         else
  171.        
  172.             if SERVER then
  173.            
  174.                 if( phys and phys:IsValid() ) then
  175.                
  176.                     Obj:SetAngles( self.LastAngle )
  177.                     self.LastAngle = Obj:GetAngles()
  178.                    
  179.                 end
  180.                
  181.             end
  182.            
  183.         end
  184.    
  185.     else
  186.    
  187.         if( self.Owner:KeyDown(IN_ATTACK) ) then
  188.        
  189.             local Trace = self.Owner:GetEyeTrace()
  190.             self:DrawBeam(true, Trace.HitPos)
  191.            
  192.         end
  193.            
  194.     end
  195.    
  196. end
  197.  
  198. function Physgun2:AttachObject(Trace)
  199.  
  200.     if( self.Attached ) then
  201.         return
  202.     end
  203.    
  204.     self.Distance = self.Owner:GetShootPos():Distance(Trace.HitPos)
  205.     self.Attached = Trace.Entity
  206.     self.LocalPos = Trace.Entity:GetPos() - Trace.HitPos
  207.     self.OldCollide = self.Attached:GetMoveCollide()
  208.  
  209.     local phys = self.Attached:GetPhysicsObject()
  210.    
  211.     if ( phys and phys:IsValid() ) then
  212.         self.RealMass = phys:GetMass()
  213.         self.RealDampingLin, self.RealDampingAng = phys:GetDamping()
  214.         self.RealMoveType = self.Attached:GetMoveType()
  215.        
  216.         if( !self.Attached:IsNPC() and !self.Attached:IsPlayer() ) then
  217.             phys:Wake()
  218.             phys:SetMass(100)
  219.             phys:SetDamping(0, 90000)
  220.             phys:EnableGravity(false)
  221.             self.LastAngle = phys:GetAngles()
  222.         else
  223.             self.Attached:SetMoveType(MOVETYPE_NONE)
  224.             self.LastAngle = self.Attached:GetAngles()
  225.         end
  226.        
  227.         phys:Wake()
  228.     end
  229.     self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
  230.    
  231.     print("Attached Object")
  232.    
  233. end
  234.  
  235. function Physgun2:DetachObject()
  236.  
  237.     if( !self.Attached ) then
  238.         return
  239.     end
  240.  
  241.     if( self.Attached:IsValid() ) then
  242.         local phys = self.Attached:GetPhysicsObject()
  243.         if ( phys and phys:IsValid() ) then
  244.             phys:SetMass(self.RealMass)
  245.             phys:SetDamping(self.RealDampingLin, self.RealDampingAng)
  246.             phys:EnableGravity(true)
  247.            
  248.             self.Attached:SetMoveType(self.RealMoveType)
  249.         end
  250.  
  251.     end
  252.            
  253.     --self.Attached:SetMoveCollide(self.OldCollide)
  254.     self.Attached = nil
  255.     self.IsRotating = false
  256.  
  257.     self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
  258.  
  259.     print("Detached Object")
  260. end
  261.  
  262. function Physgun2:PrimaryAttack()
  263.     if( !self.Attached ) then
  264.  
  265.         local Trace = self.Owner:GetEyeTrace()
  266.        
  267.         if( Trace.Entity and Trace.Hit and !Trace.HitWorld ) then
  268.  
  269.             self:AttachObject(Trace)
  270.  
  271.         end
  272.        
  273.     end
  274. end
  275.  
  276. function Physgun2:DrawBeam(draw, endpos)
  277.     self.ShouldDrawBeam = draw
  278.    
  279.     if( draw ) then
  280.    
  281.         local vm = self.Owner:GetViewModel()
  282.         local attachmentIndex = vm:LookupAttachment("1")
  283.        
  284.         if( !attachmentIndex ) then
  285.             attachmentIndex = vm:LookupAttachment("muzzle")
  286.         end
  287.        
  288.         self.BeamStartPos = vm:GetAttachment(attachmentIndex).Pos
  289.         self.BeamEndPos = endpos
  290.    
  291.     end
  292. end
  293.  
  294. weapons.Register(Physgun2, "weapon_physgun2", true)
  295.  
  296. if SERVER then
  297.  
  298.     local Physgun2_SetupMove = function(ply, move)
  299.  
  300.         local wep = ply:GetActiveWeapon()
  301.        
  302.         if( !wep ) then
  303.             return
  304.         end
  305.        
  306.         if( wep.IsPhysgun2 ) then
  307.                                    
  308.             if( wep.IsRotating ) then
  309.                 cmd = ply:GetCurrentCommand()
  310.                
  311.                 local Y = cmd:GetMouseX() / (FrameTime() * 200)
  312.                 local P = cmd:GetMouseY() / (FrameTime() * 200)
  313.                 local R = (cmd:GetMouseX() / cmd:GetMouseY()) / (FrameTime() * 200)
  314.                            
  315.                 wep.RotationAngle = Angle(P, Y, R)         
  316.             end
  317.        
  318.         end
  319.        
  320.     end
  321.     hook.Add("SetupMove", "Physgun2_SetupMove", Physgun2_SetupMove)
  322.  
  323. end
  324.  
  325. if CLIENT then
  326.  
  327.     -- Mouse movement hook
  328.     local Physgun2_InputMouseApply = function(cmd, x, y, angle)
  329.    
  330.         -- print("In Hook InputMouseApply BW")
  331.        
  332.         local ply = LocalPlayer()          
  333.         local wep = ply:GetActiveWeapon()
  334.  
  335.         cmd:SetViewAngles(angle)
  336.        
  337.         if( !wep ) then
  338.             return true
  339.         end
  340.        
  341.         -- print("In Hook InputMouseApply AW")
  342.        
  343.         if( wep.IsPhysgun2 ) then          
  344.             if( wep.IsRotating ) then
  345.                 -- print("Disabling Mouse movement")
  346.                 ply:SetEyeAngles(wep.InitialViewAngle)
  347.                 return true
  348.             end            
  349.         end
  350.        
  351.         return false
  352.        
  353.     end
  354.     hook.Add("InputMouseApply", "Physgun2_InputMouseApply", Physgun2_InputMouseApply)
  355.    
  356. end
Advertisement
Add Comment
Please, Sign In to add comment