Advertisement
Marlingaming

Barrier Builder

Mar 13th, 2022 (edited)
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. SWEP.PrintName          = "Barrier Kit"
  2. SWEP.Author         = "jamiemarlin"
  3. SWEP.Instructions       = "left mouse click to start building a barrier"
  4.  
  5. SWEP.Spawnable = true
  6. SWEP.AdminOnly = true
  7.  
  8. SWEP.Primary.ClipSize       = 5
  9. SWEP.Primary.DefaultClip    = 1
  10. SWEP.Primary.Automatic      = false
  11. SWEP.Primary.Ammo       = "Barrier"
  12.  
  13. SWEP.Secondary.ClipSize     = -1
  14. SWEP.Secondary.DefaultClip  = -1
  15. SWEP.Secondary.Automatic    = false
  16. SWEP.Secondary.Ammo     = "none"
  17.  
  18. SWEP.Weight         = 10
  19. SWEP.AutoSwitchTo       = false
  20. SWEP.AutoSwitchFrom     = false
  21.  
  22. SWEP.Slot           = 2
  23. SWEP.SlotPos            = 2
  24. SWEP.DrawAmmo           = true
  25. SWEP.DrawCrosshair      = true
  26.  
  27. SWEP.ViewModel          = "models/weapons/v_pistol.mdl"
  28. SWEP.WorldModel         = "models/weapons/w_pistol.mdl"
  29.  
  30.  
  31. -- Called when the left mouse button is pressed
  32. function SWEP:PrimaryAttack()
  33.     self:SetNextPrimaryFire( CurTime() + 12.0 )
  34.  
  35.     self:PlaceItem( "models\props_phx\construct\concrete_barrier00.mdl" )
  36. end
  37.  
  38.  
  39. -- Called when the rightmouse button is pressed
  40. function SWEP:SecondaryAttack()
  41.     -- Though the secondary fire isn't automatic
  42.     -- players shouldn't be able to fire too fast
  43.     self:SetNextSecondaryFire( CurTime() + 12.0 )
  44.  
  45.     self:PlaceItem( "models\props_phx\construct\concrete_barrier00.mdl" )
  46. end
  47.  
  48. -- A custom function we added. When you call this the player will fire a chair!
  49. function SWEP:PlaceItem( model_file )
  50.     local owner = self:GetOwner()
  51.  
  52.     -- Make sure the weapon is being held before trying to throw a chair
  53.     if ( not owner:IsValid() ) then return end
  54.  
  55.     -- Play the shoot sound we precached earlier!
  56.  
  57.     -- If we're the client then this is as much as we want to do.
  58.     -- We play the sound above on the client due to prediction.
  59.     -- ( if we didn't they would feel a ping delay during multiplayer )
  60.     if ( CLIENT ) then return end
  61.  
  62.     -- Create a prop_physics entity
  63.     local ent = ents.Create( "prop_physics" )
  64.  
  65.     -- Always make sure that created entities are actually created!
  66.     if ( not ent:IsValid() ) then return end
  67.  
  68.     -- Set the entity's model to the passed in model
  69.     ent:SetModel( model_file )
  70.  
  71.     -- This is the same as owner:EyePos() + (self.Owner:GetAimVector() * 16)
  72.     -- but the vector methods prevent duplicitous objects from being created
  73.     -- which is faster and more memory efficient
  74.     -- AimVector is not directly modified as it is used again later in the function
  75.     local aimvec = owner:GetAimVector()
  76.     local pos = aimvec * 16 -- This creates a new vector object
  77.     pos:Add( owner:EyePos() ) -- This translates the local aimvector to world coordinates
  78.  
  79.     -- Set the position to the player's eye position plus 16 units forward.
  80.     ent:SetPos( pos )
  81.  
  82.     -- Set the angles to the player'e eye angles. Then spawn it.
  83.     ent:SetAngles( owner:EyeAngles() )
  84.     ent:EnableMotion(false)
  85.     ent:SetHealth(200)
  86.     ent:Spawn()
  87.  
  88.     -- Now get the physics object. Whenever we get a physics object
  89.     -- we need to test to make sure its valid before using it.
  90.     -- If it isn't then we'll remove the entity.
  91.     local phys = ent:GetPhysicsObject()
  92.     if ( not phys:IsValid() ) then ent:Remove() return end
  93.  
  94.     -- Now we apply the force - so the chair actually throws instead
  95.     -- of just falling to the ground. You can play with this value here
  96.     -- to adjust how fast we throw it.
  97.     -- Now that this is the last use of the aimvector vector we created,
  98.     -- we can directly modify it instead of creating another copy
  99.  
  100.     -- Assuming we're playing in Sandbox mode we want to add this
  101.     -- entity to the cleanup and undo lists. This is done like so.
  102.     cleanup.Add( owner, "props", ent )
  103.  
  104.     undo.Create( "Placed_Barrier" )
  105.         undo.AddEntity( ent )
  106.         undo.SetPlayer( owner )
  107.     undo.Finish()
  108.     -- A lot of items can clutter the workspace.
  109.     -- To fix this we add a 10 second delay to remove the chair after it was spawned.
  110.     -- ent:IsValid() checks if the item still exists before removing it, eliminating errors.
  111.    
  112. end
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement