Skip to content

Commit

Permalink
Add simple ping system
Browse files Browse the repository at this point in the history
  • Loading branch information
StyledStrike committed Jun 12, 2024
1 parent b5645b7 commit 1e51f98
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lua/autorun/sh_squad_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ SquadMenu.MAX_NAME_LENGTH = 30
SquadMenu.MAX_JSON_SIZE = 49152 -- 48 kibibytes

-- Used on net.WriteUInt for the command ID
SquadMenu.COMMAND_SIZE = 3
SquadMenu.COMMAND_SIZE = 4

-- Command IDs (Max. ID when COMMAND_SIZE = 3 is 7)
-- Command IDs (Max. ID when COMMAND_SIZE = 4 is 15)
SquadMenu.BROADCAST_EVENT = 0
SquadMenu.SQUAD_LIST = 1
SquadMenu.SETUP_SQUAD = 2
Expand All @@ -29,6 +29,7 @@ SquadMenu.LEAVE_SQUAD = 4
SquadMenu.ACCEPT_REQUESTS = 5
SquadMenu.REQUESTS_LIST = 6
SquadMenu.KICK = 7
SquadMenu.PING = 8

-- Reasons given when a member is removed from a squad
SquadMenu.LEAVE_REASON_DELETED = 0
Expand Down
5 changes: 4 additions & 1 deletion lua/squad_menu/client/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function Config:Reset()

self.nameDistance = 3000
self.haloDistance = 8000
self.pingKey = KEY_B
end

function Config:Load()
Expand All @@ -27,6 +28,7 @@ function Config:Load()

self.nameDistance = SquadMenu.ValidateNumber( data.nameDistance, 3000, 500, 50000 )
self.haloDistance = SquadMenu.ValidateNumber( data.haloDistance, 8000, 500, 50000 )
self.pingKey = math.floor( SquadMenu.ValidateNumber( data.pingKey, KEY_B, 1, 159 ) )
end

function Config:Save( immediate )
Expand All @@ -46,7 +48,8 @@ function Config:Save( immediate )
showMembers = self.showMembers,
showRings = self.showRings,
showHalos = self.showHalos,
enableSounds = self.enableSounds
enableSounds = self.enableSounds,
pingKey = self.pingKey
} )

SquadMenu.PrintF( "%s: writing %s", path, string.NiceSize( string.len( data ) ) )
Expand Down
60 changes: 57 additions & 3 deletions lua/squad_menu/client/hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ function SquadMenu:RemoveMembersHUD()

hook.Remove( "PrePlayerDraw", "SquadMenu.DrawRing" )
hook.Remove( "PreDrawHalos", "SquadMenu.DrawHalos" )
hook.Remove( "HUDPaint", "SquadMenu.DrawMemberTags" )
hook.Remove( "HUDPaint", "SquadMenu.DrawHUD" )
hook.Remove( "HUDDrawTargetID", "SquadMenu.HideTargetInfo" )
hook.Remove( "PlayerButtonDown", "SquadMenu.PingKey" )
end

function SquadMenu:UpdateMembersHUD()
Expand All @@ -33,8 +34,9 @@ function SquadMenu:UpdateMembersHUD()
hook.Remove( "PreDrawHalos", "SquadMenu.DrawHalos" )
end

hook.Add( "HUDPaint", "SquadMenu.DrawMemberTags", self.DrawMemberTags )
hook.Add( "HUDPaint", "SquadMenu.DrawHUD", self.DrawHUD )
hook.Add( "HUDDrawTargetID", "SquadMenu.HideTargetInfo", self.HideTargetInfo )
hook.Add( "PlayerButtonDown", "SquadMenu.PingKey", self.PingKey )

if self.membersPanel then
self.membersPanel:SetVisible( config.showMembers )
Expand Down Expand Up @@ -267,9 +269,16 @@ local function DrawTag( ply )
end
end

