--Specjalizacja dla : Ursus c360 By Marcello1942
--#Autor: marcello1942
--#data: 25/08/2012 r.
--edit:Grzegorz056

Ursus360NN = {};

function Ursus360NN.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function Ursus360NN:load(xmlFile)

	local klapalewa = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#lewe"));
	if klapalewa ~= nil then
		self.klapalewa = klapalewa;
	end;
		local klapaprawa = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#prawe"));
	if klapaprawa ~= nil then
		self.klapaprawa = klapaprawa;
	end;
		local zaczep = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#zaczep"));
	if zaczep ~= nil then
		self.zaczep = zaczep;
	end;
		local maska = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#maska"));
	if maska ~= nil then
		self.maska = maska;
	end;
		local pompka = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#pompka"));
	if pompka ~= nil then
		self.pompka = pompka;
	end;

  	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");
    self.otworz = false
	self.zamknij = false
	
	  hor12nSoundFile = Utils.getFilename("otworzdrzwi.wav", self.baseDirectory);
    self.hor12nSoundId = createSample("LizardHor12nSound");
    loadSample(self.hor12nSoundId, hor12nSoundFile, false);
    self.hor12nPlaying = false;
    
      hor123nSoundFile = Utils.getFilename("zamknijdrzwi.wav", self.baseDirectory);
    self.hor123nSoundId = createSample("LizardHor123nSound");
    loadSample(self.hor123nSoundId, hor123nSoundFile, false);
    self.hor123nPlaying = false;

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

   if isDown and sym == Input.KEY_0 then
		self.pomoc = not self.pomoc;
	end;
	  
	  
	if isDown and sym == Input.KEY_b then
		self.Przekaznik = not self.Przekaznik;
	end;
	
	
	if isDown and sym == Input.KEY_b then
		self.wajcha = not self.wajcha;
	end;
		
  
end;


function Ursus360NN:update(dt)

        local nearestDistance = 2;
		local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
	
		
		local px, py, pz = getWorldTranslation(self.klapalewa); 
		local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);	
		if distance < nearestDistance then
			self.przyklapiel = true; 
		else
			self.przyklapiel = false; 
		end;

		local rx, ry, rz = getWorldTranslation(self.klapaprawa); 
		local distance = Utils.vector3Length(rx-vx, ry-vy, rz-vz);	
		if distance < nearestDistance then
			self.przyklapiep = true; 
		else
			self.przyklapiep = false; 
		end; 
		
		
		local sx, sy, sz = getWorldTranslation(self.zaczep); 
		local distance = Utils.vector3Length(sx-vx, sy-vy, sz-vz);	
		if distance < nearestDistance then
			self.przyzaczepie = true; 
		else
			self.przyzaczepie = false; 
		end; 

		local tx, ty, tz = getWorldTranslation(self.maska); 
		local distance = Utils.vector3Length(tx-vx, ty-vy, tz-vz);	
		if distance < nearestDistance then
			self.przymaska = true; 
		else
			self.przymaska = false; 
		end; 
		
		local ux, uy, uz = getWorldTranslation(self.pompka); 
		local distance = Utils.vector3Length(ux-vx, uy-vy, uz-vz);	
		if distance < nearestDistance then
			self.przypompka = true; 
		else
			self.przypompka = false; 
		end; 
	
		
		
--------drzwi----------------
--otwieranie--
	if self.przyklapiep or self.isEntered then
	if InputBinding.hasEvent(InputBinding.SZYBA) then
			if self.animationParts[1].clipEndTime then
				self:setAnimationTime(1, self.animationParts[1].offSet);
			elseif self.animationParts[1].clipStartTime then
				self:setAnimationTime(1, self.animationParts[1].animDuration);
			end;
		end;
	end;
	
	if self.przyklapiel or self.isEntered then
	if InputBinding.hasEvent(InputBinding.LEWE) then
			if self.animationParts[2].clipEndTime then
				self:setAnimationTime(2, self.animationParts[2].offSet);
				self.otworz = true;
				self.zamknij = false;
			elseif self.animationParts[2].clipStartTime then
				self:setAnimationTime(2, self.animationParts[2].animDuration);
				self.otworz = false;
				self.zamknij = true;
			end;
		end;
	end;
