dofile('data/Scripts/Minigames/minigameScreen.lua');
-- miniGameScreen

------------------------------------------------------------------------------

function OnSceneLoad()
	play_field = miniGamePlayField;
    LoadData();
	play_field = nil;

    miniGamePlayField:IgnoreOnTestPoint('Bg1_01');
    miniGamePlayField:IgnoreOnTestPoint('radial_glow');
	--miniGamePlayField:GetObject('fire3'):SetOpacity(0.2);
    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow'));
    
    
    miniGameState = {};
   	miniGameState.delayPeriod = 90.0;
   	miniGameState.timeElapsed = 0.0;
   	miniGameState.delayPeriodPassed = false;
   	miniGameState.completed = false;
    auxCreatePazzle();
    
    TimerDoString('OnShowHelp()', 0.5, miniGamePlayField:GetTimer());
end

function OnSceneFree()
    FreeData()
end
------------------------------------------------------------------------------

function auxCreatePazzle()
    Elements = {miniGamePlayField:GetObject("part1_01"), miniGamePlayField:GetObject("part1_02"), miniGamePlayField:GetObject("part1_03"),
                miniGamePlayField:GetObject("part1_04"), miniGamePlayField:GetObject("part1_05"), miniGamePlayField:GetObject("part1_06"),
                miniGamePlayField:GetObject("part1_07"), miniGamePlayField:GetObject("part1_08"), miniGamePlayField:GetObject("part1_09"),
                miniGamePlayField:GetObject("part1_10"), miniGamePlayField:GetObject("part1_11"), miniGamePlayField:GetObject("part1_12"),
                miniGamePlayField:GetObject("part1_13") };
    
    ElementsPos = {};
    ElementsIsActive = {};
    
    OBJECTS_AMOUNT = 13
    
    for i = 1,OBJECTS_AMOUNT do
        ElementsIsActive[i] = true;
        ElementsPos[i] = Elements[i]:GetPosition();
        Elements[i]:GetAnimation():SetColor('0xFF333333');
        Elements[i]:SetXY(math.random(600) + 300, math.random(350) + 200);
        Elements[i]:SetZ(120.0 + i);
    end
    top_z = Elements[OBJECTS_AMOUNT]:GetZ();
end

function auxStartDtag(object)
    Index = auxGetObjectIndex(object:GetName());
    
    if(ElementsIsActive[Index] == true) then
        DragObject = object;
        top_z = top_z + 1;
        DragObject:SetZ(top_z);
        MousePos = GetMousePos();
        dx = MousePos.x - DragObject:GetX();
        dy = MousePos.y - DragObject:GetY();
    end
end

function auxStopDrag()
    if(DragObject ~= nil) then
        Index = auxGetObjectIndex(DragObject:GetName());
        if (Index ~= nil) then   
            if (math.abs(DragObject:GetX() - ElementsPos[Index].x) < 30 and math.abs(DragObject:GetY() - ElementsPos[Index].y) < 30) then --если мы над нашим местом
                DragObject:Move(ElementsPos[Index].x - DragObject:GetX(), ElementsPos[Index].y - DragObject:GetY(), 0.2);
                miniGamePlayField:IgnoreOnTestPoint(DragObject:GetName());
                DragObject:GetAnimation():SetColor('0xFF000000');
                DragObject:SetZ(100.0);
                ElementsIsActive[Index] = false;
                
                if (OnLevelComplite()) then
                    miniGameState.completed = true;
                    auxShowButton(true);
                end
            end
        end
        
        DragObject = nil;
        dx = nil;
        dy = nil;
    end
end

function auxDrag(MouseX, MouseY)
    if(DragObject ~= nil) then
        local X = MouseX - DragObject:GetX() - dx;
        local Y = MouseY - DragObject:GetY() - dy;
        DragObject:Move(X, Y, 0.0);
    end
end


function auxGetObjectIndex(name)
    for i = 1,13 do
        if (Elements[i]:GetName() == name) then
            return i;
        end
    end
    
    return nil;
