local TeamTargetSystem = inherited("TeamTargetSystem", SystemMap)

function TeamTargetSystem:new()
	local ms = instance(self)
	ms.team_masks = {}
	return ms
end

TeamTargetSystem.key = "team_target"


function TeamTargetSystem:wants(ent)
	return ent.aspects.team_target and ent.aspects.team_owned
end

function TeamTargetSystem:dynamic_wants(ent)
	return ent.aspects.team_target and ent.aspects.team_owned and not ent.dead
end

function TeamTargetSystem:ent_added(ent)
	local team = ent.team
	if team then
		local mask = self.team_masks[team]
		if not mask then
			mask = {}
			self.team_masks[team] = mask
		end
		mask[ent] = true		
	end
end

function TeamTargetSystem:ent_removed(ent)
	local team = ent.team
	if team then
		local mask = self.team_masks[team]
		if mask then
			mask[ent] = nil
		end
	end
end

function TeamTargetSystem:ent_leaving_team(ent, team)
	self:ent_removed(ent)
end

function TeamTargetSystem:ent_joined_team(ent, team)
	self:ent_added(ent)
end

function TeamTargetSystem:get_target_map(team)
	return self.team_masks[team]
end

return TeamTargetSystem