--
-- AnimRicagiGid
-- This is the specialization for vehicles which have movable parts
--
-- @author  PeterJ - euroDZN
-- @date  02/04/2012
--
-- http://eurodzn.wordpress.com/
--
-- Copyright (C) euroDZN, Confidential, All Rights Reserved.
  
AnimRicagiGid = {};
  
function AnimRicagiGid.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Cylindered, specializations) and SpecializationUtil.hasSpecialization(AttacherJoints, specializations);
end;
  
function AnimRicagiGid:load(savegame)
    
    --- move levers ---
    self.lastArmAngle = 0;
    self.mainArm = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.mainArmLever#mainArm"));
    self.mainArmLever = {};
    self.mainArmLever.node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.mainArmLever#index"));
    if self.mainArmLever.node ~= nil then
        self.mainArmLever.minRot = {Utils.degToRad(getXMLFloat(self.xmlFile, "vehicle.mainArmLever#minRot"))};
        self.mainArmLever.maxRot = {Utils.degToRad(getXMLFloat(self.xmlFile, "vehicle.mainArmLever#maxRot"))};
        self.mainArmLever.Rot = {Utils.degToRad(getXMLFloat(self.xmlFile, "vehicle.mainArmLever#Rot"))};
        self.mainArmLever.rotTime = getXMLFloat(self.xmlFile, "vehicle.mainArmLever#rotTime")*1000;
        self.mainArmLever.posX = true;
        self.mainArmLever.posZ = true;
    end;
end;
  
function AnimRicagiGid:delete()
end;
  
function AnimRicagiGid:readStream(streamId, connection)
end;
  
function AnimRicagiGid:writeStream(streamId, connection)
end;
  
function AnimRicagiGid:readUpdateStream(streamId, timestamp, connection)
end;
  
function AnimRicagiGid:writeUpdateStream(streamId, connection, dirtyMask)
end;

function AnimRicagiGid:mouseEvent(posX, posY, isDown, isUp, button)
end;
  
function AnimRicagiGid:keyEvent(unicode, sym, modifier, isDown)
end;
 
function AnimRicagiGid:update(dt)
end;
  
function AnimRicagiGid:updateTick(dt)
    if self:getIsActive() or forceUpdate then    
        if self.mainArm ~= nil then
            --- arm lever ---
            local x,_,_ = getRotation(self.mainArm);
            self.armMoveAngle = x;
            if self.lastArmAngle > self.armMoveAngle then
                self.mainArmLever.posX = true;
                x,y,z = getRotation(self.mainArmLever.node);    
                local newRot = Utils.getMovedLimitedValues({x}, self.mainArmLever.maxRot, self.mainArmLever.minRot, 1, self.mainArmLever.rotTime, dt, true);
                setRotation(self.mainArmLever.node, newRot[1],y,z);
            elseif self.lastArmAngle < self.armMoveAngle then
                self.mainArmLever.posX = false;
                x,y,z = getRotation(self.mainArmLever.node);    
                local newRot = Utils.getMovedLimitedValues({x}, self.mainArmLever.maxRot, self.mainArmLever.minRot, 1, self.mainArmLever.rotTime, dt, false );
                setRotation(self.mainArmLever.node, newRot[1],y,z);
            end;
            if self.lastArmAngle == self.armMoveAngle then
                if self.mainArmLever.posX then
                    x,y,z = getRotation(self.mainArmLever.node);    
                    local newRot = Utils.getMovedLimitedValues({x}, self.mainArmLever.minRot, self.mainArmLever.Rot, 1, self.mainArmLever.rotTime/2, dt, true);
                    setRotation(self.mainArmLever.node, newRot[1],y,z);
                else
                    x,y,z = getRotation(self.mainArmLever.node);    
                    local newRot = Utils.getMovedLimitedValues({x}, self.mainArmLever.maxRot, self.mainArmLever.Rot, 1, self.mainArmLever.rotTime/2, dt, true);
                    setRotation(self.mainArmLever.node, newRot[1],y,z);        
                end;
            end;
            self.lastArmAngle = self.armMoveAngle;
        end;
    end;
end;
  
function AnimRicagiGid:draw()
end;