let player_select_End = false;
let player_select_Type = 0;
task TPlayerScene(){
	let bEndTitleScene = false;

	let objTitleText = ObjText_Create();
	ObjText_SetFontColorTop(objTitleText, 255, 255, 255);
	ObjText_SetFontColorBottom(objTitleText, 100, 100, 100);
	Obj_SetRenderPriority(objTitleText, 0.3);
	ObjText_SetFontType(objTitleText, "Russell Square Regular");
	ObjText_SetFontBorderWidth(objTitleText, 3);
	ObjText_SetFontBorderColor(objTitleText, 0,0,0);
	ObjText_SetFontBorderType(objTitleText, BORDER_FULL);
	ObjText_SetText(objTitleText, "Select Player");
	ObjText_SetFontSize(objTitleText, 30);
	ObjText_SetFontBold(objTitleText, true);
	ObjRender_SetX(objTitleText, 48);
	ObjRender_SetY(objTitleText, 48);
	controlofthefuckingtitle;
	task controlofthefuckingtitle{
		let ta = 0;
		ascent(a in -30..1){
			ObjRender_SetX(objTitleText, 58+640-640*sin(90-90/30*a));
			yield;
		}
		while(!bEndTitleScene){
			ObjRender_SetX(objTitleText, 58+5*sin(ta));
			ta++;
			yield;
		}
		ascent(a in 1..31){
			ObjRender_SetX(objTitleText, 58+640*sin(90-90/30*a)-640);
			yield;
		}
		Obj_Delete(objTitleText);
	}

	let selectIndex = 0;
	task TMenuItem(let index, let mx, let my, let text)
	{
		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
			ObjText_SetFontColorBottom(obj, 100, 100, 100);
			ObjText_SetFontType(obj, "Russell Square Regular");
			ObjText_SetFontBorderColor(obj, 0,0,0);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontSize(obj, 15);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 29);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my-selectIndex*15+index*15, text);
		let t = rand(0,360);
		ascent(a in -30..1){
			ObjRender_SetX(objText, mx+640-640*sin(90-90/30*a));
			yield;
		}
		let zy = 0;
		while(!bEndTitleScene)
		{
			if(index == selectIndex){
				ObjText_SetFontColorTop(objText, 255, 255, 255);
				ObjText_SetFontColorBottom(objText, 255, 100, 100);
			}
			else{
				ObjText_SetFontColorTop(objText, 255, 255, 255);
				ObjText_SetFontColorBottom(objText, 100, 100, 100);
			}

			ObjRender_SetAlpha(objText,255);
			if(zy != selectIndex){
				ObjRender_AddPatternA1(objText,mx, my-selectIndex*15+index*15,5);
				zy = selectIndex;
			}

			ascent(a in 0..5){
				if(my-selectIndex*15+index*15 <= my-(a+2)*15 && my-selectIndex*15+index*15 > my-(a+3)*15){
					ObjRender_SetAlpha(objText,255/5*(5-a));
				}
				else if(my-selectIndex*15+index*15 <= my-(5+2)*15){
					ObjRender_SetAlpha(objText,0);
				}
			}

			yield;
		}
		ascent(a in 1..31){
			ObjRender_SetX(objText, mx+640*sin(90-90/30*a)-640);
			yield;
		}
		Obj_Delete(objText);
	}
	task TTextBox(x,y,x2,y2, let text){
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,lineobj);
		ObjSpriteList2D_SetSourceRect(obj,0,0,1,1);
		ObjRender_SetAlpha(obj,180);
		ObjRender_SetColor(obj,0,0,0);
		Obj_SetRenderPriorityI(obj, 30);

		let xr = (x2-x)/2;
		let yr = (y2-y)/2;

		ascent(a in -30..1){
			let xl = -640*sin(90-90/30*a)+640;

			ObjSpriteList2D_ClearVertexCount(obj);

			ObjSpriteList2D_SetDestRect(obj,x+xl,y+yr+3,
				x2+xl,y+yr-3);
			ObjSpriteList2D_AddVertex(obj);

			yield;
		}

		let t = 0;
		TPlayerCreate(640-16-128,480-16-64);
		while(!bEndTitleScene)
		{
			ObjSpriteList2D_ClearVertexCount(obj);

			ObjSpriteList2D_SetDestRect(obj,x,y+yr+(yr*min(1,t/10)-3)+3,
				x2,y+yr-(yr*min(1,t/10)-3)-3);
			ObjSpriteList2D_AddVertex(obj);

			t++;

			yield;
		}
		ascent(a in 1..31){
			let xl = 640*sin(90-90/30*a)-640;
			ObjSpriteList2D_ClearVertexCount(obj);
			ObjSpriteList2D_SetDestRect(obj,x+xl,y,x2+xl,y2);
			ObjSpriteList2D_AddVertex(obj);
			yield;
		}
		Obj_Delete(obj);
	}

	let mx = 30;
	let texts = ["Aya"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, 160, texts[iText]);
	}
	TTextBox(368,16,624,464, "");

	loop(30){if(GetVirtualKeyState(VK_OK) == KEY_FREE){loop(5){yield;}break;}yield;}

	let frameKeyHold = 0;
	while(!bEndTitleScene)
	{
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
		{
			_PlaySE(5);
			TStageScene("","Player");
			bEndTitleScene = true;
		}
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
	       	_PlaySE(6);
	       	TTitleScene;
            bEndTitleScene = true;
		}

		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			_PlaySE(4);
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			_PlaySE(4);
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD || GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 5 == 0)))
			{
				_PlaySE(4);
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 5 == 0)))
			{
				_PlaySE(4);
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			selectIndex = countMenu - 1;
		}
		else
		{
			selectIndex %= countMenu;
		}

		player_select_End = bEndTitleScene;

		yield;
	}
}

