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

------------------------------------------------------------------------------
FOOTMARK_AMOUNT = 5;

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:IgnoreOnTestPoint('fonn');
    miniGamePlayField:IgnoreOnTestPoint('rast1');
    miniGamePlayField:IgnoreOnTestPoint('rast2');
    
    miniGamePlayField:GetObject('fire3'):GetAnimation():Play();
	miniGamePlayField:GetObject('fire3_2'):GetAnimation():Play();
	
    Stones = {};
    StonesPos = {};
    local Flags = {};
    
    for i = 1, FOOTMARK_AMOUNT do
		Stones[i] = miniGamePlayField:GetObject("sled"..i);
		StonesPos[i] = Stones[i]:GetPosition();
		auxAttachInPlace(miniGamePlayField:GetObject("Tr"..i.."_01"), Stones[i]);
		miniGamePlayField:IgnoreOnTestPoint("Tr"..i.."_01");
		miniGamePlayField:GetObject("Tr"..i.."_01"):SetVisible(false);
		Flags[i] = false;
    end
    
    for i = 1, FOOTMARK_AMOUNT do
		local Index;
		repeat
			Index = math.random(FOOTMARK_AMOUNT);
		until Flags[Index] == false and (StonesPos[Index].x ~= Stones[i]:GetX() or i == 5)
		
		Flags[Index] = true;
		Stones[i]:SetXY(StonesPos[Index].x, StonesPos[Index].y);
    end
    
    miniGamePlayField:IgnoreOnTestPoint('radial_glow');
    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow'));
    miniGamePlayField:IgnoreOnTestPoint('radial_glow2');
    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow2'));
    
    CreateSheetEffector(miniGamePlayField:GetObject('rast1'));
    CreateSheet2Effector(miniGamePlayField:GetObject('rast2'));
    
	FireDust = CreateParticleSystemEntity('data/ParticleSystem/TorchSparks.psi', 'TorchSparksParticle');
	FireDust:AttachTo(miniGamePlayField:GetObject('fonn'));
	FireDust:SetZ(1000.0);
	
	CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow'));
	CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow2'));
end

function OnSceneFree()
    FreeData()
end

--// auxiliary functions -----------------------------------------------------
--// ...

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

function CreateTorchStepFibrillationEffector(object)
	local duration = 1.50;

	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.2);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *3.0/4.0, 0.8);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *4.0/4.0, 1.2);

	fireFibrillationEffector:Play();
end

function CreateSheetEffector(object)
	local duration = 7.00;

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

	fireFibrillationEffector:SetKey(_TRACK_A, 0.0, 0.0);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *1.0/4.0, -0.05);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *2.0/4.0, 0.01);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *3.0/4.0, -0.06);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *4.0/4.0, 0.02);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *5.0/4.0, -0.03);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *6.0/4.0, 0.00);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *7.0/4.0, -0.06);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *8.0/4.0, 0.00);
	
	fireFibrillationEffector:Play();
end

function CreateSheet2Effector(object)
	local duration = 6.00;

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

	fireFibrillationEffector:SetKey(_TRACK_A, 0.0, 0.0);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *1.0/4.0, -0.07);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *2.0/4.0, 0.025);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *3.0/4.0, -0.05);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *4.0/4.0, 0.01);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *5.0/4.0, -0.02);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *6.0/4.0, 0.01);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *7.0/4.0, -0.03);
	fireFibrillationEffector:SetKey(_TRACK_A, duration *8.0/4.0, 0.00);
	
	fireFibrillationEffector:Play();
end

function CreateTorchFireFibrillationEffector(object)
	local duration = 0.25;

	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.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 UpdateActiveFoot()
		local ActiveAmount = 0;
		
		for i = 1, FOOTMARK_AMOUNT do
			if (Stones[i]:GetX() == StonesPos[i].x and Stones[i]:GetY() == StonesPos[i].y) then
				if(miniGamePlayField:GetObject("Tr"..i.."_01"):IsVisible() == false) then
					miniGamePlayField:GetObject("Tr"..i.."_01"):SetVisible(true);
					CreateTorchStepFibrillationEffector(miniGamePlayField:GetObject("Tr"..i.."_01"));
				end
				ActiveAmount = ActiveAmount + 1;
			else
				miniGamePlayField:GetObject("Tr"..i.."_01"):SetVisible(false);
			end
		end
		
		if(ActiveAmount == FOOTMARK_AMOUNT) then
			miniGameState.completed = true;
		end
end

