#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["SP3"]
#Text["SP3"]
#Background["./BG.txt"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let s = 1;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0];
let g_acc = 2.5;
let g_s = 1;
@Initialize
{
SetCommonData("KanakoS",0);
SetBoss(true);
}

@MainLoop
{
	
	//ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	//ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(56);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3000000);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
}

//----------------------------------------------------
task mainTask{
	Circle(64,0,0,0);
	Circle(0,64,0,1);
	Circle(0,0,64,2);
	DamageRateAtFrame(50,50,240);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,stgy,10,7);
	Cutin("\"The Avatar of Mountains and Lakes\"",15,"SHOU");
	ObjEnemyBossScene_StartSpell(objScene);
	wait(90);
	TShot1;
	}
//----------------------------------------------------

task TShot1{
		PlaySound("Weird1",100,0);
		ascent(i in 1..7){
		SpawnFam(i);
		}
		wait(2520);
		ClearBullet(100,20);
		wait(60);
		PlaySound("Weird1",100,0);
		ascent(i in 1..7){
		SpawnFam2(i,90,-1);
		SpawnFam2(i,180,-1);
		}
	}
	
task ChangeAcc{
		/*descent(i in -50..50){
		g_acc = i/25*g_s;
		yield;
		}
		g_s *= -1;*/
	}
	
task SpawnFam(num){
	let ene = fam(GetEnemyX,GetEnemyY,100000,0.5,num,false);
	let r = 0;
	let c = 0;
	let ang = 0;
	let maxalign = 360/6*(num-1);
	let align = 0;
		Stable;
		wait(180);
		SShot;
		task Stable{
			while(!Obj_IsDeleted(ene)){
			let px = GetGapX2(GetEnemyX,r*2,r,ang,align);
			let py = GetGapY2(GetEnemyY,r,r*2,ang,align);
			ObjMove_SetPosition(ene,px,py);
			if(c<180){
			align += maxalign/180;
			r+=1;
			}
			if(c>180 && c<240){r-=1.1;}
			ang += g_acc/6.25;
			align+=g_acc;
			c++;
			if(c==2520){ObjEnemy_SetLife(ene,0);}
			yield;
			}
		}
		task SShot{
			let color = [99,107,103,108,100,111];
			let delaycolor = [202,206,204,207,203,208];
			let arrow = [3,11,7,14,5,15];
			let d = 0;
			while(!Obj_IsDeleted(ene)){
				let px = ObjMove_GetX(ene);
				let py = ObjMove_GetY(ene);
				if(d%15==num*3-3 && c-180 < 600){
				PlaySound("BulletWave",90,0);
				ShotA1(px,py,1.25,getDotAngle(GetEnemyX,GetEnemyY,px,py),arrow[num-1],delaycolor[num-1],12);
				}
				if(num==1 && c-180==601){ChangeAcc;}
				else if(d%15==num*3-3 && c-180 < 1320 && c-180>720){
				PlaySound("BulletWave",90,0);
				ShotA1(px,py,1.25,getDotAngle(GetPlayerX,GetPlayerY,px,py),arrow[num-1],delaycolor[num-1],12);
				}
				if(num==1 && c-180==1321){ChangeAcc;}
				else if(d%20==num*4-4 && c-180 < 1920 && c-180>1440){
				PlaySound("BulletWave",90,0);
				ShotA2(px,py,0,getDotAngle(GetEnemyX,GetEnemyY,px,py),0.01,2,arrow[num-1],delaycolor[num-1],12);
				ShotA2(px,py,0,getDotAngle(GetPlayerX,GetPlayerY,px,py),0.01,2,arrow[num-1],delaycolor[num-1],12);
				}
				if(num==1 && c-180==1921){ChangeAcc;}
				else if(d%10==num*2-2 && c-180 < 2400 && c-180>2040){
				PlaySound("BulletWave",90,0);
					ascent(i in 0..4){
						ascent(a in 0..2){
						ShotA1(px,py,0.7+a/2,getDotAngle(GetPlayerX,GetPlayerY,px,py)+d*13+i*90,arrow[num-1],delaycolor[num-1],12);
						}
					}
				}
				ShotA2(px,py,0,getDotAngle(stgx,stgy,px,py)+180,0.05,5,color[num-1],delaycolor[num-1],8);
				wait(2);
				d++;
			}
		}
	}
	
task SpawnFam2(num,ang,way){
	let ene = fam(GetEnemyX,GetEnemyY,100000,0.5,num,false);
	let r = 0;
	let c = 0;
	let maxalign = 360/6*num;
	let align = 0;
		Stable;
		wait(180+num*2);
		SShot;
		task Stable{
			while(!Obj_IsDeleted(ene) && ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
			let px = GetGapX2(GetEnemyX,r*2,r,ang,align);
			let py = GetGapY2(GetEnemyY,r,r*2,ang,align);
			ObjMove_SetPosition(ene,px,py);
			if(c<180){
			align += maxalign/180;
			r+=0.75;
			}
			ang += 0.4*way;
			align+=2.5;
			c++;
			yield;
			}
			ObjEnemy_SetLife(ene,0);
		}
		task SShot{
			let color = [99,107,103,108,100,111];
			let delaycolor = [202,206,204,207,203,208];
			let arrow = [3,11,7,14,5,15];
			let d = 0;
			while(!Obj_IsDeleted(ene) && ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
				PlaySound("Shot9",95,0);
				let px = ObjMove_GetX(ene);
				let py = ObjMove_GetY(ene);
				let shot = ShotA2(px,py,0,getDotAngle(stgx,stgy,px,py),0.01,1,color[num-1],delaycolor[num-1],8);
				Manage(shot);
				wait(12);
				d++;
			}
				task Manage(shot){
					while(GetDistance(ObjMove_GetX(shot),ObjMove_GetY(shot),GetEnemyX,GetEnemyY)>5){yield;}
					ObjShot_FadeDelete(shot);
				}
		}
	}

//----------------------------------------------------

task TRender
{
	DrawBoss;
}

//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
		{AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));}
		let dis = 0;
		let px = GetEnemyX;
		let py = GetEnemyY;
		ClearBullet(105,6);
		StartSlow(TARGET_ALL,30);
		MapleExplode(200,1,2,5);
		PlaySound("enemoff",95,0);
		wait(105);
		StopSlow(TARGET_ALL);
		PlaySound("enemoff",100,0);
		MapleExplode(50,20,1,6);
		Obj_Delete(objBoss);
		loop(240){yield;}
		CloseScript(GetOwnScriptID());
		return;
	}