--Specjalizacja dla : Ursus c360 By Marcello1942
--#Autor: marcello1942
--#data: 21/08/2012 r.
--Przy edycji nie zapomnij o oryginalnym autorze!

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_1a = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Maska_1a#index"));
	if Maska_1a ~= nil then
		self.Maska_1a = Maska_1a;
	end;
		local Maska_1b = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Maska_1b#index"));
	if Maska_1b ~= nil then
		self.Maska_1b = Maska_1b;
	end;
		local Maska_1c = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Maska_1c#index"));
	if Maska_1c ~= nil then
		self.Maska_1c = Maska_1c;
	end;
	    local wycieraczka_2 = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#wycieraczka_2"));
	if wycieraczka_2 ~= nil then
		self.wycieraczka_2 = wycieraczka_2;
	end;
		local pompka = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#pompka"));
	if pompka ~= nil then
		self.pompka = pompka;
	end;
		local gabka = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lok#gabka"));
	if gabka ~= nil then
		self.gabka = gabka;
	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_l then
		self.wycieraczka = not self.wycieraczka;
	end;
	
	if isDown and sym == Input.KEY_b then
		self.Przekaznik = not self.Przekaznik;
	end;
	
	if isDown and sym == Input.KEY_l then
		self.wlacznik = not self.wlacznik;
	end;
	
	if isDown and sym == Input.KEY_b then
		self.wajcha = not self.wajcha;
	end;
		
  
end;


