

let m_flag_gameover = false;;



function system_Title(){

	let flag_start = false;;
	Logo();
	BackScreen();
	Screen();

	loop(5){Rock();}
	while(!g_Stick[_KEY_SHOT]){
		yield;
	}
	flag_start = true;
	SetCommonDataEx("SPHERIA_PLAYER", "PLAYER_START", true);
	loop(100){yield;}

	task Logo(){

		let obj = REMY_CreateEffectObject(img_Words, 4, 7);
		let rect = system_GetGraphicRect("WORDS_TITLE");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, GetCenterX(), GetCenterY());

		ascent(i in 0..8){
			ObjEffect_SetScale(obj, 1.3, 1.3);
			REMY_SetObjectVertexSquare(obj, rect, 0, 0, 255 * (i/7), 255, 255, 255);
			loop(4){ yield; }
		}
		while(!flag_start){
			yield;
		}
		Obj_Delete(obj);
	}


	task BackScreen(){

		let obj = REMY_CreateEffectObject(img_Sphere, 4, 6);
		let rect = system_GetGraphicRect("SPHERE_WHITE");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, GetCenterX(), GetCenterY());

		ascent(i in 0..8){
			ObjEffect_SetScale(obj, 12, 6 * (i/7));
			REMY_SetObjectVertexSquare(obj, rect, 0, 0, 120 * (i/8), 50, 50, 50);
			loop(4){ yield; }
		}
		while(!flag_start){
			yield;
		}
		Obj_Delete(obj);


	}

	task Screen(){

		let u_count = 0;
		let v_count = 0;

		let obj = REMY_CreateEffectObject(img_BG01, 4, 4);
		let rect = system_GetGraphicRect("BG_TOWN01");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, 0, 0);

		ascent(i in 0..4){
			ObjEffect_SetVertexXY(obj, i, [GetClipMinX(),GetClipMaxX()][i%2],[GetClipMinY(),GetClipMaxY()][trunc(i/2)]);
			ObjEffect_SetVertexUV(obj, i, u_count +  [0,GetClipMaxX()- GetClipMinX()][i%2], v_count + [0, GetClipMaxY()-GetClipMinY()][trunc(i/2)]);
			ObjEffect_SetVertexColor(obj, i, 255,200,200,200);
		}

		ascent(i in 0..8){
			REMY_SetObjectColorSquare(obj, 255 * (i/8), 250, 250, 250);
			loop(4){ yield; }
		}

		let counter = 0;
		while(!flag_start){

			if(u_count<=0){ v_count ++; }
			if(u_count>=(1024 - GetClipMaxX())){ v_count --; }
			if(v_count<=0){ u_count --; }
			if(v_count>=(960 - GetClipMaxY())){ u_count ++; }

			ascent(i in 0..4){
				ObjEffect_SetVertexUV(obj, i, u_count +  [0,GetClipMaxX()- GetClipMinX()][i%2], v_count + [0, GetClipMaxY()-GetClipMinY()][trunc(i/2)]);
			}
			yield;

		}

		descent(i in 0..8){
			REMY_SetObjectColorSquare(obj, 255 * (i/8), 250, 250, 250);
			loop(4){ yield; }
		}

		Obj_Delete(obj);
	}



	task Rock(){

		let obj = REMY_CreateEffectObject(img_TitleRock, 4, 6);
		let rect = system_GetGraphicRect("ROCK01");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, rand(GetClipMinX(), GetClipMaxX()), GetClipMaxY()+30);
		REMY_SetObjectVertexSquare(obj, rect, 0, 0, 255, 255, 255, 255);

		ObjEffect_SetScale(obj, 1.3, 1.3);
		Obj_SetSpeed(obj, rand(0.6, 3));
		Obj_SetAngle(obj, 270);

		let counter = 0;
		while(!flag_start){
			counter++;
			rect = system_GetGraphicRect(["ROCK01","ROCK02","ROCK03","ROCK04"][trunc(counter/4)%4]);
			REMY_SetObjectVertexSquare(obj, rect, 0, 0, 255, 255, 255, 255);
			if(Obj_GetY(obj)<-10){Rock(); break;}
			yield;
		}
		Obj_Delete(obj);
		
	}


}//end function


