--
-- funkcja ktora umozliwia otwieranie sie automatycznie drzwi, gdy gracz jest w poblizu, nie dotyczy, gdy drzwi są otworzone
-- Autor - Grzegorz056
-- 17.01.2011
-- nie edytowac 

drzwiautomat = {};

function drzwiautomat.prerequisitesPresent(specializations)
    return true
end;

function drzwiautomat:load(xmlFile)
	
	local rotationNodePartlewe = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.lewe#index"));
    if rotationNodePartlewe ~= nil then
        self.rotationPartlewe = {};
        self.rotationPartlewe.node = rotationNodePartlewe;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.lewe#minRot"));
        self.rotationPartlewe.minRot = {};
        self.rotationPartlewe.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartlewe.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartlewe.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.lewe#maxRot"));
        self.rotationPartlewe.maxRot = {};
        self.rotationPartlewe.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartlewe.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartlewe.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotationPartlewe.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.lewe#rotTime"), 2)*1000;
        self.rotationPartlewe.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.lewe#touchRotLimit"), 10));
    end;  
	local rotationNodePartprawe = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.prawe#index"));
    if rotationNodePartprawe ~= nil then
        self.rotationPartprawe = {};
        self.rotationPartprawe.node = rotationNodePartprawe;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.prawe#minRot"));
        self.rotationPartprawe.minRot = {};
        self.rotationPartprawe.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartprawe.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartprawe.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.prawe#maxRot"));
        self.rotationPartprawe.maxRot = {};
        self.rotationPartprawe.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartprawe.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartprawe.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotationPartprawe.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.prawe#rotTime"), 2)*1000;
        self.rotationPartprawe.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.prawe#touchRotLimit"), 10));
    end; 
 	local rotationPartNodeleftDoor = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.leftDoor#index"));
    	if rotationPartNodeleftDoor ~= nil then
        self.rotationPartleftDoor = {};
        self.rotationPartleftDoor.node = rotationPartNodeleftDoor;

        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotationPartleftDoor#minRot"));
        self.rotationPartleftDoor.minRot = {};
        self.rotationPartleftDoor.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartleftDoor.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartleftDoor.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.leftDoor#maxRot"));
        self.rotationPartleftDoor.maxRot = {};
        self.rotationPartleftDoor.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartleftDoor.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartleftDoor.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotationPartleftDoor.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.leftDoor#rotTime"), 2)*1000;
        self.rotationPartleftDoor.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.leftDoor#touchRotLimit"), 10));
    end;
 	local rotationPartNoderightdoor = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rightdoor#index"));
    	if rotationPartNoderightdoor ~= nil then
        self.rotationPartrightdoor = {};
        self.rotationPartrightdoor.node = rotationPartNoderightdoor;

        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotationPartrightdoor#minRot"));
        self.rotationPartrightdoor.minRot = {};
        self.rotationPartrightdoor.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartrightdoor.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartrightdoor.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rightdoor#maxRot"));
        self.rotationPartrightdoor.maxRot = {};
        self.rotationPartrightdoor.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartrightdoor.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartrightdoor.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotationPartrightdoor.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rightdoor#rotTime"), 2)*1000;
        self.rotationPartrightdoor.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rightdoor#touchRotLimit"), 10));
    end; 

end;

function drzwiautomat:delete()
end;

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

function drzwiautomat:keyEvent(unicode, sym, modifier, isDown)
	if isDown and sym == Input.KEY_KP_4 then 
		self.rotationMaxrightdoor = not self.rotationMaxrightdoor;
	end; 
	if isDown and sym == Input.KEY_KP_6 then 
		self.rotationMaxleftDoor = not self.rotationMaxleftDoor;
	end; 
end;

function drzwiautomat:update(dt)


	local nearestDistance = 2.2; 
	local ax, ay, az = getWorldTranslation(self.rotationPartprawe.node);
	local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
	local distance = Utils.vector3Length(ax-vx, ay-vy, az-vz);
	
	if distance < nearestDistance then
		self.player_wpoblizu = true; 
	else
		self.player_wpoblizu = false; 
	end;
	if self.player_wpoblizu and not self.rotationMaxleftdoor == true then
		self.rotationMaxprawe = true;
	else
		self.rotationMaxprawe = false;
	end;

	if self.rotationPartprawe ~= nil then
        local x, y, z = getRotation(self.rotationPartprawe.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartprawe.maxRot, self.rotationPartprawe.minRot, 3, self.rotationPartprawe.rotTime, dt, not self.rotationMaxprawe);
        setRotation(self.rotationPartprawe.node, unpack(newRot));
    end;
	
	local nearestDistance = 2.2; 
	local ax, ay, az = getWorldTranslation(self.rotationPartlewe.node);
	local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
	local distance = Utils.vector3Length(ax-vx, ay-vy, az-vz);
	
	if distance < nearestDistance then
		self.player_wpoblizu = true; 
	else
		self.player_wpoblizu = false; 
	end;
	if self.player_wpoblizu and not self.rotationMaxrightdoor == true then
		self.rotationMaxlewe = true;
	else
		self.rotationMaxlewe = false;
	end;

	if self.rotationPartlewe ~= nil then
        local x, y, z = getRotation(self.rotationPartlewe.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartlewe.maxRot, self.rotationPartlewe.minRot, 3, self.rotationPartlewe.rotTime, dt, not self.rotationMaxlewe);
        setRotation(self.rotationPartlewe.node, unpack(newRot));
    end;

	    if self.rotationPartrightdoor ~= nil then
		   local x, y, z = getRotation(self.rotationPartrightdoor.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartrightdoor.maxRot, self.rotationPartrightdoor.minRot, 3, self.rotationPartrightdoor.rotTime, dt, not self.rotationMaxrightdoor);
		   setRotation(self.rotationPartrightdoor.node, unpack(newRot));
        end;
	   if self.rotationPartleftDoor ~= nil then
		   local x, y, z = getRotation(self.rotationPartleftDoor.node);
		   local rot = {x,y,z};
		   local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartleftDoor.maxRot, self.rotationPartleftDoor.minRot, 3, self.rotationPartleftDoor.rotTime, dt, not self.rotationMaxleftDoor);
		   setRotation(self.rotationPartleftDoor.node, unpack(newRot));

        end;


end;
function drzwiautomat:onLeave()
end;

function drzwiautomat:onEnter()
end;

function drzwiautomat:draw()
end;