#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell4"]
#Text["Spell4"]
#Background["./BG.txt"]
#Player["./MoF_Reimu/Reimu.txt"]
#System["./system/Default_System.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/ZUNShot_Const.txt"
#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 state = 0;
let way = 1;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0];
@Initialize
{
SetBoss(true);
SetShotAutoDeleteClip(32,32,32,32);
}

@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(3500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3000000);
	}
}

//----------------------------------------------------
task mainTask{
	//SetCommonData("Difficulty","Normal");
	DamageRateAtFrame(50,0,240);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
	Cutin("Anxiety Semblance\"Smile Hiding Sorrow\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(30);
	Fam;
	wait(60);
		if(GetLevel=="Normal"){
		WallOrb;
		}
		else if(GetLevel=="Extra"){
		TOrb;
		wait(360);
		TAim;
		}
	}
//----------------------------------------------------

task WallOrb{
	let s = 1;
	while(!Obj_IsDeleted(objBoss)){
	smaple(0,60,50,-1.5,1,30);
	wait(75);
	PlaySound("Boon1",95,0);
	TExpan(GetEnemyX,GetEnemyY,8,60,0,128,255);
		OrbSet(0.5-0.5*s);
		wait(80);
		TMovement(25,2,1);
		wait(80);
		OrbSet(0.5+0.5*s);
		wait(80);
		TMovement(25,2,1);
		wait(80);
		smaple(0,240,50,1.5,1,30);
		wait(75);
		//TAccel(35,5,0.05,60);
		PlaySound("Boon1",95,0);
		TExpan(GetEnemyX,GetEnemyY,8,60,0,255,128);
		OrbSet(2);
		wait(150);
		AccelMove(objBoss,stgx,150,0.05,60);
		wait(150);
	s = -s;
	DamageRateAtFrame(60,0,0);
	}
			task EOrb(dir,color){
			let shot = ShotA2(GetEnemyX,GetEnemyY,1,dir,0.04,5,color,0,8);
			let c = 0;
			let pellet = 0;
			ObjShot_SetSpellResist(shot,true);
			if(color==401){pellet = 114;}
			else if(color==402){pellet = 116;}
				while(ObjMove_GetX(shot)>0 && ObjMove_GetX(shot)<384 && ObjMove_GetY(shot)>0){
				if(c%9==0){spongy(shot,0.05,10);}
				c++;
				yield;
				}
				if(!Obj_IsDeleted(shot)){
				TExpan(ObjMove_GetX(shot),ObjMove_GetY(shot),6,30,0,255,255);
				PlaySound("Eff6",85,0);
				ascent(i in 0..12){
					ascent(a in 1..3){
					let ang = i*30+a*15;
					let obj = ShotA2(ObjMove_GetX(shot)+25*a*cos(ang),ObjMove_GetY(shot)+25*a*sin(ang),0,ang,0.015,rand(1,1.125),pellet,0,8);
					fragment(obj,0,1);
					}
				}
				spongy(shot,0.1,10);
				Obj_Delete(shot);
				}
			}
			task OrbSet(type){
				if(type==0){
					ascent(n in 0..5){
					PlaySound("Shot1",90,0);
					let dir = 135+n*15;
					EOrb(dir,401);
					wait(20);
					}
				}
				if(type==1){
					ascent(n in 0..5){
					PlaySound("Shot1",90,0);
					let dir = 45-n*15;
					EOrb(dir,401);
					wait(20);
					}
				}
				if(type==2){
					ascent(n in 0..10){
					PlaySound("Shot1",90,0);
					let dir = 270-n*35;
					EOrb(dir,402);
						if(n>0){
						let dir2 = 270+n*35;
						EOrb(dir2,402);
						}
					wait(20);
					}
				}
			}
	}

task TState{
		smaple(0,60,50,1.5,1,45);
		wait(75);
		way = -way;
		state++;
		wait(120);
		TAccel(30,5,0.1,60);
		wait(120);
		}
		
task TAim{
		let n = 0;
		let d=0;
		let s = 1;
		CurveCharge;
		wait(75);
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>1750){
		PlaySound("BulletWave",90,0);
		ascent(i in 0..12){
			let shot = ShotA2(GetEnemyX,GetEnemyY,4,i*30+n*s+d*10*s,-4/90,0.85,98,0,8);
			ObjMove_AddPatternA2(shot,120,NO_CHANGE,NO_CHANGE,0.015,0,4);
		}
			if(n%24==0){let shot = ShotA2(GetEnemyX,GetEnemyY,4,angleToPlayer,-4/90,0.85,344,0,8);
			ObjMove_AddPatternA2(shot,120,NO_CHANGE,NO_CHANGE,0.015,0,4);
			}
			n+= 8;
			d++;
			if(n>100 && n%128==0){
			s = -s;
			TMovement(15,5,3);
			}
		wait(13);
		}
		TOrb2;
	}

