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

------------------------------------------------------------------------------
--вес медальена
MEDAL_MASSA = 10.0;

MEDEL_HEIGHT_DIV3 = 10.0

--Количество монет
COINS_AMOUNT = 6;

function OnSceneLoad()
	play_field = miniGamePlayField;
    LoadData();
	play_field = nil;
    
    miniGameState = {};
   	miniGameState.delayPeriod = 90.0;
   	miniGameState.timeElapsed = 0.0;
   	miniGameState.delayPeriodPassed = false;
   	miniGameState.completed = false;
   	
   	miniGamePlayField:EnableRightMouseButtonProcessing(true);
   	
   	miniGamePlayField:IgnoreOnTestPoint('ZOOM_vesi');
   	miniGamePlayField:IgnoreOnTestPoint('tarelka_levaja');
   	miniGamePlayField:IgnoreOnTestPoint('tarelka_pravaja');
	miniGamePlayField:IgnoreOnTestPoint('koromislo');
	miniGamePlayField:IgnoreOnTestPoint('act_zoneLeft');
	miniGamePlayField:IgnoreOnTestPoint('act_zoneRight');
   	
   	Koromislo = miniGamePlayField:GetObject('koromislo');
   	LeftPlateTrigger = miniGamePlayField:GetObject('act_zoneLeft');
   	RightPlateTrigger = miniGamePlayField:GetObject('act_zoneRight');
    miniGamePlayField:GetObject('kamen_1'):SetVisible(false);
    miniGamePlayField:GetObject('radial_glow'):SetVisible(false);
   	
   	LeftMassa = 0;
   	RightMassa = 0 + MEDAL_MASSA;
   	
   	MoveRight = false
   	MoveLeft = false
   	
   	auxAttachInPlace(miniGamePlayField:GetObject('leftNode_act_zone01'), miniGamePlayField:GetObject('koromislo'));
   	auxAttachInPlace(miniGamePlayField:GetObject('rightNode_act_zone01'), miniGamePlayField:GetObject('koromislo'));
	auxAttachInPlace(miniGamePlayField:GetObject('act_zoneLeft'), miniGamePlayField:GetObject('tarelka_levaja'));
   	auxAttachInPlace(miniGamePlayField:GetObject('act_zoneRight'), miniGamePlayField:GetObject('tarelka_pravaja'));
   	
   	CoinsObj = {miniGamePlayField:GetObject('moneta_1'), miniGamePlayField:GetObject('moneta_2'), miniGamePlayField:GetObject('moneta_3'),
				miniGamePlayField:GetObject('moneta_4'), miniGamePlayField:GetObject('moneta_5'), miniGamePlayField:GetObject('moneta_6')};
	
	StartPos = {};
	IsActive= {};
	
	LeftPlateArray = {};
	RightPlateArray = {};
	
	movingLockedobjects = {};
	
	LeftHeight = 0;
	RightHeight = MEDEL_HEIGHT_DIV3;
	
	ObjMassa = {0.5, 3.0, 1.5, 2.0, 6.0, 1.0};
	
	for i = 1,COINS_AMOUNT do
		IsActive[i] = true;
		StartPos[i] = CoinsObj[i]:GetPosition();
	end
	
	TimerDoString('OnShowHelp()', 0.5, miniGamePlayField:GetTimer());
end

function OnSceneFree()
    FreeData()
end

------------------------------------------------------------------------------
function CreateTorchFireFibrillationEffector(object)
	local duration = 2.0;

	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.8);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *2.0/4.0, 1.0);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *3.0/4.0, 0.7);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *4.0/4.0, 1.0);

	fireFibrillationEffector:Play();
end

function auxAttachInPlace(object, parentObject)
	local scrX,scrY = object:GetScrX(), object:GetScrY();
	object:AttachTo(parentObject);
	object:SetScrXY(scrX, scrY);
end


