let S4_imgBACK	= csd~"img\4001.png";
let S4_imgTITLE	= csd~"img\TITLE4.png";
let S4_imgBOSST	= csd~"img\CAUTION.png";
let S4_imgEF	= csd~"img\4002.png";

sub STAGE4T{
	LoadGraphic(S4_imgBACK);
	LoadGraphic(S4_imgTITLE);
	LoadGraphic(S4_imgBOSST);
	LoadGraphic(S4_imgEF);

	SetCommonData("STAGE",4);
	SetCommonData("Sfade",1);

	function Wait(let frame){loop(frame){yield;}}
	function WaitForZeroEnemy(){while(GetEnemyNum()!=0){yield;}}

	PlayMusic(GetCurrentScriptDirectory~"BGM\bgm4a.mp3");
	SetCommonData("EFC",4);
	Wait(100);
	let i=0;
        loop(3){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",100+i*80,0,0,0,448);
		Wait(40);
		i++;
        	}
	Wait(50);
	let i=0;
        loop(3){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",348-i*80,0,0,180,0);
		Wait(40);
		i++;
        	}
	Wait(50);
	let i=0;
        loop(3){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",100+i*60,0,0,0,448);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",348-i*60,0,0,180,0);
		Wait(40);
		i++;
        	}
	Wait(50);
	let i=0;
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",100+i*40,0,0,0,448);
		Wait(20);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",348-i*40,0,0,180,0);
		Wait(20);
		i++;
        	}
	Wait(50);
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",rand(80,140),0,0,0,448);
		Wait(30);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",rand(308,368),0,0,180,0);
		Wait(30);
        	}
	Wait(50);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",80,0,0,0,448);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",160,0,0,0,448);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",288,0,0,180,0);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy41_T.txt",368,0,0,180,0);
	Wait(200);
	TITLE4;
	Wait(250);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy43_T.txt",80,0,0,0,0);
	Wait(200);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy43_T.txt",364,0,0,0,0);
	Wait(200);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy43_T.txt",80,0,0,0,-10);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy43_T.txt",364,0,0,0,10);
	Wait(200);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",124,0,0,0,0);
	Wait(75);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",324,0,0,0,0);
	Wait(75);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",224,0,0,0,0);
	Wait(200);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",224,0,0,0,0);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",124,0,0,0,0);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",324,0,0,0,0);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(200);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",124,0,0,0,0);
	Wait(25);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",324,0,0,0,0);
	Wait(25);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",224,0,0,0,0);
	Wait(200);
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy47_T.txt",GetPlayerX,rand(100,200),0,0,0);
		Wait(50);
        	}
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",224,0,0,0,0);
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy47_T.txt",GetPlayerX,rand(50,150),0,0,0);
		Wait(25);
        	}
	Wait(125);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy48_T.txt",224,0,0,0,0);
	Wait(300);
	SetCommonData("Sfade",3);
	Wait(900);
        loop(20){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",rand(40,188),0,0,0,448);
		Wait(30);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",rand(268,408),0,0,0,0);
		Wait(30);
        	}
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",124,0,0,0,0);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",224,0,0,0,0);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",324,0,0,0,0);
	Wait(300);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(20);
	let i=0;
        loop(10){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy46_T.txt",i*20,0,0,0,0);
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy45_T.txt",448-i*20,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(5){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",104+i*60,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(5){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",344-i*60,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(200);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",40,0,0,0,448);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",188,0,0,0,448);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",268,0,0,0,0);
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy40_T.txt",408,0,0,0,0);
	Wait(200);
	let i=0;
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",44+i*60,0,0,0,0);
		Wait(20);
		i++;
        	}
	let i=0;
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",404-i*60,0,0,0,0);
		Wait(20);
		i++;
        	}
	Wait(100);
	let i=0;
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",44+i*60,0,0,0,0);
		Wait(5);
		i++;
        	}
	let i=0;
        loop(6){
		CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy44_T.txt",404-i*60,0,0,0,0);
		Wait(5);
		i++;
        	}
	Wait(500);
	DeleteAllEnemyWithoutBoss;
	DeleteEnemyShotInCircle(SHOT,224,240,500);
	FadeOutMusic(GetCurrentScriptDirectory~"BGM\bgm4a.mp3",20);
	OVER(1,-224,224,-50,50,0,447,0,99,25,100,70);
	OVER(2,-224,224,-50,50,0,447,0,99,25,100,70);
	OVER(3,-224,224,-50,50,0,447,0,99,25,100,70);
	SetCommonData("Sfade",2);
	Wait(175);
	PlayMusic(GetCurrentScriptDirectory~"BGM\bgm4b.mp3");
	CreateEnemyBossFromFile(GetCurrentScriptDirectory~"こいし連続２_T.txt",0,0,0,0,0);
	WaitForZeroEnemy();//敵が消えるまで停止
	Wait(200);
	SetCommonData("EFC",0);
	OVER(1,-224,224,-50,50,0,447,100,199,25,125,70);
	OVER(2,-224,224,-50,50,0,447,100,199,25,125,70);
	OVER(3,-224,224,-50,50,0,447,100,199,25,125,70);
	Wait(50);
	OVER(1,-115,115,-15,15,0,230,294,326,25,75,45);
	OVER(2,-115,115,-15,15,0,230,294,326,25,75,45);
	OVER(3,-115,115,-15,15,0,230,294,326,25,75,45);
	Wait(50);
	AddScore(20000000);
	FadeOutMusic(GetCurrentScriptDirectory~"BGM\bgm4b.mp3",20);
	SetCommonData("Sfade",1);
	Wait(350);
	DeleteGraphic(S4_imgBACK);
	DeleteGraphic(S4_imgTITLE);
	DeleteGraphic(S4_imgBOSST);
	DeleteGraphic(S4_imgEF);

