#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["\"Nightmare Reversal [Normal]\""]
#Text["Spell4N"]
#Background["./BG.txt"]
#Player[DEFAULT]
#System["./system/Default_System2.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 fog = 0;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0];
let zis = 0;
let yis = 0;
let change = false;
let phase = 1;
@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(2225);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3000000);
	}
}

//----------------------------------------------------
task mainTask{
	SetShotAutoDeleteClip(96,96,96,96);
	DamageRateAtFrame(30,0,180);
	DamageRateAtFrame(75,0,1380);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,140,10,7);
	Cutin("\"Nightmare Reversal [Normal]\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	TShot1;
	}
//----------------------------------------------------

task TShot1{
	let way = 1;
	let c = 0;
	let st = rand(-12,12);
	TRotate;
	wait(180);
		st = rand(-12,12);
		ascent(n in 0..18){
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
			PlaySound("Shot1",95,0);
			PlaySound("BulletWave",95,60);
			ascent(i in 0..12){
			let v = 2;
			let dir = i*30-n*way*2.7+st;
			let shot = ShotA2(GetEnemyX,GetEnemyY,v,dir,-v/90,0,207,0,8);
			ObjMove_SetAngularVelocity(shot,2*way);
			ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.03,0,5);
			DelayShot3(shot,236,1,96,2,0,12,60);
			}
			wait(10);
			way = -way;
		}
		while(!Obj_IsDeleted(objBoss)){
		wait(90);
		st = rand(-12,12);
		ascent(n in 0..21){
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
		if(n==0){TRotate2;}
			PlaySound("Shot1",95,0);
			PlaySound("BulletWave",95,60);
			ascent(i in 0..12){
			let v = 2;
			let dir = i*30-n*way*3.3+st;
			let shot = ShotA2(GetEnemyX,GetEnemyY,v,dir,-v/90,0,203,0,8);
			ObjMove_SetAngularVelocity(shot,2*way);
			ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.03,0,4);
			DelayShot3(shot,234,1,92,2,0,12,60);
			}
			wait(10);
			way = -way;
		}
		wait(60);
			if(c%2==0){
			ascent(n in 0..3){
			if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
				PlaySound("Shot1",95,0);
				ascent(i in 0..20){
					let v = 3;
					ShotA2(GetEnemyX,GetEnemyY,v,i*18+angleToPlayer,-v/30,v/3,205,0,12);
				}
				wait(30);
			}
			}
			else{
				ascent(n in 0..9){
				if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
					PlaySound("Shot1",95,0);
					ascent(i in 0..10){
					let v = 3;
					ShotA2(GetEnemyX,GetEnemyY,v,i*36+angleToPlayer,-v/30,v/3,205,0,12);
					}
					wait(10);
				}
			}
		st = rand(-12,12);
		ascent(n in 0..21){
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
			PlaySound("Shot1",95,0);
			PlaySound("BulletWave",95,60);
			if(n==0){TRotate3;}
			ascent(i in 0..12){
			let v = 2;
			let dir = i*30-n*way*2.7+st;
			let shot = ShotA2(GetEnemyX,GetEnemyY,v,dir,-v/90,0,207,0,8);
			ObjMove_SetAngularVelocity(shot,2*way);
			ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.03,0,5);
			DelayShot3(shot,236,1,96,2,0,12,60);
			}
			wait(10);
			way = -way;
		}
		c++;
		}
}

task TRotate{
	ReverseAng;
	maple(2);
	wait(70);
	PlaySound("Boon1",100,0);
	loop(90){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
	Set2DCameraAngleZ(270*sin(zis));
	zis+=1;
	yield;
	}
	StartClip;
}

task TRotate2{
	ReverseAng;
	maple(2);
	wait(70);
	zis++;
	PlaySound("Boon1",100,0);
	loop(90){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
	Set2DCameraAngleZ(0-90*sin(zis));
	zis+=2;
	yield;
	}
	SetPlayerClip(80, -64, 480+80,640-96,);
}

task TRotate3{
	ReverseAng;
	maple(2);
	wait(70);
	zis++;
	PlaySound("Boon1",100,0);
	loop(90){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<1){break;}
	Set2DCameraAngleZ(180+90*sin(zis));
	zis+=2;
	yield;
	}
	SetPlayerClip(80, -64, 480+80,640-96,);
}

task RotateEnd{
	let z = 0;
	loop(90){
	Set2DCameraAngleZ(90+270*sin(z));
	z += 1;
	yield;
	}
}

task StartClip{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
	SetPlayerClip(80, -64, 480+80,640-96,);
	yield;
	}
}

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

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));}
		let dis = 0;
		let px = GetEnemyX;
		let py = GetEnemyY;
		ClearBullet(90,10);
		TAccel(50,10,0.02,120);
		MapleExplode(180,1,2,5);
		PlaySound("enemoff",95,0);
		wait(105);
		RotateEnd;
		PlaySound("enemoff",100,0);
		MapleExplode(20,100,1,8);
		Obj_Delete(objBoss);
		wait(30);
		SetAutoDeleteObject(true);
		loop(180){yield;}
		CloseScript(GetOwnScriptID());
		return;
	}