--[[

|=================================================|
|-----------Script do Bizon Rekord Z058-----------|
|-----------------by PowerIsBack------------------|
|-------------Skrypt BY maciusboss1---------------|
|----------------Data: 10.04.2013-----------------|
|------------------Pomoc: Burner------------------|
|--------------------No Edit!!!-------------------|
|-------------------------------------------------|
|=================================================|

]]--

Bizon_Rekord_Z058 = {};

function Bizon_Rekord_Z058.prerequisitesPresent(specializations)
    Vehicle.registerJointType("cutter");
    Vehicle.registerJointType("trailerCombine");
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function Bizon_Rekord_Z058:load(xmlFile)

    self.rollNodes = {};

    local rollNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roll#index"));
    if rollNode ~= nil then
        local speed = 0.003*3;
        table.insert(self.rollNodes, {node=rollNode, speed=speed});
    end;
    local i = 0;
    while true do
        local key = string.format("vehicle.rolls.roll(%d)", i);
        if not hasXMLProperty(xmlFile, key) then
            break;
        end;
        local rollNode = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
        local speed = Utils.getNoNil(getXMLFloat(xmlFile, key.."#speed"), 0.003);
        if rollNode ~= nil then
            table.insert(self.rollNodes, {node=rollNode, speed=speed});
        end;
        i = i + 1;
    end;
    
     self.roll1Nodes = {};

    local roll1Node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roll1#index"));
    if roll1Node ~= nil then
        local speed = 0.003*3;
        table.insert(self.roll1Nodes, {node=roll1Node, speed=speed});
    end;
    local i = 0;
    while true do
        local key = string.format("vehicle.rolls1.roll1(%d)", i);
        if not hasXMLProperty(xmlFile, key) then
            break;
        end;
        local roll1Node = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
        local speed = Utils.getNoNil(getXMLFloat(xmlFile, key.."#speed"), 0.003);
        if roll1Node ~= nil then
            table.insert(self.roll1Nodes, {node=roll1Node, speed=speed});
        end;
        i = i + 1;
    end;
    
      self.roll2Nodes = {};

    local roll2Nodes = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.roll2#index"));
    if roll2Node ~= nil then
        local speed = 0.003*3;
        table.insert(self.roll2Nodes, {node=roll2Node, speed=speed});
    end;
    local i = 0;
    while true do
        local key = string.format("vehicle.rolls2.roll2(%d)", i);
        if not hasXMLProperty(xmlFile, key) then
            break;
        end;
        local roll2Node = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
        local speed = Utils.getNoNil(getXMLFloat(xmlFile, key.."#speed"), 0.003);
        if roll2Node ~= nil then
            table.insert(self.roll2Nodes, {node=roll2Node, speed=speed});
        end;
        i = i + 1;
    end;
    	
	self.objectInRange = Bizon_Rekord_Z058.objectInRange;

end;

function Bizon_Rekord_Z058:delete()

end;

function Bizon_Rekord_Z058:readStream(streamId, connection)

end;

function Bizon_Rekord_Z058:writeStream(streamId, connection)

end;

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

	if isDown and sym == Input.KEY_0 then
		self.helpPanel = not self.helpPanel;
   	end;

end;