--// torch sparks generation disabling routine -------------------------------
function DisableFlowerDustGeneration()
	assert(FireDust ~= nil);
    FireDust:SetGeneration(false);
end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function OnUpdate(dt)
	
	UpdateActiveFoot();
	
	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()
end

function OnObjectMouseDown(object)
	
	 if(string.sub(object:GetName(), 1, string.len('act_')) == 'act_') then
		return;
	end
	
	if(miniGameState.completed == false) then
		if (SelectObj == nil) then
			SelectObj = object;
			SelectObj:GetAnimation():SetColor("0xCCFFFFCC");
		else
			if (math.abs(object:GetX() - SelectObj:GetPosition().x) == 130) then
				local Pos = SelectObj:GetPosition();
				SelectObj:GetAnimation():SetColor("0xFFFFFFFF");
				SelectObj:Move(object:GetX() - SelectObj:GetX(), object:GetY() - SelectObj:GetY(), 0.3);
				object:Move(Pos.x - object:GetX(), Pos.y - object:GetY(), 0.3);
				SelectObj = nil;
				UpdateActiveFoot();
			else
				if(SelectObj:GetName() == object:GetName())then
					SelectObj:GetAnimation():SetColor("0xFFFFFFFF");
					SelectObj = nil;
				else
					SelectObj:GetAnimation():SetColor("0xFFFFFFFF");
					SelectObj = object;
					SelectObj:GetAnimation():SetColor("0xCCFFFFCC");
				end
			end
		end
	end
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnMouseMove(previousPos, currentPos)
	 if FireDust ~= nil then
        FireDust:SetScrXY(currentPos.x, currentPos.y);
     end
end


function OnObjectMouseEnter(object)
	local objectName = object:GetName();
	
	if game_state.active_zone == nil and
	   string.sub(objectName, 1, string.len('act_zone01_')) == 'act_zone01_' then	--// act_zone01_flowerXX
		assert(FireDust ~= nil);
        FireDust:SetGeneration(true);
		TimerDoString('DisableFlowerDustGeneration()', 0.1, miniGamePlayField:GetTimer());
    end
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/Animals/fonn.png',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('fonn',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/rast1.png',0,0,259,281,1,0)
	anim:SetHotSpot(250,140)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('rast1',anim,1044,472,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/rast2.png',0,0,318,141,1,0)
	anim:SetHotSpot(185,140)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('rast2',anim,893,788,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/sled1.png',0,0,117,126,1,0)
	anim:SetHotSpot(58,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('sled1',anim,270,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/sled2.png',0,0,116,126,1,0)
	anim:SetHotSpot(58,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('sled2',anim,400,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/sled3.png',0,0,116,126,1,0)
	anim:SetHotSpot(58,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('sled3',anim,530,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/sled4.png',0,0,116,126,1,0)
	anim:SetHotSpot(58,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('sled4',anim,660,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/sled5.png',0,0,116,135,1,0)
	anim:SetHotSpot(58,72)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('sled5',anim,790,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Tr1_01.png',0,0,92,92,1,0)
	anim:SetHotSpot(46,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Tr1_01',anim,273,421,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Tr2_01.png',0,0,92,92,1,0)
	anim:SetHotSpot(46,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Tr2_01',anim,399,423,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Tr3_01.png',0,0,92,92,1,0)
	anim:SetHotSpot(46,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Tr3_01',anim,532,418,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Tr4_01.png',0,0,92,92,1,0)
	anim:SetHotSpot(46,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Tr4_01',anim,663,422,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Tr5_01.png',0,0,92,92,1,0)
	anim:SetHotSpot(46,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Tr5_01',anim,793,422,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(0)
	anim:SetColor('0xFFFF8800')
	play_field:CreateObject('radial_glow',anim,217,165,107,0,5,5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFF8800')
	play_field:CreateObject('radial_glow2',anim,866,186,110,0,5,5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,12)
	anim:SetHotSpot(32,32)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFF00')
	play_field:CreateObject('fire3',anim,203,-14,102,5,3.1,3.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/fire3.png',0,0,64,128,31,12)
	anim:SetHotSpot(32,32)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFF00')
	play_field:CreateObject('fire3_2',anim,867,1,101,-10,3.3,3.6)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Layer01.png',0,0,128,134,1,0)
	anim:SetHotSpot(64,67)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Layer01',anim,156,209,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Animals/Layer02.png',0,0,134,134,1,0)
	anim:SetHotSpot(67,67)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Layer02',anim,902,227,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/act_zone01.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_01',anim,187,150,600,0,0.5,1.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/act_zone01.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_02',anim,891,136,600,0,0.6,1.5)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
