#“Œ•û’e–‹•—[Single]
#ScriptVersion[3]
#Title["Spell4"]
#Text["Spell4"]
#Background["./BG.txt"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Player["./MoF_Reimu/Reimu.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"script/Marisa/Shot/Default_ShotConst.txt"
#include"script/Default_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,7];
let shotarr = [31,32,33,34,35,37,38];
let charge = false;
let charge2 = false;

@Initialize
{
SetBoss;
}

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

//----------------------------------------------------
task mainTask{
	magiccircle(objBoss,0.8,true);
	wait(45);
	Cutin("Magic Sign\"Mysterious Trap\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,stgx,100,10,7);
	wait(60);
	TFamiliar;
	wCharge;
	wCharge2;
	wait(240);
	ObjEnemy_SetDamageRate(objBoss,50,25);
	}
//----------------------------------------------------

task wCharge2{
		while(charge2==false){yield;}
		wait(90);
		maple(2);
		let x = GetEnemyX;
		let y = GetEnemyY;
		ascent(n in 0..15){
			ascent(i in 0..18){
			let shot = ShotA1(x,y,5,i*20,171+n%7,0,8);
			ObjMove_AddPatternA1(shot,5,0.5+n/3,NO_CHANGE);
			shot = ShotA1(x,y,5,i*20+3,171+n%7,0,8);
			ObjMove_AddPatternA1(shot,5,0.5+n/3,NO_CHANGE);
			}
			wait(4);
		}
	wait(180);
	charge2=false;
	wCharge2;
		}
task wCharge{
	while(charge==false){yield;}
	maple(1);
	wait(120);
	ascent(i in 0..18){
		PlaySound("Shot6",95,0);
		ascent(a in 0..3){
		let shot = ShotOA1(objBoss,5,i*20,258,0,8);
		ObjMove_AddPatternA1(shot,15,0.75+a/2,NO_CHANGE);
		}
	}
	wait(240);
	charge=false;
	wCharge;
	}

task TFamiliar{
	let s = -1;
	let section = false;
	let nfire = 0;
	maple(2);
	wait(50);
	PlaySound("Eff5",95,0);
	let enemy = ShotOA1(objBoss,0,90,11,0,0);
	Control(enemy);
	wait(135);
	AccelMove(objBoss,stgx,150,0.1,30);
	wait(60);
	section = true;
	wait(600);
		task Control(enemy){
		Obj_SetVisible(enemy,false);
		ObjShot_SetIntersectionEnable(enemy,false);
		ObjShot_SetSpellResist(enemy,true);
		ObjMove_SetDestAtFrame(enemy,stgx,stgy,90);
			ascent(i in 0..6){
			let ene = fam(GetEnemyX,GetEnemyY,200,arr[i],false);
			let shot = ShotOA1(objBoss,0,0,shotarr[i],0,0);
			ObjShot_SetIntersectionEnable(shot,false);
			ObjShot_SetSpellResist(shot,true);
			Control2(ene,shot,i,s,i%3);
			s = -s;
			}
				task Control2(ene,shot,num,way,pos){
					let r = 0;
					let c = 0;
					let value = 1;
					let color = ReturnColor(num);
					let dir = 0;
					ManageSpeed;
					ManageTail;
						task ManageSpeed{
							ascent(i in 0..90){
							r = 110*sin(i*2);
							c += 5*sin(i+90);
							yield;
							}
						}
						task ManageTail{
							let count = 0;
							while(!Obj_IsDeleted(enemy)){
							ObjMove_SetPosition(shot,ObjMove_GetX(ene),ObjMove_GetY(ene));
							if(count%value==0){
							RStar(shot,0,2,30);
							}
							count++;
							yield;
						}
						}
					loop(100){
						ObjMove_SetPosition(ene,ObjMove_GetX(enemy)+r*cos(360/6*num+c),ObjMove_GetY(enemy)+r*sin(360/6*num+c));
						yield;
					}
					wait(num*10);
					PlaySound("byoro07",95,0);
					ObjMove_SetDestAtWeight(ene,stgx+170*way,40+pos*180,10,7);
					while(section == false){yield;}
					value = 3;
					PlaySound("Charge2",95,0);
					while(!Obj_IsDeleted(ene)){
					wait(30+num*60);
					NCharge01(ene,1.5,30,color[0],color[1],color[2]);
					wait(30);
					StarShot(nfire);
					nfire++;
					charge = true;
					wait(300-num*30);
					NCharge01(ene,1.5,30,color[0],color[1],color[2]);
					wait(30);
					StarShot(nfire);
					nfire++;
					charge = true;
					wait(150-num*15);
					NCharge01(ene,1.5,30,color[0],color[1],color[2]);
					wait(30);
					StarShot(nfire);
					nfire++;
					charge2 = true;
					wait(90-num*15);
					}
								task StarShot(count){
								PlaySound("byoro07",90,0);
								PlaySound("BulletWave",85,60);
								if(nfire%2==0){dir = GetAngleToPlayer(ene);}
								else{dir = rand(0,72);}
								dir = rand(0,89);
								ascent(i in 0..4){
								let star = ShotA2(ObjMove_GetX(ene),ObjMove_GetY(ene),3,i*90+dir,-3/45,1,251+num,0,12);
								StarEff(star,20);
								}
								ascent(i in 0..12){
								let obj = ShotA2(ObjMove_GetX(ene),ObjMove_GetY(ene),1,i*30+dir,-1/45,0,131+num,0,8);
								ObjMove_AddPatternA2(obj,45,0,NO_CHANGE,0.01,0,1);
								}
								BossShot(count);
								}
					}
						task StarEff(star,t){
							let count = 0;
							ObjRender_SetAlpha(star,0);
							while(!Obj_IsDeleted(star)){
							count++;
							if(count%2==0){
							RStar(star,count*3,1.1,t);
							}
							yield;
							}
						}
						task BossShot(count){
						TMovement(20,5,1);
						/*let dir = count*57;
						let gcount = 0;
						let bstar = ShotA2(GetEnemyX,GetEnemyY,4,count*57,-4/45,0,251,0,12);
						StarEff(bstar,25);
						wait(45);
						if(count%20==19){dir = GetAngleToPlayer(bstar);}
						ObjMove_AddPatternA2(bstar,0,0,dir,0.035,0,4);
						while(!Obj_IsDeleted(bstar)){
						ObjShot_SetGraphic(bstar,251+gcount%7);
						gcount += 0.5;
						yield;
						}*/
						}
				
		}
	}

task TFam2{
	ascent(i in 0..4){
	let ene = fam(GetEnemyX,GetEnemyY,200,0,false);
	Control(ene,i);
	}
		task Control(ene,num){
		Stable;
		Fire;
			task Stable{
				let r = 0;
				let c = 0;
				while(!Obj_IsDeleted(ene)){
				ObjMove_SetPosition(ene,GetEnemyX+r*2*cos(num*90+c),GetEnemyY-r*1.25+r*0.5*sin(num*90+c));
				if(r<60){r+=0.5;}
				c+=2;
				yield;
				}
			}
			task Fire{
			wait(120);
				while(!Obj_IsDeleted(ene)){
				ascent(n in 0..27){
				PlaySound("BulletWave",90,0);
					ascent(i in 0..6){
						ShotA2(ObjMove_GetX(ene),ObjMove_GetY(ene),4,i*360/7+GetAngleToPlayer(ene),-0.1,0.5+n/9,138,0,4);
						}
					wait(4);
				}
				wait(60);
				}
			}
		}
	}

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

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));}
		EndBoss(1);
		wait(60);
		CloseScript(GetOwnScriptID());
		return;
	}