-- Fredzaza --

chenille688CK = {};
chenille688CK.modDirectory = g_currentModDirectory;

function chenille688CK.prerequisitesPresent(specializations)
      return true;
end;
function chenille688CK.initSpecialization()
end
function chenille688CK.registerOverwrittenFunctions(vehicleType)
end
function chenille688CK.registerFunctions(vehicleType)	
end
function chenille688CK.registerEvents(vehicleType)
end
function chenille688CK:registerActionEventsPlayer()
end
function chenille688CK:registerActionEventsMenu()
end

function chenille688CK:onRegisterActionEvents(isSelected, isOnActiveVehicle)
end

function chenille688CK.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", chenille688CK)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", chenille688CK)
    SpecializationUtil.registerEventListener(vehicleType, "onReadStream", chenille688CK)
    SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", chenille688CK)
    --SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", chenille688CK)
    --SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", chenille688CK)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", chenille688CK)
	SpecializationUtil.registerEventListener(vehicleType, "onDraw", chenille688CK)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", chenille688CK)

end

function chenille688CK:onLoad(vehicle)	
	self.indexTracRight = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsRight#index"));    
    self.charIdAnimR = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsRight#node"));
	self.charIdAnimR2 = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsRight#node2"));
    self.clipIndexAnimR = getXMLString(self.xmlFile, "vehicle.TracWheelsRight#animationClip");
    self.spedR = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.TracWheelsRight#sped"))
    self.TracWheelsRight = {}
    local i = 0
    while true do
        local baseName = string.format("vehicle.TracWheelsRight.rotatingPart(%d)", i)
        local index = getXMLString(self.xmlFile, baseName .. "#index")
        if index == nil then
            break
        end
        local node = I3DUtil.indexToObject(self.components, index)
        if node ~= nil then
            local entry = {}
            entry.node = node
            entry.speedScale = getXMLFloat(self.xmlFile, baseName .. "#speedScale")
            if entry.speedScale == nil then
                entry.speedScale = 1 / Utils.getNoNil(getXMLFloat(self.xmlFile, baseName .. "#radius"), 1)
            end
            table.insert(self.TracWheelsRight, entry)
        end
        i = i + 1        
    end
    
    self.indexTracLeft = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsLeft#index"));    
    self.charIdAnimL = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsLeft#node"));
	self.charIdAnimL2 = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.TracWheelsLeft#node2")); 	
    self.clipIndexAnimL = getXMLString(self.xmlFile, "vehicle.TracWheelsLeft#animationClip");
    self.spedL = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.TracWheelsLeft#sped"))     
    self.TracWheelsLeft = {}
    local i = 0
    while true do
        local baseName = string.format("vehicle.TracWheelsLeft.rotatingPart(%d)", i)
        local index = getXMLString(self.xmlFile, baseName .. "#index")
        if index == nil then
            break
        end
        local node = I3DUtil.indexToObject(self.components, index)
        if node ~= nil then
            local entry = {}
            entry.node = node
            entry.speedScale = getXMLFloat(self.xmlFile, baseName .. "#speedScale")
            if entry.speedScale == nil then
                entry.speedScale = 1 / Utils.getNoNil(getXMLFloat(self.xmlFile, baseName .. "#radius"), 1)
            end
            table.insert(self.TracWheelsLeft, entry)
        end
        i = i + 1        
    end	
	self.WheelRPG = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.WheelRPG#index"));
	self.WheelRPD = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.WheelRPD#index"));
	self.PedaleG = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.PedaleG#index"));
	self.PedaleD = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.PedaleD#index"));
	
end;

function chenille688CK:onDelete() 
end;

function chenille688CK:onReadStream(streamId, connection)
end;
  
function chenille688CK:onWriteStream(streamId, connection)
end;

function chenille688CK:mouseEvent(posX, posY, isDown, isUp, button)
end;

function chenille688CK:keyEvent(unicode, sym, modifier, isDown)    
end;

