local HudNameRenderSystem = inherited("HudNameRenderSystem", SystemLinear)

function HudNameRenderSystem:new()
	local ms = instance(self)
	return ms
end

HudNameRenderSystem.key = "hud_name"

function HudNameRenderSystem:dynamic_wants(ent)
	return ent.aspects.hud_name and ent.hud_name and ent.hud_name_display and not ent.hud_info_display
end

function HudNameRenderSystem:ent_added(ent)
	ent.rx = ent.x
	ent.ry = ent.y
	ent.rz = ent.z
end

function HudNameRenderSystem:ent_removed(ent)
	ent.hud_name_display = false
end

local function ry_sort(a,b)
	return a.ry < b.ry
end

function HudNameRenderSystem:step()
	shellsort(self.ents, ry_sort)
end

function HudNameRenderSystem:render_hud(x, y, scale, perspective, angle, a, r, g, b)
	for index, ent in ipairs(self.ents) do
		local op = math.max(0,(ent.hud_name_display_fade or 1))
		if op > 0 then
			local hud_info = ent.hud_name
			local rx = x + ent.rx*scale
			local ry = y + ent.ry*scale*perspective -(ent.rz)*scale - ent.hud_radius*scale - hud_info.hud_info_h*0.5
			hud_info:render(rx, ry, 0.75, perspective, angle, a*op, r, g, b)
		end
	end
end

return HudNameRenderSystem