task TPlayerCreate(POS_X,POS_Y){
	loop(15){yield;}
	function trun(w){let x = truncate(w);return x;}
	let playerObj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(playerObj, player_sprite ~ "player_sprite_00.png");

	let imgrep = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(imgrep, 90);

	let animA = 0;
	
	let A = 0;
	let x = 256/8;
	let y = 142/3;
	
	Obj_SetRenderPriorityI(playerObj, 0);
	
	let player_select_Index = 0;

	option;

	while(!player_select_End){

		player_select_Index = GetCommonData("Shottype",0);

		if(animA < 6){A = 0;}
		else if((animA-6)%(6*7) < 6*1){A = 1;}
		else if((animA-6)%(6*7) < 6*2){A = 2;}
		else if((animA-6)%(6*7) < 6*3){A = 3;}
		else if((animA-6)%(6*7) < 6*4){A = 4;}
		else if((animA-6)%(6*7) < 6*5){A = 5;}
		else if((animA-6)%(6*7) < 6*6){A = 6;}
		else if((animA-6)%(6*7) < 6*7){A = 7;}
		
		animA++;

		ObjPrim_SetTexture(playerObj, player_sprite ~ "player_sprite_0"~IntToString(player_select_Index)~".png");

		if(player_select_Index == 0){
			if(animA%3 == 0){
				CreateAShot(-90,-8,-8,14,75,26,133);
				CreateAShot(-90,8,-8,14,75,26,133);
			}
		}
		else if(player_select_Index == 1){
			if(animA%3 == 0){
				CreateAShot(-90,-8,-8,87,87,101,117);
				CreateAShot(-90,8,-8,87,87,101,117);
			}
		}
		else if(player_select_Index == 2){
			if(animA%3 == 0){
				CreateAShot(-90,-8,-8,149,86,167,116);
				CreateAShot(-90,8,-8,149,86,167,116);
			}
		}
		
		ObjSprite2D_SetSourceRect(playerObj, x*(A%8), 1+trun(A/8)*y, x*(A%8)+x, 1+trun(A/8)*y+y);
		ObjSprite2D_SetDestCenter(playerObj);
		ObjRender_SetPosition(playerObj,POS_X,POS_Y,0);

		CreateRenderTarget("Playa");
		RenderToTextureA1("Playa",0,0,true);

		ObjPrim_SetTexture(imgrep, "Playa");
		ObjSprite2D_SetSourceRect(imgrep, 640-16-256,16,640-16,480-16);
		ObjSprite2D_SetDestRect(imgrep,640-16-256,16,640-16,480-16);
		
		yield;
	}

	Obj_Delete(playerObj);
	Obj_Delete(imgrep);

	task CreateAShot(ang,x,y,setx,sety,setx2,sety2){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, player_sprite ~ "shot.png");
		Obj_SetRenderPriorityI(obj, 0);
		ObjSprite2D_SetSourceRect(obj,setx,sety,setx2,sety2);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAngleZ(obj,ang+90);

		let t = 0;
		ObjRender_SetPosition(obj,POS_X+x+t*35*cos(ang),POS_Y+y+t*35*sin(ang),0);

		while(!player_select_End && ObjRender_GetY(obj) > 0){

			ObjRender_SetPosition(obj,POS_X+x+t*35*cos(ang),POS_Y+y+t*35*sin(ang),0);

			t++;

			yield;
		}

		Obj_Delete(obj);
	}

	task CreateBShot(ang,x,y,setx,sety,setx2,sety2){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, player_sprite ~ "shot.png");
		Obj_SetRenderPriorityI(obj, 0);
		ObjSprite2D_SetSourceRect(obj,setx,sety,setx2,sety2);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetAngleZ(obj,ang+90);

		let t = 0;
		ObjRender_SetPosition(obj,x+t*35*cos(ang),y+t*35*sin(ang),0);

		while(!player_select_End && ObjRender_GetY(obj) > 0){

			ObjRender_SetPosition(obj,x+t*45*cos(ang),y+t*45*sin(ang),0);

			t++;

			yield;
		}

		Obj_Delete(obj);
	}

	task CreateDShot(x,y,dir){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, player_sprite ~ "shot.png");
		Obj_SetRenderPriorityI(obj, 0);
		ObjSprite2D_SetSourceRect(obj,120,87,135,114);
		ObjSprite2D_SetDestCenter(obj);

		let t = 0;

		while(ObjRender_GetY(obj) > -40 && !Obj_IsDeleted(obj)){

			ObjRender_SetPosition(obj,x+30*dir*sin(t*180/6),y-30/2*t,0);
			ObjRender_SetAngleZ(obj,30*dir*sin(-t*180/6+90));

			t++;

			yield;
		}
	}

	task CreateFShot(let x,let y,ang){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, player_sprite ~ "shot.png");
		Obj_SetRenderPriorityI(obj, 0);
		ObjSprite2D_SetSourceRect(obj,177,70,207,119);
		ObjSprite2D_SetDestCenter(obj);

		let t = 0;
		let r = -24;
		let ang2 = -90+rand(10,-10);

		ObjRender_SetPosition(obj,x,y,0);

		while(!player_select_End && ObjRender_GetY(obj) > 0){

			if(r < 0){
				x+= r*cos(ang-180);
				y+= r*sin(ang-180);
				ObjRender_SetAngleZ(obj,ang+90);
			}
			else{
				x+= r*cos(ang2);
				y+= r*sin(ang2);
				ObjRender_SetAngleZ(obj,ang2+90);
			}

			r+= 24/15;

			ObjRender_SetPosition(obj,x,y,0);

			t++;

			yield;
		}

		Obj_Delete(obj);

	}

	task option{
	
		let x1 = [-40,-40,40,40];
		let	y1 = [40,-40,-40,40];
		
		let x2 = [-30,-15,15,30];
		let	y2 = [-5,-15,-15,-5];

		let x3 = [-100,-50,50,100];
		let	y3 = [-15,25,25,-15];
			
		let x4 = [36,18,-18,-36];
		let	y4 = [-15,15,15,-15];

		let angl = [-5,2,-2,5];
		let ang2l = [50,20,-20,-50];

		ascent(a in 0..4){
			Fam([x1[a],x2[a],x3[a],x4[a]],[y1[a],y2[a],y3[a],y4[a]],[angl[a],ang2l[a]]);
		}
	}

	task Fam(lx,ly,data){
		let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(obj2, 0);
		ObjPrim_SetTexture(obj2, player_sprite ~ "shot.png");
		ObjSprite2D_SetSourceRect(obj2, 0,0,15,15);
		ObjSprite2D_SetDestCenter(obj2);
		ObjRender_SetScaleXYZ(obj2,1.4,1.4,0);
		ObjRender_SetAlpha(obj2,120);
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(obj, 0);
		ObjPrim_SetTexture(obj, player_sprite ~ "shot.png");
		ObjSprite2D_SetSourceRect(obj, 0,0,15,15);
		ObjSprite2D_SetDestCenter(obj);
		
		let count = 0;
		
		Pos(POS_X,POS_Y);
		let xa;
		let ya;

		while(!player_select_End){
			if(0 == player_select_Index){
				ObjSprite2D_SetSourceRect(obj2, 0,0,15,15);
				ObjSprite2D_SetDestCenter(obj2);
				ObjRender_SetScaleXYZ(obj2,1.4,1.4,0);
				ObjSprite2D_SetSourceRect(obj, 0,0,15,15);
				ObjSprite2D_SetDestCenter(obj);

				ObjRender_SetAngleZ(obj,count*10);
				ObjRender_SetAngleZ(obj2,-count*10);

				if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
					xa = POS_X+lx[0];
					ya = POS_Y+ly[0];
				}
				else{
					xa = POS_X+lx[1];
					ya = POS_Y+ly[1];
				}
				if(count%3 == 0){
					if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
						CreateBShot(data[0]-90,ObjRender_GetX(obj)-8,ObjRender_GetY(obj)+5,44,76,55,133);
						CreateBShot(data[0]-90,ObjRender_GetX(obj)+8,ObjRender_GetY(obj)+5,44,76,55,133);
					}
					else{
						CreateBShot(-90,ObjRender_GetX(obj)-8,ObjRender_GetY(obj)+5,44,76,55,133);
						CreateBShot(-90,ObjRender_GetX(obj)+8,ObjRender_GetY(obj)+5,44,76,55,133);
					}
				}
			}
			else if(1 == player_select_Index){
				ObjSprite2D_SetSourceRect(obj2, 19,0,33,15);
				ObjSprite2D_SetDestCenter(obj2);
				ObjSprite2D_SetSourceRect(obj, 19,0,33,15);
				ObjSprite2D_SetDestCenter(obj);

				ObjRender_SetAngleZ(obj,0);
				ObjRender_SetAngleZ(obj2,0);

				ObjRender_SetScaleXYZ(obj2,1.4+0.1*cos(count*6),1.4+0.1*cos(count*6),0);

				if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
					xa = POS_X+lx[2];
					ya = POS_Y+ly[2];
				}
				else{
					xa = POS_X+lx[3];
					ya = POS_Y+ly[3];
				}
				if(count%8 == 0){
					CreateDShot(ObjRender_GetX(obj),ObjRender_GetY(obj),1);
					CreateDShot(ObjRender_GetX(obj),ObjRender_GetY(obj),-1);
				}
			}
			else if(2 == player_select_Index){
				ObjSprite2D_SetSourceRect(obj2, 0,0,0,0);
				ObjSprite2D_SetDestCenter(obj2);
				ObjSprite2D_SetSourceRect(obj, 0,0,0,0);
				ObjSprite2D_SetDestCenter(obj);

				if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){
					xa = POS_X;
					ya = POS_Y;
				}
				else{
					xa = POS_X;
					ya = POS_Y;
				}
				if(count%8 == 0){
					CreateFShot(ObjRender_GetX(obj),ObjRender_GetY(obj),data[1]+rand(-5,5)+90);
				}
			}

			Pos(ObjRender_GetX(obj) + (xa - ObjRender_GetX(obj)) / 3, ObjRender_GetY(obj) + (ya - ObjRender_GetY(obj)) / 3);
			
			count++;
			
			yield;
		}

		task Pos(x,y){
			ObjRender_SetPosition(obj, x, y, 0);
			ObjRender_SetPosition(obj2, x, y, 0);
		}
		
		Obj_Delete(obj);
		Obj_Delete(obj2);
	}
}