Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laser Pointer refactoring #3232

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions lua/weapons/laserpointer/cl_init.lua
Original file line number Diff line number Diff line change
@@ -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
34 changes: 22 additions & 12 deletions lua/weapons/laserpointer/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
include("shared.lua")

SWEP.Weight = 8
SWEP.AutoSwitchTo = false
Expand All @@ -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

local trace = owner:GetEyeTrace()

if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_las_receiver" and gamemode.Call("CanTool", self:GetOwner(), trace, "wire_las_receiver") then
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
10 changes: 7 additions & 3 deletions lua/weapons/laserpointer/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Loading