#include "./../../system/functions/functions_enemy.txt"
#include "./../../system/functions/functions_stage.txt"

let BossScene;

@Initialize{TMain;}
@MainLoop{yield;}

task TMain
	{
	LoadMidboss;
	loop(30){yield;}
	loop(0)
		{
		ObjEnemyBossScene_Regist(BossScene);
		while(!Obj_IsDeleted(BossScene)){yield;}
		}
	local //Chapter 1
		{
		ascent(t in 0..22)
			{
			CreateEnemyA1((t%2)*2-1);
			loop(12){yield;}
			}
		loop(60*5.5+120){yield;}
		}
	local //Chapter 2
		{
		ascent(dir in 0..35)
			{
			dir = (dir%2)*2-1;
			loop(2)
				{
				CreateEnemyA2(dir);
				loop(8){yield;}
				}
			}
		loop(105){yield;}
		}
	local //Chapter 3
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			CreateEnemyA3(GetStgFrameWidth*(0.5+dir*0.325),GetStgFrameHeight*0.5-105);
			}
		loop(600){yield;}
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			ascent(n in 0..2)
				{
				CreateEnemyA3(GetStgFrameWidth*(0.5+dir*(0.325-n*0.2)),GetStgFrameHeight*0.5-105-n*50);
				}
			}
		loop(720){yield;}
		}
	local //Chapter 4
		{
		let spread = 16;
		ascent(n in 0..4)
			{
			let spreadA = trunc(spread*(0.5+n*0.5/3));
			let dir = (n%2)*2-1;
			ascent(i in 0..spreadA)
				{
				CreateEnemyA4(270+dir*(-135+i*360/spread),dir);
				loop(2){yield;}
				}
			loop(240){yield;}
			}
		}
	loop(270){yield;}
	local
		{
		ObjEnemyBossScene_Regist(BossScene);
		while(!Obj_IsDeleted(BossScene)){yield;}
		}
	loop(120){yield;}
	CloseScript(GetOwnScriptID);
	}

function LoadMidboss
{
	BossScene = ObjEnemyBossScene_Create();
	let path = GetCurrentScriptDirectory ~ "midboss/";
	ObjEnemyBossScene_Add(BossScene, 0, path ~ "M_Init.txt");
	ObjEnemyBossScene_Add(BossScene, 0, path ~ "M_Spell1.txt");
	ObjEnemyBossScene_Add(BossScene, 1, path ~ "M_Spell2.txt");
	ObjEnemyBossScene_Add(BossScene, 2, path ~ "M_Spell3.txt");
	ObjEnemyBossScene_Add(BossScene, 2, path ~ "M_End.txt");
	ObjEnemyBossScene_LoadInThread(BossScene);
}

task CreateEnemyA1(dir)
	{	
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*(0.5-dir*0.5),-60,4,90,200,100,type_fairy,fairy_blue,0);
	ObjMove_SetAcceleration(obj,-3/180);
	ObjMove_SetMaxSpeed(obj,0);
	ObjMove_SetAngularVelocity(obj,-dir*90/120);
	let counterM = -1;	
	let counter = floor(rand(0,22));
	while(!Obj_IsDeleted(obj) && !IsBeyondClip(obj,64))
		{
		counterM+=1;
		if(counterM == 120)
			{
			ObjMove_SetAcceleration(obj,1/30);
			ObjMove_SetMaxSpeed(obj,5);
			ObjMove_SetAngularVelocity(obj,-dir*270/180);
			}
		if(counterM >= 0)
			{
			counter+=1;
			if(counter >= 45)
				{
				ascent(i in -1..2)
					{
					CreateBulletA2(ObjMove_GetX(obj),ObjMove_GetY(obj),9.5,GetAngleToPlayer(obj)+i*5,-6/20,0,3.5,PELLET,8,1.125,SHOT_BLUE);
					}
				counter = trunc(rand(0,11));
				}
			}
		yield;
		}
	Obj_Delete(obj);
	}