task TOrb{
		let dir = 0;
		let dir2 = 0;
		CurveCharge;
		wait(75);
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>1750){
		PlaySound("Boon2",90,0);
		ascent(i in 0..16){
		let shot = ShotA2(GetEnemyX,GetEnemyY,0,i*360/16+dir,0.02,5,401,0,8);
		Manage(shot,dir2,0);
		}
		dir += 13;
		dir2 +=180;
		wait(90);
		}
}
			task Manage(shot,dir,type){
			let t = 0;
			let c = state;
			let graphic = 0;
			let count = 0;
				while(!Obj_IsDeleted(shot)){
						if(count%10==0){spongy(shot,0.05,10);}
						if(type==0){
						ObjMove_SetSpeed(shot,0.75+0.15*sin((count*2)%180));
						ObjMove_SetAngularVelocity(shot,1.25*sin(count*2));
						}
						else{
						ObjMove_SetSpeed(shot,1+0.15*sin((count*2)%180));
						ObjMove_SetAngularVelocity(shot,1*sin(count*2));
						}
					count++;
					yield;
					}
			}
			
task TOrb2{
		let dir = 0;
		let dir2 = 0;
		DamageRateAtFrame(75,0,180);
		ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
		ClearBullet(0);
		wait(60);
		CurveCharge;
		wait(75);
		TAim2;
		while(!Obj_IsDeleted(objBoss)){
		PlaySound("Boon2",90,0);
		ascent(i in 0..14){
		let shot = ShotA2(GetEnemyX,GetEnemyY,0,i*360/14+dir,0.02,5,402,0,8);
		Manage(shot,dir2,1);
		}
		dir -= 17;
		dir2 +=180;
		wait(60);
		}
	}

task TAim2{
		let n = 0;
		let d=0;
		let s = 1;
		while(!Obj_IsDeleted(objBoss)){
		PlaySound("BulletWave",90,0);
		ascent(i in 0..12){
			let shot = ShotA2(GetEnemyX,GetEnemyY,0.5,i*30+n*s+d*10*s,0,0,100,0,8);
			ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.015,0,4);
			ObjMove_AddPatternA2(shot,300,NO_CHANGE,NO_CHANGE,-0.015,0,0.5);
		}
			/*if(n%24==0){let shot = ShotA2(GetEnemyX,GetEnemyY,0.5,angleToPlayer,0,0,345,0,8);
			ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.015,0,4);
			ObjMove_AddPatternA2(shot,300,NO_CHANGE,NO_CHANGE,-0.015,0,0.5);
			}*/
			n+= 8;
			d++;
			if(n>100 && n%128==0){
			s = -s;
			TMovement(15,5,3);
			}
		wait(13);
		}
	}
	
	task CurveCharge{
	PlaySound("Charge2",90,0);
	Bright(objBoss,60);
	TCharge02(objBoss,4,60,255,0,255);
	DCharge(2,1,75,30,30,0);
	DCharge(-2,1,75,30,30,0);
	}
	
task Fam{
	ascent(i in 0..9){
	let ene = fam(GetEnemyX,GetEnemyY,25000,0.5,2,false);
	Control(ene,i);
	}
		task Control(ene,num){
			let maxa = num*40;
			let ang = 0;
			let c = 0;
			let r = 0;
			while(!Obj_IsDeleted(ene)){
			if(ang<maxa){ang += maxa/120;}
			if(r<60){r+=0.5;}
			ObjMove_SetPosition(ene,GetEnemyX+r*cos(ang+c),GetEnemyY+r*sin(ang+c));
			c+=0.5;
			yield;
			}
		}
	}

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

task TRender
{
	DrawBoss;
}

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

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}