--
-- Specialization arrowAnimation
--
-- @Author  	Aluha74rus
-- @version 	v1.0
-- @date  		04/09/19

arrowAnimation = {};

arrowAnimation.modDir = g_currentModDirectory;

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

function arrowAnimation.registerEventListeners(vehicleType)	
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", arrowAnimation)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", arrowAnimation)
end
function arrowAnimation:onLoad(savegame)
	if self.isClient then
        local arrowAnimIndex = getXMLString(self.xmlFile, "vehicle.motorized.arrowAnim#node");
        if arrowAnimIndex ~= nil then
            self.arrowAnim = {};
            self.arrowAnim.node = I3DUtil.indexToObject(self.components, arrowAnimIndex, self.i3dMappings)
            self.arrowAnim.maxRot = MathUtil.degToRad(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.motorized.arrowAnim#maxRot"),0));
        end
	end
end;

function arrowAnimation:onUpdateTick(dt)
    if self.isClient then
        if self:getIsMotorStarted() then
	        if self.arrowAnim ~= nil then
	            local minRandom = -0.1;
	            local maxRandom = 0.1;
	            local angle = MathUtil.lerp(minRandom, maxRandom, math.random()) + self.arrowAnim.maxRot * (self.spec_motorized.motor:getEqualizedMotorRpm() / self.spec_motorized.motor:getMaxRpm());
	            angle = MathUtil.clamp(angle, 0, self.arrowAnim.maxRot);
	            setRotation(self.arrowAnim.node, 0, 0, angle);
	        end
        end
	end;
end;
function Motor:stopMotor(noEventSend)	
	if self.isClient then
	    if self.arrowAnim ~= nil then
	        setRotation(self.arrowAnim.node, 0, 0, 0);
	    end
    end
end