task CreateEnemyA2(dir)
	{	
	loop(trunc(rand(0,8))){yield;}
	let spread = 3;
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*(0.5+dir*rand(0.1,0.5)),-32,3,90,400,100,type_fairy,fairy_blue,0);
	let counterM = -1;	
	let counter = floor(rand(0,22));
	ObjMove_SetAngularVelocity(obj,dir*45/60);
	let aM = 90-dir*45;
	while(!Obj_IsDeleted(obj) && !IsBeyondClip(obj,64))
		{
		counterM+=1;
		if(counterM == 60)
			{
			ObjMove_SetAngularVelocity(obj,dir*90/600);
			}
		if(counterM == 120)
			{
			ObjMove_SetAcceleration(obj,1/30);
			ObjMove_SetMaxSpeed(obj,5);
			}
		if(counterM == 240)
			{
			ObjMove_SetAngularVelocity(obj,0);
			}
		if(counterM >= 0 && !IsBeyondClip(obj,0))
			{
			counter+=1;
			aM+=dir*2;
			if(counter%14 == 0)
				{
				let a = aM;
				ascent(i in 0..spread)
					{
					let d = 4;
					let s = 1+(i+1)*0.05;
					CreateBulletA2(ObjMove_GetX(obj)+d*cos(a),ObjMove_GetY(obj)+d*sin(a),s,a,s/75,0,10,BL_SMALL,6,1,SHOT_PURPLE);
					}
				}
			}
		yield;
		}
	Obj_Delete(obj);
	}

task CreateEnemyA3(x,y)
	{	
	let spread = [20,2,12];
	let obj = CreateBasicEnemyA1(x,-48,0,90,2100,50,type_fairy,fairy_red,6);
	ObjMove_SetDestAtWeight(obj,x,y,25,3.5);
	let sw = false;
	let c = -1;
	loop(2){yield;}
	while(!Obj_IsDeleted(obj) && !IsBeyondClip(obj,64))
		{
		c+=1;
		if(c >= 120 && (c-120)%75 == 0 && !IsBeyondClip(obj,0))
			{
			ascent(i in 0..spread[0])
				{	
				TShot((i%spread[1])/max(1,spread[1]-1),i*360/spread[0]+270);
				}
			}
		if(c == 120+150)
			{
			ObjMove_SetAngle(obj,atan2(0,GetStgFrameWidth*0.5-ObjMove_GetX(obj)));
			ObjMove_SetAcceleration(obj,1/40);
			ObjMove_SetMaxSpeed(obj,1);
			}
		yield;
		}
	Obj_Delete(obj);
	
	task TShot(s,a)
		{
		s = 3+s*0.5;
		let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),s,a,LARGE,6,1,SHOT_RED);
		loop(6){yield;}
		let c = -1;
		let n = 0;
		while(!Obj_IsDeleted(shot))
			{
			c+=1;
			if(c >= 20 && (c-20)%6 == 0 && n < spread[2] && !IsBeyondClip(shot,8))
				{
				let spam = CreateBulletA1(ObjMove_GetX(shot),ObjMove_GetY(shot),0,a,PELLET,6,1.2,SHOT_PURPLE);
				ObjShot_SetGraphic(spam,PELLET[SHOT_PURPLE]+1);
				ObjMove_AddPatternA2(spam,75,NO_CHANGE,NO_CHANGE,1/40,0,2);
				n+=1;
				}
			yield;
			}
		}
	}

task CreateEnemyA4(a,dir)
	{	
	let time =150;
	let scM = 36;
	let obj = CreateBasicEnemyA1(GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-25,195/time,a,200,0,type_orb,orb_blue,0);
	ObjMove_SetAngularVelocity(obj,-dir*210/time);
	ObjEnemy_SetIntersect(obj,false);
	let c = -1;
	let aM = a-dir*110;
	while(!Obj_IsDeleted(obj) && !IsBeyondClip(obj,64))
		{
		c+=1;
		if(c <= time)
			{
			let sc = c*1/time;
			ObjRender_SetColorHSV(obj,0,0,125);
			ObjEnemy_SetScale(obj,sc*scM);
			}
		if(c == time)
			{
			ObjEnemy_SetIntersect(obj,true);
			ObjRender_SetColorHSV(obj,0,0,255);
			ObjEnemy_SetDamageRate(obj,50,50);
			ObjMove_SetAcceleration(obj,1/60);
			ObjMove_SetMaxSpeed(obj,2);	
			}
		if(c == time+60)
			{
			ObjMove_SetAngularVelocity(obj,0);		
			}
		if(c >= time && !IsBeyondClip(obj,0))
			{
			if((c-time)%5 == 0)
				{
				CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1.5,aM+180,PELLET,6,1.2,SHOT_RED);
				}
			aM+=dir*1.75;
			}
		yield;
		}
	Obj_Delete(obj);
	}