end 

function OnLevelComplite()
    for i = 1,13 do
        if (ElementsIsActive[i]) then
            return false;
        end;
    end 
    
    return true;
end


function CreateTorchFireFibrillationEffector(object)
	local duration = 0.25;

	fireFibrillationEffector = CreateAnimationEffector();
	fireFibrillationEffector:AttachTo(object);
	fireFibrillationEffector:SetMode(_ANIM_LOOP);

	fireFibrillationEffector:SetKey(_TRACK_O, 0.0, 0.6);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *1.0/4.0, 0.95);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *2.0/4.0, 1.0);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *3.0/4.0, 0.90);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *4.0/4.0, 1.0);

	fireFibrillationEffector:Play();
end

------------------------------------------------------------------------------

function OnUpdate(dt)
    miniGameState.timeElapsed = miniGameState.timeElapsed + dt;
	auxUpdateProgressBar(miniGameState.timeElapsed > miniGameState.delayPeriod and 1.0 or miniGameState.timeElapsed / miniGameState.delayPeriod);
    if ((miniGameState.completed or miniGameState.timeElapsed > miniGameState.delayPeriod) and  miniGameState.delayPeriodPassed == false)  then
        miniGameState.delayPeriodPassed = true;
        auxShowButton(miniGameState.completed);
    end
end

function OnShowHelp()
    miniGamePlayField:GetLevelScreen():ShowDialog(500, 300, GetTextString('MINIGAME_PUZZLE_HELP'), GetTextString('MINIGAME_HELP_DIALOG_TITLE'));
end

function OnObjectMouseDown(object)
    auxStartDtag(object);
end
function OnObjectMouseUp(object)
    auxStopDrag();
end

function OnObjectRightMouseDown(object)
end
function OnObjectRightMouseUp(object)
end

function OnMouseMove(previous_pos, current_pos)
    auxDrag(current_pos.x, current_pos.y);
end

function OnObjectMouseEnter(object)
end
function OnObjectMouseLeave(object)
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData()
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/Bg1_01.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Bg1_01',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_01.png',0,0,176,200,1,0)
	anim:SetHotSpot(89,89)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_01',anim,243,422,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_02.png',0,0,247,166,1,0)
	anim:SetHotSpot(124,124)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_02',anim,311,304,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_03.png',0,0,175,84,1,0)
	anim:SetHotSpot(87,87)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_03',anim,300,203,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_04.png',0,0,180,91,1,0)
	anim:SetHotSpot(89,89)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_04',anim,442,205,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_05.png',0,0,291,274,1,0)
	anim:SetHotSpot(146,146)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_05',anim,385,407,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_06.png',0,0,254,197,1,0)
	anim:SetHotSpot(127,127)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_06',anim,514,462,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_07.png',0,0,280,179,1,0)
	anim:SetHotSpot(140,140)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_07',anim,742,491,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_08.png',0,0,251,181,1,0)
	anim:SetHotSpot(126,126)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_08',anim,667,445,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_09.png',0,0,207,134,1,0)
	anim:SetHotSpot(104,104)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_09',anim,496,275,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_10.png',0,0,229,127,1,0)
	anim:SetHotSpot(114,114)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_10',anim,622,336,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_11.png',0,0,170,215,1,0)
	anim:SetHotSpot(85,85)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_11',anim,770,271,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_12.png',0,0,181,129,1,0)
	anim:SetHotSpot(90,90)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_12',anim,596,204,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/MapPazzle/part1_13.png',0,0,187,142,1,0)
	anim:SetHotSpot(93,93)
	anim:SetBlendMode(3)
	anim:SetColor('0xFF000000')
	play_field:CreateObject('part1_13',anim,735,204,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xCCFFCC44')
	play_field:CreateObject('radial_glow',anim,135,109,400,0,8,8)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,10)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(0)
	anim:SetColor('0x20FFEE00')
	play_field:CreateObject('fire3',anim,167,92,300,-20,0.6,0.6)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
