local FEEDBACK_DEFS = dofile("../defs/feedback_defs.lua")
local EffectFeedbackSystem = inherited("EffectFeedbackSystem", SystemMap)

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

function EffectFeedbackSystem:dynamic_wants(ent)
	return ent.any_feedback
end

function EffectFeedbackSystem:ent_added(ent)
end

function EffectFeedbackSystem:ent_removed(ent)
end

function EffectFeedbackSystem:step(dt)
	local world = self.world
	for ent, _ in pairs(self.ents) do
		if ent.any_feedback then			
			for feedback_type, amt in pairs(ent.feedback) do
				local fdef = FEEDBACK_DEFS[feedback_type]
				--apply effect
				fdef.apply(ent, amt)
				--print("apply", amt, ent._index)
				--decay
				local speed = fdef.decay_speed
				amt = amt - dt*speed - amt*dt*speed


				if amt <= 0 then
					ent.feedback[feedback_type] = nil
					fdef.cleanup(ent, amt)
				else
					ent.feedback[feedback_type] = amt
				end
				--print("animating", feedback_type, amt)
			end
			if not next(ent.feedback) then
				ent:clear_feedback()
			end
		end
	end
end

return EffectFeedbackSystem