--
-- physicalCamera
-- Specialization for physically adapting Camera
--
-- @author  Knagsted
-- @date  3/04/10
--

-- Do not edit without my permission
--
physicalCamera = {};

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

function physicalCamera:load(xmlFile)
	self.cameraRotNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cameraRotNode#index"));
	self.seatNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.seatNode#index"));
	self.farmerNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.characterNode#index"));
	
	if self.cameraRotNode ~=nil then
		local x,y,z = getTranslation(self.cameraRotNode);
		self.startXTranslation = x;
		self.startYTranslation = y;
		self.startZTranslation = z;
	end;
	
	if self.seatNode ~=nil then
		local x,y,z = getTranslation(self.seatNode);
		self.startSeatXTranslation = x;
		self.startSeatYTranslation = y;
		self.startSeatZTranslation = z;
	end;

	if self.farmerNode ~=nil then
		local x,y,z = getTranslation(self.farmerNode);
		self.startFarmerXTranslation = x;
		self.startFarmerYTranslation = y;
		self.startFarmerZTranslation = z;
	end;
	
	--Sounds
	SeatSoundFile = Utils.getFilename("seatSound.wav", self.baseDirectory);
    self.SeatSoundId = createSample("SeatSound");
    loadSample(self.SeatSoundId, SeatSoundFile, false);
    self.SeatPlaying = false;
	self.SeatSoundOffset = getSampleDuration(self.SeatSoundId);
	
	self.averageYTranslation = self.startYTranslation;
	self.camTranslation = self.startYTranslation;
	self.maxTranslationPrCycle = 0.00008;
	self.offsetTranslation = 0.0;
	self.offsetAdjustmentTranslation = 0;
	
	if self.wheels[3].repr ~= nil and self.wheels[3].repr ~= nil then
		local x,y,z = getTranslation(self.wheels[3].repr);
		local x2,y2,z2 = getTranslation(self.wheels[3].repr);

		self.averageYTranslation = (y + y2)/2;
	end;
	
	self.camTranslation = self.averageYTranslation;
	
	--###
	self.internalCamera = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.internalCamera#index"));
	self.camPositionX = 0;
	self.camPositionZ = 0;	
end;

function physicalCamera:delete()
	if self.SeatPlaying ~= nil then
		stopSample(self.SeatSoundId);
	end;
end;

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

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

function physicalCamera:update(dt)
	if self.wheels[3].repr ~= nil and self.wheels[3].repr ~= nil then
		local x,y,z = getTranslation(self.wheels[3].repr);
		local x2,y2,z2 = getTranslation(self.wheels[4].repr);

		self.averageYTranslation = (y + y2)/2;
	end;
	
	self.playSeatSound = false;
	if self.camTranslation > self.averageYTranslation then
		self.camTranslation = self.camTranslation - self.maxTranslationPrCycle * dt;
		if self.camTranslation < self.averageYTranslation then
			self.camTranslation = self.averageYTranslation;
		end;
	elseif self.camTranslation < self.averageYTranslation then
		self.camTranslation = self.camTranslation + self.maxTranslationPrCycle * dt;
		if self.camTranslation > self.averageYTranslation then
			self.camTranslation = self.averageYTranslation;
		end;
		--set sound to play if seat damping is very big.
		if self.camTranslation < self.averageYTranslation - 0.0002 and self.isEntered then
			self.playSeatSound = true;
		end;
	end;
	
	if self.cameraRotNode ~= nil then
		setTranslation(self.cameraRotNode, self.startXTranslation, self.camTranslation+self.offsetAdjustmentTranslation, self.startZTranslation);
	end;
	
	--setup offsetAdjustmentTranslation accoding to weight on tractor
	if self.currentWeight ~= nil then
		self.offsetAdjustmentTranslation = -0.82 - self.currentWeight * 0.00063;
	else
		self.offsetAdjustmentTranslation = -0.82;
	end;
	
	self.offsetTranslation = self.camTranslation - self.startYTranslation;
	
	local adjustedOffsetTranslation = (self.offsetTranslation)+self.offsetAdjustmentTranslation;
	
	if self.seatNode ~= nil then
		setTranslation(self.seatNode, self.startSeatXTranslation, self.startSeatYTranslation + adjustedOffsetTranslation, self.startSeatZTranslation);
	end;
	
	if self.farmerNode ~= nil then
		setTranslation(self.farmerNode, self.startFarmerXTranslation, self.startFarmerYTranslation+adjustedOffsetTranslation, self.startFarmerZTranslation);
	end;
	
	if self.seatNode ~= nil and self.camIndex == self.indoorCamIndex then		-- uses information from InteractiveControl
		if self.playSeatSound then
			if not self.SeatPlaying then
				playSample(self.SeatSoundId, 0, 0.75, 0);
				self.SeatPlaying = true;
			end;
		else
			if self.SeatPlaying then
				stopSample(self.SeatSoundId);
				self.SeatPlaying = false;
			end;
		end;
	else
		if self.SeatPlaying then
			stopSample(self.SeatSoundId);
			self.SeatPlaying = false;
		end;	
	end;
	
	--###
	if self.isEntered then
		
		if InputBinding.isPressed(InputBinding.MOVECAMLEFT) then
			self.camPositionX = self.camPositionX + 0.02;
			if self.camPositionX >= 0.3 then
				self.camPositionX = 0.3;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.MOVECAMRIGHT) then
			self.camPositionX = self.camPositionX - 0.02;
			if self.camPositionX <= -0.3 then
				self.camPositionX = -0.3;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.MOVECAMFORWARD) then
			self.camPositionZ = self.camPositionZ + 0.02;
			if self.camPositionZ >= 0.3 then
				self.camPositionZ = 0.3;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.MOVECAMBACK) then
			self.camPositionZ = self.camPositionZ - 0.02;
			if self.camPositionZ <= -0.3 then
				self.camPositionZ = -0.3;
			end;
		end;
		
		if InputBinding.hasEvent(InputBinding.MOVECAMRESET) then
			self.camPositionX = 0;
			self.camPositionZ = 0;
		end;
		
		if self.internalCamera ~= nil then
			setTranslation(self.internalCamera, self.camPositionX, 0, self.camPositionZ);
		end;		
	end;	
end;

function physicalCamera:onLeave()
end;

function physicalCamera:draw()
end;

