Skip to content

Commit

Permalink
Let multiple huds link to single vehicle. Fixes: #1515
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 committed Oct 16, 2023
1 parent 2c0e22d commit 6e84fa2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
34 changes: 21 additions & 13 deletions lua/entities/starfall_hud/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,33 @@ end

function ENT:LinkVehicle(ent)
if ent then
vehiclelinks[ent] = self
if not vehiclelinks[ent] then vehiclelinks[ent] = {} end
vehiclelinks[ent][self] = true
else
for k, v in pairs(vehiclelinks) do
if self == v then
vehiclelinks[k] = nil
end
for k, huds in pairs(vehiclelinks) do
huds[self] = nil
end
end
end

hook.Add("PlayerEnteredVehicle", "Starfall_HUD_PlayerEnteredVehicle", function(ply, vehicle)
for k, v in pairs(vehiclelinks) do
if vehicle == k and v:IsValid() then
if v.link and v.link:IsValid() then
SF.EnableHud(ply, v.link, vehicle, true)
local function vehicleEnableHud(ply, vehicle, enabled)
for k, huds in pairs(vehiclelinks) do
if vehicle == k then
for v in pairs(huds) do
if v:IsValid() then
if v.link and v.link:IsValid() then
SF.EnableHud(ply, v.link, vehicle, enabled)
end
else
huds[v] = nil
end
end
end
end
end)
end

hook.Add("PlayerEnteredVehicle", "Starfall_HUD", function(ply, vehicle) vehicleEnableHud(ply, vehicle, true) end)
hook.Add("PlayerLeaveVehicle", "Starfall_HUD", function(ply, vehicle) vehicleEnableHud(ply, vehicle, false) end)

function ENT:PreEntityCopy()
if self.EntityMods then self.EntityMods.SFLink = nil end
Expand All @@ -58,8 +66,8 @@ function ENT:PreEntityCopy()
info.link = self.link:EntIndex()
end
local linkedvehicles = {}
for k, v in pairs(vehiclelinks) do
if v == self and k:IsValid() then
for k, huds in pairs(vehiclelinks) do
if huds[self] and k:IsValid() then
linkedvehicles[#linkedvehicles + 1] = k:EntIndex()
end
end
Expand Down
4 changes: 0 additions & 4 deletions lua/entities/starfall_processor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ if SERVER then
local function disconnect(sync)
huds[ply] = nil
hook.Remove("EntityRemoved", n)
hook.Remove("PlayerLeaveVehicle", n)
ply:SetViewEntity()
if activator.locksControls and activator.link and activator.link:IsValid() then
net.Start("starfall_lock_control")
Expand All @@ -279,9 +278,6 @@ if SERVER then
if enabled then
huds[ply] = true
hook.Add("EntityRemoved",n,function(e) if e==ply or e==activator then disconnect(true) end end)
if activator:IsVehicle() then
hook.Add("PlayerLeaveVehicle",n,function(p,v) if p==ply or v==activator then disconnect(true) end end)
end
if activator.locksControls and activator.link and activator.link:IsValid() then
net.Start("starfall_lock_control")
net.WriteEntity(activator.link)
Expand Down
2 changes: 1 addition & 1 deletion lua/starfall/libs_sh/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function ents_methods:getLinkedComponents()
end
elseif ent:GetClass() == "starfall_hud" then
if SERVER then
for k, v in pairs(SF.HudVehicleLinks) do if v == ent then list[#list+1] = owrap(k) end end
for k, huds in pairs(SF.HudVehicleLinks) do if huds[ent] then list[#list+1] = owrap(k) end end
else
SF.Throw("You may only get starfall_hud links through the server", 2)
end
Expand Down

0 comments on commit 6e84fa2

Please sign in to comment.