--
-- ClaasDisco3450Plus
-- Specialization for animation of the claas disco 3450 plus
--
-- @author  Stefan Maurus    
-- @date  2016/07/17

-- Copyright © Stefan Maurus, www.stefanmaurus.de

ClaasDisco3450Plus = {};

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

function ClaasDisco3450Plus:load(savegame)
    self.lowerDamper = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.ClaasDisco3450Plus.lowerDamper#index"));

    self.lowerDamperMaxZ = getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.lowerDamper#maxZ");
    self.lowerDamperMinZ = getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.lowerDamper#minZ");
    
    self.lowerDamperRef = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.ClaasDisco3450Plus.lowerDamper#reference"));
    
    self.limiter = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#referencePoint"));
    self.limiterMaxZ = getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#maxZ");
    self.limiterMinZ = getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#minZ");
    
    self.limiterRef = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#reference"));
    self.limiterRefMaxRot = math.rad(getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#maxRot"));
    self.limiterRefMinRot = math.rad(getXMLFloat(self.xmlFile, "vehicle.ClaasDisco3450Plus.limiter#minRot"));
end;

function ClaasDisco3450Plus:delete()
end;

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

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

function ClaasDisco3450Plus:update(dt)
    if self.lowerDamper ~= nil then
        local _, _, sz = getScale(self.lowerDamperRef);
        local x, y, z = getTranslation(self.lowerDamper);
        local factor = math.min(math.abs(sz-1)/0.05, 1);

        local changed = 0;
        if sz > 1.02 then
            changed = math.min(z+0.004*factor, self.lowerDamperMaxZ);
        elseif sz < 0.98 then
            changed = math.max(z-0.004*factor, self.lowerDamperMinZ);
        end;
        
        if changed ~= 0 then
            setTranslation(self.lowerDamper, x, y, changed);
        end;
    end;
    
    if self.limiter ~= nil then
        local x, _, _ = getRotation(self.limiterRef);
        x = math.max(math.min(x, self.limiterRefMaxRot), self.limiterRefMinRot);
        local pos = 1-((x-self.limiterRefMinRot)/(self.limiterRefMaxRot-self.limiterRefMinRot));
        local x, y, z = getTranslation(self.limiter);
        setTranslation(self.limiter, x, y, self.limiterMinZ+(self.limiterMaxZ-self.limiterMinZ)*pos);
    end;
end;

function ClaasDisco3450Plus:updateTick(dt)
end;

function ClaasDisco3450Plus:draw()
end;