#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell3"]
#Text["Spell3"]
#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 fog = 0;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0];
let color = [0,0,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(2400);
	}
	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,135,10,7);
	Cutin("Misery\"Trail of Woe\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(90);
	TShot2;
	wait(180);
	TShot1;
	}
//----------------------------------------------------

task TShot2{
	//SetCommonData("Difficulty","Extra");
	if(GetLevel=="Normal"){color = [400,92,61];}
	if(GetLevel=="Extra"){color = [403,94,59];}
	let n = 0;
	smaple(0,60,50,-1.5,1,45);
	wait(75);
	while(!Obj_IsDeleted(objBoss)){
	PlaySound("Shot6",90,0);
		ascent(i in 0..9){
		let shot = ShotOA1(objBoss,1.5,i*40+n*20,color[2],0,8);
		if(GetLevel=="Normal"){
		ObjMove_AddPatternA2(shot,0,NO_CHANGE,NO_CHANGE,0,rand(-0.25,0.25),0);
		ObjMove_AddPatternA2(shot,30,NO_CHANGE,NO_CHANGE,0,rand(-0.25,0.25),0);
		ObjMove_AddPatternA2(shot,75,NO_CHANGE,NO_CHANGE,0,0,0);
		}
		if(GetLevel=="Extra"){
		ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0,rand(-0.5,0.5),0);
		ObjMove_AddPatternA2(shot,75,NO_CHANGE,NO_CHANGE,0,rand(-0.5,0.5),0);
		}
		}
	n++;
	wait(9);
	}
	}

task TShot1{
	let s = 1;
	let p = 0;
	let count = 0;
	while(!Obj_IsDeleted(objBoss)){
	smaple(0,90-30,50,1.5,1,45);
	wait(75);
	ascent(i in 0..10){
	PlaySound("Shot6",95,0);
	let obj = ShotA2(GetEnemyX,GetEnemyY,0,angleToPlayer+i*360/10*s+p,0.03,5,color[0],0,8);
	ObjMove_SetAngularVelocity(obj,0.25*s);
	ObjShot_SetSpellResist(obj,true);
	Manage(obj);
	wait(7);
	}
	wait(120);
	TAccel(20,5,0.1,30);
	wait(60);
	s = -s;
	count++;
	p+=37;
	}
		task Manage(obj){
			let r = 0;
			let c = 0;
				while(!Obj_IsDeleted(obj)){
				r+=0.75;
				c++;
				if(c%9==8 && c>60){
					ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angleToEnemy(obj)+c*1.7,0.01,1,color[1],0,8);
					ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angleToEnemy(obj)+c*1.7+180,0.01,1,color[1],0,8);
				}
				yield;
				}
				task Control(shot){
					ObjRender_SetColor(shot,64,64,64);
					ObjShot_SetIntersectionEnable(shot,false);
					let ang = ObjMove_GetAngle(shot);
					let c2 = 0;
					Stable;
					while(!Obj_IsDeleted(obj) && (GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(obj),ObjMove_GetY(obj)) > r*0.80)){
					ObjMove_SetPosition(shot,ObjMove_GetX(obj)+r*cos(ang+c2),ObjMove_GetY(obj)+r*sin(ang+c2));
					c2++;
					yield;
					}
					ObjRender_SetColor(shot,255,255,255);
					ObjShot_SetIntersectionEnable(shot,true);
					PlaySound("BulletWave",90,0);
					if(!Obj_IsDeleted(obj)){
					ascent(a in 0..4){
					ShotA2(ObjMove_GetX(shot),ObjMove_GetY(shot),0,getAtanAngle(obj,shot),0.005+a/100,5,110,0,8);
					}
					if(ang%30==0){
					let amulet = ShotA2(ObjMove_GetX(shot),ObjMove_GetY(shot),0,getAtanAngle(obj,shot)+180,0.005,5,61,0,0.75);
					Cal(amulet,r);
					}
					}
					ObjShot_FadeDelete(shot);
					if(ang==0){
					yield;
					spongy(obj,0.1,15);
					Obj_Delete(obj);
					}
						task Stable{
						while(!Obj_IsDeleted(obj)){yield;}
						ObjShot_FadeDelete(shot);
						}
						task Cal(amulet,dis){
						wait((dis*400)^0.5);
						DelayShot(amulet,77,2.5,0,20,0);
						}
				}
		}
	}

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

task TRender
{
	DrawBoss;
}

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

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