Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------------------------
- -- Physgun v2
- --
- -- Author: CPA
- --------------------------------------------------------------------------
- Physgun2 = nil
- Physgun2 = {}
- Physgun2.__index = Physgun2
- Physgun2.Primary = {}
- Physgun2.Secondary = {}
- Physgun2.Base = "weapon_base"
- Physgun2.Spawnable = true
- Physgun2.AdminSpawnable = true
- Physgun2.PrintName = "Physgun 2"
- Physgun2.Slot = 1
- Physgun2.SlotPos = 1
- Physgun2.DrawAmmo = false
- Physgun2.DrawCrosshair = true
- Physgun2.Primary.ClipSize = -1
- Physgun2.Primary.DefaultClip = -1
- Physgun2.Primary.Automatic = true
- Physgun2.Primary.Ammo = "none"
- Physgun2.Secondary.ClipSize = -1
- Physgun2.Secondary.DefaultClip = -1
- Physgun2.Secondary.Automatic = false
- Physgun2.Secondary.Ammo = "none"
- Physgun2.ViewModel = "models/weapons/v_pistol.mdl"
- Physgun2.WorldModel = "models/weapons/w_pistol.mdl"
- -- Object Info
- Physgun2.Attached = nil
- Physgun2.Distance = 0
- Physgun2.LocalPos = nil
- Physgun2.OldCollide = nil
- Physgun2.LastAngle = Angle()
- Physgun2.RealMass = 0
- Physgun2.RealDampingLin = 0
- Physgun2.RealDampingAng = 0
- Physgun2.Force = 10
- Physgun2.RotationForce = 2
- Physgun2.IsRotating = false
- Physgun2.IsPhysgun2 = true
- Physgun2.InitialViewAngle = Angle()
- Physgun2.RotationAngle = Angle()
- Physgun2.BeamStartPos = Vector()
- Physgun2.BeamEndPos = Vector()
- Physgun2.ShouldDrawBeam = false
- Physgun2.BeamColor = Color(255, 255, 255, 255)
- Physgun2.RealMoveType = nil
- function Physgun2:Initialize()
- end
- function Physgun2:Think()
- if CLIENT then
- self.IsRotating = self:GetNWBool("IsRotating")
- --self:SetNWAngle("RotationAngle", self.RotationAngle)
- self.InitialViewAngle = self:GetNWAngle("InitialViewAngle")
- --self.ShouldDrawBeam = self:GetNWBool("DrawBeam")
- --self.BeamStartPos = self:GetNWVector("BeamStartPos")
- --self.BeamEndPos = self:GetNWVector("BeamEndPos")
- elseif SERVER then
- self:SetNWBool("IsRotating", self.IsRotating)
- --self.RotationAngle = self:GetNWAngle("RotationAngle");
- self:SetNWAngle("InitialViewAngle", self.InitialViewAngle)
- --self:SetNWBool("DrawBeam", self.ShouldDrawBeam)
- --self:SetNWVector("BeamStartPos", self.BeamStartPos)
- --self:SetNWVector("BeamEndPos", self.BeamEndPos)
- end
- if( !self.Owner:KeyDown(IN_ATTACK) ) then
- self:DrawBeam(false, 0)
- if( self.Attached ) then
- self:DetachObject()
- return
- end
- end
- if( self.Attached ) then
- local Obj = self.Attached
- if( !Obj:IsValid() ) then
- self:DetachObject()
- return
- end
- local NewPos = self.Owner:GetShootPos() + (self.Owner:EyeAngles():Forward() * self.Distance) + self.LocalPos
- local OldPos = Obj:GetPos()
- self:DrawBeam(true, NewPos)
- local Vel = (NewPos - OldPos) * self.Force; -- Delta has to be added here.
- local phys = Obj:GetPhysicsObject()
- if( Obj:IsNPC() || Obj:IsPlayer() ) then
- Obj:SetPos(NewPos)
- else
- if( phys and phys:IsValid() ) then
- phys:SetVelocity( Vel )
- else
- Obj:SetVelocity( Vel * 10 )
- end
- end
- if( self.Owner:KeyDown(IN_USE) and !self.IsRotating ) then
- -- Freeze
- self.IsRotating = true
- self.InitialViewAngle = self:EyeAngles()
- -- self.Owner:Freeze(true)
- print("Started Rotation")
- elseif ( !self.Owner:KeyDown(IN_USE) and self.IsRotating ) then
- -- Unfreeze
- self.IsRotating = false
- self.InitialViewAngle = Angle()
- -- self.Owner:Freeze(false)
- print("Stopped Rotation")
- end
- if( self.IsRotating ) then
- if( Obj:IsNPC() or Obj:IsPlayer() ) then
- Obj:SetAngles( (Obj:GetAngles() + self.RotationAngle) )
- self.LastAngle = Obj:GetAngles()
- else
- if( phys and phys:IsValid() ) then
- -- phys:AddAngleVelocity( AngleDiff )
- phys:SetAngle( (phys:GetAngles() + self.RotationAngle) )
- self.LastAngle = phys:GetAngles()
- end
- end
- else
- if SERVER then
- if( phys and phys:IsValid() ) then
- Obj:SetAngles( self.LastAngle )
- self.LastAngle = Obj:GetAngles()
- end
- end
- end
- else
- if( self.Owner:KeyDown(IN_ATTACK) ) then
- local Trace = self.Owner:GetEyeTrace()
- self:DrawBeam(true, Trace.HitPos)
- end
- end
- end
- function Physgun2:AttachObject(Trace)
- if( self.Attached ) then
- return
- end
- self.Distance = self.Owner:GetShootPos():Distance(Trace.HitPos)
- self.Attached = Trace.Entity
- self.LocalPos = Trace.Entity:GetPos() - Trace.HitPos
- self.OldCollide = self.Attached:GetMoveCollide()
- local phys = self.Attached:GetPhysicsObject()
- if ( phys and phys:IsValid() ) then
- self.RealMass = phys:GetMass()
- self.RealDampingLin, self.RealDampingAng = phys:GetDamping()
- self.RealMoveType = self.Attached:GetMoveType()
- if( !self.Attached:IsNPC() and !self.Attached:IsPlayer() ) then
- phys:Wake()
- phys:SetMass(100)
- phys:SetDamping(0, 90000)
- phys:EnableGravity(false)
- self.LastAngle = phys:GetAngles()
- else
- self.Attached:SetMoveType(MOVETYPE_NONE)
- self.LastAngle = self.Attached:GetAngles()
- end
- phys:Wake()
- end
- self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
- print("Attached Object")
- end
- function Physgun2:DetachObject()
- if( !self.Attached ) then
- return
- end
- if( self.Attached:IsValid() ) then
- local phys = self.Attached:GetPhysicsObject()
- if ( phys and phys:IsValid() ) then
- phys:SetMass(self.RealMass)
- phys:SetDamping(self.RealDampingLin, self.RealDampingAng)
- phys:EnableGravity(true)
- self.Attached:SetMoveType(self.RealMoveType)
- end
- end
- --self.Attached:SetMoveCollide(self.OldCollide)
- self.Attached = nil
- self.IsRotating = false
- self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
- print("Detached Object")
- end
- function Physgun2:PrimaryAttack()
- if( !self.Attached ) then
- local Trace = self.Owner:GetEyeTrace()
- if( Trace.Entity and Trace.Hit and !Trace.HitWorld ) then
- self:AttachObject(Trace)
- end
- end
- end
- function Physgun2:DrawBeam(draw, endpos)
- self.ShouldDrawBeam = draw
- if( draw ) then
- local vm = self.Owner:GetViewModel()
- local attachmentIndex = vm:LookupAttachment("1")
- if( !attachmentIndex ) then
- attachmentIndex = vm:LookupAttachment("muzzle")
- end
- self.BeamStartPos = vm:GetAttachment(attachmentIndex).Pos
- self.BeamEndPos = endpos
- end
- end
- weapons.Register(Physgun2, "weapon_physgun2", true)
- if SERVER then
- local Physgun2_SetupMove = function(ply, move)
- local wep = ply:GetActiveWeapon()
- if( !wep ) then
- return
- end
- if( wep.IsPhysgun2 ) then
- if( wep.IsRotating ) then
- cmd = ply:GetCurrentCommand()
- local Y = cmd:GetMouseX() / (FrameTime() * 200)
- local P = cmd:GetMouseY() / (FrameTime() * 200)
- local R = (cmd:GetMouseX() / cmd:GetMouseY()) / (FrameTime() * 200)
- wep.RotationAngle = Angle(P, Y, R)
- end
- end
- end
- hook.Add("SetupMove", "Physgun2_SetupMove", Physgun2_SetupMove)
- end
- if CLIENT then
- -- Mouse movement hook
- local Physgun2_InputMouseApply = function(cmd, x, y, angle)
- -- print("In Hook InputMouseApply BW")
- local ply = LocalPlayer()
- local wep = ply:GetActiveWeapon()
- cmd:SetViewAngles(angle)
- if( !wep ) then
- return true
- end
- -- print("In Hook InputMouseApply AW")
- if( wep.IsPhysgun2 ) then
- if( wep.IsRotating ) then
- -- print("Disabling Mouse movement")
- ply:SetEyeAngles(wep.InitialViewAngle)
- return true
- end
- end
- return false
- end
- hook.Add("InputMouseApply", "Physgun2_InputMouseApply", Physgun2_InputMouseApply)
- end
Advertisement
Add Comment
Please, Sign In to add comment