#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell7"]
#Text["Spell7"]
#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 v = 0.3;
let SoulMove = -1;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0];
@Initialize
{
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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3000000);
	}
}

//----------------------------------------------------
task mainTask{
	DamageRateAtFrame(50,0,240);
	DamageRateAtFrame(75,0,1380);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
	Cutin("Repressive\"Wandering Soul\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(30);
	//SetShotAutoDeleteClip(32,32,32,32);
	//Fam;
	wait(60);
	Soul;
	CSoul;
	}
//----------------------------------------------------

task TShot1{
	let n = 0;
	//smaple(0,-60,50,-1.5,1,30);
	//wait(75);
		while(!Obj_IsDeleted(objBoss)){
		PlaySound("Shot6",90,0);
		ShotA2(GetEnemyX+180*cos(n),GetEnemyY+rand(-20,20),0,rand(85,95),0.01,rand(1,2),324,0,12);
		ShotA2(GetEnemyX+180*cos(n),GetEnemyY+rand(-20,20),0,rand(85,95)+180,0.01,rand(1,2),324,0,12);
		n += rand(77,137);
		wait(10);
		}
	}

task CSoul{
	let result = 0;
	let temp = 0;
	let s = 1;
	let color = [60,64,68];
	wait(120);
		while(!Obj_IsDeleted(objBoss)){
		smaple(0,75,50,1.5,1,30);
		if(v==0.4){smaple(0,-60,50,-1.5,1,30);}
		wait(75);
		if(v==0.4){TShot1;}
		PlaySound("Boon1",95,0);
		while(result == temp){
		result = truncate(rand(0,3)); // srand function
		}
		temp = result;
		SoulMove = result;
		yield;
		SoulMove = -1;
		wait(105);
		TAccel(15,3,0.1,30);
		wait(105);
		v+=0.1;
		}
	}

task Soul{
	let px=0;
	let py=0;
	descent(n in 0..11){
		PlaySound("Boon2",90,0);
		ascent(i in 0..5){
		px = 87*i+40*sin(n*77);
		py = 44*n+20;
		let shot = ShotA2(px,py,v,270,0,0,382,0,12);
		Manage(shot,truncate(rand(0,3)));
		}
		wait(10);
	}
	let n = 0;
	while(!Obj_IsDeleted(objBoss)){
	PlaySound("Boon2",90,0);
		ascent(i in 0..5){
		px = 87*i+40*sin(n*77);
		let shot = ShotA2(px,448+16,v,270,0,0,382,0,12);
		Manage(shot,round(rand(0,2)));
		}
		n++;
		wait(44/v);
	}
	}
		task Manage(shot,type){
		ObjShot_SetSpellResist(shot,true);
		let g = [375,376,377];
		let t = 0;
		let isShot = false;
		if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(shot),ObjMove_GetY(shot))<45){
		spongy(shot,0.1,15);
		Obj_Delete(shot);
		}
			while(!Obj_IsDeleted(shot) && isShot == false){
				ObjMove_SetSpeed(shot,v);
				t=0;
				while(SoulMove == -1){yield;}
					if(SoulMove == type && t==0){
					ObjShot_SetSpellResist(shot,false);
					ObjMove_AddPatternA2(shot,0,0,GetAngleToPlayer(shot),0.01,0,3);
					DelayShot(shot,g[type],2.5,0,12,0);
					t=1;
					isShot = true;
						if(GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(shot),ObjMove_GetY(shot))<45){
						spongy(shot,0.1,15);
						Obj_Delete(shot);
						}
					}
					yield;
		}
}
	
task Fam{
	ascent(i in 0..9){
	let ene = fam(GetEnemyX,GetEnemyY,25000,0.5,3,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());
	}