--[[ script de fonctionnement de la classe ScreenEmitter --]]


.namespace ScreenEmitter


function OnEnable()
	self.Emitter = nil
	self.TimerBeforeRestart = 0
	
	local error = CreateEmitter()
	
	if self.Emitter == nil or self.Emitter== nil then
		AssertPopup(false, "Erreur, unit source absente et classe non reconnue")
		error = true
	elseif self.Emitter.CreationPerSec == nil then
		AssertPopup(false, "Erreur, la classe saisie n'est pas un emetteur de particules")
		error = true
	elseif self.Emitter.ClassName == "ScreenEmitter" then 
		AssertPopup(false, "Erreur, la classe saisie n'est pas un emetteur de particules")
		error = true
	end
	
	if error == true then
		OPUnitFactory.CloseUnit(self.Emitter)
		self.Emitter = nil
		return
	end
	
	--self.Emitter.Layer = OPUnitFactory.GetUnit(Cursor.GetLayerName( self, self.Name ),"Layer")
	UpdateParticlesParam()

	
	Enable(self.Emitter)
end

function UpdateParticlesParam()
	self.Emitter.Position_X = self.Position_X 
	self.Emitter.Position_Y = self.Position_Y 
	
	if self.CreationPerSec ~= 0 then
		self.Emitter.CreationPerSec = self.CreationPerSec
	end
	
	if self.CreationTimeOut ~= 0 then
		self.Emitter.CreationTimeout = self.CreationTimeOut
		self.Emitter.AutoDisable = true
	end
	
	-- attention : les ScreenEmitter ont un attribut rotation inutile, il est donc à présent utilisé
	-- comme offset sur les rotation Start, Middle et End des particules emitters liés
	self.Emitter.RotationEnd = self.Emitter.RotationEnd + self.Rotation
	self.Emitter.RotationMiddle = self.Emitter.RotationMiddle + self.Rotation
	self.Emitter.RotationStart = self.Emitter.RotationStart + self.Rotation
	
	if math.abs(self.Direction) > 0.01 then
		self.Emitter.Direction = self.Direction
	end
	
	self.Emitter.StartDelay = self.StartDelay
end


function OnUpdate(dt)
	if self.Loop == true and self.CreationTimeOut ~= 65535 then
		if self.Emitter.Enabled == false then
			self.TimerBeforeRestart = self.TimerBeforeRestart + dt
		end
		
		if self.TimerBeforeRestart > self.RestartDelay then
			UpdateParticlesParam()
			self.Emitter.StartDelay = 0			
			
			
			self.Emitter.Enabled = true
			self.TimerBeforeRestart = 0
		end
	end
end


function OnDisable()
	if self.Emitter ~= nil then
		OPUnitFactory.CloseUnit( self.Emitter )
		self.Emitter = nil
	end
end


function HideParticuleEmitter()
	if self.Emitter ~= nil then
		self.Emitter.Flags_Visible = false
	end
end


function ShowParticuleEmitter()
	if self.Emitter ~= nil then
		self.Emitter.Flags_Visible = true
	end
end


function CreateEmitter()
	-- on récupére 
	if self.SourceEmitter ~= nil then
		self.Emitter = OPUnitFactory.CopyUnit( self.SourceEmitter )
		--on change le parent
		self.Emitter.Parent = self.Parent
		
		--OPLog.Log("Emitter créé à partir de l'unit "..self.SourceEmitter.Name)
	elseif self.EmitterClass ~= nil or self.EmitterClass ~= "" then
		self.Emitter = OPUnitFactory.CreateUnit(self.EmitterClass)
		--OPLog.Log("Emitter créé à partir de la classe "..self.EmitterClass)
	else
		return true
	end
	
	return false
end


function StopEmitter()
	self.Emitter.CreationPerSec = 0
end

