-- DepthAnimation for implements
-- author FS13 NKB-Modding
-- authors FS19/22 Andriy52522, AndyV
-- finished on 15/03/2023

DepthAnimation = {};

function DepthAnimation.prerequisitesPresent(specializations)
    return true
end;

function DepthAnimation.registerFunctions(vehicleType)
end;

function DepthAnimation.registerOverwrittenFunctions(vehicleType)
end;

function DepthAnimation.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", DepthAnimation);
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", DepthAnimation);
end;

function DepthAnimation:onLoad(savegame)
        self.heightNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#heightNode"), self.i3dMappings);	
	self.Dura = Utils.getNoNil(getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#Duration"),10);
	self.maxD = Utils.getNoNil(getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#maxDepth"),0.45);
	self.minD = Utils.getNoNil(getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#minDepth"),0);
	self.ZOffset = Utils.getNoNil(getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#ZOffset"),0);	
	self.nF = Utils.getNoNil(getXMLString(self.xmlFile.handle, "vehicle.DepthAnimation#normalizeFactor"),0.99);
	self.posHeight = 0;
	self.negHeight = 0;
end;

function DepthAnimation:delete()
end;

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

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

function DepthAnimation:onUpdate(dt)

	local _,yHeight,_ = getTranslation(self.heightNode);
	local x,y,z = getWorldTranslation(self.rootNode);
	local length = 0;
	if self.lastWP ~= nil then
		length = MathUtil.vector2Length(self.lastWP[1]-x , self.lastWP[3]-z);
	end;	
	if self:getIsActive() then
		if self.isClient then
			if self:getIsLowered() then
				local loweringAction = self.posHeight;
				self.posHeight = length/self.Dura;
	
				self.posHeight = math.max(self.minD , math.min(self.maxD,self.posHeight));
				if self.movingDirection == 1 and loweringAction ~= self.posHeight then
					setTranslation (self.heightNode, 0, self.posHeight, self.ZOffset);
				end;
			else
                                self.lastWP = {x,y,z};
				if yHeight ~= 0 then
					self.negHeight = yHeight * self.nF
					setTranslation (self.heightNode, 0, self.negHeight, self.ZOffset);
                                        self:setAnimationTime (self.heightNode, 1, true);      
				end;
			end;     
		end;
	end; 		
end;
 
function DepthAnimation:draw()
end;