if self.przyklapiep or self.isEntered then
	if InputBinding.hasEvent(InputBinding.PRAWE) then
			if self.animationParts[28].clipEndTime then
				self.otworz = true;
				self.zamknij = false;
			elseif self.animationParts[28].clipStartTime then
				self.otworz = false;
				self.zamknij = true;
			end;
		end;
	end;
if self.przyklapiep or self.isEntered then
	if InputBinding.hasEvent(InputBinding.PRAWE) then
			if self.animationParts[28].clipEndTime then
				self:setAnimationTime(28, self.animationParts[28].offSet);
			elseif self.animationParts[28].clipStartTime then
				self:setAnimationTime(28, self.animationParts[28].animDuration);
			end;
		end;
	end;
	--Maska--
	if self.przymaska then
		if InputBinding.hasEvent(InputBinding.maska) then
			if self.animationParts[7].clipEndTime then
				self:setAnimationTime(7, self.animationParts[7].offSet);
			elseif self.animationParts[7].clipStartTime then
				self:setAnimationTime(7, self.animationParts[7].animDuration);
			end;
		end;
	end;
	--dzwiek--	
 	if self.przyklapiep or self.isEntered then
	if InputBinding.hasEvent(InputBinding.maska) then
			if self.animationParts[7].clipEndTime then
				self.otworz = true
				self.zamknij = false
			elseif self.animationParts[7].clipStartTime then
				self.otworz = false
				self.zamknij = true;
			end;
		end;
	end;
	--dach-	
	if self.isEntered then				
		if InputBinding.hasEvent(InputBinding.DACH) then
			if self.animationParts[3].clipEndTime then
				self:setAnimationTime(3, self.animationParts[3].offSet);
			elseif self.animationParts[3].clipStartTime then
				self:setAnimationTime(3, self.animationParts[3].animDuration);
			end;
		end;
	--podnosnik
		if InputBinding.isPressed(InputBinding.PODW) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition+(self.animationParts[5].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.PODN) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition-(self.animationParts[5].offSet+dt), false);
        end;
       end;
	--zaczep--
	if self.przyzaczepie then
	 	if InputBinding.isPressed(InputBinding.zaczepP) then
               self:setAnimationTime(4, self.animationParts[4].currentPosition+(self.animationParts[4].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.zaczepL) then
               self:setAnimationTime(4, self.animationParts[4].currentPosition-(self.animationParts[4].offSet+dt), false);
        end;
        end;
if self.przyklapiep then
	if self.animationParts[1].clipStartTime then
			g_currentMission:addExtraPrintText(string.format("%s: otworz Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.PRAWE)));
		else
			g_currentMission:addExtraPrintText(string.format("%s: zamknij Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.PRAWE)));
		end;
		end

		--pompka--
	if self.przypompka then
	 	if InputBinding.isPressed(InputBinding.pompkaup) then
               self:setAnimationTime(24, self.animationParts[24].currentPosition+(self.animationParts[24].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.pompkadown) then
               self:setAnimationTime(24, self.animationParts[24].currentPosition-(self.animationParts[24].offSet+dt), false);
        end;
        end;
	
		
	if self.setAnimationTime ~= nil then
		if self.isMotorStarted then
			self:setAnimationTime(6, 1);
		else
			self:setAnimationTime(6, 0);
		end;
		if self.isMotorStarted then
			self:setAnimationTime(8, 1);
		else
			self:setAnimationTime(8, 0);
		end;
		if self.Przekaznik then
           self:setAnimationTime(9, 1);
        else
           self:setAnimationTime(9, 0);
        end;
	    if self.isMotorStarted then
			self:setAnimationTime(11,self.animationParts[11].animDuration);
		else
			self:setAnimationTime(11,self.animationParts[11].offSet);
		end;
		 if self.lastSpeed*3600 > 3 then
			self:setAnimationTime(12,self.animationParts[12].animDuration);
		else
			self:setAnimationTime(12,self.animationParts[12].offSet);
		end;
		 if self.lastSpeed*3600 > 6 then
			self:setAnimationTime(13,self.animationParts[13].animDuration);
		else
			self:setAnimationTime(13,self.animationParts[13].offSet);
		end;
		 if self.lastSpeed*3600 > 9 then
			self:setAnimationTime(14,self.animationParts[14].animDuration);
		else
			self:setAnimationTime(14,self.animationParts[14].offSet);
		end;
		 if self.lastSpeed*3600 > 12 then
			self:setAnimationTime(15,self.animationParts[15].animDuration);
		else
			self:setAnimationTime(15,self.animationParts[15].offSet);
		end;
		if self.lastSpeed*3600 > 15 then
			self:setAnimationTime(16,self.animationParts[16].animDuration);
		else
			self:setAnimationTime(16,self.animationParts[16].offSet);
		end;
		 if self.lastSpeed*3600 > 18 then
			self:setAnimationTime(17,self.animationParts[17].animDuration);
		else
			self:setAnimationTime(17,self.animationParts[17].offSet);
		end;
		if self.lastSpeed*3600 > 21 then
			self:setAnimationTime(18,self.animationParts[18].animDuration);
		else
			self:setAnimationTime(18,self.animationParts[18].offSet);
		end;
		 if self.lastSpeed*3600 > 24 then
			self:setAnimationTime(19,self.animationParts[19].animDuration);
		else
			self:setAnimationTime(19,self.animationParts[19].offSet);
		end;
		if self.lastSpeed*3600 > 27 then
			self:setAnimationTime(20,self.animationParts[20].animDuration);
		else
			self:setAnimationTime(20,self.animationParts[20].offSet);
		end;
		 if self.lastSpeed*3600 > 30 then
			self:setAnimationTime(21,self.animationParts[21].animDuration);
		else
			self:setAnimationTime(21,self.animationParts[21].offSet);
		end;
		if self.isMotorStarted then
			self:setAnimationTime(22,self.animationParts[22].animDuration);
		else
			self:setAnimationTime(22,self.animationParts[22].offSet);
		end;
		if self.hamuj then
			self:setAnimationTime(23,self.animationParts[23].animDuration);
		else
			self:setAnimationTime(23,self.animationParts[23].offSet);
		end;
		if self.wajcha then
			self:setAnimationTime(10,self.animationParts[10].animDuration);
		else
			self:setAnimationTime(10,self.animationParts[10].offSet);
		end;
		if self.lastSpeed*3600 > 23 then
			self:setAnimationTime(26,self.animationParts[26].animDuration);
		else
			self:setAnimationTime(26,self.animationParts[26].offSet);
		end;
		if self.isMotorStarted then
			self:setAnimationTime(25, 1);
		else
			self:setAnimationTime(25, 0);
		end;
	end;
	  --napisy
		if self.przyzaczepie then
			g_currentMission:addExtraPrintText(string.format("%s: zaczep prawo", InputBinding.getKeyNamesOfDigitalAction(InputBinding.zaczepP)));
			g_currentMission:addExtraPrintText(string.format("%s: zaczep lewo", InputBinding.getKeyNamesOfDigitalAction(InputBinding.zaczepL)));
		end;
		if self.przyklapiel then
	if self.animationParts[2].clipStartTime then
			g_currentMission:addExtraPrintText(string.format("%s: otworz Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.LEWE)));
		else
			g_currentMission:addExtraPrintText(string.format("%s: zamknij Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.LEWE)));
		end;
		end;
		if self.przyklapiep then
	if self.animationParts[1].clipStartTime then
			g_currentMission:addExtraPrintText(string.format("%s: otworz Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.PRAWE)));
		else
			g_currentMission:addExtraPrintText(string.format("%s: zamknij Drzwi", InputBinding.getKeyNamesOfDigitalAction(InputBinding.PRAWE)));
		end;
		end
		if self.przymaska then
	if self.animationParts[7].clipStartTime then
			g_currentMission:addExtraPrintText(string.format("%s: otworz Maske", InputBinding.getKeyNamesOfDigitalAction(InputBinding.maska)));
		else
			g_currentMission:addExtraPrintText(string.format("%s: zamknij Maske", InputBinding.getKeyNamesOfDigitalAction(InputBinding.maska)));
		end;
		end
		if self.przypompka then
			g_currentMission:addExtraPrintText(string.format("%s: pompka up", InputBinding.getKeyNamesOfDigitalAction(InputBinding.pompkaup)));
			g_currentMission:addExtraPrintText(string.format("%s: pompka down", InputBinding.getKeyNamesOfDigitalAction(InputBinding.pompkadown)));
		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;
	
	
	  if self.otworz then
       if not self.hor12nPlaying then
            playSample(self.hor12nSoundId, 1, 3, 3);
            self.hor12nPlaying = true;
        end;
       
    else
        if self.hor12nPlaying then
            stopSample(self.hor12nSoundId);
            self.hor12nPlaying = false;
        end;
         
    end;
      if self.zamknij then
       if not self.hor123nPlaying then
            playSample(self.hor123nSoundId, 1, 3, 3);
            self.hor123nPlaying = true;
        end;
       
    else
        if self.hor123nPlaying then
            stopSample(self.hor123nSoundId);
            self.hor123nPlaying = false;
        end;
         
    end;
	
end;


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

function Ursus360NN:validateAttacherJoint(implement, jointDesc, dt)
    return true;
end;
function Ursus360NN: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;
function Ursus360NN:draw()
 if self.isEntered then
		if not self.pomoc then
			renderText(0.833, 0.275, 0.02, "Wcisnij 0: Pokaz pomoc");
		else
			renderText(0.833, 0.275, 0.02, "Wcisnij 0: Schowaj pomoc");
		end;
		if self.pomoc then
			renderText(0.72, 0.825, 0.025, "----->Ursus c 360<-----");
			if not self.isMotorStarted then
			renderText(0.72, 0.800, 0.02, " Num Enter: odpal 60-ke");
			else
			renderText(0.72, 0.800, 0.02, " Num Enter: zgas 60-ke");
			end;
	
	
		     renderText(0.72, 0.775, 0.02, "drzwi lewe: N ");
	
		
			renderText(0.72, 0.750, 0.02, "drzwi prawe: M ");
	
		
			renderText(0.72, 0.725, 0.02, "dach: T ");

	
			renderText(0.72, 0.7, 0.02, "Szyba tyl up: G ");
			
		
			renderText(0.72, 0.675, 0.02, "Tylny podnosnik down: hyrauliczny ");
			
			
			renderText(0.72, 0.650, 0.02, "halogen: 7 ");
			
			
			renderText(0.72, 0.625, 0.02, "zaczep  prawo: X, lewo: Z ");
			
			
			renderText(0.72, 0.600, 0.02, "pompka  up: Z,  down: X ");
			
		if not self.wycieraczka then
			renderText(0.72, 0.575, 0.02, "wycieraczka ON : L ");
			else
			renderText(0.72, 0.575, 0.02, "wycieraczka OFF : L ");
			end;
			
			
			renderText(0.72, 0.550, 0.02, "maska: J ");
			
			
			renderText(0.72, 0.525, 0.02, "zaloz belke: X ");
			
			
			renderText(0.72, 0.500, 0.02, "dodaj, zmniejsz gaz: Num_plus, Num_minus ");
			
			
			renderText(0.72, 0.475, 0.022, "----->MARCELLO 1942<-----");
		end;
	end;		
end;