-- 
-- Terra Trac
--
-- BETA by shangri66
-- 
-- Nimmt die rotation der Räder und überträgt sie auf die TTwheels.
-- Jedes TTwheel kann ein "reference Wheel" zugewiesen werden, dessen rotation übertragen wird.
-- Das erlaubt, das sich die Tracs unterschiedlich schnell in den Kurven drehen können
--
-- Zusätzlich wird die Y-translation auf das Laufband übertragen, damit das Laufband auch "federt".
-- Um eine möglichst einfache und zudem noch anschauliche Terra Trac zu bekommen, wird dazu noch ein custom shader genutzt
-- dafür nehmen wir einfach den ScrollUV-Shader des Strohballen-Elevators und lassen die UVmap nach unten/oben verschieben
--
-- Vorsicht, BETA-Version!
--


TerraTrac = {};

function TerraTrac.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function TerraTrac:load(xmlFile)
 
 self.numTerraTrackWheels = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.TerraTrackWheels#count"), 0);
    self.TerraTracks = {};
    for i=1, self.numTerraTrackWheels do
        local TerraTracksnamei = string.format("vehicle.TerraTrackWheels.TTWheel" .. "%d", i);
        self.TerraTracks[i] = {};
		self.TerraTracks[i].index = Utils.indexToObject(self.components, getXMLString(xmlFile, TerraTracksnamei .. "#index"));
		self.TerraTracks[i].refWheel = Utils.getNoNil(getXMLInt(xmlFile,  TerraTracksnamei .. "#referenceWheel"), 1);			
    end;
		
	self.trackL = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TerraTracLeft#index"));
	self.trackR = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TerraTracRight#index"));
	
	self.terraL = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TracBodyLeft#index"));
	self.terraR = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TracBodyRight#index"));
	
	self.trackL2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TerraTracLeft2#index"));
	self.trackR2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TerraTracRight2#index"));
	
	self.terraL2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TracBodyLeft2#index"));
	self.terraR2 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.TracBodyRight2#index"));
	
	self.txL, self.tyL, self.tzL = getTranslation(self.terraL);
	self.txR, self.tyR, self.tzR = getTranslation(self.terraR);
	self.wxL, self.wyL, self.wzL = getTranslation(self.wheels[3].driveNode);
	self.wxR, self.wyR, self.wzR = getTranslation(self.wheels[4].driveNode);
	self.txL2, self.tyL2, self.tzL2 = getTranslation(self.terraL2);
	self.txR2, self.tyR2, self.tzR2 = getTranslation(self.terraR2);
	self.wxL2, self.wyL2, self.wzL2 = getTranslation(self.wheels[3].driveNode);
	self.wxR2, self.wyR2, self.wzR2 = getTranslation(self.wheels[4].driveNode);
	self.deltaLwy = self.tyL - self.wyL;
	self.deltaRwy = self.tyR - self.wyR;
	self.deltaLwy2 = self.tyL2 - self.wyL2;
	self.deltaRwy2 = self.tyR2 - self.wyR2;
end;
	
function TerraTrac:delete()
end;

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

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

function TerraTrac:update(dt)

	---
	--- let the tracs "bounce" ;)
	---
	local Lwx, Lwy, Lwz = getTranslation(self.wheels[3].driveNode);
	local Ltx, Lty, Ltz = getTranslation(self.terraL);
	setTranslation(self.terraL, Ltx, (Lwy+self.deltaLwy), Ltz);
	
	local Rwx, Rwy, Rwz = getTranslation(self.wheels[4].driveNode);
	local Rtx, Rty, Rtz = getTranslation(self.terraR);
	setTranslation(self.terraR, Rtx, (Rwy+self.deltaRwy), Rtz);
	
	local Lwx2, Lwy2, Lwz2 = getTranslation(self.wheels[3].driveNode);
	local Ltx2, Lty2, Ltz2 = getTranslation(self.terraL2);
	setTranslation(self.terraL2, Ltx2, (Lwy2+self.deltaLwy2), Ltz2);
	
	local Rwx2, Rwy2, Rwz2 = getTranslation(self.wheels[4].driveNode);
	local Rtx2, Rty2, Rtz2 = getTranslation(self.terraR2);
	setTranslation(self.terraR2, Rtx2, (Rwy2+self.deltaRwy2), Rtz2);
	
	---
	--- the uv-coords are set by the rotation value of the wheel
	---
	local xt,yt,zt = getRotation(self.wheels[3].driveNode);	
	setShaderParameter(self.trackL, "UVPlaySpeed", 0, xt*0.34, 0, 0, false);
	local xt2,yt2,zt2 = getRotation(self.wheels[4].driveNode);	
	setShaderParameter(self.trackR, "UVPlaySpeed", 0, xt2*0.34, 0, 0, false);
	local xt3,yt3,zt3 = getRotation(self.wheels[3].driveNode);	
	setShaderParameter(self.trackL2, "UVPlaySpeed", 0, xt3*1.7, 0, 0, false);
	local xt4,yt4,zt4 = getRotation(self.wheels[4].driveNode);	
	setShaderParameter(self.trackR2, "UVPlaySpeed", 0, xt4*1.7, 0, 0, false);
		
	---
	--- rotate the little wheels
	---	
	for i=1, self.numTerraTrackWheels do	
			
		local q = self.TerraTracks[i].refWheel
	
		local x,y,z = getRotation(self.wheels[q].driveNode);
		local TTWheel = self.TerraTracks[i].index;
		setRotation(TTWheel, x,y,z);
	end;
		

		

end;

function TerraTrac:draw()
end;