SquadMenu.DrawMemberTags = function()
local DrawOutlinedText = draw.SimpleTextOutlined
local pingMat = Material( "squad_menu/ping.png", "smooth ignorez nocull" )
local pings = SquadMenu.pings or {}

SquadMenu.pings = pings

SquadMenu.DrawHUD = function()
surface.SetFont( "SquadMenuInfo" )

-- Draw player tags
local origin = EyePos()
local me = LocalPlayer()
local byId = AllPlayersById()
Expand All @@ -288,5 +297,50 @@ SquadMenu.DrawMemberTags = function()
end
end

-- Draw pings
local t = RealTime()
local sw, sh = ScrW(), ScrH()
local r, g, b = squad.color:Unpack()
local size = sh * 0.025
local border = sh * 0.01

SetMaterial( pingMat )

for id, p in pairs( pings ) do
if t > p.start + p.lifetime then
pings[id] = nil
else
local showAnim = Clamp( ( t - p.start ) * 6, 0, 1 )
local hideAnim = Clamp( p.start + p.lifetime - t, 0, 1 )

SetAlphaMultiplier( showAnim * hideAnim )

local pos = p.pos:ToScreen()
local x = Clamp( pos.x, border, sw - border )
local y = Clamp( pos.y, sh * 0.05, sh - border )
local iconY = y - size - ( size * ( 1 - showAnim ) )

SetColor( 0, 0, 0 )
DrawTexturedRect( 1 + x - size * 0.5, 1 + iconY, size, size )

SetColor( r, g, b )
DrawTexturedRect( x - size * 0.5, iconY, size, size )
DrawOutlinedText( p.label, "SquadMenuInfo", x, y - size - sh * 0.006, squad.color, 1, 4, 1, color_black )
end
end

SetAlphaMultiplier( 1 )
end

SquadMenu.PingKey = function( ply, button )
if ply ~= LocalPlayer() then return end
if not IsFirstTimePredicted() then return end
if button ~= SquadMenu.Config.pingKey then return end

local tr = ply:GetEyeTrace()
if not tr.Hit then return end

SquadMenu.StartCommand( SquadMenu.PING )
net.WriteVector( tr.HitPos )
net.SendToServer()
end
27 changes: 26 additions & 1 deletion lua/squad_menu/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ commands[SquadMenu.REQUESTS_LIST] = function()
SquadMenu:UpdateRequestsPanel()
end

commands[SquadMenu.PING] = function()
local pos = net.ReadVector()
local label = net.ReadString()
local id = net.ReadString()

local ping = SquadMenu.pings[id]

if not ping then
ping = {}
end

ping.pos = pos
ping.label = label
ping.start = RealTime()
ping.lifetime = 5

SquadMenu.pings[id] = ping

local eyePos = EyePos()
local soundDir = pos - eyePos
soundDir:Normalize()

sound.Play( "friends/friend_join.wav", eyePos + soundDir * 500, 100, 120, 1 )
end

commands[SquadMenu.BROADCAST_EVENT] = function()
local data = SquadMenu.ReadTable()
local event = data.event
Expand Down Expand Up @@ -307,7 +332,7 @@ if engine.ActiveGamemode() == "sandbox" then
"SquadMenuDesktopIcon",
{
title = SquadMenu.GetLanguageText( "title" ),
icon = "materials/icon128/squad_menu.png",
icon = "materials/squad_menu/squad_menu.png",
init = function() SquadMenu:OpenFrame() end
}
)
Expand Down
25 changes: 25 additions & 0 deletions lua/squad_menu/client/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ function SquadMenu:OpenFrame()
self.Config:Save()
end

local panelPing = vgui.Create( "DPanel", scroll )
panelPing:SetPaintBackground( false )
panelPing:SetTall( 30 )
panelPing:Dock( TOP )
panelPing:DockMargin( 0, 0, 0, 8 )

local labelPing = vgui.Create( "DLabel", panelPing )
labelPing:SetText( L( "settings.ping_key" ) )
labelPing:SizeToContents()
labelPing:Dock( LEFT )

