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

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

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

    miniGamePlayField:IgnoreOnTestPoint('fon');
    
    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

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

--количество букв
LETERS_AMOUNT = 8

function auxCreatePazzle()
    LetersObj = {  	miniGamePlayField:GetObject("C"), miniGamePlayField:GetObject("A"), miniGamePlayField:GetObject("T"),
					miniGamePlayField:GetObject("A2"), miniGamePlayField:GetObject("L"), miniGamePlayField:GetObject("I"),
					miniGamePlayField:GetObject("N"), miniGamePlayField:GetObject("A3") };
    
    Leters = {1, 2, 3, 2, 4, 5, 6, 2};
    
    LetersPos = {};
    LetersStartPos = {};
    LeterIsActive = {};
    BoxEnable = {};
    
    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject("radial_glow"));
    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject("radial_glow2"));
    
    for i = 1,LETERS_AMOUNT do
        LeterIsActive[i] = true;
        BoxEnable[i] = false;
        LetersPos[i] = LetersObj[i]:GetPosition();
        LetersObj[i]:SetZ(200.0);

        local x;
        local y;
        local delta = 100;
        repeat
            local f_cont = true;
            x = math.random(SCREEN_WIDTH - 210) + 170;
            y = math.random(150) + 550;
            for k = 1, i - 1 do
                if math.abs(LetersStartPos[k].x - x) < delta and math.abs(LetersStartPos[k].y - y) < delta then
                    f_cont = false;
                    break;
                end
            end
        until f_cont
        
		LetersObj[i]:SetXY(x, y);
		LetersStartPos[i] = LetersObj[i]:GetPosition();
    end
end

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

function auxStopDrag()
    if(DragObject ~= nil) then
        DragObject:SetZ(200.0);
        
        local Index = auxGetObjectIndex(DragObject:GetName());
        local Flag = false;
 
        for i = 1,LETERS_AMOUNT do
			if (Leters[i] == Leters[Index] and  BoxEnable[i] == false) then
				if (math.abs(DragObject:GetX() - LetersPos[i].x) < 30 and math.abs(DragObject:GetY() - LetersPos[i].y) < 30) then --если мы над нашим местом
					DragObject:Move(LetersPos[i].x - DragObject:GetX(), LetersPos[i].y - DragObject:GetY(), 0.2);
					miniGamePlayField:IgnoreOnTestPoint(DragObject:GetName());
					DragObject:SetZ(100.0);
					LeterIsActive[Index] = false;
					BoxEnable[i] = true;
					Flag = true;
					PlaySound('L20S00')
					if (OnLevelComplite()) then
						miniGameState.completed = true;
					end
				end
			end
        end
        
        if (Flag == false) then
			DragObject:Move(LetersStartPos[Index].x - DragObject:GetX(), LetersStartPos[Index].y - DragObject:GetY(), 0.1);
        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,LETERS_AMOUNT do
        if (LetersObj[i]:GetName() == name) then
            return i;
        end
    end
    
    return nil;
end 

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


function CreateTorchFireFibrillationEffector(object)
	local duration = 0.3;

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

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

	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_SHIP_HELP'), GetTextString('MINIGAME_HELP_DIALOG_TITLE'));
end

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

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

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end
function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectRightMouseDown(object)
end
function OnObjectRightMouseUp(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/Ship/fon.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('fon',anim,512,382,99,0,1,1)
--[[	local anim = CreateAnimation('data/Texture/Minigames/Ship/2a_a_new.png',0,0,1024,268,1,0)
	anim:SetHotSpot(512,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('2a_a_new',anim,512,132,120,0,1,1)]]
	local anim = CreateAnimation('data/Texture/Minigames/Ship/C.png',0,0,78,93,1,0)
	anim:SetHotSpot(39,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('C',anim,244,343,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/A.png',0,0,90,95,1,0)
	anim:SetHotSpot(45,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('A',anim,336,333,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/T.png',0,0,79,95,1,0)
	anim:SetHotSpot(39,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('T',anim,411,314,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/I.png',0,0,52,93,1,0)
	anim:SetHotSpot(26,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('I',anim,661,282,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/L.png',0,0,80,92,1,0)
	anim:SetHotSpot(40,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('L',anim,586,296,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/A.png',0,0,90,95,1,0)
	anim:SetHotSpot(45,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('A2',anim,499,308,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/N.png',0,0,94,100,1,0)
	anim:SetHotSpot(47,50)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('N',anim,736,270,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/A.png',0,0,90,95,1,0)
	anim:SetHotSpot(45,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('A3',anim,844,257,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Ship/kamat1_01.png',0,0,451,502,1,0)
	anim:SetHotSpot(225,251)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('kamat1_01',anim,129,57,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFEE00')
	play_field:CreateObject('radial_glow',anim,161,116,107,0,7,7)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFEE00')
	play_field:CreateObject('radial_glow2',anim,625,-4,107,0,7,7)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
