From 656c2556a6f82b5fad9e7868f5af7871364775f5 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:05:04 +0300 Subject: [PATCH] Laser Pointer refactoring (#3232) * Laser Pointer refactoring * Linter pass * Remove COLOSSAL_SANDBOX check --- lua/weapons/laserpointer/cl_init.lua | 45 ++++++++++++++++++---------- lua/weapons/laserpointer/init.lua | 34 +++++++++++++-------- lua/weapons/laserpointer/shared.lua | 10 +++++-- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/lua/weapons/laserpointer/cl_init.lua b/lua/weapons/laserpointer/cl_init.lua index 4e789988..9b6472cf 100644 --- a/lua/weapons/laserpointer/cl_init.lua +++ b/lua/weapons/laserpointer/cl_init.lua @@ -1,53 +1,66 @@ -include('shared.lua') +include("shared.lua") + SWEP.PrintName = "Laser Pointer" SWEP.Slot = 0 SWEP.SlotPos = 4 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true -local LASER = Material('cable/redlaser') +local color_red = Color(255, 0, 0) +local laser = Material("cable/redlaser") function SWEP:Setup(ply) - if ply.GetViewModel and ply:GetViewModel():IsValid() then - local attachmentIndex = ply:GetViewModel():LookupAttachment("muzzle") - if attachmentIndex == 0 then attachmentIndex = ply:GetViewModel():LookupAttachment("1") end + if ply:IsValid() then + local viewmodel = ply:GetViewModel() + if not viewmodel:IsValid() then return end + + local attachmentIndex = viewmodel:LookupAttachment("muzzle") + if attachmentIndex == 0 then attachmentIndex = viewmodel:LookupAttachment("1") end + if LocalPlayer():GetAttachment(attachmentIndex) then - self.VM = ply:GetViewModel() + self.VM = viewmodel self.Attach = attachmentIndex end end + self.WAttach = self:LookupAttachment("muzzle") end + function SWEP:Initialize() self:Setup(self:GetOwner()) end -function SWEP:Deploy(ply) + +function SWEP:Deploy() self:Setup(self:GetOwner()) end function SWEP:ViewModelDrawn() - if self.Weapon:GetNWBool("Active") and self.VM then - -- Draw the laser beam. - render.SetMaterial( LASER ) - render.DrawBeam(self.VM:GetAttachment(self.Attach).Pos, self:GetOwner():GetEyeTrace().HitPos, 2, 0, 12.5, Color(255, 0, 0, 255)) + if self:GetLaserEnabled() and self.VM then + render.SetMaterial(laser) + render.DrawBeam(self.VM:GetAttachment(self.Attach).Pos, self:GetOwner():GetEyeTrace().HitPos, 2, 0, 12.5, color_red) end end + function SWEP:DrawWorldModel() - self.Weapon:DrawModel() - if self.Weapon:GetNWBool("Active") then + self:DrawModel() + + if self:GetLaserEnabled() then local att = self:GetAttachment(self.WAttach) if not att then return end + local owner = self:GetOwner() local startpos = att.Pos local endpos + if IsValid(owner) then endpos = owner:GetEyeTrace().HitPos else - local tr = util.TraceLine({start = att.Pos, endpos = att.Pos+att.Ang:Forward()*16384, filter = self}) + local tr = util.TraceLine({ start = startpos, endpos = startpos + att.Ang:Forward() * 16384, filter = self }) endpos = tr.HitPos end + -- Draw the laser beam. - render.SetMaterial( LASER ) - render.DrawBeam(startpos, endpos, 2, 0, 12.5, Color(255, 0, 0, 255)) + render.SetMaterial(laser) + render.DrawBeam(startpos, endpos, 2, 0, 12.5, color_red) end end diff --git a/lua/weapons/laserpointer/init.lua b/lua/weapons/laserpointer/init.lua index 65d131b0..f5f2f6c1 100644 --- a/lua/weapons/laserpointer/init.lua +++ b/lua/weapons/laserpointer/init.lua @@ -1,6 +1,6 @@ AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") -include('shared.lua') +include("shared.lua") SWEP.Weight = 8 SWEP.AutoSwitchTo = false @@ -13,51 +13,61 @@ function SWEP:Initialize() self.Pointing = false end -function SWEP:Equip( newOwner ) +function SWEP:Equip(newOwner) if IsValid(newOwner.LasReceiver) then self.Receiver = newOwner.LasReceiver newOwner.LasReceiver = nil - newOwner:PrintMessage( HUD_PRINTTALK, "Relinked Sucessfully" ) + newOwner:PrintMessage(HUD_PRINTTALK, "Relinked Sucessfully") end end function SWEP:PrimaryAttack() self.Pointing = not self.Pointing - self.Weapon:SetNWBool("Active", self.Pointing) + self:SetLaserEnabled(self.Pointing) + if self.Pointing and IsValid(self.Receiver) then - Wire_TriggerOutput(self.Receiver,"Active",1) + Wire_TriggerOutput(self.Receiver,"Active", 1) else - Wire_TriggerOutput(self.Receiver,"Active",0) + Wire_TriggerOutput(self.Receiver,"Active", 0) end end function SWEP:SecondaryAttack() - local trace = self:GetOwner():GetEyeTrace() + local owner = self:GetOwner() + if not IsValid(owner) then return end - if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_las_receiver" and gamemode.Call("CanTool", self:GetOwner(), trace, "wire_las_receiver") then + local trace = owner:GetEyeTrace() + + if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_las_receiver" and gamemode.Call("CanTool", owner, trace, "wire_las_receiver") then self.Receiver = trace.Entity - self:GetOwner():PrintMessage( HUD_PRINTTALK, "Linked Sucessfully" ) + owner:PrintMessage(HUD_PRINTTALK, "Linked Sucessfully") + return true end end function SWEP:Think() - if(self.Pointing and self.Receiver and self.Receiver:IsValid())then + if self.Pointing and IsValid(self.Receiver) then local owner = self:GetOwner() + if not IsValid(owner) then return end + local trace + if IsValid(owner) then trace = owner:GetEyeTrace() else local att = self:GetAttachment(self:LookupAttachment("muzzle")) - trace = util.TraceLine({start = att.Pos, endpos = att.Pos+att.Ang:Forward()*16384, filter = self}) + trace = util.TraceLine({ start = att.Pos, endpos = att.Pos + att.Ang:Forward() * 16384, filter = self }) end + local point = trace.HitPos - if (COLOSSAL_SANDBOX) then point = point * 6.25 end + Wire_TriggerOutput(self.Receiver, "X", point.x) Wire_TriggerOutput(self.Receiver, "Y", point.y) Wire_TriggerOutput(self.Receiver, "Z", point.z) Wire_TriggerOutput(self.Receiver, "Pos", point) Wire_TriggerOutput(self.Receiver, "RangerData", trace) + self.Receiver.VPos = point end end diff --git a/lua/weapons/laserpointer/shared.lua b/lua/weapons/laserpointer/shared.lua index fe73c6c0..ebfcc328 100644 --- a/lua/weapons/laserpointer/shared.lua +++ b/lua/weapons/laserpointer/shared.lua @@ -4,11 +4,11 @@ SWEP.Purpose = "" SWEP.Instructions = "Left Click to designate targets. Right click to select laser receiver." SWEP.Category = "Wiremod" -SWEP.Spawnable = true; +SWEP.Spawnable = true SWEP.AdminOnly = false -SWEP.viewModel = "models/weapons/v_pistol.mdl"; -SWEP.worldModel = "models/weapons/w_pistol.mdl"; +SWEP.viewModel = "models/weapons/v_pistol.mdl" +SWEP.worldModel = "models/weapons/w_pistol.mdl" SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 @@ -19,3 +19,7 @@ SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" + +function SWEP:SetupDataTables() + self:NetworkVar("Bool", 0, "LaserEnabled") +end