task once_system_TaskGameOver(){

	m_flag_gameover = false;
	while(GetCommonDataDefaultEx("SPHERIA_PLAYER", "PLAYER_LIFE", 1) >0 ){yield;}
	m_flag_gameover = true;
	StopPlayer();
	loop(20){yield;}
	Screen();
	Word();

	loop(100){yield;}
	if(!Select()){Retry();}
	else{
		SetClearFrameEffectEnable(false);
		ClearStage();
	}

	task StopPlayer(){
		let x = GetPlayerX();
		let y = GetPlayerY();
		loop{
			SetPlayerX(x);
			SetPlayerY(y);
			yield;
		}
	}


	task Screen(){

		let obj = REMY_CreateEffectObject(img_Sphere, 4, 7);
		let rect = system_GetGraphicRect("SPHERE_WHITE");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, GetCenterX(), GetCenterY());

		ascent(i in 0..8){
			ObjEffect_SetScale(obj, 20, 5 * (i/7));
			REMY_SetObjectVertexSquare(obj, rect, 0, 0, 200 * (i/8), 50, 50, 50);
			loop(4){ yield; }
		}
//		Obj_Delete(obj);


	}

	task Word(){
		let obj = REMY_CreateEffectObject(img_Words, 20, 7);
		let rect = system_GetGraphicRect("WORDS_GAMEOVER");
		ObjEffect_SetRenderState(obj, ALPHA);
		Obj_SetPosition(obj, GetCenterX()-(rect[2]-rect[0]), GetCenterY() - (rect[3]-rect[1]));
		ObjEffect_SetScale(obj, 2, 2);

		ascent(i in 0..20){
			ObjEffect_SetVertexXY(obj, i, (rect[2]-rect[0])*(trunc(i/2)/9), (rect[3]-rect[1])*(i%2));
			ObjEffect_SetVertexUV(obj, i, rect[0] + (rect[2]-rect[0])*(trunc(i/2)/9), rect[1] + (rect[3]-rect[1])*(i%2) );
			ObjEffect_SetVertexColor(obj, i, 255, 255, 255, 255);
		}

		ascent(j in 0..21){
			ascent(i in 0..20){
				ObjEffect_SetVertexXY(obj, i, (rect[2]-rect[0])*(trunc(i/2)/9), REMY_GetNumMin((rect[3]-rect[1])*(i%2)*((j+i)/20), rect[3]-rect[1]));
			}
			loop(4){ yield; }
		}
	}

	function Select()
	{
		let selection = false;
		let selectable = false;
		let movable = false;

		sel(false);
		sel(true);

		loop
		{
			if(!g_Stick[_KEY_SHOT]){ selectable = true; }
			if(g_Stick[_KEY_SHOT] && selectable){ break; }
			if(movable && REMY_GetStickY() != 0){ selection = !selection; movable = false;}
			else if(REMY_GetStickY() == 0){ movable = true; }
			yield;
		}
		return selection;

		task sel(let type)
		{
			let counter = 0;
			let obj = REMY_CreateEffectObject(img_Words, 4, 7);
			let rect = system_GetGraphicRect(["WORDS_TO_TITLE", "WORDS_END_GAME"][type]);
			ObjEffect_SetRenderState(obj, ALPHA);
			REMY_SetObjectVertexSquare(obj, rect, 0, 0, 255, 255, 255, 255);
			Obj_SetPosition(obj, GetCenterX(), GetCenterY() + [70, 100][type]);
			loop
			{
				let selected = (selection == type);
				if(selected){ counter ++; }
				else{counter = 0;}

				ObjEffect_SetScale(obj, [1, 1.5 + 0.2 * sin(counter*4)][selected], [1, 1.5][selected]);
				yield;
			}
		}
		
	}


}//end task






/*
task system_TaskCreateEnemyShot(let x, let y, let speed, let angle, let graphic){

	let obj = REMY_CreateShotObject(x, y, speed, angle, graphic, 10);
	let flag_field_inside = true;
	let old_x; 
	let old_y; 
	let old_angle; 


	while(!Obj_BeDeleted(obj)){
		flag_field_inside = !REMY_IsOutOfClip(Obj_GetX(obj), Obj_GetY(obj),0);
		old_angle = Obj_GetAngle(obj); 
		yield;
	}
	if(flag_field_inside){
		PlaySE(se_Reflect);
		system_AddWeight(-10);
		loop(10){
			system_TaskWeightItemLost(system_GetSphereX(), system_GetSphereY(), old_angle+180);
		}
		yield;
	}


}//end task

*/


function system_IsGameOver(){

	return m_flag_gameover;

}//end function
