if(nextphase==1 && stagephase==2){ phase+=1; frame=0; enemyx=0; enemyy=0; enemiescalled=0; nextphase=0; SetCommonData("IncreasePhase",0); }

//401 = fireworks
//402 = aimed lines
//404 = yellow aimed/misaimed
//407 = colorful spread

if(phase==1 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-30; viewmovetimexy=120; }
	if(frame%30==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",minx-32,miny+rand(150,200),0,0,[5,20,3.5,1]); }
		if(enemiescalled==3){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",maxx+32,miny+rand(150,200),0,0,[8,20,3.5,1]); }
	enemiescalled++; }
	if(enemiescalled>=5){ nextphase=1; }
}
if(phase==2 && stagephase==2){
	if(enemiescalled==0){ newviewx=10; newviewy=-10; viewmovetimexy=90; }
	if(frame%20==0){
		if(enemiescalled<10 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",maxx+32,miny+100,0,0,1+enemyx); }
		if(enemiescalled<10 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",minx-32,miny+100,0,0,1+enemyx); }
	enemiescalled++; enemyx++; }
	if(enemiescalled>=18){ nextphase=1; }
}
if(phase==3 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-30; viewmovetimexy=120; }
	if(frame%30==0){
		if(enemiescalled<9 && enemiescalled%3==0 && enemiescalled%6==0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",maxx+32,miny+rand(150,200),0,0,[rand_int(1,3),20,3.5,1]); }
		if(enemiescalled<9 && enemiescalled%3==0 && enemiescalled%6!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",minx-32,miny+rand(150,200),0,0,[rand_int(5,7),20,3.5,1]); }
		if(enemiescalled==9 && enemiescalled%3==0 && enemiescalled%6!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",minx-32,miny+rand(150,200),0,0,[rand_int(5,7),20,3.5,2]); }
		enemiescalled++; }
	if(enemiescalled>=12){ nextphase=1; }
}
if(phase==4 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-10; viewmovetimexy=90; }
	if(frame%20==0){
		if(enemiescalled<10 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",maxx+32,miny+100,0,0,1+enemyx); }
		if(enemiescalled<10 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",minx-32,miny+100,0,0,1+enemyx); }
	enemiescalled++; enemyx++; }
	if(enemiescalled>=18){ nextphase=1; }
}
if(phase==5 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=0; viewmovetimexy=120; }
	if(frame%40==0){
		if(enemiescalled<6 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",minx-32,miny+75+enemyy,0,0,[cx,miny+100+enemyy,0,30,9]); }
		if(enemiescalled<6 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",maxx+32,miny+75+enemyy,0,0,[cx,miny+100+enemyy,0,30,7]); }
	enemiescalled++; enemyy+=60; }
	if(enemiescalled>=12){ nextphase=1; }
}
if(phase==6 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-15; viewmovetimexy=90; }
	if(frame%20==0){
		if(enemiescalled<10){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",maxx+32,miny+100,0,0,1+enemyx); }
		if(enemiescalled>0 && enemiescalled<8 && enemiescalled%3==0){ CreateEnemyFromFile("script\Enemies\Stage 4\405.txt",cx+120-enemyy,rand(50,150),0,0,0); enemyy+=240/3; }
		if(enemiescalled==16){ enemyx=0; enemyy=0; }
		if(enemiescalled>=16 && enemiescalled<26){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",minx-32,miny+100,0,0,1+enemyx); }
		if(enemiescalled>=16 && enemiescalled<21 && enemiescalled%3==0){ CreateEnemyFromFile("script\Enemies\Stage 4\405.txt",cx-120+enemyy,rand(50,150),0,0,0); enemyy+=240/3; }
	enemiescalled++; enemyx++; }
	if(enemiescalled>=38){ nextphase=1; }
}
if(phase==7 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-30; viewmovetimexy=60; }
	if(enemiescalled==0){ enemyx=120; }
	if(frame==enemyx){
		if(enemiescalled<10 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",maxx+32,miny+rand(150,200),0,0,[rand_int(1,3),20,3.5,1]); }
		if(enemiescalled<10 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",minx-32,miny+rand(150,200),0,0,[rand_int(5,7),20,3.5,1]); }
		enemiescalled++; if(enemyx>20){ enemyx-=10; } frame=0; }
	if(enemiescalled>=20){ nextphase=1; }
}
if(phase==8 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-15; viewmovetimexy=120; }
	if(frame%40==0){
		if(enemiescalled<6 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",minx-32,miny+40+enemyy,0,0,[minx-32,miny+40+enemyy,rand_int(0,1),30,3+enemyx]); }
		if(enemiescalled<6 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",maxx+32,miny+40+enemyy,0,0,[maxx+32,miny+40+enemyy,rand_int(0,1),30,3+enemyx]); }
	enemiescalled++; enemyy+=75; enemyx+=1; }
	if(enemiescalled>=12){ nextphase=1; }
}
if(phase==9 && stagephase==2){
	if(frame%20==0){
		if(enemiescalled<10){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",maxx+32,miny+100,0,0,1+enemyx); }
		if(enemiescalled>0 && enemiescalled<10 && enemiescalled%4==0){ CreateEnemyFromFile("script\Enemies\Stage 4\406.txt",cx+rand(-120,120),miny-32,0,0,0); }
		if(enemiescalled==16){ enemyx=0; }
		if(enemiescalled>=16 && enemiescalled<26){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",minx-32,miny+100,0,0,1+enemyx); }
		if(enemiescalled>=16 && enemiescalled<26 && enemiescalled%4==0){ CreateEnemyFromFile("script\Enemies\Stage 4\406.txt",cx+rand(-120,120),miny-32,0,0,0); }
	enemiescalled++; enemyx++; }
	if(enemiescalled>=42){ nextphase=1; }
}
if(phase==10 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=0; viewmovetimexy=90; }
	if(frame%60==0){
		if(enemiescalled<6 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",cx+100,miny-32,0,0,2); }
		if(enemiescalled<6 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",cx-100,miny-32,0,0,1); }
	enemiescalled++; }
	if(enemiescalled>=10){ nextphase=1; }
}
if(phase==11 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=10; viewmovetimexy=300; }
	if(frame%40==0){
		if(enemiescalled<3){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",cx+150-enemyx,miny-32,0,0,[cx+150-enemyx,cy,rand_int(0,1),30,1+enemyy]); }
		if(enemiescalled<2){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",cx-150+enemyx,miny-32,0,0,[cx-150+enemyx,cy,rand_int(0,1),30,1+enemyy]); }
	enemiescalled++; enemyx+=75; enemyy+=2; }
	if(enemiescalled>=9){ nextphase=1; }
}
if(phase==12 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-30; viewmovetimexy=60; }
	if(enemiescalled==0){ bgphase=2; } // Start end of background
	if(frame%150==0){
		if(enemiescalled==0){
		let speed=3;
			loop(4){
			CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",maxx+32,miny+75,0,0,[speed,rand(-10,10),speed,1]);
			speed+=1;
			}		
		}
		if(enemiescalled==1){
		let speed=3;
			loop(4){
			CreateEnemyFromFile("script\Enemies\Stage 4\401.txt",minx-32,miny+75,0,0,[4+speed,rand(-10,10),speed,1]);
			speed+=1;
			}		
		}	enemiescalled++; }
	if(enemiescalled>=4){ nextphase=1; }
}
if(phase==13 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=0; viewmovetimexy=120; }
	if(frame%60==0){
		if(enemiescalled<12 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",minx-32,maxy-40-enemyy,0,0,[GetPlayerX,GetPlayerY,rand_int(0,1),30,12]); }
		if(enemiescalled<12 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\402.txt",maxx+32,miny+40+enemyy,0,0,[GetPlayerX,GetPlayerY,rand_int(0,1),30,11]); }
	enemiescalled++; enemyy+=50; }
	if(enemiescalled>=15){ nextphase=1; }
}
if(phase==14 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=-10; viewmovetimexy=90; }
	if(enemiescalled==14){ newviewx=0; newviewy=0; viewmovetimexy=90; }
	if(frame%60==0){
		if(enemiescalled<8 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",minx-32,miny+100,0,0,2); }
		if(enemiescalled<8 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",maxx+32,miny+100,0,0,1); }
	enemiescalled++; }
	if(enemiescalled>=13){ nextphase=1; }
}
if(phase==15 && stagephase==2){
	if(enemiescalled==0){ newviewx=0; newviewy=0; viewmovetimexy=90; }
	if(frame%20==0){
	if(enemyx>=10){ enemyx=0; }
		if(enemiescalled<24 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",maxx+32,miny+100,0,0,1+enemyx); }
		if(enemiescalled<24 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Stage 4\407.txt",minx-32,miny+100,0,0,1+enemyx); }
		if(enemiescalled==4){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",minx-32,miny+100,0,0,2); }
		if(enemiescalled==10){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",maxx+32,miny+100,0,0,1); }
		if(enemiescalled==16){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",maxx+32,miny+100,0,0,2); }
		if(enemiescalled==22){ CreateEnemyFromFile("script\Enemies\Stage 4\404.txt",minx-32,miny+100,0,0,1); }
	enemiescalled++; enemyx++; }
	if(enemiescalled>=40){ nextphase=1; }
}