function auxStartDtag(object)
    local Index = auxGetObjectIndex(object:GetName());
    
    if(miniGameState.completed == false) then
		if(IsActive[Index] == true) then
			auxTakeObject(object);
		else
			if(object:GetName() == auxGetLastName(LeftPlateArray)) then
				auxTakeObject(object);
				auxAttachInPlace(object, miniGamePlayField);
				auxPopBack(LeftPlateArray);
				IsActive[Index] = true;
				LeftHeight = LeftHeight - object:GetAnimation():GetHeight() / 3;
				LeftMassa = LeftMassa - ObjMassa[Index];
			else
				if(object:GetName() == auxGetLastName(RightPlateArray)) then
					auxTakeObject(object);
					auxAttachInPlace(object, miniGamePlayField);
					auxPopBack(RightPlateArray);
					IsActive[Index] = true;
					RightHeight = RightHeight - object:GetAnimation():GetHeight() / 3;
					RightMassa = RightMassa - ObjMassa[Index];
				end
			end
		end
	end
end

function auxTakeObject(object)
	DragObject = object;
	DragObject:SetZ(300.0);
end

function auxBallanceSound()
    local rand = math.random(1, 3)
    PlaySound('L13S0'..rand)
end

function auxStopDrag()
    if(DragObject ~= nil) then
		local Index = auxGetObjectIndex(DragObject:GetName());
		
		if (auxIsCollision(DragObject, LeftPlateTrigger) and miniGameState.completed == false) then
			DragObject:SetScrXY(LeftPlateTrigger:GetScrX(), LeftPlateTrigger:GetScrY() - LeftHeight);
			LeftMassa = LeftMassa + ObjMassa[Index];
			LeftHeight = LeftHeight + DragObject:GetAnimation():GetHeight() / 3;
			auxPushBack(LeftPlateArray, DragObject);
			DragObject:SetZ(150.0 + auxGetArrayLenght(LeftPlateArray));
			auxAttachInPlace(DragObject, miniGamePlayField:GetObject('tarelka_levaja'));
			IsActive[Index] = false;
			PlaySound('L13S00')
		else
			if(auxIsCollision(DragObject, RightPlateTrigger) and miniGameState.completed == false) then
				DragObject:SetScrXY(RightPlateTrigger:GetScrX(), RightPlateTrigger:GetScrY() - RightHeight);
				RightMassa = RightMassa + ObjMassa[Index];
				RightHeight = RightHeight + DragObject:GetAnimation():GetHeight() / 3;
				auxPushBack(RightPlateArray, DragObject);
				DragObject:SetZ(150.0 + auxGetArrayLenght(RightPlateArray));
				auxAttachInPlace(DragObject, miniGamePlayField:GetObject('tarelka_pravaja'));
				IsActive[Index] = false;
				PlaySound('L13S00')
			else
				local duration = 0.1;
				DragObject:Move(StartPos[Index].x - DragObject:GetX(), StartPos[Index].y - DragObject:GetY(), duration);
				DragObject:SetZ(100);
			end
		end		
		DragObject = nil;
    end
end

function auxDrag(MouseX, MouseY)
    if(DragObject ~= nil) then
        local X = MouseX - DragObject:GetScrX();
        local Y = MouseY - DragObject:GetScrY();
        DragObject:SetXY(DragObject:GetScrX() + X, DragObject:GetScrY() + Y);
    end
end

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

function auxIsCollision(objectPos, triggerPos)
	if	(math.abs(objectPos:GetScrX() - triggerPos:GetScrX()) < 100 and math.abs(objectPos:GetScrY() - triggerPos:GetScrY()) < 100) then
			return true
	end
	
	return false;
end

function auxPushBack(Array, Value)
	Lenght = table.getn(Array);
	Array[Lenght + 1] = Value;
end

function auxPopBack(Array)
	Array[table.getn(Array)] = nil;
end

function auxGetLastName(Array)
	local Index = table.getn(Array);
	
	if (Index == 0) then
		return "";
	end
	
	return Array[Index]:GetName();
end

function auxGetArrayLenght(Array)
	return table.getn(Array);
end

