Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 54 additions & 11 deletions lua/weapons/laserpointer/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,76 @@ SWEP.DrawCrosshair = true
local color_red = Color(255, 0, 0)
local laser = Material("cable/redlaser")

-- Adapted from the example in https://wiki.facepunch.com/gmod/Entity:GetAttachment
local function formatViewModelAttachment(origin)
local view = render.GetViewSetup()

local eye_pos = view.origin
local eye_ang = view.angles
local offset = origin - eye_pos
local forward = eye_ang:Forward()

local view_x = math.tan(view.fovviewmodel_unscaled * math.pi / 360)

if view_x == 0 then
forward:Mul(forward:Dot(offset))
eye_pos:Add(forward)

return eye_pos
end

local world_x = math.tan(view.fov_unscaled * math.pi / 360)

if world_x == 0 then
forward:Mul(forward:Dot(offset))
eye_pos:Add(forward)

return eye_pos
end

local right = eye_ang:Right()
local up = eye_ang:Up()

local factor = view_x / world_x
right:Mul(right:Dot(offset) * factor)
up:Mul(up:Dot(offset) * factor)

forward:Mul(forward:Dot(offset))

eye_pos:Add(right)
eye_pos:Add(up)
eye_pos:Add(forward)

return eye_pos
end

function SWEP:Setup(ply)
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
local attachment_index = viewmodel:LookupAttachment("muzzle")
if attachment_index == 0 then attachment_index = viewmodel:LookupAttachment("1") end

if LocalPlayer():GetAttachment(attachmentIndex) then
if LocalPlayer():GetAttachment(attachment_index) then
self.VM = viewmodel
self.Attach = attachmentIndex
self.Attach = attachment_index
end
end
end

function SWEP:Initialize()
self:Setup(self:GetOwner())
end

function SWEP:Deploy()
self:Setup(self:GetOwner())
end

function SWEP:ViewModelDrawn()
if self:GetLaserEnabled() and self.VM then
local vm = self.VM

if self:GetLaserEnabled() and vm then
local startpos = vm:GetAttachment(self.Attach).Pos

render.SetMaterial(laser)
render.DrawBeam(self.VM:GetAttachment(self.Attach).Pos, self:GetOwner():GetEyeTrace().HitPos, 2, 0, 12.5, color_red)
render.DrawBeam(formatViewModelAttachment(startpos), self:GetOwner():GetEyeTrace().HitPos, 2, 0, 12.5, color_red)
end
end

Expand All @@ -61,4 +104,4 @@ function SWEP:DrawWorldModel()
render.SetMaterial(laser)
render.DrawBeam(startpos, endpos, 2, 0, 12.5, color_red)
end
end
end
40 changes: 2 additions & 38 deletions lua/weapons/laserpointer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,11 @@ AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

SWEP.Weight = 8
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Receiver = nil
SWEP.Pointing = false

function SWEP:Initialize()
self.Pointing = false
end

function SWEP:Equip(newOwner)
if IsValid(newOwner.LasReceiver) then
self.Receiver = newOwner.LasReceiver
newOwner.LasReceiver = nil
newOwner:PrintMessage(HUD_PRINTTALK, "Relinked Sucessfully")
end
end

function SWEP:PrimaryAttack()
self.Pointing = not self.Pointing
self:SetLaserEnabled(self.Pointing)

if self.Pointing and IsValid(self.Receiver) then
Wire_TriggerOutput(self.Receiver,"Active", 1)
else
Wire_TriggerOutput(self.Receiver,"Active", 0)
end
end

function SWEP:SecondaryAttack()
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", owner, trace, "wire_las_receiver") then
self.Receiver = trace.Entity
owner:PrintMessage(HUD_PRINTTALK, "Linked Sucessfully")

return true
newOwner:PrintMessage(HUD_PRINTTALK, "Relinked Successfully!")
end
end

Expand Down Expand Up @@ -70,4 +34,4 @@ function SWEP:Think()

