Skip to content

Commit a722233

Browse files
Micro-optimizations for base_wire_entity (#3192)
* base_wire_entity optimizations * Do not create new vectors * table.IsEmpty -> halos[1] == nil Co-authored-by: thegrb93 <[email protected]> * Revert some changes --------- Co-authored-by: thegrb93 <[email protected]>
1 parent 831bc00 commit a722233

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

lua/entities/base_wire_entity.lua

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ if CLIENT then
4242
if pos == spos then -- if the position is right in your face, get a better position
4343
pos = spos + localPly:GetAimVector() * 5
4444
end
45+
4546
pos = pos:ToScreen()
4647

4748
pos.x = math.Round(pos.x)
@@ -117,14 +118,14 @@ if CLIENT then
117118
}
118119

119120
render.CullMode(MATERIAL_CULLMODE_CCW)
120-
surface.DrawPoly( poly )
121+
surface.DrawPoly(poly)
121122

122123
surface.SetDrawColor(0, 0, 0, 255)
123124

124-
for i=1,#poly-1 do
125+
for i = 1, 5 do
125126
surface.DrawLine( poly[i].x, poly[i].y, poly[i+1].x, poly[i+1].y )
126127
end
127-
surface.DrawLine( poly[#poly].x, poly[#poly].y, poly[1].x, poly[1].y )
128+
surface.DrawLine( poly[6].x, poly[6].y, poly[1].x, poly[1].y )
128129
end
129130

130131
local function getWireName( ent )
@@ -142,7 +143,7 @@ if CLIENT then
142143
-- This is overridable by other wire entities which want to customize the overlay
143144
function ENT:DrawWorldTipBody( pos )
144145
local data = self:GetOverlayData()
145-
draw.DrawText( data.txt, "GModWorldtip", pos.center.x, pos.min.y + edgesize/2, Color(255,255,255,255), TEXT_ALIGN_CENTER )
146+
draw.DrawText( data.txt, "GModWorldtip", pos.center.x, pos.min.y + edgesize/2, color_white, TEXT_ALIGN_CENTER )
146147
end
147148

148149
-- This is overridable by other wire entities which want to customize the overlay
@@ -206,11 +207,11 @@ if CLIENT then
206207
end
207208

208209
if info_requires_multiline then
209-
draw.DrawText( class, "GModWorldtip", pos.center.x, offset + 8, Color(255,255,255,255), TEXT_ALIGN_CENTER )
210-
draw.DrawText( name, "GModWorldtip", pos.center.x, offset + h_class + 16, Color(255,255,255,255), TEXT_ALIGN_CENTER )
210+
draw.DrawText( class, "GModWorldtip", pos.center.x, offset + 8, color_white, TEXT_ALIGN_CENTER )
211+
draw.DrawText( name, "GModWorldtip", pos.center.x, offset + h_class + 16, color_white, TEXT_ALIGN_CENTER )
211212
else
212-
draw.DrawText( class, "GModWorldtip", pos.min.x + edgesize, offset + 16, Color(255,255,255,255) )
213-
draw.DrawText( name, "GModWorldtip", pos.min.x + pos.size.w - w_name - edgesize, offset + 16, Color(255,255,255,255) )
213+
draw.DrawText( class, "GModWorldtip", pos.min.x + edgesize, offset + 16, color_white )
214+
draw.DrawText( name, "GModWorldtip", pos.min.x + pos.size.w - w_name - edgesize, offset + 16, color_white )
214215
end
215216
end
216217

@@ -282,12 +283,14 @@ if CLIENT then
282283
end
283284

284285
function ENT:Think()
285-
if (CurTime() >= (self.NextRBUpdate or 0)) then
286+
local tab = self:GetTable()
287+
288+
if (CurTime() >= (tab.NextRBUpdate or 0)) then
286289
-- We periodically update the render bounds every 10 seconds - the
287290
-- reasons why are mostly anecdotal, but in some circumstances
288291
-- entities might 'forget' their renderbounds. Nobody really knows
289292
-- if this is still needed or not.
290-
self.NextRBUpdate = CurTime() + 10
293+
tab.NextRBUpdate = CurTime() + 10
291294
Wire_UpdateRenderBounds(self)
292295
end
293296
end
@@ -301,9 +304,11 @@ if CLIENT then
301304
halos_inv[self] = true
302305
end
303306

307+
local color_halo = Color(100, 100, 255)
308+
304309
hook.Add("PreDrawHalos", "Wiremod_overlay_halos", function()
305-
if #halos == 0 then return end
306-
halo.Add(halos, Color(100,100,255), 3, 3, 1, true, true)
310+
if halos[1]==nil then return end
311+
halo.Add(halos, color_halo, 3, 3, 1, true, true)
307312
halos = {}
308313
halos_inv = {}
309314
end)
@@ -510,15 +515,18 @@ ENT.LINK_STATUS_DEACTIVATED = 2 -- alias
510515
ENT.LINK_STATUS_ACTIVE = 3
511516
ENT.LINK_STATUS_ACTIVATED = 3 -- alias
512517
function ENT:ColorByLinkStatus(status)
513-
local a = self:GetColor().a
514-
515-
if status == self.LINK_STATUS_UNLINKED then
516-
self:SetColor(Color(255,0,0,a))
517-
elseif status == self.LINK_STATUS_LINKED then
518-
self:SetColor(Color(255,165,0,a))
519-
elseif status == self.LINK_STATUS_ACTIVE then
520-
self:SetColor(Color(0,255,0,a))
518+
local tab = self:GetTable()
519+
local color = self:GetColor()
520+
521+
if status == tab.LINK_STATUS_UNLINKED then
522+
color.r, color.g, color.b = 255, 0, 0
523+
elseif status == tab.LINK_STATUS_LINKED then
524+
color.r, color.g, color.b = 255, 165, 0
525+
elseif status == tab.LINK_STATUS_ACTIVE then
526+
color.r, color.g, color.b = 0, 255, 0
521527
else
522-
self:SetColor(Color(255,255,255,a))
528+
color.r, color.g, color.b = 255, 255, 255
523529
end
530+
531+
self:SetColor(color)
524532
end

0 commit comments

Comments
 (0)