--
-- Script Specializat Pentru RM2
-- Versiunea 2.2.0.30
-- Autor: Mihai94
-- A Nu Se Modifica...
--


RM2 = {};

function RM2.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Trailer, specializations);
end;

function RM2:load(xmlFile)	
   -- roti fata tramocar 
   self.extraWheels = {};
	local i=0;
	while true do
		local wheelnamei = string.format("vehicle.extraWheels.extraWheel(%d)", i);
		local extraWheel = {};
		local reprStr = getXMLString(xmlFile, wheelnamei .. "#repr");
		if reprStr == nil then
			break;
		end;
		extraWheel.repr = Utils.indexToObject(self.components, reprStr);
		extraWheel.driveNode = Utils.indexToObject(self.components, getXMLString(xmlFile, wheelnamei .. "#driveNode"));
		if extraWheel.driveNode == nil then
			extraWheel.driveNode = extraWheel.repr;
		end;		
		extraWheel.rotXActor = Utils.indexToObject(self.components, getXMLString(xmlFile, wheelnamei .. "#rotXActor"));
		extraWheel.rotYActor = Utils.indexToObject(self.components, getXMLString(xmlFile, wheelnamei .. "#rotYActor"));
		table.insert(self.extraWheels, extraWheel);
		i = i+1;
	end;

    -- fixare capede de bara 
    self.setHydraulicDirection = SpecializationUtil.callSpecializationsFunction("setHydraulicDirection");
	local hydraulicsCount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.hydraulics#count"), 0);	
	self.hydraulics = {};	
	for i=1, hydraulicsCount do
		local hydraulicName = string.format("vehicle.hydraulics.hydraulic%d", i);		
		self.hydraulics[i] = {};		
		self.hydraulics[i].node = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#index"));
		self.hydraulics[i].punch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punch"));
		self.hydraulics[i].translationPunch = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#punchFixpoint"));
		self.hydraulics[i].fixPoint = Utils.indexToObject(self.components, getXMLString(xmlFile, hydraulicName .. "#fixpoint"));	
		local xUp, yUp, zUp = Utils.getVectorFromString(Utils.getNoNil(getXMLString(xmlFile, hydraulicName .. "#upVectors"),"0 1 0"));
		self.hydraulics[i].upVectors = {xUp, yUp, zUp};
		
		local ax, ay, az;		
		if self.hydraulics[i].punch ~= nil then
			ax, ay, az = getWorldTranslation(self.hydraulics[i].punch);
		else
			ax, ay, az = getWorldTranslation(self.hydraulics[i].node);
		end;
		if self.hydraulics[i].translationPunch ~= nil then
			local bx, by, bz = getWorldTranslation(self.hydraulics[i].translationPunch);		
			self.hydraulics[i].punchDistance = Utils.vector3Length(ax-bx, ay-by, az-bz);
		end;
	end;
	self.setHydraulicTime = 30;

end;

function RM2:getSaveAttributesAndNodes(nodeIdent)
    end;
  


function RM2:delete()
end;

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

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

function RM2:update(dt)

 
     -- fixare capete de bara 
    --if self:getIsActive() then			
		self.setHydraulicTime = 30;	
	--end;

	if self.setHydraulicTime > 0 then
		for k,v in pairs(self.hydraulics) do 
			self:setHydraulicDirection(k);
		end;
		self.setHydraulicTime = self.setHydraulicTime - 1;
	end;
    
    -- roti fata tramocar
     for k, extraWheel in pairs(self.extraWheels) do
		local zX, zY, zZ = localDirectionToWorld(extraWheel.rotYActor, 0, 0, 1);
		local zX, zY, zZ = worldDirectionToLocal(getParent(extraWheel.repr), zX, zY, zZ);
		local wX, wY, wZ = getRotation(extraWheel.rotXActor);
		local xX, xY, xZ = getRotation(extraWheel.driveNode);
		setDirection(extraWheel.repr, zX, zY, zZ, 0, 1, 0);
		setRotation(extraWheel.driveNode, wX, xY, xZ);
	end;


end;

function RM2:draw()
end;

function RM2:setHydraulicDirection(index)
	local hydraulic = self.hydraulics[index];
	if hydraulic.fixPoint ~= nil then
		local ax, ay, az = getWorldTranslation(hydraulic.node);
		local bx, by, bz = getWorldTranslation(hydraulic.fixPoint);
		local x, y, z = worldDirectionToLocal(getParent(hydraulic.node), bx-ax, by-ay, bz-az);
		local xUp, yUp, zUp = unpack(hydraulic.upVectors);
		setDirection(hydraulic.node, x, y, z, xUp, yUp, zUp);
		if hydraulic.punch ~= nil then
			local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
			setTranslation(hydraulic.punch, 0, 0, distance-hydraulic.punchDistance);
		end;
	end;
end;
