--heder do z056
--#autor: Asd4
--$data: 03/11/2011r.
heder_bs = {};

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

function heder_bs:load(xmlFile)

 	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;

       self.setHydraulicDirection = SpecializationUtil.callSpecializationsFunction("setHydraulicDirection");


end;

function heder_bs:delete()
   
end;

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

function heder_bs:keyEvent(unicode, sym, modifier, isDown)

end;


function heder_bs:update(dt)
--Animacje
    	if self:getIsActiveForInput() then
 	if InputBinding.isPressed(InputBinding.MOTOW) then
               self:setAnimationTime(3, self.animationParts[3].currentPosition+(self.animationParts[3].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.MOTON) then
               self:setAnimationTime(3, self.animationParts[3].currentPosition-(self.animationParts[3].offSet+dt), false);
        end;
        	if InputBinding.isPressed(InputBinding.MOTOP) then
               self:setAnimationTime(4, self.animationParts[4].currentPosition+(self.animationParts[4].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.MOTOT) then
               self:setAnimationTime(4, self.animationParts[4].currentPosition-(self.animationParts[4].offSet+dt), false);
        end;
          end;
    if self.reelStarted then    
      self:setAnimationTime(1, 1);
      self:setAnimationTime(2, 1);
      else
      self:setAnimationTime(1, 0);
      self:setAnimationTime(2, 0);
      end;
      	if self:getIsActiveForInput() then
    if InputBinding.isPressed(InputBinding.MOTOS) then
     if self.animationParts[2].clipSpeed < 5 then
      self.animationParts[2].clipSpeed = self.animationParts[2].clipSpeed+0.01;
	   end;
	  elseif InputBinding.isPressed(InputBinding.MOTOWO) then
	    if self.animationParts[2].clipSpeed > 0.5 then
      self.animationParts[2].clipSpeed = self.animationParts[2].clipSpeed-0.01;
      end
      end
	   end;
      
      	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;
end;

function heder_bs:draw()
end;

function heder_bs:onDetach()

end;

function heder_bs:onLeave()

end;

function heder_bs:onDeactivate()

end;
function heder_bs: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;

