local EntityRenderSystem = inherited("EntityRenderSystem", SystemLinear)

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

function EntityRenderSystem:wants(ent)
	return ent.aspects.paperdoll_render or ent.aspects.sprite_render or ent.aspects.item_render or ent.aspects.sprite_composite_render
end

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

function EntityRenderSystem:ent_removed(ent)
end

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

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

function EntityRenderSystem:render(x, y, scale, perspective, angle, a, r, g, b)
	for index, ent in ipairs(self.ents) do
		local rx = x + ent.rx*scale
		local ry = y + ent.ry*scale*perspective -ent.rz*scale
		ent:render(rx, ry, scale, perspective, angle, a, r, g, b)
		--[[
		if ent.char_index then
			video.renderTextSprites(ent.char_index, rx, ry+15, 1, "small", 255, 155, 255, 0, nil, 0.7)
		end	

		if ent.ai_tactics then
			video.renderTextSprites(ent.ai_tactics, rx, ry, 1, "small", 255, 255, 255, 0)			
			
			local target = ent.ai_target 
			if target then
				local tx = x + target.rx*scale
				local ty = y + target.ry*scale*perspective -target.rz*scale
				video.renderSpriteLine(rx,ry,tx, ty, 255, 255, 0, 0)
				if target.char_index then
					video.renderTextSprites(target.char_index,tx, ty+15, 1, "small", 255, 155, 255, 0, nil, 0.7)
				end	
			end
		end]]
		
	end
end

return EntityRenderSystem