-- Specjalizacja dla Romet Chart --
-- Autor: Rockstar94 --
-- 02.01.2015 --
-- Zakaz wyciagania z modu --



Romet = {};

function Romet.prerequisitesPresent(specializations)
    return true
end;

function Romet:load(savegame)
    self.lancuch = {};
    
    local i = 0;
    
    while true do
        local key = string.format("vehicle.lancuch.lancuch(%d)", i);
        if not hasXMLProperty(xmlFile, key) then
            break;
        end;
        local node = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
        if node ~= nil then
            table.insert(self.lancuch, {node=node});
        end;
        i = i +1;
    end;

    self.animation = {};
    self.animation.animCharSet = 0;
    self.animationEnabled = false;

    local rootNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.animation#rootNode"));

    if rootNode ~= nil then
        self.animation.animCharSet = getAnimCharacterSet(rootNode);
        if self.animation.animCharSet ~= 0 then
            self.animation.clip = getAnimClipIndex(self.animation.animCharSet, getXMLString(xmlFile, "vehicle.animation#animationClip"));
            if self.animation.clip >= 0 then
                assignAnimTrackClip(self.animation.animCharSet, 0, self.animation.clip);
                self.animation.speedScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.animation#speedScale"), 1);
                setAnimTrackSpeedScale(self.animation.animCharSet, self.animation.clip, self.animation.speedScale);
                setAnimTrackLoopState(self.animation.animCharSet, 0, true);
            end;
        end;
    end;

    self.ropePanelAnimation = getXMLString(xmlFile, "vehicle.ropePanel#animationName");
    self.setRopePanel = SpecializationUtil.callSpecializationsFunction("setRopePanel");
    
    self.ropePanelAnimation1 = getXMLString(xmlFile, "vehicle.ropePanel1#animationName");
    self.setRopePanel1 = SpecializationUtil.callSpecializationsFunction("setRopePanel1");
    
    
    openSoundFile = Utils.getFilename("Dzwieki/bieg.wav", self.baseDirectory);
    self.openSoundId = createSample("Sample");
    loadSample(self.openSoundId, openSoundFile, false);
    self.openPlaying = false;    
    
    open1SoundFile = Utils.getFilename("Dzwieki/bieg.wav", self.baseDirectory);
    self.open1SoundId = createSample("Sample");
    loadSample(self.open1SoundId, open1SoundFile, false);
    self.open1Playing = false;    
end;

function Romet:delete()
end;

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

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

function Romet:update(dt)
    if self.sound1 then
        if not self.openPlaying then
            playSample(self.openSoundId, 1, 3, 3);
            self.openPlaying = true;
        end;      
    else
        if self.openPlaying then
            stopSample(self.openSoundId);
            self.openPlaying = false;
        end;        
    end;
    
    if self.sound2 then
        if not self.open1Playing then
            playSample(self.open1SoundId, 1, 3, 3);
            self.open1Playing = true;
        end;      
        else
        if self.open1Playing then
            stopSample(self.open1SoundId);
            self.open1Playing = false;
        end;        
    end;
    
    if self.isMotorStarted then
        if not self.brakeLightsVisibility and self.lastSpeed*4800 < 1 then
            if InputBinding.hasEvent(InputBinding.sprzeglo) or InputBinding.hasEvent(InputBinding.sprzeglo1) then
                self:setRopePanel1(not self.ropePanel1);
            end;
        end
    end;

    if self.isMotorStarted then
        if  self.brakeLightsVisibility then
            self:playAnimation(self.ropePanelAnimation, 1, nil, true);
        end;
        if  not self.brakeLightsVisibility then
            self:playAnimation(self.ropePanelAnimation, -1, nil, true);
        end;
    end;

    if self.isMotorStarted then
        if not self.animationEnabled then
            enableAnimTrack(self.animation.animCharSet, 0);
            self.animationEnabled = true;
        end;
    else
        if self.animationEnabled then
            disableAnimTrack(self.animation.animCharSet, 0);
            self.animationEnabled = false;
        end;
    end;

    if self.isMotorStarted then
        for _, part in pairs(self.lancuch) do
            if self.lastSpeed*4800 > 1 then
                setShaderParameter(part.node, "uvScrollSpeed", 0.8, 0, 0, 0, false);
            end;
            if self.lastSpeed*4800 > 6 then
                setShaderParameter(part.node, "uvScrollSpeed", 1.4, 0, 0, 0, false);
            end;
            if self.lastSpeed*4800 > 10 then
                setShaderParameter(part.node, "uvScrollSpeed", 2, 0, 0, 0, false);
            end;
            if self.lastSpeed*4800 < 1 then
                setShaderParameter(part.node, "uvScrollSpeed", 0, 0, 0, 0, false);
            end;
        end;        
    end;    
end;

function Romet:setRopePanel(isRopePanel)
    if isRopePanel then
        if self.ropePanelAnimation ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.ropePanelAnimation, -1, nil, true);
            self.ropePanel = true;
        end;
    else
        if self.ropePanelAnimation ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.ropePanelAnimation, 1, nil, true);
            self.ropePanel = false;
        end;
    end;
end;

function Romet:setRopePanel1(isRopePanel1)
    if isRopePanel1 then
        if self.ropePanelAnimation1 ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.ropePanelAnimation1, -1, nil, true);
            self.ropePanel1 = true;
            self.sound1 = true;
            self.sound2 = false;
        end;
    else
        if self.ropePanelAnimation1 ~= nil and self.playAnimation ~= nil then
            self:playAnimation(self.ropePanelAnimation1, 1, nil, true);
            self.ropePanel1 = false;
            self.sound1 = false;
            self.sound2 = true;
        end;
    end;
end;

function Romet:onLeave()
end;

function Romet:onEnter()
end;

function Romet:onDeactivate()
end;

function Romet:draw()
end;