local HudInfoRenderSystem = inherited("HudInfoRenderSystem", SystemLinear)

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

HudInfoRenderSystem.key = "hud_info"

function HudInfoRenderSystem:dynamic_wants(ent)
	return ent.aspects.hud_info and ent.hud_info and ent.hud_info_display
end

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

function HudInfoRenderSystem:ent_removed(ent)
end

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

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

function HudInfoRenderSystem:render_hud(x, y, scale, perspective, angle, a, r, g, b)
	for index, ent in ipairs(self.ents) do
		local hud_info = ent.hud_info
		local rx = x + ent.rx*scale
		local ry = y + ent.ry*scale*perspective -(ent.rz)*scale - ent.pickup_radius*scale
		local w,h = hud_info:get_dimensions()
		local ox,oy = window.get_best_ui_offset(rx,ry,w,h,10)
		hud_info:render(rx+ox, ry+oy, 1, perspective, angle, a, r, g, b)		
	end
end

return HudInfoRenderSystem