-- by modelleicher
-- www.schwabenmodding.bplaced.net

-- adds additional particle system 
-- particle system emits if the engine load is above a certain threshold (psThreshold value)
-- maxMotorLoad, the max. value motorLoad can reach witch the particular vehicle (use vehicle debug rendering to figure that value out)

-- edit: Rockstar94

newExhaustEffects = {};

function newExhaustEffects.prerequisitesPresent(specializations)
    return true;
end;

function newExhaustEffects:load(xmlFile)
	self.nrep = {}; -- all variables will be stored in this table to prevent interference with other scripts 
	self.nrep.maxMotorLoad = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.newExhaustEffects#maxMotorLoad"), 20);
	self.nrep.psThreshold = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.newExhaustEffects#psThreshold"), 0.85);
	self.nrep.ps = {};
	Utils.loadParticleSystem(xmlFile, self.nrep.ps, "vehicle.newExhaustEffects.particle", self.components, false, nil, self.baseDirectory)


	self.nrep1 = {}; -- all variables will be stored in this table to prevent interference with other scripts 
	self.nrep1.maxMotorLoad = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.startExhaustEffects#maxMotorLoad"), 20);
	self.nrep1.psThreshold = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.startExhaustEffects#psThreshold"), 0.85);
	
	self.nrep1.ps = {};	
	Utils.loadParticleSystem(xmlFile, self.nrep1.ps, "vehicle.startExhaustEffects.particle", self.components, false, nil, self.baseDirectory)
	
	self.czas1 = 0;
end;


function newExhaustEffects:update(dt)	
	if self:getIsActive() and self.isMotorStarted then		
		local mLoad =  self.motor.motorLoad / self.nrep.maxMotorLoad; -- converting the individual motor load to 0-1 range value 	
		
		if mLoad > self.nrep.psThreshold then
		    if self.czas1 <= 2000 then
				self.czas1 = self.czas1 +(dt/1);
			end;
		else
			Utils.setEmittingState(self.nrep.ps, false)
		end;
		
		if self.motor.lastMotorRpm > 1400 then
			if self.czas1 > 0 and self.czas1 < 2000 then
				Utils.setEmittingState(self.nrep.ps, true);
			else
				Utils.setEmittingState(self.nrep.ps, false)
			end;
		else
			self.czas1 = 0;
		end;
	end;
	
	if Utils.isSamplePlaying(self.sampleMotorStart) then
		Utils.setEmittingState(self.nrep1.ps, true);
	else
		Utils.setEmittingState(self.nrep1.ps, false);
	end;
end;
function newExhaustEffects:delete()
	if self.nrep.ps ~= nil then
		Utils.deleteParticleSystem(self.nrep.ps);
	end;
	if self.nrep1.ps ~= nil then
		Utils.deleteParticleSystem(self.nrep1.ps);
	end;
end;
function newExhaustEffects:onLeave()
	if self.nrep.ps then
		Utils.setEmittingState(self.nrep.ps, false);
	end;
	if self.nrep1.ps then
		Utils.setEmittingState(self.nrep1.ps, false);
	end;
end;
function newExhaustEffects:mouseEvent(posX, posY, isDown, isUp, button)
end;
function newExhaustEffects:keyEvent(unicode, sym, modifier, isDown)
end;
function newExhaustEffects:draw()
end;