function chenille688CK:onUpdate(dt, vehicle)
		local spece = self.spec_motorized
		self.movedDistanceL = 0
		self.movedDistanceR = 0
		if self.WheelRPG ~= nil then
			local newX, _, _ = getRotation(self.WheelRPG)
            if self.lastRotationL == nil then
                self.lastRotationL = newX
            end
            if newX - self.lastRotationL < -math.pi then
                self.lastRotationL = self.lastRotationL - 2*math.pi
            elseif newX - self.lastRotationL > math.pi then
                self.lastRotationL = self.lastRotationL + 2*math.pi
            end
            self.movedDistanceL = 0.34 * (newX-self.lastRotationL)
            self.lastRotationL = newX
			local rotaPL = self.movedDistanceL*2.67
			setRotation(self.PedaleG, rotaPL, 0, 0)
		end; 
		if self.WheelRPD ~= nil then
			local newX, _, _ = getRotation(self.WheelRPD)
            if self.lastRotationR == nil then
                self.lastRotationR = newX
            end
            if newX - self.lastRotationR < -math.pi then
                self.lastRotationR = self.lastRotationR - 2*math.pi
            elseif newX - self.lastRotationR > math.pi then
                self.lastRotationR = self.lastRotationR + 2*math.pi
            end
            self.movedDistanceR = 0.34 * (newX-self.lastRotationR)
            self.lastRotationR = newX
			local rotaPR = self.movedDistanceR*2.67
			setRotation(self.PedaleD, rotaPR, 0, 0)
		end; 
		if self.movedDistanceL ~= 0 then
			local newX1, newY1, newZ1 = getWorldTranslation(self.indexTracLeft);
			if self.lastTranslationL == nil then
				self.lastTranslationL = {newX1, newY1, newZ1};
			end;
			local dx1, dy1, dz1 = worldDirectionToLocal(self.indexTracLeft, newX1-self.lastTranslationL[1], newY1-self.lastTranslationL[2], newZ1-self.lastTranslationL[3]);
			local movingDirectionL = 0
			local lastSpeedL = spece:getLastSpeed()
			if lastSpeedL > 0.0001 then    
				if dz1 < 0.0001 then
					movingDirectionL = -1;
				elseif dz1 > 0.0001 then
					movingDirectionL = 1;    
				end;
			end;    
			local lastMovedDistanceL = MathUtil.vector3Length(dx1, dy1, dz1) ;
			self.lastTranslationL[1] = newX1
			self.lastTranslationL[2] = newY1
			self.lastTranslationL[3] = newZ1
			local charIdL = getAnimCharacterSet(self.charIdAnimL);
			local charIdL2 = getAnimCharacterSet(self.charIdAnimL2);
			local clipIndexL = getAnimClipIndex(charIdL, self.clipIndexAnimL);
			assignAnimTrackClip(charIdL , 0, clipIndexL);
			assignAnimTrackClip(charIdL2 , 0, clipIndexL);
			setAnimTrackLoopState(charIdL, 0, true);
			setAnimTrackLoopState(charIdL2, 0, true);
			setAnimTrackSpeedScale(charIdL, 0, self.spedL*lastMovedDistanceL*movingDirectionL);
			setAnimTrackSpeedScale(charIdL2, 0, self.spedL*lastMovedDistanceL*movingDirectionL);
			enableAnimTrack(charIdL, 0);
			enableAnimTrack(charIdL2, 0);  
			local rotationPL = self.spedL*lastMovedDistanceL*movingDirectionL
			setRotation(self.PedaleG, (rotationPL/7), 0, 0)
			for _, v in pairs(self.TracWheelsLeft) do
				rotate(v.node, v.speedScale * lastMovedDistanceL*movingDirectionL, 0, 0)
			end
		else	
			local charIdL = getAnimCharacterSet(self.charIdAnimL);
			local charIdL2 = getAnimCharacterSet(self.charIdAnimL2);
			local clipIndexL = getAnimClipIndex(charIdL, self.clipIndexAnimL);
			assignAnimTrackClip(charIdL , 0, clipIndexL);
			assignAnimTrackClip(charIdL2 , 0, clipIndexL);
			setAnimTrackLoopState(charIdL, 0, true);
			setAnimTrackLoopState(charIdL2, 0, true);
			setAnimTrackSpeedScale(charIdL, 0, self.spedL*0);
			setAnimTrackSpeedScale(charIdL2, 0, self.spedL*0);
			enableAnimTrack(charIdL, 0);
			enableAnimTrack(charIdL2, 0); 
			setRotation(self.PedaleG, 0, 0, 0)
			for _, v in pairs(self.TracWheelsLeft) do
				rotate(v.node, v.speedScale * 0*0, 0, 0)
			end
		end; 
		if self.movedDistanceR ~= 0 then
			local newX2, newY2, newZ2 = getWorldTranslation(self.indexTracRight);
			if self.lastTranslationR == nil then
				self.lastTranslationR = {newX2, newY2, newZ2};
			end;
			local dx2, dy2, dz2 = worldDirectionToLocal(self.indexTracRight, newX2-self.lastTranslationR[1], newY2-self.lastTranslationR[2], newZ2-self.lastTranslationR[3]);
			local movingDirectionR = 0
			local lastSpeedR = spece:getLastSpeed()
			if lastSpeedR > 0.0001 then    
				if dz2 < 0.0001 then
					movingDirectionR = -1;
				elseif dz2 > 0.0001 then
					movingDirectionR = 1;        
				end;
			end;    
			local lastMovedDistanceR = MathUtil.vector3Length(dx2, dy2, dz2);    
			self.lastTranslationR[1] = newX2
			self.lastTranslationR[2] = newY2
			self.lastTranslationR[3] = newZ2    
			local charIdR = getAnimCharacterSet(self.charIdAnimR);
			local charIdR2 = getAnimCharacterSet(self.charIdAnimR2);
			local clipIndexR = getAnimClipIndex(charIdR, self.clipIndexAnimR);      
			assignAnimTrackClip(charIdR , 0, clipIndexR);
			assignAnimTrackClip(charIdR2 , 0, clipIndexR);
			setAnimTrackLoopState(charIdR, 0, true);
			setAnimTrackLoopState(charIdR2, 0, true);
			setAnimTrackSpeedScale(charIdR, 0, self.spedR*lastMovedDistanceR*movingDirectionR);
			setAnimTrackSpeedScale(charIdR2, 0, self.spedR*lastMovedDistanceR*movingDirectionR);
			enableAnimTrack(charIdR, 0);
			enableAnimTrack(charIdR, 0);
			local rotationPR = self.spedR*lastMovedDistanceR*movingDirectionR
			setRotation(self.PedaleD, (rotationPR/7), 0, 0)
			for _, v in pairs(self.TracWheelsRight) do
				rotate(v.node, v.speedScale * lastMovedDistanceR*movingDirectionR, 0, 0)
			end; 
		else
			local charIdR = getAnimCharacterSet(self.charIdAnimR);
			local charIdR2 = getAnimCharacterSet(self.charIdAnimR2);
			local clipIndexR = getAnimClipIndex(charIdR, self.clipIndexAnimR);      
			assignAnimTrackClip(charIdR , 0, clipIndexR);
			assignAnimTrackClip(charIdR2 , 0, clipIndexR);
			setAnimTrackLoopState(charIdR, 0, true);
			setAnimTrackLoopState(charIdR2, 0, true);
			setAnimTrackSpeedScale(charIdR, 0, self.spedR*0);
			setAnimTrackSpeedScale(charIdR2, 0, self.spedR*0);
			enableAnimTrack(charIdR, 0);
			enableAnimTrack(charIdR2, 0);
			setRotation(self.PedaleD, 0, 0, 0)
			for _, v in pairs(self.TracWheelsRight) do
				rotate(v.node, v.speedScale * 0*0, 0, 0)
			end; 		
		end; 
end;
function chenille688CK:onUpdateTick() 
end;	
function chenille688CK:onLeaveVehicle()
end;       
function chenille688CK:onDraw()  
end;
function chenille688CK:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
end