ApplyTheme( labelPing )

local binderPing = vgui.Create( "DBinder", panelPing )
binderPing:SetValue( self.Config.pingKey )
binderPing:Dock( FILL )
binderPing:DockMargin( 20, 0, 0, 0 )

ApplyTheme( binderPing )

binderPing.OnChange = function( _, key )
self.Config.pingKey = key
self.Config:Save()
end

CreateToggleButton( scroll, "settings.show_members", self.Config.showMembers, function( checked )
self.Config.showMembers = checked
self.Config:Save()
Expand Down
2 changes: 2 additions & 0 deletions lua/squad_menu/client/theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Theme.classes["DButton"] = {
end
}

Theme.classes["DBinder"] = Theme.classes["DButton"]

Theme.classes["DTextEntry"] = {
Prepare = function( self )
self:SetDrawBorder( false )
Expand Down
2 changes: 1 addition & 1 deletion lua/squad_menu/client/vgui/tabbed_frame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function PANEL:Init()
local h = math.max( ScrH() * 0.45, 400 )

self:SetTitle( L"title" )
self:SetIcon( "icon128/squad_menu.png" )
self:SetIcon( "squad_menu/squad_menu.png" )
self:SetPos( 0, 0 )
self:SetSize( w, h )
self:SetSizable( true )
Expand Down
22 changes: 21 additions & 1 deletion lua/squad_menu/server/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,34 @@ commands[SquadMenu.KICK] = function( ply )
end
end

commands[SquadMenu.PING] = function( ply )
local squadId = ply:GetSquadID()
if squadId == -1 then return end

local squad = SquadMenu:GetSquad( squadId )
if not squad then return end

local pos = net.ReadVector()

local members, count = squad:GetActiveMembers()
if count == 0 then return end

SquadMenu.StartCommand( SquadMenu.PING )
net.WriteVector( pos )
net.WriteString( ply:Nick() )
net.WriteString( ply:SteamID() )
net.Send( members )
end

-- Safeguard against spam
local cooldowns = {
[SquadMenu.SQUAD_LIST] = { interval = 0.5, players = {} },
[SquadMenu.SETUP_SQUAD] = { interval = 1, players = {} },
[SquadMenu.JOIN_SQUAD] = { interval = 0.1, players = {} },
[SquadMenu.LEAVE_SQUAD] = { interval = 1, players = {} },
[SquadMenu.ACCEPT_REQUESTS] = { interval = 0.2, players = {} },
[SquadMenu.KICK] = { interval = 0.1, players = {} }
[SquadMenu.KICK] = { interval = 0.1, players = {} },
[SquadMenu.PING] = { interval = 0.5, players = {} }
}

net.Receive( "squad_menu.command", function( _, ply )
Expand Down
Binary file added materials/squad_menu/ping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
3 changes: 2 additions & 1 deletion resource/localization/en/squad_menu.properties
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ squad_menu.settings.show_halos=Show halos around members
squad_menu.settings.show_rings=Show rings around members
squad_menu.settings.enable_sounds=Play UI sounds
squad_menu.settings.name_draw_distance=Name draw distance
squad_menu.settings.halo_draw_distance=Halo draw distance
squad_menu.settings.halo_draw_distance=Halo draw distance
squad_menu.settings.ping_key=Ping key
3 changes: 2 additions & 1 deletion resource/localization/pt-br/squad_menu.properties
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ squad_menu.settings.show_halos=Mostrar aréolas nos membros
squad_menu.settings.show_rings=Mostrar anéis nos membros
squad_menu.settings.enable_sounds=Tocar sons da interface
squad_menu.settings.name_draw_distance=Distância de renderização dos nomes
squad_menu.settings.halo_draw_distance=Distância de renderização das aréolas
squad_menu.settings.halo_draw_distance=Distância de renderização das aréolas
squad_menu.settings.ping_key=Tecla de ping

0 comments on commit 1e51f98

Please sign in to comment.