Advertisement
Guest User

weapon_lightsaber.lua

a guest
Jun 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.84 KB | None | 0 0
  1.  
  2. --[[-------------------------------------------------------------------
  3.     Modified Lightsaber:
  4.         Runs on the intuitive wOS Lightsaber Base
  5.             Powered by
  6.                           _ _ _    ___  ____  
  7.                 __      _(_) | |_ / _ \/ ___|
  8.                 \ \ /\ / / | | __| | | \___ \
  9.                  \ V  V /| | | |_| |_| |___) |
  10.                   \_/\_/ |_|_|\__|\___/|____/
  11.                                              
  12.  _____         _                 _             _          
  13. |_   _|__  ___| |__  _ __   ___ | | ___   __ _(_) ___  ___
  14.   | |/ _ \/ __| '_ \| '_ \ / _ \| |/ _ \ / _` | |/ _ \/ __|
  15.   | |  __/ (__| | | | | | | (_) | | (_) | (_| | |  __/\__ \
  16.   |_|\___|\___|_| |_|_| |_|\___/|_|\___/ \__, |_|\___||___/
  17.                                          |___/            
  18. ----------------------------- Copyright 2017, David "King David" Wiltos ]]--[[
  19.                              
  20.     Lua Developer: King David
  21.     Contact: http://steamcommunity.com/groups/wiltostech
  22.        
  23. -- Copyright 2017, David "King David" Wiltos ]]--
  24.  
  25. AddCSLuaFile()
  26.  
  27. SWEP.Base = "weapon_lightsaber"
  28.  
  29. SWEP.PrintName = "Training Saber"
  30. SWEP.Author = "Robotboy655 + King David"
  31. SWEP.Category = "wiltOS Technologies"
  32. SWEP.Contact = ""
  33. SWEP.Purpose = "To train in the ways of lightsaber combat."
  34. SWEP.Instructions = "Harden your drive."
  35. SWEP.RenderGroup = RENDERGROUP_BOTH
  36.  
  37. SWEP.Slot = 0
  38. SWEP.SlotPos = 4
  39.  
  40. SWEP.Spawnable = true
  41. SWEP.DrawAmmo = false
  42. SWEP.DrawCrosshair = false
  43. SWEP.AutoSwitchTo = false
  44. SWEP.AutoSwitchFrom = false
  45. SWEP.DrawWeaponInfoBox = false
  46.  
  47. SWEP.ViewModel = "models/weapons/v_crowbar.mdl"
  48. SWEP.WorldModel = "models/sgg/starwars/weapons/w_anakin_ep2_saber_hilt.mdl"
  49. SWEP.ViewModelFOV = 55
  50.  
  51. SWEP.Primary.ClipSize = -1
  52. SWEP.Primary.DefaultClip = -1
  53. SWEP.Primary.Automatic = false
  54. SWEP.Primary.Ammo = "none"
  55.  
  56. SWEP.Secondary.ClipSize = -1
  57. SWEP.Secondary.DefaultClip = -1
  58. SWEP.Secondary.Automatic = true
  59. SWEP.Secondary.Ammo = "none"
  60.  
  61. SWEP.HoldType = "bs-arrogant-balanced"
  62.  
  63. SWEP.IsLightsaber = true
  64. SWEP.ForceSelectEnabled = false
  65.  
  66. function SWEP:PrimaryAttack()
  67.     if ( !IsValid( self.Owner ) ) then return end
  68.  
  69.     self:SetNextAttack( 1 )
  70.  
  71.     if ( !self.Owner:IsNPC() && self:GetEnabled() ) then
  72.         self.Owner:AnimResetGestureSlot( GESTURE_SLOT_CUSTOM )
  73.         self.Owner:SetAnimation( PLAYER_ATTACK1 )
  74.     end
  75. end
  76.  
  77. function SWEP:GetTargetHoldType()
  78.  
  79.     return "bs-arrogant-balanced"
  80.    
  81. end
  82.  
  83. if SERVER then return end
  84.  
  85. local rb655_lightsaber_hud_blur = CreateClientConVar( "rb655_lightsaber_hud_blur", "0" )
  86.  
  87. local grad = Material( "gui/gradient_up" )
  88. local matBlurScreen = Material( "pp/blurscreen" )
  89. matBlurScreen:SetFloat( "$blur", 3 )
  90. matBlurScreen:Recompute()
  91.  
  92. local function DrawHUDBox( x, y, w, h, b )
  93.  
  94.     x = math.floor( x )
  95.     y = math.floor( y )
  96.     w = math.floor( w )
  97.     h = math.floor( h )
  98.  
  99.     surface.SetMaterial( matBlurScreen )
  100.     surface.SetDrawColor( 255, 255, 255, 255 )
  101.  
  102.     if ( rb655_lightsaber_hud_blur:GetBool() ) then
  103.         render.SetScissorRect( x, y, w + x, h + y, true )
  104.             for i = 0.33, 1, 0.33 do
  105.                 matBlurScreen:SetFloat( "$blur", 5 * i )
  106.                 matBlurScreen:Recompute()
  107.                 render.UpdateScreenEffectTexture()
  108.                 surface.DrawTexturedRect( 0, 0, ScrW(), ScrH() )
  109.             end
  110.         render.SetScissorRect( 0, 0, 0, 0, false )
  111.     else
  112.         draw.NoTexture()
  113.         surface.SetDrawColor( Color( 0, 0, 0, 128 ) )
  114.         surface.DrawTexturedRect( x, y, w, h )
  115.     end
  116.  
  117.     surface.SetDrawColor( Color( 0, 0, 0, 128 ) )
  118.     surface.DrawRect( x, y, w, h )
  119.  
  120.     if ( b ) then
  121.         surface.SetMaterial( grad )
  122.         surface.SetDrawColor( Color( 0, 128, 255, 4 ) )
  123.         surface.DrawTexturedRect( x, y, w, h )
  124.     end
  125.  
  126. end
  127.  
  128.  
  129. local ForceBar = 100
  130. function SWEP:DrawHUD()
  131.     if ( !IsValid( self.Owner ) or self.Owner:GetViewEntity() != self.Owner or self.Owner:InVehicle() ) then return end
  132.  
  133.     -----------------------------------
  134.  
  135.     local icon = 52
  136.     local gap = 5
  137.  
  138.     local bar = 4
  139.     local bar2 = 16
  140.  
  141.     if ( self.ForceSelectEnabled ) then
  142.         icon = 128
  143.         bar = 8
  144.         bar2 = 24
  145.     end
  146.  
  147.     local ForcePowers = self:GetActiveForcePowers()
  148.  
  149.     if ( #ForcePowers < 1 ) then self:DrawHUDTargetSelection() return end
  150.  
  151.     ----------------------------------- Force Bar -----------------------------------
  152.  
  153.     ForceBar = math.min( 100, Lerp( 0.1, ForceBar, math.floor( self:GetForce() ) ) )
  154.  
  155.     local w = #ForcePowers * icon + ( #ForcePowers - 1 ) * gap
  156.     local h = bar2
  157.     local x = math.floor( ScrW() / 2 - w / 2 )
  158.     local y = ScrH() - gap - bar2
  159.  
  160.     DrawHUDBox( x, y, w, h )
  161.  
  162.     local barW = math.ceil( w * ( ForceBar / 100 ) )
  163.     if ( self:GetForce() <= 1 && barW <= 1 ) then barW = 0 end
  164.     draw.RoundedBox( 0, x, y, barW, h, Color( 0, 128, 255, 255 ) )
  165.  
  166.     draw.SimpleText( math.floor( self:GetForce() ) .. "%", "SelectedForceHUD", x + w / 2, y + h / 2, Color( 255, 255, 255 ), 1, 1 )
  167.  
  168.     ----------------------------------- Force Icons -----------------------------------
  169.  
  170.     local y = y - icon - gap
  171.     local h = icon
  172.  
  173.     for id, t in pairs( ForcePowers ) do
  174.         local x = x + ( id - 1 ) * ( h + gap )
  175.         local x2 = math.floor( x + icon / 2 )
  176.  
  177.         DrawHUDBox( x, y, h, h, self:GetForceType() == id )
  178.         draw.SimpleText( t.icon or "", "SelectedForceType", x2, math.floor( y + icon / 2 ), Color( 255, 255, 255 ), 1, 1 )
  179.         if ( self.ForceSelectEnabled ) then
  180.             draw.SimpleText( ( input.LookupBinding( "slot" .. id ) or "<NOT BOUND>" ):upper(), "SelectedForceHUD", x + gap, y + gap, Color( 255, 255, 255 ) )
  181.         end
  182.         if ( self:GetForceType() == id ) then
  183.             local y = y + ( icon - bar )
  184.             surface.SetDrawColor( 0, 128, 255, 255 )
  185.             draw.NoTexture()
  186.             surface.DrawPoly( {
  187.                 { x = x2 - bar, y = y },
  188.                 { x = x2, y = y - bar },
  189.                 { x = x2 + bar, y = y }
  190.             } )
  191.             draw.RoundedBox( 0, x, y, h, bar, Color( 0, 128, 255, 255 ) )
  192.         end
  193.     end
  194.  
  195.     ----------------------------------- Force Description -----------------------------------
  196.  
  197.     local selectedForcePower = self:GetActiveForcePowerType( self:GetForceType() )
  198.  
  199.     if ( selectedForcePower && self.ForceSelectEnabled ) then
  200.  
  201.         surface.SetFont( "SelectedForceHUD" )
  202.         local tW, tH = surface.GetTextSize( selectedForcePower.description or "" )
  203.  
  204.         --[[local x = x + w + gap
  205.         local y = y]]
  206.         local x = ScrW() / 2 + gap-- - tW / 2
  207.         local y = y - tH - gap * 3
  208.  
  209.         DrawHUDBox( x, y, tW + gap * 2, tH + gap * 2 )
  210.  
  211.         for id, txt in pairs( string.Explode( "\n", selectedForcePower.description or "" ) ) do
  212.             draw.SimpleText( txt, "SelectedForceHUD", x + gap, y + ( id - 1 ) * ScreenScale( 6 ) + gap, Color( 255, 255, 255 ) )
  213.         end
  214.  
  215.     end
  216.  
  217.     ----------------------------------- Force Label -----------------------------------
  218.  
  219.     if ( !self.ForceSelectEnabled ) then
  220.         surface.SetFont( "SelectedForceHUD" )
  221.         local txt = "Press " .. ( input.LookupBinding( "impulse 100" ) or "<NOT BOUND>" ):upper() .. " to toggle Force selection"
  222.         local tW, tH = surface.GetTextSize( txt )
  223.  
  224.         local x = x + w / 2
  225.         local y = y - tH - gap
  226.  
  227.         DrawHUDBox( x - tW / 2 - 5, y, tW + 10, tH )
  228.         draw.SimpleText( txt, "SelectedForceHUD", x, y, Color( 255, 255, 255 ), 1 )
  229.  
  230.  
  231.         if ( self:GetIncorrectPlayerModel() != 0 ) then
  232.             local txt = "Server is missing the player model or missing owner! Send a screenshot of this error to the mod page!\nPlayer model: " .. self.Owner:GetModel() .. " - Error Code: " .. self:GetIncorrectPlayerModel()
  233.             local tW, tH = surface.GetTextSize( txt )
  234.  
  235.             y = y - tH - gap
  236.  
  237.             DrawHUDBox( x - tW / 2 - 5, y, tW + 10, tH )
  238.             for id, str in pairs( string.Explode( "\n", txt ) ) do
  239.                 local _, lineH = surface.GetTextSize( str )
  240.                 draw.SimpleText( str, "SelectedForceHUD", x, y + ( id - 1 ) * lineH, Color( 255, 200, 200 ), 1 )
  241.             end
  242.         end
  243.     end
  244.  
  245.     if ( selectedForcePower && self.ForceSelectEnabled ) then
  246.         surface.SetFont( "SelectedForceType" )
  247.         local txt = selectedForcePower.name or ""
  248.         local tW2, tH2 = surface.GetTextSize( txt )
  249.  
  250.         local x = x + w / 2 - tW2 - gap * 2 --+ w / 2
  251.         local y = y + gap - tH2 - gap * 2
  252.  
  253.         DrawHUDBox( x, y, tW2 + 10, tH2 )
  254.         draw.SimpleText( txt, "SelectedForceType", x + gap, y, Color( 255, 255, 255 ) )
  255.     end
  256.  
  257.     ----------------------------------- Force Target -----------------------------------
  258.  
  259.     self:DrawHUDTargetSelection()
  260.  
  261. end
  262.  
  263. function SWEP:PlayWeaponSound( snd )
  264.     if ( CLIENT ) then return end
  265.     if ( IsValid( self:GetOwner() ) && IsValid( self:GetOwner():GetActiveWeapon() ) && self:GetOwner():GetActiveWeapon() != self ) then return end
  266.     if ( !IsValid( self.Owner ) ) then return self:EmitSound( snd ) end
  267.     self.Owner:EmitSound( snd )
  268. end
  269.  
  270. function SWEP:SelectTargets( num )
  271.     local t = {}
  272.     local dist = 512
  273.  
  274.     --[[local tr = util.TraceLine( {
  275.         start = self.Owner:GetShootPos(),
  276.         endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * dist,
  277.         filter = self.Owner
  278.     } )]]
  279.  
  280.     local p = {}
  281.     for id, ply in pairs( ents.GetAll() ) do
  282.         if ( !ply:GetModel() or ply:GetModel() == "" or ply == self.Owner or ply:Health() < 1 ) then continue end
  283.         if ( string.StartWith( ply:GetModel() or "", "models/gibs/" ) ) then continue end
  284.         if ( string.find( ply:GetModel() or "", "chunk" ) ) then continue end
  285.         if ( string.find( ply:GetModel() or "", "_shard" ) ) then continue end
  286.         if ( string.find( ply:GetModel() or "", "_splinters" ) ) then continue end
  287.  
  288.         local tr = util.TraceLine( {
  289.             start = self.Owner:GetShootPos(),
  290.             endpos = ply.GetShootPos && ply:GetShootPos() or ply:GetPos(),
  291.             filter = self.Owner,
  292.         } )
  293.  
  294.         if ( tr.Entity != ply && IsValid( tr.Entity ) or tr.Entity == game.GetWorld() ) then continue end
  295.  
  296.         local pos1 = self.Owner:GetPos() + self.Owner:GetAimVector() * dist
  297.         local pos2 = ply:GetPos()
  298.         local dot = self.Owner:GetAimVector():Dot( ( self.Owner:GetPos() - pos2 ):GetNormalized() )
  299.  
  300.         if ( pos1:Distance( pos2 ) <= dist && ply:EntIndex() > 0 && ply:GetModel() && ply:GetModel() != "" ) then
  301.             table.insert( p, { ply = ply, dist = tr.HitPos:Distance( pos2 ), dot = dot, score = -dot + ( ( dist - pos1:Distance( pos2 ) ) / dist ) * 50 } )
  302.         end
  303.     end
  304.  
  305.     for id, ply in SortedPairsByMemberValue( p, "dist" ) do
  306.         table.insert( t, ply.ply )
  307.         if ( #t >= num ) then return t end
  308.     end
  309.  
  310.     return t
  311. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement