#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell10"]
#Text["Spell10"]
#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 Phase1 = true;
let objplayer = GetPlayerObjectID;
@Initialize
{
SetBoss(true);
SetCommonData("Bomb",2);
}

@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(116);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(7777777);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
}

//----------------------------------------------------
task mainTask{
	DamageRateAtFrame(0,0,0);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,100,10,3);
	wait(120);
	BeginEffect;
	}
//----------------------------------------------------

task RealTask{
	wait(60);
	//------------------------------Phase1--------------------------------//
	
	PlaySound("um4",100,0);
	wait(180);
	BlackFlash(120);
	PlaySound("Slash",100,0);
	UtterMask(stgx,100,1);
	wait(840);
	wait(60);
	PlaySound("um4",100,0);
	wait(180);
	BlackFlash(120);
	PlaySound("Slash",100,0);
	UtterMask(stgx,348,0);
	wait(540);
	wait(60);
	PlaySound("um4",100,0);
	wait(180);
	BlackFlash(120);
	PlaySound("Slash",100,0);
	UtterMask(stgx-100,stgy,0);
	wait(120);
	UtterMask(stgx+100,stgy,0);
	wait(600);
	PlaySound("Eff1",100,0);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	BlackFlash(120);
	Phase1 = false;
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	
	wait(120);
	//------------------------------Phase2--------------------------------//
	PlaySound("ahaha",100,0);
	wait(90);
	PlaySound("Slash",100,0);
	BlackFlash(150);
	let tx = stgx+100;
	let ty = stgy+100;
	FreezePlayer(tx,ty,150);
		ascent(r in 15..90){
		PlaySound("Shot1",95,0);
		loop(6+r/15){
		let rang = rand(0,360);
		let shot = ShotA2(tx+r*6*cos(rang),ty+r*6*sin(rang),0.2,0,0,0,42,58,8);
		ObjShot_SetSpellResist(shot,true);
		ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
		ObjMove_AddPatternA2(shot,150,NO_CHANGE,NO_CHANGE,0.005,0,1.5);
		}
		wait(2);
		}
	wait(300);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	PlaySound("Slash",100,0);
	BlackFlash(120);
	tx = stgx+100;
	ty = stgy-100;
	FreezePlayer(tx,ty,120);
		ascent(r in 15..90){
		PlaySound("Shot1",95,0);
		loop(6+r/15){
		let rang = rand(0,360);
		let shot = ShotA2(tx+r*6*cos(rang),ty+r*6*sin(rang),0.2,0,0,0,138,58,8);
		ObjShot_SetSpellResist(shot,true);
		ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
		ObjMove_AddPatternA2(shot,120,NO_CHANGE,NO_CHANGE,0.006,0,1.5);
		}
		wait(2);
		}
	wait(270);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	PlaySound("Slash",100,0);
	BlackFlash(90);
	tx = stgx-100;
	ty = stgy-100;
	FreezePlayer(tx,ty,90);
		ascent(r in 15..90){
		PlaySound("Shot1",95,0);
		loop(6+r/15){
		let rang = rand(0,360);
		let shot = ShotA2(tx+r*6*cos(rang),ty+r*6*sin(rang),0.2,0,0,0,216,58,8);
		ObjShot_SetSpellResist(shot,true);
		ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
		ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,0.007,0,1.5);
		}
		wait(1+r%2);
		}
		wait(240);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	PlaySound("Slash",100,0);
	BlackFlash(60);
	tx = stgx-50;
	ty = stgy+50;
	FreezePlayer(tx,ty,60);
		ascent(r in 15..90){
		PlaySound("Shot1",95,0);
		loop(3+r/15){
		let rang = rand(0,360);
		let shot = ShotA2(tx+r*6*cos(rang),ty+r*6*sin(rang),0.2,0,0,0,324,58,8);
		ObjShot_SetSpellResist(shot,true);
		ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
		ObjMove_AddPatternA2(shot,40,NO_CHANGE,NO_CHANGE,0.01,0,1.5);
		}
		wait(1);
		}
		wait(180);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	PlaySound("Eff1",100,0);
	
	wait(60);
	//------------------------------Phase3--------------------------------//
	wait(60);
	PlaySound("Spooky",100,0);
	FadeDark; // 180+180
	wait(180);
	FreezePlayer(stgx,stgy,300);
	wait(120);
	wait(60);
	PlaySound("ahaha",100,0);
	CircleMask;
	BlackFlash(120);
	BlackDot;
	wait(150);
	PlaySound("Charge1",100,0);
}

task CircleMask{
	ascent(i in 0..12){
	CMask(i*30,i);
	}
		task CMask(angle,num){
		let c = 0;
		let r = 170;
		let ene = fam(stgx+r*cos(angle+c),stgy+r*sin(angle+c),250000,0.75,5,true);
		while(!Obj_IsDeleted(ene)){
		ObjMove_SetPosition(ene,stgx+r*cos(angle+c),stgy+r*sin(angle+c));
		c++;
		if(c%10==0){
		ProtectShot(angle+c);
		}
		if(c%96==(num*8) && c>=300){
		PlaySound("Shot6",92,0);
		RealShot(angle+c+180);
		}
		yield;
		}
			task ProtectShot(sa){
				ascent(i in -2..3){
				CreateShotA1(ObjMove_GetX(ene),ObjMove_GetY(ene),4,sa+i*20,201,0);
				}
			}
			
			task CenterShot(sa){
				let obj = CreateShotA1(ObjMove_GetX(ene),ObjMove_GetY(ene),1,sa,248,0);
				while(GetDistance(ObjMove_GetX(obj),ObjMove_GetY(obj),stgx,stgy)>2){yield;}
				ObjShot_FadeDelete(obj);
			}
			
			task RealShot(sa){
			let color = [42,74,90,122,138,216];
			let r_rand = round(rand(0,5));
			let colorshot = color[r_rand];
			let obj = CreateShotA1(ObjMove_GetX(ene),ObjMove_GetY(ene),rand(0.5,0.9),sa+rand(-20,20),colorshot,0);
			ObjShot_SetSpellResist(obj,true);
			}
		}
}

task FreezePlayer(px,py,time){
	loop(time){
	ObjMove_SetPosition(objplayer,px,py);
	yield;
	}
	PlaySound("Slash",100,0);
}

task UtterMask(x,y,type){
	let ene = fam(x,y,25000,0.75,5,false);
	Stable;
	ObjMove_SetPosition(ene,x,y);
	wait(120);
	if(type==1){
	AimPlayer(ObjMove_GetX(ene),ObjMove_GetY(ene),stgx,stgy);
	wait(90);
	ObjMove_SetPosition(ene,stgx,stgy);
	wait(150);
	}
	while(!Obj_IsDeleted(ene)){
		let px = GetPlayerX;
		let py = GetPlayerY;
		if(Phase1==false){break;}
		AimPlayer(ObjMove_GetX(ene),ObjMove_GetY(ene),px,py);
		wait(90);
		if(Phase1==false){break;}
		ObjMove_SetPosition(ene,px,py);
		wait(150);
		if(Phase1==false){break;}
	}
		task AimPlayer(x,y,px,py){
			PlaySound("Laser6",95,0);
			let angle = getDotAngle(px,py,ObjMove_GetX(ene),ObjMove_GetY(ene));
			let dis = GetDistance(px,py,ObjMove_GetX(ene),ObjMove_GetY(ene));
			loop(15){
			EffectLaser(dis,angle);
			wait(6);
			}
			if(Phase1==false){break;}
			let l = dis/7;
			ascent(i in 0..18){
			ShotA1(px,py,1,i*24+angle,122,0,8);
			}
			
			ascent(i in -2..3){
				ascent(a in 0..6){
				ShotA1(px,py,0.5+a/2,i*3+angle,122,0,6);
				}
			}
			
			ascent(i in 0..l){
			StickyBullet(x+i*7*cos(angle),y+i*7*sin(angle),angle+i*13);
			StickyBullet(x+i*7*cos(angle),y+i*7*sin(angle),angle-i*13);
			}
			BlackFlash2(30);
			PlaySound("Slash",95,0);
				task StickyBullet(sx,sy,dir){
				let obj = ShotA2(sx,sy,0.05,dir,0.005,0.75,90,0,8);
				wait(720);
				Obj_Delete(obj);
				}
				task EffectLaser(dis,angle){
				let obj = CreateStraightLaserA1(ObjMove_GetX(ene),ObjMove_GetY(ene),angle,dis,0,0,74,0);
				ObjRender_SetColor(obj,128,0,128);
				ascent(i in 0..10){
				ObjLaser_SetRenderWidth(obj,i);
				yield;
				}
				Obj_Delete(obj);
				}
		}
		task Stable{
			while(Phase1==true){
			yield;
			}
			Obj_Delete(ene);
		}
}

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

task TRender
{
	DrawBoss;
}

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

task BeginEffect{
	ascent(i in 1..5){
	SpawnFam(i,i*90+45+135,i*15);
	SpawnFam(i,i*90+45,i*15);
	wait(15);
	}
	ascent(i in 0..18){
	PlaySound("Eff2",92,0);
	FEffect(GetEnemyX, GetEnemyY, GetEnemyX, 448/2, rand(0,360), rand(0,360),74,75);
	FEffect(GetEnemyX, GetEnemyY, GetEnemyX, 448/2, rand(0,360), rand(0,360),74,75);
	wait(5);
	}
	wait(90);
	let ene = fam(GetEnemyX,448/2,25000,0.75,5,false);
	wait(60);
	Obj_Delete(ene);
	PlaySound("clash",100,0);
	BlackFlash(120);
	ObjMove_SetPosition(objBoss,GetEnemyX,-500);
	ascent(i in 0..12){
	SpawnDeath(i*30);
	}
	wait(150);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("\"Hopeless Masquerade\"",15);
	wait(60);
	RealTask;
	}
	
task SpawnFam(type,angle,time){
	PlaySound("Eff5",100,0);
	let ene = fam(GetEnemyX,GetEnemyY,25000,0.5,type,false);
	ObjMove_SetDestAtWeight(ene,GetEnemyX+111*cos(angle),GetEnemyY+111*sin(angle),10,3);
	wait(60);
	AccelMove(ene,GetEnemyX,448/2,0.2,120);
	wait(180-time);
	ObjEnemy_SetLife(ene,0);
}

task SpawnDeath(angle){
	let ene = fam(GetEnemyX,448/2,25000,0.75,5,false);
	ObjMove_SetDestAtWeight(ene,stgx+500*cos(angle),stgy+500*sin(angle),0,5);
	wait(180);
	Obj_Delete(ene);
}

task FEffect(x, y, x2, y2, angle, angle2,color,time){
				let vx=(x2-x)/time;
				let vy=(y2-y)/time;
				let r=0;
				let a=0;
					loop(time){
					angle+=1; angle2+=1;
					x+=vx; y+=vy;
					a+=360/time; r=120-120*cos(a);
					let dummy = CreateShotA1(x+r*cos(angle), y+r*sin(angle2),0,0,color,8);
					ObjShot_SetDeleteFrame(dummy,0);
					yield;
					}
}

task BlackFlash(time){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/5.png";
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,384,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetColor(obj,0,0,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	ObjRender_SetScaleXYZ(obj,3,3,3);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,99);
	descent(i in 0..time){
	ObjRender_SetAlpha(obj,i*255/time);
	yield;
	}
	Obj_Delete(obj);
}

task BlackFlash2(time){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/5.png";
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,384,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetColor(obj,0,0,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	ObjRender_SetScaleXYZ(obj,3,3,3);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,99);
	descent(i in 0..time){
	ObjRender_SetAlpha(obj,0.5*i*255/time);
	yield;
	}
	Obj_Delete(obj);
}

task FadeDark{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/5.png";
	let time = 180;
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,384,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetColor(obj,0,0,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	ObjRender_SetScaleXYZ(obj,3,3,3);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,99);
	ascent(i in 0..time){
	ObjRender_SetAlpha(obj,i*1.4);
	yield;
	}
	wait(180);
	Obj_Delete(obj);
}

task BlackDot{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/dark.png";
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,848,896);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetPosition(obj,384/2+28,448/2+5,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,99);
	descent(i in 150..1350){
	if(i==750){StartTimer;}
	ObjRender_SetScaleXYZ(obj,i/100,i/100,i/100);
	yield;
	}
	while(!Obj_IsDeleted(objBoss)){yield;}
	Obj_Delete(obj);
}

task StartTimer{
	loop(5){
	PlaySound("time1",100,0);
	wait(60);
	}
	loop(5){
	PlaySound("time2",100,0);
	wait(60);
	}
	ObjEnemy_SetLife(objBoss,0);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
}

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