--
-- Support
-- This is the specialization for Support
--
-- M@D Author  Headshot XXL
-- M@D date 03.10.2009
--
-- > Copyright (C) Headshot XXL - www.planet-ls.de < --

Support = {};

function Support.prerequisitesPresent(specializations)
    return true;
end;

function Support:load(xmlFile)

	local supportRotNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.supportRot#index"));
    if supportRotNode ~= nil then
        self.supportRot = {};
        self.supportRot.node = supportRotNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.supportRot#minRot"));
        self.supportRot.minRot = {};
        self.supportRot.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.supportRot.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.supportRot.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

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

        self.supportRot.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.supportRot#rotTime"), 2)*1000;
    end;
	
	local supportTransNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.supportTrans#index"));
    if supportTransNode ~= nil then
        self.supportTrans = {};
        self.supportTrans.node = supportTransNode;
        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.supportTrans#minTrans"));
        self.supportTrans.minRot = {};
        self.supportTrans.minRot[1] = x;
        self.supportTrans.minRot[2] = y;
        self.supportTrans.minRot[3] = z;

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.supportTrans#maxTrans"));
        self.supportTrans.maxRot = {};
        self.supportTrans.maxRot[1] = x;
        self.supportTrans.maxRot[2] = y;
        self.supportTrans.maxRot[3] = z;

        self.supportTrans.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.supportTrans#transTime"), 2)*1000;
    end;

	self.supportActive = true;
	
end;

function Support:delete()
end;

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

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

function Support:update(dt)
	
		if self.supportRot ~= nil then
		local x, y, z = getRotation(self.supportRot.node);
		local rot = {x,y,z};
		local newRot = Utils.getMovedLimitedValues(rot, self.supportRot.maxRot, self.supportRot.minRot, 3, self.supportRot.rotTime, dt, self.supportActive);
		setRotation(self.supportRot.node, unpack(newRot));
	end;
	
	if self.supportTrans ~= nil then
		local x, y, z = getTranslation(self.supportTrans.node);
		local trans = {x,y,z};
		local newTrans = Utils.getMovedLimitedValues(trans, self.supportTrans.maxRot, self.supportTrans.minRot, 3, self.supportTrans.rotTime, dt, self.supportActive);
		setTranslation(self.supportTrans.node, unpack(newTrans));
	end;
	
	for i, jointDesc in pairs(self.componentJoints) do
	   setJointFrame(self.componentJoints[i].jointIndex, 0, self.componentJoints[i].jointNode);
	end;
	
end;

function Support:draw()
end;

function Support:onDetach()

	self.supportActive = true;

end;

function Support:onAttach()
	
	self.supportActive = false;

end;
