--
-- Hilken 18000 ZK
-- 
-- spring is inspired by the powerShaft from SFM 
--
--
-- @author:		fruktor (wwww.modding-society.de)
-- @version:	v0.1
-- @date:		08/12/10
-- @history:	v0.1 - inital implementation
--
--

Hilken18000ZK = {};

function Hilken18000ZK.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Trailer, specializations);
end;

function Hilken18000ZK:load(xmlFile)

	self.spring = {};		
	self.spring.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Hilken18000ZK.spring(0)#index"));
	local x,y,z = getRotation(self.spring.node);
	
	self.spring.part = {x,y,z};
	self.spring.part = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Hilken18000ZK.spring(0)#part"));
	x,y,z = getTranslation(self.spring.part);
	
	self.spring.fixPoint = {x,y,z};
	self.spring.fixPoint = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Hilken18000ZK.spring(0)#fixPoint"));
	
	local ax, ay, az = getWorldTranslation(self.spring.part);
	local bx, by, bz = getWorldTranslation(self.spring.fixPoint);		
	self.spring.distance = Utils.vector3Length(ax-bx, ay-by, az-bz);	

	self.spring.target = {x,y,z};
	self.spring.target = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Hilken18000ZK.spring(0)#target"));
	
end;	
	

function Hilken18000ZK:delete()
end;

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

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

function Hilken18000ZK:update(dt)
	--if self:getIsActive() then		
		if self.spring.target ~= nil then		
			local ax, ay, az = getWorldTranslation(self.spring.node);
			local bx, by, bz = getWorldTranslation(self.spring.target);
			local x, y, z = worldDirectionToLocal(getParent(self.spring.node), bx-ax, by-ay, bz-az);
			
			local upX, upY, upZ = 0,1,0;
			if math.abs(y) > 0.99*Utils.vector3Length(x, y, z) then
				-- direction and up is parallel
				upY = 0;
				if y > 0 then
					upZ = 1;
				else
					upZ = -1;
				end;
			end;
			
			setDirection(self.spring.node, x, y, z, upX, upY, upZ);
			local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
			setTranslation(self.spring.part, 0, 0, distance-self.spring.distance);		
		end;		
	--end;
end;

function Hilken18000ZK:draw()
end;

function Hilken18000ZK:onAttach()
	setJointRotationLimit(self.componentJoints[2].jointIndex, 0, true, Utils.degToRad(-65), Utils.degToRad(65));	
end;

function Hilken18000ZK:onDetach()
	setJointRotationLimit(self.componentJoints[2].jointIndex, 0, true, Utils.degToRad(-8), Utils.degToRad(8));	
end;

function Hilken18000ZK:onLeave()
end;

function Hilken18000ZK:onDeactivate()
end;

function Hilken18000ZK:onDeactivateSounds()
end;


