-- Dimogan
-- by Aluha74rus


Dimogan = {};

function Dimogan.prerequisitesPresent(specializations)
    return true;
end;
function Dimogan.registerEventListeners(vehicleType)
	local functionNames = { "onLoad", "onUpdate", "onDelete", "onLeaveVehicle" }

	for i, v in ipairs(functionNames) do
		SpecializationUtil.registerEventListener(vehicleType, v, Dimogan)
	end
end

function Dimogan:onLoad(savegame)
	
	self.dim = {};  
	self.dim.psPorog = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.motorized.dimogan#psPorog"), 0.85);
	self.dim.ps = {};
	ParticleUtil.loadParticleSystem(self.xmlFile, self.dim.ps, "vehicle.motorized.dimogan.Runi3d", self.components, false, nil, self.baseDirectory)
	self.dim.psStart = {};
	ParticleUtil.loadParticleSystem(self.xmlFile, self.dim.psStart, "vehicle.motorized.dimogan.Start3d", self.components, false, nil, self.baseDirectory)
end;
function Dimogan:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
	
    local dimWStartEnableTimeStartDelay = 2040;
	local dimWStartEnableTime = (self.spec_motorized.motorStartTime - self.spec_motorized.motorStartDuration) + dimWStartEnableTimeStartDelay;
	local Duration = 650;
	local DisableTime = dimWStartEnableTime + Duration;
	
	if self:getIsActive() and self.spec_motorized.isMotorStarted  then		
		local mLoad =  self.spec_motorized.motor:getMotorAppliedTorque() / math.max(self.spec_motorized.motor:getMotorAvailableTorque(), 0.0001)
		if mLoad > self.dim.psPorog then
			ParticleUtil.setEmittingState(self.dim.ps, true);
		else
			ParticleUtil.setEmittingState(self.dim.ps, false)
		end;
		if (g_currentMission.time <= DisableTime and g_currentMission.time >= dimWStartEnableTime ) then
			ParticleUtil.setEmittingState(self.dim.psStart, true);
		else
			ParticleUtil.setEmittingState(self.dim.psStart, false);
		end;
	end;
end;
function Dimogan:onDelete()
	if self.dim.ps ~= nil then
		ParticleUtil.deleteParticleSystem(self.dim.ps);
	end;
	if self.dim.psStart ~= nil then
		ParticleUtil.deleteParticleSystem(self.dim.psStart);
	end;
end;
function Dimogan:onLeaveVehicle()
	if self.dim.ps then
		ParticleUtil.setEmittingState(self.dim.ps, false);
	end;
	if self.dim.psStart then
		ParticleUtil.setEmittingState(self.dim.psStart, false);
	end;
end;