------------------------------------------------------------------------------
function auxBallanceUpdate(dt)
	local ballanse_koef = 0.001 * (dt * 40);
		
	local m_l = MoveLeft
	local m_r = MoveRigth
		
	if ((RightMassa - LeftMassa) / 10 > Koromislo:GetAngle()) then
	    --[[if KoromisloMove == false then
	        KoromisloMove = true
	        auxBallanceSound()
	    end]]
	    MoveRight = true
	    
		Koromislo:SetAngle(Koromislo:GetAngle() + ballanse_koef);
		if(Koromislo:GetAngle() > M_PI_OVER_4) then
			Koromislo:SetAngle(M_PI_OVER_4);
			MoveRight = false
		end
	else
	    MoveRight = false
	end
	if ((RightMassa - LeftMassa) / 10 < Koromislo:GetAngle()) then
	    --[[if KoromisloMove == false then
	        KoromisloMove = true
	        auxBallanceSound()
	    end]]
	    MoveLeft = true
	    
		Koromislo:SetAngle(Koromislo:GetAngle() - ballanse_koef);
		if(Koromislo:GetAngle() < -M_PI_OVER_4) then
			Koromislo:SetAngle(-M_PI_OVER_4);
			MoveLeft = false
		end
	else 
	    MoveLeft = false
	end
	if ((MoveRight == true and m_r == false) or (MoveLeft == true and m_l == false) or (m_r == true and MoveLeft == true) or (m_l == true and MoveRight == true)) and (MoveRight ~= MoveLeft) then
	    auxBallanceSound()
	end
	
	miniGamePlayField:GetObject('tarelka_levaja'):SetScrXY(miniGamePlayField:GetObject('leftNode_act_zone01'):GetScrX(),miniGamePlayField:GetObject('leftNode_act_zone01'):GetScrY());
	miniGamePlayField:GetObject('tarelka_pravaja'):SetScrXY(miniGamePlayField:GetObject('rightNode_act_zone01'):GetScrX(),miniGamePlayField:GetObject('rightNode_act_zone01'):GetScrY());
	
	
	if(Koromislo:GetAngle() < 0.01 and Koromislo:GetAngle() > -0.01) then
		if (RightMassa == LeftMassa and miniGameState.completed == false) then
			miniGameState.completed = true;
			miniGamePlayField:GetObject('radial_glow'):SetVisible(true);
			CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow'));
			auxShowButton(true)
		end
		miniGamePlayField:GetObject('kamen_1'):SetVisible(true);
	else
		miniGamePlayField:GetObject('kamen_1'):SetVisible(false);
	end
end

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

function OnShowHelp()
    miniGamePlayField:GetLevelScreen():ShowDialog(500, 300, GetTextString('MINIGAME_BALANCE_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)
    DefaultOnObjectMouseEnter(object)
end
function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(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/Balance/ZOOM_vesi.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('ZOOM_vesi',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/tarelka_levaja.png',0,0,185,391,1,0)
	anim:SetHotSpot(84,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('tarelka_levaja',anim,367,181,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/tarelka_pravaja.png',0,0,170,364,1,0)
	anim:SetHotSpot(98,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('tarelka_pravaja',anim,808,172,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/koromislo.png',0,0,488,120,1,0)
	anim:SetHotSpot(244,90)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('koromislo',anim,589,182,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_2.png',0,0,77,30,1,0)
	anim:SetHotSpot(38,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_2',anim,148,547,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_3.png',0,0,68,26,1,0)
	anim:SetHotSpot(34,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_3',anim,209,511,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_4.png',0,0,79,32,1,0)
	anim:SetHotSpot(39,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_4',anim,240,570,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_1.png',0,0,63,22,1,0)
	anim:SetHotSpot(31,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_1',anim,45,565,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_5.png',0,0,90,36,1,0)
	anim:SetHotSpot(45,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_5',anim,118,599,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/moneta_6.png',0,0,54,22,1,0)
	anim:SetHotSpot(27,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('moneta_6',anim,96,516,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	play_field:CreateObject('leftNode_act_zone01',anim,365,186,100,0,0.3,0.3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	play_field:CreateObject('rightNode_act_zone01',anim,810,177,100,0,0.3,0.3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	play_field:CreateObject('act_zoneLeft',anim,373,508,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	play_field:CreateObject('act_zoneRight',anim,795,480,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/kamen_0.png',0,0,37,61,1,0)
	anim:SetHotSpot(18,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('kamen_0',anim,599,37,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Balance/kamen_1.png',0,0,65,118,1,0)
	anim:SetHotSpot(32,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('kamen_1',anim,599,38,102,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('0xFF0000FF')
	play_field:CreateObject('radial_glow',anim,592,36,100,0,3,3)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
