--Specjalizacja dla : Ursus 904 By Maciusboss1 & Asd4 & Lechu
--#Autor: Asd4
--#data: 27/03/2011 r.
--#Totalna edycja: 15/11/2011 r.
--Przy edycji nie zapomnij o oryginalnym autorze!
--

Ursus914 = {};

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

function Ursus914:load(xmlFile)

local motorMaxRpmStr = getXMLString(xmlFile, "vehicle.motor#maxRpm");
    local motorMaxRpm1, motorMaxRpm2, motorMaxRpm3 = Utils.getVectorFromString(motorMaxRpmStr);
    motorMaxRpm1 = Utils.getNoNil(motorMaxRpm1, 800);
    motorMaxRpm2 = Utils.getNoNil(motorMaxRpm2, 1000);
    motorMaxRpm3 = Utils.getNoNil(motorMaxRpm3, 1800);
    local motorMaxRpm = {motorMaxRpm1, motorMaxRpm2, motorMaxRpm3};
    self.motorMaxRpmLimit = motorMaxRpm;
	
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;

      
     


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

   if isDown and sym == Input.KEY_0 then
		self.pomoc = not self.pomoc;
	end;
  if sym == Input.KEY_KP_7 then
		self.GazMin = isDown;

	end;
	if sym == Input.KEY_KP_9 then
		self.GazMax = isDown;

	end;
end;


function Ursus914:update(dt)

      if self:getIsActiveForInput() then
        if self.GazMax then
		    self.motor.minRpm = self.motor.minRpm - 50;
		elseif self.GazMin then
		    self.motor.minRpm = self.motor.minRpm + 50;
	    end;
    end;  

local nearestDistance = 2.1;
		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 ax, ay, az = getWorldTranslation(self.zaczep); 
		local distance = Utils.vector3Length(ax-vx, ay-vy, az-vz);	
		if distance < nearestDistance then
			self.przyzaczepie = true; 
		else
			self.przyzaczepie = false; 
		end; 
--------drzwi----------------
	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;
	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;
	--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 or self.isEntered then
	 	if InputBinding.isPressed(InputBinding.ZACZEPW) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition+(self.animationParts[5].offSet+dt), false);
           elseif InputBinding.isPressed(InputBinding.ZACZEPN) then
               self:setAnimationTime(5, self.animationParts[5].currentPosition-(self.animationParts[5].offSet+dt), false);
        end;
        end;
	  --napisy
		if self.przyzaczepie then
			g_currentMission:addExtraPrintText(string.format("%s: Zaczep Nizej", InputBinding.getKeyNamesOfDigitalAction(InputBinding.ZACZEPW)));
			g_currentMission:addExtraPrintText(string.format("%s: Zaczep Wyzej", InputBinding.getKeyNamesOfDigitalAction(InputBinding.ZACZEPN)));
		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				 

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

function Ursus914:validateAttacherJoint(implement, jointDesc, dt)
    return true;
end;
function Ursus914: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.84, 0.025, "----->Ursus 904<-----");
			if not self.isMotorStarted then
			renderText(0.72, 0.815, 0.02, "Nacisnij Z: Zapal Silnik");
			else
			renderText(0.72, 0.815, 0.02, "Nacisnij Z: Zgas Silnik");
			end;
	
			renderText(0.72, 0.8, 0.02, "Nacisnij 7: Halogeny Przednie");
	
		
			renderText(0.72, 0.785, 0.02, "Nacisnij 8: Halogeny Tylne");
	
	    	renderText(0.72, 0.77, 0.02, "Nacisnij t: Podwyz Zaczep");
	    
			renderText(0.72, 0.755, 0.02, "Nacisnij y: Obniz Zaczep");
	
	
			renderText(0.72, 0.74, 0.02, "Nacisnij Num 4: Lewe Drzwi");
	
		
			renderText(0.72, 0.725, 0.02, "Nacisnij Num 6: Prawe Drzwi");
	
		
			renderText(0.72, 0.71, 0.02, "Nacisnij Num 8: Dach");
	

            renderText(0.72, 0.695, 0.02, "Nacisnij Num 5: Tylna Szybe");
         
           
			renderText(0.72, 0.68, 0.02, "Nacisnij Num 1/2/3: Migacze");
	
		
			renderText(0.72, 0.665, 0.02, "Nacisnij n: Tylny Podnosnik Gora");
		
			renderText(0.72, 0.65, 0.02, "Nacisnij m: Tylny podnosnik Dol");
			renderText(0.72, 0.635, 0.022, "-U-R-S-U-S-          904");

		end;
	end;
	
  
end;

