Skip to content

Commit 0cd9a90

Browse files
Improve SWEP visuals/audio (#3377)
* Improve SWEP visuals/audio * Replace deprecated function * Address some feedback * Rework laser viewmodel code * Remove IsFirstTimePredicted checks for sounds * Fix sounds in singleplayer
1 parent 66f03d5 commit 0cd9a90

File tree

4 files changed

+104
-82
lines changed

4 files changed

+104
-82
lines changed

lua/weapons/laserpointer/cl_init.lua

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,26 @@ SWEP.DrawCrosshair = true
99
local color_red = Color(255, 0, 0)
1010
local laser = Material("cable/redlaser")
1111

12-
function SWEP:Setup(ply)
13-
if ply:IsValid() then
14-
local viewmodel = ply:GetViewModel()
15-
if not viewmodel:IsValid() then return end
16-
17-
local attachmentIndex = viewmodel:LookupAttachment("muzzle")
18-
if attachmentIndex == 0 then attachmentIndex = viewmodel:LookupAttachment("1") end
19-
20-
if LocalPlayer():GetAttachment(attachmentIndex) then
21-
self.VM = viewmodel
22-
self.Attach = attachmentIndex
23-
end
24-
end
12+
-- Scale screen coords by linear proportion of viewmodel and world fov
13+
local function WorldToViewModel(point)
14+
local view = render.GetViewSetup()
15+
local factor = math.tan(math.rad(view.fovviewmodel_unscaled) * 0.5) / math.tan(math.rad(view.fov_unscaled) * 0.5)
16+
point = WorldToLocal(point, angle_zero, view.origin, view.angles)
17+
point:Mul(Vector(1, factor, factor))
18+
point = LocalToWorld(point, angle_zero, view.origin, view.angles)
19+
return point
2520
end
2621

27-
function SWEP:Initialize()
28-
self:Setup(self:GetOwner())
29-
end
22+
function SWEP:PostDrawViewModel(vm, wep, ply)
23+
if self:GetLaserEnabled() then
24+
local att = vm:GetAttachment(vm:LookupAttachment("muzzle") or 0)
25+
if not att then return end
3026

31-
function SWEP:Deploy()
32-
self:Setup(self:GetOwner())
33-
end
27+
local startpos = WorldToViewModel(att.Pos)
28+
local endpos = ply:GetEyeTrace().HitPos
3429

35-
function SWEP:ViewModelDrawn()
36-
if self:GetLaserEnabled() and self.VM then
3730
render.SetMaterial(laser)
38-
render.DrawBeam(self.VM:GetAttachment(self.Attach).Pos, self:GetOwner():GetEyeTrace().HitPos, 2, 0, 12.5, color_red)
31+
render.DrawBeam(startpos, endpos, 2, 0, 12.5, color_red)
3932
end
4033
end
4134

@@ -61,4 +54,4 @@ function SWEP:DrawWorldModel()
6154
render.SetMaterial(laser)
6255
render.DrawBeam(startpos, endpos, 2, 0, 12.5, color_red)
6356
end
64-
end
57+
end

lua/weapons/laserpointer/init.lua

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,11 @@ AddCSLuaFile("cl_init.lua")
22
AddCSLuaFile("shared.lua")
33
include("shared.lua")
44

5-
SWEP.Weight = 8
6-
SWEP.AutoSwitchTo = false
7-
SWEP.AutoSwitchFrom = false
8-
9-
SWEP.Receiver = nil
10-
SWEP.Pointing = false
11-
12-
function SWEP:Initialize()
13-
self.Pointing = false
14-
end
15-
165
function SWEP:Equip(newOwner)
176
if IsValid(newOwner.LasReceiver) then
187
self.Receiver = newOwner.LasReceiver
198
newOwner.LasReceiver = nil
20-
newOwner:PrintMessage(HUD_PRINTTALK, "Relinked Sucessfully")
21-
end
22-
end
23-
24-
function SWEP:PrimaryAttack()
25-
self.Pointing = not self.Pointing
26-
self:SetLaserEnabled(self.Pointing)
27-
28-
if self.Pointing and IsValid(self.Receiver) then
29-
Wire_TriggerOutput(self.Receiver,"Active", 1)
30-
else
31-
Wire_TriggerOutput(self.Receiver,"Active", 0)
32-
end
33-
end
34-
35-
function SWEP:SecondaryAttack()
36-
local owner = self:GetOwner()
37-
if not IsValid(owner) then return end
38-
39-
local trace = owner:GetEyeTrace()
40-
41-
if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_las_receiver" and gamemode.Call("CanTool", owner, trace, "wire_las_receiver") then
42-
self.Receiver = trace.Entity
43-
owner:PrintMessage(HUD_PRINTTALK, "Linked Sucessfully")
44-
45-
return true
9+
newOwner:PrintMessage(HUD_PRINTTALK, "Relinked Successfully!")
4610
end
4711
end
4812

@@ -61,13 +25,14 @@ function SWEP:Think()
6125
end
6226

6327
local point = trace.HitPos
28+
local receiver = self.Receiver
6429

65-
Wire_TriggerOutput(self.Receiver, "X", point.x)
66-
Wire_TriggerOutput(self.Receiver, "Y", point.y)
67-
Wire_TriggerOutput(self.Receiver, "Z", point.z)
68-
Wire_TriggerOutput(self.Receiver, "Pos", point)
69-
Wire_TriggerOutput(self.Receiver, "RangerData", trace)
30+
WireLib.TriggerOutput(receiver, "X", point.x)
31+
WireLib.TriggerOutput(receiver, "Y", point.y)
32+
WireLib.TriggerOutput(receiver, "Z", point.z)
33+
WireLib.TriggerOutput(receiver, "Pos", point)
34+
WireLib.TriggerOutput(receiver, "RangerData", trace)
7035

71-
self.Receiver.VPos = point
36+
receiver.VPos = point
7237
end
73-
end
38+
end
Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
SWEP.Author = ""
22
SWEP.Contact = ""
3-
SWEP.Purpose = ""
4-
SWEP.Instructions = "Left Click to designate targets. Right click to select laser receiver."
3+
SWEP.Purpose = "Input source for Laser Pointer Receivers in Wiremod."
4+
SWEP.Instructions = "Left click to designate targets. Right click to select laser receiver."
55
SWEP.Category = "Wiremod"
66

77
SWEP.Spawnable = true
88
SWEP.AdminOnly = false
99

10-
SWEP.viewModel = "models/weapons/v_pistol.mdl"
11-
SWEP.worldModel = "models/weapons/w_pistol.mdl"
10+
SWEP.UseHands = true
11+
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
12+
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
1213

1314
SWEP.Primary.ClipSize = -1
1415
SWEP.Primary.DefaultClip = -1
@@ -20,6 +21,55 @@ SWEP.Secondary.DefaultClip = -1
2021
SWEP.Secondary.Automatic = false
2122
SWEP.Secondary.Ammo = "none"
2223

24+
SWEP.Weight = 8
25+
SWEP.AutoSwitchTo = false
26+
SWEP.AutoSwitchFrom = false
27+
28+
SWEP.Receiver = nil
29+
SWEP.Pointing = false
30+
31+
local singleplayer = game.SinglePlayer()
32+
2333
function SWEP:SetupDataTables()
24-
self:NetworkVar("Bool", 0, "LaserEnabled")
34+
self:NetworkVar("Bool", 0, "LaserEnabled")
2535
end
36+
37+
function SWEP:PrimaryAttack()
38+
self.Pointing = not self.Pointing
39+
self:SetLaserEnabled(self.Pointing)
40+
41+
if CLIENT or singleplayer then
42+
local pitch = self.Pointing and 120 or 80
43+
self:EmitSound("ambient/energy/newspark03.wav", 100, pitch, 0.5)
44+
45+
if CLIENT then return end
46+
end
47+
48+
if self.Pointing and IsValid(self.Receiver) then
49+
WireLib.TriggerOutput(self.Receiver, "Active", 1)
50+
else
51+
WireLib.TriggerOutput(self.Receiver, "Active", 0)
52+
end
53+
end
54+
55+
function SWEP:SecondaryAttack()
56+
local owner = self:GetOwner()
57+
if not IsValid(owner) then return end
58+
59+
local trace = owner:GetEyeTrace()
60+
61+
if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_las_receiver" and gamemode.Call("CanTool", owner, trace, "wire_las_receiver") then
62+
if SERVER then
63+
self.Receiver = trace.Entity
64+
owner:PrintMessage(HUD_PRINTTALK, "Linked Successfully!")
65+
end
66+
67+
if CLIENT or singleplayer then
68+
self:EmitSound("buttons/bell1.wav")
69+
end
70+
71+
return true
72+
elseif CLIENT or singleplayer then
73+
self:EmitSound("buttons/button16.wav", 100, 50, 0.5)
74+
end
75+
end

lua/weapons/remotecontroller.lua

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ AddCSLuaFile()
22

33
SWEP.Author = "Divran" -- Originally by ShaRose, rewritten by Divran at 2011-04-03
44
SWEP.Contact = ""
5-
SWEP.Purpose = "Remote control for Pod Controllers in wire."
6-
SWEP.Instructions = "Left Click on Pod Controller to link up, and use to start controlling."
5+
SWEP.Purpose = "Remote control for Pod Controllers in Wiremod."
6+
SWEP.Instructions = "Left click on a Pod Controller to link up, and use to start controlling."
77
SWEP.Category = "Wiremod"
8+
SWEP.IconOverride = "entities/weapon_pistol.png"
89

910
SWEP.PrintName = "Remote Control"
1011
SWEP.Slot = 0
@@ -24,20 +25,34 @@ SWEP.Secondary.ClipSize = -1
2425
SWEP.Secondary.DefaultClip = -1
2526
SWEP.Secondary.Automatic = false
2627
SWEP.Secondary.Ammo = "none"
27-
SWEP.viewModel = "models/weapons/v_pistol.mdl"
28-
SWEP.worldModel = "models/weapons/w_pistol.mdl"
2928

30-
if CLIENT then return end
29+
SWEP.UseHands = true
30+
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
31+
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
3132

3233
function SWEP:PrimaryAttack()
3334
local ply = self:GetOwner()
3435
local trace = ply:GetEyeTrace()
35-
if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_pod" and gamemode.Call("PlayerUse", ply, trace.Entity) then
36-
self.Linked = trace.Entity
37-
ply:ChatPrint("Remote Controller linked.")
36+
local ent = trace.Entity
37+
38+
if IsValid(ent) and ent:GetClass() == "gmod_wire_pod" then
39+
if SERVER and gamemode.Call("PlayerUse", ply, ent) then
40+
self.Linked = ent
41+
ply:ChatPrint("Remote Controller linked.")
42+
end
43+
44+
if CLIENT or game.SinglePlayer() then
45+
self:EmitSound("buttons/bell1.wav")
46+
end
47+
elseif CLIENT or game.SinglePlayer() then
48+
self:EmitSound("buttons/button16.wav", 100, 50)
3849
end
3950
end
4051

52+
function SWEP:SecondaryAttack() end
53+
54+
if CLIENT then return end
55+
4156
function SWEP:Holster()
4257
local ply = self:GetOwner()
4358
if IsValid(ply) and self.Linked then
@@ -112,5 +127,4 @@ end
112127

113128
hook.Add("PlayerNoClip", "wire_remotecontroller_antinoclip", function(ply, cmd)
114129
if ply.using_wire_remote_control then return false end
115-
end)
116-
130+
end)

0 commit comments

Comments
 (0)