function Bizon_Rekord_Z058:update(dt)

		if self:objectInRange(self.animationParts[1].rootNode) then
		if InputBinding.hasEvent(InputBinding.BizonZ058_KlapaPrawa) 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:objectInRange(self.animationParts[1].rootNode) then
		if self.animationParts[1].clipEndTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijKlapaPrawa")), InputBinding.BizonZ058_KlapaPrawa);
		else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzKlapaPrawa")), InputBinding.BizonZ058_KlapaPrawa);
		end;
		end;
		
		if self:objectInRange(self.animationParts[2].rootNode) then
		if InputBinding.hasEvent(InputBinding.BizonZ058_KlapaLewa) 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:objectInRange(self.animationParts[2].rootNode) then
		if self.animationParts[2].clipEndTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijKlapaLewa")), InputBinding.BizonZ058_KlapaLewa);
		else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzKlapaLewa")), InputBinding.BizonZ058_KlapaLewa);
		end;
		end;
		
		if self:objectInRange(self.animationParts[3].rootNode) then
		if InputBinding.hasEvent(InputBinding.BizonZ058_KlapaTyl) 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;
		end;
			if self:objectInRange(self.animationParts[3].rootNode) then
		if self.animationParts[3].clipEndTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijKlapaTyl")), InputBinding.BizonZ058_KlapaTyl);
		else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzKlapaTyl")), InputBinding.BizonZ058_KlapaTyl);
		end;
		end;
		
		if self:objectInRange(self.animationParts[4].rootNode) then
		if InputBinding.hasEvent(InputBinding.BizonZ058_KlapaSilnik) 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;
		end;
			if self:objectInRange(self.animationParts[4].rootNode) then
		if self.animationParts[4].clipEndTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijKlapaSilnik")), InputBinding.BizonZ058_KlapaSilnik);
		else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzKlapaSilnik")), InputBinding.BizonZ058_KlapaSilnik);
		end;
		end;
		
		if self:objectInRange(self.animationParts[5].rootNode) then
		if InputBinding.hasEvent(InputBinding.BizonZ058_KlapaZbiornik) then
			if self.animationParts[5].clipEndTime then
				self:setAnimationTime(5, self.animationParts[5].offSet);
			elseif self.animationParts[5].clipStartTime then
				self:setAnimationTime(5, self.animationParts[5].animDuration);
			end;
		end;
		end;
		
		if self:objectInRange(self.animationParts[5].rootNode) then
		if self.animationParts[5].clipEndTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijKlapaZbiornik")), InputBinding.BizonZ058_KlapaZbiornik);
		else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzKlapaZbiornik")), InputBinding.BizonZ058_KlapaZbiornik);
		end;
		end;
		
	if self:getIsActiveForInput(false)then
		if self.isClient then
			if InputBinding.hasEvent(InputBinding.BizonZ058_Drzwi) then
				if self.animationParts[6].clipEndTime then
					self:setAnimationTime(6, self.animationParts[6].offSet);
				else
					self:setAnimationTime(6, self.animationParts[6].animDuration);
				end;
			end;
			if self.animationParts[6].clipEndTime then
					g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijDrzwi")), InputBinding.BizonZ058_Drzwi);
				else
					g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzDrzwi")), InputBinding.BizonZ058_Drzwi);
			end;
			
			if InputBinding.hasEvent(InputBinding.BizonZ058_Okno) then
				if self.animationParts[7].clipEndTime then
					self:setAnimationTime(7, self.animationParts[7].offSet);
				else
					self:setAnimationTime(7, self.animationParts[7].animDuration);
				end;
			end;
			if self.animationParts[7].clipEndTime then
					g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_ZamknijOkno")), InputBinding.BizonZ058_Okno);
				else
					g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Z058_OtworzOkno")), InputBinding.BizonZ058_Okno);
			end;
		end;
	end;
	
	
	
			if self.isThreshing then
				for _, rollNode in pairs(self.rollNodes) do
					rotate(rollNode.node, -dt*rollNode.speed, 0, 0);
				end;
			end;
			if self.isMotorStarted then
				for _, roll1Node in pairs(self.roll1Nodes) do
					rotate(roll1Node.node, -dt*roll1Node.speed, 0, 0);
				end;
			end;
			if self.chopperActive then
				for _, roll2Node in pairs(self.roll2Nodes) do
					rotate(roll2Node.node, -dt*roll2Node.speed, 0, 0);
				end;
			end;
			if self.isThreshing then
					self:setAnimationTime(8, self.animationParts[8].animDuration);
			
					self:setAnimationTime(9, 1);
				else 
					self:setAnimationTime(8, self.animationParts[8].offSet);
			
					self:setAnimationTime(9, 0);
			end;
		
		
end;

function Bizon_Rekord_Z058:draw()

	 

end;

function Bizon_Rekord_Z058:validateAttacherJoint(implement, jointDesc, dt)
   return true;
end;

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

function Bizon_Rekord_Z058:objectInRange(object)
	if g_currentMission.player ~= nil and object ~= nil then
		local nearestDistance = 3;
		local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
		local px, py, pz = getWorldTranslation(object); 
		local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);	
		if distance < nearestDistance then
			return true;
		else
			return false;
		end;
	else
		return false;
	end;
end;
