
.namespace s15_c01

gp_calecount = 0
gp_warning = 1
function OnEnable()
	Closeup.OnEnable("MiniGame")
	Reset()
	if DebugOptions.DebugMode == true then
	 InventoryManager.AddInInventory("InventoryCorde")
	end 
end


function Reset()
	dragged=false
	gp_calecount = 0
	gp_warning = 1
	cale1posinix=1178
	cale1posiniy=192
	cale2posinix=952
	cale2posiniy=49
	cale3posinix=961
	cale3posiniy=204
	cale4posinix=1081
	cale4posiniy=534
	cale5posinix=936
	cale5posiniy=319
	cale6posinix=931
	cale6posiniy=528
	cale1.Position_X=cale1posinix
	cale1.Position_Y=cale1posiniy
	cale2.Position_X=cale2posinix
	cale2.Position_Y=cale2posiniy
	cale3.Position_X=cale3posinix
	cale3.Position_Y=cale3posiniy
	cale4.Position_X=cale4posinix
	cale4.Position_Y=cale4posiniy
	cale5.Position_X=cale5posinix
	cale5.Position_Y=cale5posiniy
	cale6.Position_X=cale6posinix
	cale6.Position_Y=cale6posiniy
	
	tabcaleref={2,2,2,2,2,2,1,2,2,2,2,2,
			    2,2,2,2,0,1,1,2,0,2,2,2,
	   		    0,0,1,1,1,1,1,0,0,0,2,2,
			    0,2,2,1,0,2,0,2,0,0,0,2,
			    0,0,0,1,1,2,2,2,2,0,0,2,
			    2,0,2,2,1,1,1,2,2,2,0,2,
			    2,0,2,0,2,0,1,2,0,0,0,2,
			    0,0,0,0,0,1,1,0,0,0,0,0,
			    0,0,0,0,2,2,1,1,2,2,2,2,
			    2,2,0,0,0,2,2,1,1,0,2,2,
			    2,2,2,0,2,2,2,0,1,2,2,2,
			    2,2,0,0,2,2,2,1,1,2,2,2}
			 
	tabcale={2,2,2,2,2,2,0,2,2,2,2,2,
			 2,2,2,2,0,0,0,2,0,2,2,2,
			 0,0,0,0,0,0,0,0,0,0,2,2,
			 0,2,2,0,0,2,0,2,0,0,0,2,
			 0,0,0,0,0,2,2,2,2,0,0,2,
			 2,0,2,2,0,0,0,2,2,2,0,2,
			 2,0,2,0,2,0,0,2,0,0,0,2,
			 0,0,0,0,0,0,0,0,0,0,0,0,
			 0,0,0,0,2,2,0,0,2,2,2,2,
			 2,2,0,0,0,2,2,0,0,0,2,2,
			 2,2,2,0,2,2,2,0,0,2,2,2,
			 2,2,0,0,2,2,2,0,0,2,2,2}
			 
			 
	longcarre=62
	valdecalage = 10
	calestate={0,0,0,0,0,0} --permet de savoir si la cale est placé ou pas
	calepos={0,0,0,0,0,0} --permet de savoir quel est la position la cale
	icale=1 -- var qui parcourt les tab pour comparer
	col=0 -- quel est la colonne que l'on a touché
	ligne=0 -- quel est la ligne que l'on a touché
	lastcale=0 -- quel est l'emplacement de cale que l'on a touché (on l'obtient grace à col et ligne)
	deplace=0 -- permet de savoir si on vient de bouger une cale.
	won=false -- permet de savoir on a gagné le mini-jeu, utile pour lancer le levelmanager une fois
end

function OnUpdate()
	
	-- OPLog.Log("col"..col)
	-- OPLog.Log("ligne"..ligne)
	-- OPLog.Log("lastcale"..lastcale)
	--OPLog.Log("calestate0"..calestate[1])
	
	cursorpos = GetCenter(Cursor)
	
	if dragged then
		if piecedragged.Name=="cale1" then
			piecedragged.Position_X=cursorpos.x - 41
			piecedragged.Position_Y=cursorpos.y - 110
			if calestate[1]==1 then
				tabcale[calepos[1]]=0
				tabcale[calepos[1]-12]=0
				calestate[1]=0
				gp_calecount=gp_calecount-1
			end
		elseif piecedragged.Name=="cale2" then
			piecedragged.Position_X=cursorpos.x - 110
			piecedragged.Position_Y=cursorpos.y - 110
			if calestate[2]==1 then
				tabcale[calepos[2]]=0
				tabcale[calepos[2]-1]=0
				tabcale[calepos[2]-12-1]=0
				calestate[2]=0
				gp_calecount=gp_calecount-1
			end
		elseif piecedragged.Name=="cale3" then
			piecedragged.Position_X=cursorpos.x - 160
			piecedragged.Position_Y=cursorpos.y - 110
			if calestate[3]==1 then
				tabcale[calepos[3]]=0
				tabcale[calepos[3]-1]=0
				tabcale[calepos[3]-12-1]=0
				tabcale[calepos[3]-12-2]=0
				calestate[3]=0
				gp_calecount=gp_calecount-1
			end
		elseif piecedragged.Name=="cale4" then
			piecedragged.Position_X=cursorpos.x - 100
			piecedragged.Position_Y=cursorpos.y - 180
			if calestate[4]==1 then
				tabcale[calepos[4]]=0
				tabcale[calepos[4]-12]=0
				tabcale[calepos[4]-24]=0
				tabcale[calepos[4]-24-1]=0
				tabcale[calepos[4]-24+1]=0
				calestate[4]=0
				gp_calecount=gp_calecount-1
			end
		elseif piecedragged.Name=="cale5" then
			piecedragged.Position_X=cursorpos.x - 180
			piecedragged.Position_Y=cursorpos.y - 180
			if calestate[5]==1 then
				tabcale[calepos[5]]=0
				tabcale[calepos[5]-12]=0
				tabcale[calepos[5]-12-1]=0
				tabcale[calepos[5]-12-2]=0
				tabcale[calepos[5]-24-2]=0
				calestate[5]=0
				gp_calecount=gp_calecount-1
			end
		elseif piecedragged.Name=="cale6" then
			piecedragged.Position_X=cursorpos.x - 110
			piecedragged.Position_Y=cursorpos.y - 180
			if calestate[6]==1 then
				tabcale[calepos[6]]=0
				tabcale[calepos[6]-1]=0
				tabcale[calepos[6]-12]=0
				tabcale[calepos[6]-24]=0
				tabcale[calepos[6]-24-1]=0
				calestate[6]=0
				gp_calecount=gp_calecount-1
			end	
		end
		deplace=1
	end
		
		
	

	if  tabcale[7]==1 and tabcale[19]==1 and tabcale[18]==1 and tabcale[31]==1 and tabcale[30]==1 and tabcale[29]==1 and
		tabcale[28]==1 and tabcale[27]==1 and tabcale[40]==1 and tabcale[52]==1 and tabcale[53]==1 and tabcale[65]==1 and 
		tabcale[66]==1 and tabcale[67]==1 and tabcale[79]==1 and tabcale[90]==1 and tabcale[91]==1 and tabcale[103]==1 and
		tabcale[104]==1 and tabcale[116]==1 and tabcale[117]==1 and tabcale[129]==1 and tabcale[141]==1 and tabcale[140]==1 then
		
		Itsawin()
	elseif  tabcale[7]==1 and tabcale[19]==1 and tabcale[18]==1 and tabcale[17]==1 and tabcale[31]==1 and tabcale[30]==1 and 
		tabcale[29]==1 and tabcale[28]==1 and tabcale[27]==1 and tabcale[40]==1 and tabcale[52]==1 and tabcale[53]==1 and 
		tabcale[65]==1 and tabcale[66]==1 and tabcale[78]==1 and tabcale[90]==1 and tabcale[91]==1 and tabcale[92]==1 and
		tabcale[104]==1 and tabcale[116]==1 and tabcale[117]==1 and tabcale[129]==1 and tabcale[141]==1 and tabcale[140]==1 then
		
		Itsawin()
	elseif  tabcale[7]==1 and tabcale[19]==1 and tabcale[18]==1 and tabcale[17]==1 and tabcale[31]==1 and tabcale[30]==1 and 
		tabcale[29]==1 and tabcale[28]==1 and tabcale[27]==1 and tabcale[40]==1 and tabcale[52]==1 and tabcale[53]==1 and 
		tabcale[65]==1 and tabcale[66]==1 and tabcale[67]==1 and tabcale[79]==1 and tabcale[91]==1 and tabcale[103]==1 and
		tabcale[104]==1 and tabcale[116]==1 and tabcale[117]==1 and tabcale[129]==1 and tabcale[141]==1 and tabcale[140]==1 then
		
		Itsawin()
	elseif gp_calecount == 6 and gp_warning == 1 then
			SequencePlayer.PlaySequence("GP_1509",nil,nil)
			gp_warning = 0
	end
	
	if gp_calecount < 6 then 
		gp_warning = 1
	end
	
	
	
end	

function Itsawin()
	PlaySoundEffect("int_closeupwin")
	if won==false then
		NavigationManager.SwitchTo(nil,"s16_SearchSite")
		won=true
	end
end

function OnEnter()
	SequencePlayer.Enabled = true
	if InventoryCorde.Equipped == false and SampleLevel.IsEqual("s15_c01_cale",false) then		
		SequencePlayer.PlaySequence("GP_1507",self,nil)
		return true
	else
		SequencePlayer.PlaySequence("GP_1508",self,nil)
		
	end
end

function OnSequenceEnd(dialogue)
	if dialogue == "GP_1505" then
		SequencePlayer.PlaySequence("AI_"..GetCurrentManager().Name,s15_c01,nil,"help")
		return true
	end
	if dialogue == "GP_1507" then
		NavigationManager.SwitchTo(nil,"s15_SearchSite")
	end
end

function OnPickObject(objname)

end

function OnSearchEnd(typesearch)

end

function CondSpecificToFinish() 

end