function Ursus360NN:update(dt)

        local nearestDistance = 1.5;
		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_1a); 
		local distance = Utils.vector3Length(tx-vx, ty-vy, tz-vz);	
		if distance < nearestDistance then
			self.przyMaska_1a = true; 
		else
			self.przyMaska_1a = 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.PRAWE) 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;
 --dzwiek--	
 	if self.przyklapiep or self.isEntered then
	if InputBinding.hasEvent(InputBinding.PRAWE) then
			if self.animationParts[1].clipEndTime then
				self.otworz = true
				self.zamknij = false
			elseif self.animationParts[1].clipStartTime then
				self.otworz = false
				self.zamknij = true;
			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);
			elseif self.animationParts[2].clipStartTime then
				self:setAnimationTime(2, self.animationParts[2].animDuration);
			end;
		end;
	end;
	-----
		if self.przyklapiel or self.isEntered then
	if InputBinding.hasEvent(InputBinding.LEWE) then
			if self.animationParts[2].clipEndTime then
				self.otworz = true
				self.zamknij = false
			elseif self.animationParts[2].clipStartTime then
				self.otworz = false
				self.zamknij = true;
			end;
		end;
	end;	
	--Maska--
	if self.przyMaska_1a then
		if InputBinding.hasEvent(InputBinding.Maska_1a) then
			if self.animationParts[8].clipEndTime then
				self:setAnimationTime(8, self.animationParts[8].offSet);
			elseif self.animationParts[8].clipStartTime then
				self:setAnimationTime(8, self.animationParts[8].animDuration);
			end;
		end;
	end;
	--Klapka1--
	if self.isEntered then
		if InputBinding.hasEvent(InputBinding.Maska_1b) then
			if self.animationParts[9].clipEndTime then
				self:setAnimationTime(9, self.animationParts[9].offSet);
			elseif self.animationParts[9].clipStartTime then
				self:setAnimationTime(9, self.animationParts[9].animDuration);
			end;
		end;
	end;
	--klapka2--
	if self.przyMaska_1c then
		if InputBinding.hasEvent(InputBinding.Maska_1c) then
			if self.animationParts[10].clipEndTime then
				self:setAnimationTime(10, self.animationParts[10].offSet);
			elseif self.animationParts[10].clipStartTime then
				self:setAnimationTime(10, self.animationParts[10].animDuration);
			end;
		end;
	end;
	--dach-	
	if self.isEntered then				
		if InputBinding.hasEvent(InputBinding.DACH) then
			if self.animationParts[4].clipEndTime then
				self:setAnimationTime(4, self.animationParts[4].offSet);
			elseif self.animationParts[4].clipStartTime then
				self:setAnimationTime(4, self.animationParts[4].animDuration);
			end;
		end;
	--szyba--
		if InputBinding.hasEvent(InputBinding.SZYBA) 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(6, self.animationParts[6].currentPosition+(self.animationParts[6].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.PODN) then
               self:setAnimationTime(6, self.animationParts[6].currentPosition-(self.animationParts[6].offSet+dt), false);
        end;
       end;
	--zaczep--
	if self.przyzaczepie then
	 	if InputBinding.isPressed(InputBinding.zaczepP) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition+(self.animationParts[5].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.zaczepL) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition-(self.animationParts[5].offSet+dt), false);
        end;
        end;
	--wycieraczkatyl--	
	if self.isEntered then
	 	if InputBinding.isPressed(InputBinding.wycieraczka_2L) then
               self:setAnimationTime(13, self.animationParts[13].currentPosition+(self.animationParts[13].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.wycieraczka_2P) then
               self:setAnimationTime(13, self.animationParts[13].currentPosition-(self.animationParts[13].offSet+dt), false);
        end;
        end;
		--pompka--
	if self.przypompka then
	 	if InputBinding.isPressed(InputBinding.pompkaup) then
               self:setAnimationTime(32, self.animationParts[32].currentPosition+(self.animationParts[32].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.pompkadown) then
               self:setAnimationTime(32, self.animationParts[32].currentPosition-(self.animationParts[32].offSet+dt), false);
        end;
        end;
			--gabka--
	if self.isEntered then
	 	if InputBinding.isPressed(InputBinding.gabkaup) then
               self:setAnimationTime(33, self.animationParts[33].currentPosition+(self.animationParts[33].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.gabkadown) then
               self:setAnimationTime(33, self.animationParts[33].currentPosition-(self.animationParts[33].offSet+dt), false);
        end;
        end;
	
		
	if self.setAnimationTime ~= nil then
		if self.isMotorStarted then
			self:setAnimationTime(7, 1);
		else
			self:setAnimationTime(7, 0);
		end;
		end;
		if self.setAnimationTime ~= nil then
		if self.isMotorStarted then
			self:setAnimationTime(11, 1);
		else
			self:setAnimationTime(11, 0);
		end;
		if self.isMotorStarted then
			self:setAnimationTime(12, 1);
		else
			self:setAnimationTime(12, 0);
		end;
		if self.wycieraczka then
           self:setAnimationTime(14, 1);
        else
           self:setAnimationTime(14, 0);
        end;
		if self.Przekaznik then
           self:setAnimationTime(15, 1);
        else
           self:setAnimationTime(15, 0);
        end;
		end;
		if self.setAnimationTime ~= nil then
	    if self.isMotorStarted then
			self:setAnimationTime(17,self.animationParts[17].animDuration);
		else
			self:setAnimationTime(17,self.animationParts[17].offSet);
		end;
		 if self.lastSpeed*3600 > 3 then
			self:setAnimationTime(18,self.animationParts[18].animDuration);
		else
			self:setAnimationTime(18,self.animationParts[18].offSet);
		end;
		 if self.lastSpeed*3600 > 6 then
			self:setAnimationTime(19,self.animationParts[19].animDuration);
		else
			self:setAnimationTime(19,self.animationParts[19].offSet);
		end;
		 if self.lastSpeed*3600 > 9 then
			self:setAnimationTime(20,self.animationParts[20].animDuration);
		else
			self:setAnimationTime(20,self.animationParts[20].offSet);
		end;
		 if self.lastSpeed*3600 > 12 then
			self:setAnimationTime(21,self.animationParts[21].animDuration);
		else
			self:setAnimationTime(21,self.animationParts[21].offSet);
		end;
		if self.lastSpeed*3600 > 15 then
			self:setAnimationTime(22,self.animationParts[22].animDuration);
		else
			self:setAnimationTime(22,self.animationParts[22].offSet);
		end;
		 if self.lastSpeed*3600 > 18 then
			self:setAnimationTime(23,self.animationParts[23].animDuration);
		else
			self:setAnimationTime(23,self.animationParts[23].offSet);
		end;
		if self.lastSpeed*3600 > 21 then
			self:setAnimationTime(24,self.animationParts[24].animDuration);
		else
			self:setAnimationTime(24,self.animationParts[24].offSet);
		end;
		 if self.lastSpeed*3600 > 24 then
			self:setAnimationTime(25,self.animationParts[25].animDuration);
		else
			self:setAnimationTime(25,self.animationParts[25].offSet);
		end;
		if self.lastSpeed*3600 > 27 then
			self:setAnimationTime(26,self.animationParts[26].animDuration);
		else
			self:setAnimationTime(26,self.animationParts[26].offSet);
		end;
		 if self.lastSpeed*3600 > 30 then
			self:setAnimationTime(27,self.animationParts[27].animDuration);
		else
			self:setAnimationTime(27,self.animationParts[27].offSet);
		end;
		if self.lastSpeed*3600 > 25 then
			self:setAnimationTime(28,self.animationParts[28].animDuration);
		else
			self:setAnimationTime(28,self.animationParts[28].offSet);
		end;
		if self.isMotorStarted then
			self:setAnimationTime(29,self.animationParts[29].animDuration);
		else
			self:setAnimationTime(29,self.animationParts[29].offSet);
		end;
		if self.hamuj then
			self:setAnimationTime(30,self.animationParts[30].animDuration);
		else
			self:setAnimationTime(30,self.animationParts[30].offSet);
		end;
		if self.wlacznik then
			self:setAnimationTime(31,self.animationParts[31].animDuration);
		else
			self:setAnimationTime(31,self.animationParts[31].offSet);
		end;
		if self.wajcha then
			self:setAnimationTime(16,self.animationParts[16].animDuration);
		else
			self:setAnimationTime(16,self.animationParts[16].offSet);
		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_1a then
	if self.animationParts[8].clipStartTime then
			g_currentMission:addExtraPrintText(string.format("%s: otworz Maske", InputBinding.getKeyNamesOfDigitalAction(InputBinding.Maska_1a)));
		else
			g_currentMission:addExtraPrintText(string.format("%s: zamknij Maske", InputBinding.getKeyNamesOfDigitalAction(InputBinding.Maska_1a)));
		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.175, 0.02, "Wcisnij 0: Pokaz pomoc");
		else
			renderText(0.833, 0.175, 0.02, "Wcisnij 0: Schowaj pomoc");
		end;
		if self.pomoc then
			renderText(0.72, 0.85, 0.025, "----->Ursus c 360<-----");
			if not self.isMotorStarted then
			renderText(0.72, 0.825, 0.02, " Z: odpal 60-ke");
			else
			renderText(0.72, 0.825, 0.02, " Z: zgas 60-ke");
			end;
	
	
		    renderText(0.72, 0.8, 0.02, "drzwi lewe: KP_4");
	
		
			renderText(0.72, 0.775, 0.02, "drzwi prawe: KP_6 ");
	
		
			renderText(0.72, 0.75, 0.02, "dach: KP_8 ");
         
           
			renderText(0.72, 0.725, 0.02, "Kierunkowskazy: Num 1/2/3 ");
	
		
			renderText(0.72, 0.7, 0.02, "Szyba tyl : KP_5 ");
			
		
			renderText(0.72, 0.675, 0.02, "Tylny podnosnik down: hydrauliczny ");
			
			
			renderText(0.72, 0.650, 0.02, "halogeny: 7,8,9 ");
			
			
			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, "wycieraczka tyl:  prawo: K, lewo: J ");
			
			
			renderText(0.72, 0.525, 0.02, "gabka  up: U,  down: Y ");
			
			
			renderText(0.72, 0.500, 0.02, "maska: J ");
			
			
			renderText(0.72, 0.475, 0.02, "dodaj, zmniejsz gaz: Num_plus, Num_minus ");
			
			
			renderText(0.72, 0.450, 0.02, "zaloz belke: X ");
			
			
			renderText(0.72, 0.425, 0.022, "----->MARCELLO 1942<-----");
		end;
	end;	
end;