// ========== ステージ１用関数やタスク ========= //
task TITLE4{
	let obj=Obj_Create(OBJ_EFFECT);
	let Talpha=0;
	let csd=GetCurrentScriptDirectory;

	ObjEffect_SetLayer(obj,3);
	ObjEffect_SetTexture(obj,csd~"img\TITLE4.png");
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj,4);
	ObjEffect_SetRenderState(obj,ALPHA);

	ObjEffect_SetVertexXY(obj,0,-163,-40);
	ObjEffect_SetVertexXY(obj,1,-163,40);
	ObjEffect_SetVertexXY(obj,2,163,-40);
	ObjEffect_SetVertexXY(obj,3,163,40);

	ObjEffect_SetVertexUV(obj,0,0,0);
	ObjEffect_SetVertexUV(obj,1,0,80);
	ObjEffect_SetVertexUV(obj,2,326,0);
	ObjEffect_SetVertexUV(obj,3,326,80);

        loop(51){
		ObjEffect_SetVertexColor(obj,0,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,1,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,2,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,3,Talpha*5,255,255,255);
		Obj_SetPosition(obj,170+Talpha/2,150);
		Talpha++;
		yield;
		}
        loop(150){yield;}
        loop(51){
		ObjEffect_SetVertexColor(obj,0,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,1,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,2,Talpha*5,255,255,255);
		ObjEffect_SetVertexColor(obj,3,Talpha*5,255,255,255);
		Obj_SetPosition(obj,170+Talpha/2,150);
		Talpha--;
		yield;
		}
	Obj_Delete(obj);
	}

task OVER(AP,Xmin,Xmax,Ymin,Ymax,XA,XB,YA,YB,LA,LB,YC){
	let obj=Obj_Create(OBJ_EFFECT);
	let big1=0;
	let big2=0;
	let csd=GetCurrentScriptDirectory;
	let imgCAUTION=csd~"img\CAUTION.png";

	ObjEffect_SetLayer(obj,3);
	ObjEffect_SetTexture(obj,imgCAUTION);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj,4);
	ObjEffect_SetRenderState(obj,ALPHA);
	Obj_SetPosition(obj,GetCenterX,GetCenterY-YC);

	ObjEffect_SetVertexXY(obj,0,Xmin,Ymin);
	ObjEffect_SetVertexXY(obj,1,Xmin,Ymax);
	ObjEffect_SetVertexXY(obj,2,Xmax,Ymin);
	ObjEffect_SetVertexXY(obj,3,Xmax,Ymax);

	ObjEffect_SetVertexUV(obj,0,XA,YA);
	ObjEffect_SetVertexUV(obj,1,XA,YB);
	ObjEffect_SetVertexUV(obj,2,XB,YA);
	ObjEffect_SetVertexUV(obj,3,XB,YB);

	PlaySE("seSuperNaturalBorder2.wav");
        loop(LA){
		big1++;
		ObjEffect_SetScale(obj,2-big1*0.04,big1*0.04);
		ObjEffect_SetVertexColor(obj,0,255,255,255,255);
		ObjEffect_SetVertexColor(obj,1,255,255,255,255);
		ObjEffect_SetVertexColor(obj,2,255,255,255,255);
		ObjEffect_SetVertexColor(obj,3,255,255,255,255);
		yield;
		}
        loop(LB){yield;}
        loop(50){
		big2++;
		ObjEffect_SetScale(obj,1+0.004*AP*big2,1+0.004*AP*big2);
		ObjEffect_SetVertexColor(obj,0,255-5*big2,255,255,255);
		ObjEffect_SetVertexColor(obj,1,255-5*big2,255,255,255);
		ObjEffect_SetVertexColor(obj,2,255-5*big2,255,255,255);
		ObjEffect_SetVertexColor(obj,3,255-5*big2,255,255,255);
		yield;
		}
	Obj_Delete(obj);
	}
}