self.Receiver.VPos = point
end
end
end
66 changes: 61 additions & 5 deletions lua/weapons/laserpointer/shared.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
SWEP.Author = ""
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = "Left Click to designate targets. Right click to select laser receiver."
SWEP.Purpose = "Input source for Laser Pointer Receivers in Wiremod."
SWEP.Instructions = "Left click to designate targets. Right click to select laser receiver."
SWEP.Category = "Wiremod"

SWEP.Spawnable = true
SWEP.AdminOnly = false

SWEP.viewModel = "models/weapons/v_pistol.mdl"
SWEP.worldModel = "models/weapons/w_pistol.mdl"
SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
Expand All @@ -20,6 +21,61 @@ SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Weight = 8
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Receiver = nil
SWEP.Pointing = false

local singleplayer = game.SinglePlayer()

function SWEP:SetupDataTables()
self:NetworkVar("Bool", 0, "LaserEnabled")
self:NetworkVar("Bool", 0, "LaserEnabled")
end

function SWEP:Initialize()
self.Pointing = false

if CLIENT then
self:Setup(self:GetOwner())
end
end

function SWEP:PrimaryAttack()
self.Pointing = not self.Pointing
self:SetLaserEnabled(self.Pointing)

if CLIENT or singleplayer then
local pitch = self.Pointing and 120 or 80
self:EmitSound("ambient/energy/newspark03.wav", 100, pitch, 0.5)

return
end

if self.Pointing and IsValid(self.Receiver) then
Wire_TriggerOutput(self.Receiver, "Active", 1)
else
Wire_TriggerOutput(self.Receiver, "Active", 0)
end
end

function SWEP:SecondaryAttack()
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", owner, trace, "wire_las_receiver") then
if SERVER then
self.Receiver = trace.Entity
owner:PrintMessage(HUD_PRINTTALK, "Linked Successfully!")
elseif (CLIENT or singleplayer) and IsFirstTimePredicted() then
self:EmitSound("buttons/bell1.wav")
end

return true
elseif (CLIENT or singleplayer) and IsFirstTimePredicted() then
self:EmitSound("buttons/button16.wav", 100, 50, 0.5)
end
end
33 changes: 23 additions & 10 deletions lua/weapons/remotecontroller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ AddCSLuaFile()

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

SWEP.PrintName = "Remote Control"
SWEP.Slot = 0
Expand All @@ -24,20 +25,33 @@ SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
SWEP.viewModel = "models/weapons/v_pistol.mdl"
SWEP.worldModel = "models/weapons/w_pistol.mdl"

if CLIENT then return end
SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/c_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"

local singleplayer = game.SinglePlayer()

function SWEP:PrimaryAttack()
local ply = self:GetOwner()
local trace = ply:GetEyeTrace()
if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_pod" and gamemode.Call("PlayerUse", ply, trace.Entity) then
self.Linked = trace.Entity
ply:ChatPrint("Remote Controller linked.")

if IsValid(trace.Entity) and trace.Entity:GetClass() == "gmod_wire_pod" then
if SERVER and gamemode.Call("PlayerUse", ply, trace.Entity) then
self.Linked = trace.Entity
ply:ChatPrint("Remote Controller linked.")
elseif (CLIENT or singleplayer) and IsFirstTimePredicted() then
self:EmitSound("buttons/bell1.wav")
end
elseif (CLIENT or singleplayer) and IsFirstTimePredicted() then
self:EmitSound("buttons/button16.wav", 100, 50)
end
end

function SWEP:SecondaryAttack() end

if CLIENT then return end

function SWEP:Holster()
local ply = self:GetOwner()
if IsValid(ply) and self.Linked then
Expand Down Expand Up @@ -112,5 +126,4 @@ end

hook.Add("PlayerNoClip", "wire_remotecontroller_antinoclip", function(ply, cmd)
if ply.using_wire_remote_control then return false end
end)

end)
Loading