#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell11"]
#Text["Spell11"]
#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];
@Initialize
{
SetBoss(true);
SetCommonData("Bomb",1);
}

@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(8000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(10000000);
	}
}

//----------------------------------------------------
task mainTask{
	DamageRateAtFrame(0,0,0);
	magiccircle(objBoss,0.8,true);
	ObjMove_SetDestAtWeight(objBoss,stgx,120,10,7);
	Cutin("\"Monkey Possession\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(90);
	TShot1;
	}
//----------------------------------------------------

task TShot1{
	let result = 0;
	smaple(0,10,360,0,1,80);
	smaple(1,10,360,0,1,80);
	PlaySound("Charge3",100,0);
	wait(120);
	PlaySound("Eff1",100,0);
	ascent(i in 0..24){
	FakeLaser(i*15+angleToPlayer,9);
	}
	ascent(i in 0..24){
		ascent(a in 0..15){
		CreateShotA2(GetEnemyX,GetEnemyY,0,i*15+angleToPlayer,0.005+a/150,10,304,12);
		}
	}
	DamageRateAtFrame(40,0,0);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>6000){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	wait(105);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	ChooseOrb("R");
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	ChooseOrb("L");
	wait(105);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	result = round(rand(67,77));
	ascent(n in 0..5){
		PlaySound("Eff7",95,0);
		ascent(i in 0..24){
		let shot = OrbBullet(GetEnemyX,GetEnemyY,0,0,401,12);
		ObjMove_AddPatternA2(shot,0,4,i*15+8*sin(n*result),-4/60,0,0.5);
		ObjMove_AddPatternA2(shot,50,NO_CHANGE,NO_CHANGE,4/75,0,6);
		}
		ascent(i in 0..24){
		let shot = OrbBullet(GetEnemyX,GetEnemyY,0,0,401,12);
		ObjMove_AddPatternA2(shot,0,4,i*15+8*sin(n*result),-4/90,0,0.5);
		ObjMove_AddPatternA2(shot,50,NO_CHANGE,NO_CHANGE,4/75,0,6);
		}
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
		wait(30);
	}
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	ChooseOrb("L");
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	ChooseOrb("R");
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	smaple(0,-45,50,1.5,1,45);
	smaple(0,185,50,-1.5,1,45);
	wait(75);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	PlaySound("Eff7",95,0);
	PlaySound("Eff1",95,100);
	ascent(a in 0..7){
		ascent(i in 0..24){
		let v = 1+a;
		let shot = OrbBullet(GetEnemyX,GetEnemyY,0,0,401,12);
		ObjMove_AddPatternA2(shot,0,v,i*15+7*sin(a*result*2),-v/90,0,v/8);
		ObjMove_AddPatternA2(shot,100,NO_CHANGE,NO_CHANGE,v/120,0,v);
		}
	}
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=6000){break;}
	}
	ObjMove_SetDestAtWeight(objBoss,stgx,120,10,5);
	DamageRateAtFrame(0,0,0);
	ClearBullet(1);
	wait(30);
	PlaySound("Eff1",100,0);
	ascent(i in 0..24){
	FakeLaser(i*15+angleToPlayer,4);
	}
	ascent(i in 0..24){
		ascent(a in 0..15){
		CreateShotA2(GetEnemyX,GetEnemyY,0,i*15+angleToPlayer,0.005+a/150,10,302,12);
		}
	}
	DamageRateAtFrame(40,0,0);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>4000){
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	smaple(0,-45,50,-1.5,1,30);
	smaple(0,185,50,+1.5,1,30);
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX-75,GetEnemyY+50);
	wait(20);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX+75,GetEnemyY-50);
	wait(40);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX+75,GetEnemyY+50);
	wait(20);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX-75,GetEnemyY-50);
	wait(40);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX-100,GetEnemyY);
	wait(20);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	PurpleOrb(GetEnemyX+100,GetEnemyY);
	wait(30);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=4000){break;}
	TMovement(35,5,4);
	}
	ObjMove_SetDestAtWeight(objBoss,stgx,120,10,5);
	DamageRateAtFrame(0,0,0);
	ClearBullet(1);
	wait(30);
	PlaySound("Eff1",100,0);
	ascent(i in 0..24){
	FakeLaser(i*15+angleToPlayer,3);
	}
	ascent(i in 0..24){
		ascent(a in 0..15){
		CreateShotA2(GetEnemyX,GetEnemyY,0,i*15+angleToPlayer,0.005+a/150,10,301,12);
		}
	}
	DamageRateAtFrame(40,0,0);
	wait(30);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>2000){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
	smaple(0,-45,50,-1.5,1,30);
	smaple(0,185,50,+1.5,1,30);
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
	let angle = 90;
	ascent(n in 0..4){
		RedOrb(GetEnemyX+75*cos(angle),GetEnemyY+75*sin(angle));
		angle -= 45;
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
		wait(20);
	}
	angle = 90;
	wait(40);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
	ascent(n in 0..4){
		RedOrb(GetEnemyX+75*cos(angle),GetEnemyY+75*sin(angle));
		angle += 45;
		wait(20);
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
	}
	wait(40);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
	ascent(n in 0..2){
		PlaySound("Shot1",95,0);
		ascent(i in 0..24){
		let dir = GetAngleToPlayer(objBoss)+i*15;
		ShotA1(GetEnemyX+45*cos(dir),GetEnemyY+45*sin(dir),3,dir,333,0,12);
		ShotA1(GetEnemyX+45*cos(dir),GetEnemyY+45*sin(dir),1.5,dir,333,0,12);
		}
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=2000){break;}
		wait(30);
	}
	TMovement(35,5,4);
	}
	ObjMove_SetDestAtWeight(objBoss,stgx,120,10,5);
	DamageRateAtFrame(0,0,0);
	ClearBullet(1);
	PlaySound("Charge3",100,0);
	ascent(i in 0..6){
	let co = ReturnColor(i);
	TExpan(GetEnemyX,GetEnemyY,10,40,co[0],co[1],co[2]);
	wait(20);
	}
	DamageRateAtFrame(40,0,0);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
	RainbowOrb(stgx+rand(-120,120),rand(60,180),1);
	wait(60);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)<=0){break;}
	RainbowOrb(stgx+rand(-120,120),rand(60,180),-1);
	wait(60);
	}
		task FakeLaser(angle,color){
		let obj = CreateStraightLaserA1(GetEnemyX,GetEnemyY,angle,600,10,0,color,0);
		ObjRender_SetColor(obj,128,128,128);
		}
}

task RainbowOrb(x,y,way){
	let sangle = rand(0,14);
	let angular = rand(0.5,0.75);
	let rainset = [403,404,401,402,405,407,400];
	PlaySound("Eff7",95,0);
	TExpan(x,y,8,30,128,128,128);
	ascent(i in 0..24){
	let shot = ShotA1(x,y,2-angular,i*15+sangle,rainset[0],0,12);
	ObjShot_SetSpellResist(shot,true);
	ObjMove_SetAngularVelocity(shot,angular*way);
	ObjMove_AddPatternA2(shot,150,NO_CHANGE,NO_CHANGE,0,0,0);
	Stable(shot);
	}
		task Stable(shot){
		let c = 0;
		let cc = 0;
			while(!Obj_IsDeleted(shot)){
			if(c%9==0)
			{
				spongy(shot,0.04,4);
				cc++;
				ObjShot_SetGraphic(shot,rainset[cc%7]);
			}
			c++;
			yield;
			}
		}
}

task PurpleOrb(x,y){
	let sangle = rand(0,19);
	let maxspeed = rand(1,1.5);
	PlaySound("Eff7",95,0);
	TExpan(x,y,10,30,255,0,255);
	ascent(i in 0..20){
	let shot = OrbBullet(x,y,0,i*18+sangle,403,12);
	ObjMove_AddPatternA2(shot,0,4,NO_CHANGE,-4/75,0,maxspeed);
	}
}

task RedOrb(x,y){
	let sangle = rand(-3,3);
	let maxspeed = rand(3,3.5);
	PlaySound("Eff7",95,0);
	TExpan(x,y,10,30,255,0,128);
	ascent(i in 0..20){
	let shot = OrbBullet(x,y,0,0,400,12);
	ObjMove_AddPatternA2(shot,0,0.5,i*18+sangle+GetAngleToPlayer(shot),4/120,0,maxspeed);
	}
}

task ChooseOrb(type){
	if(type=="R"){
		PlaySound("Eff7",95,0);
		ascent(i in 0..24){
		let shot = OrbBullet(GetEnemyX,GetEnemyY,0,0,401,12);
		ObjMove_AddPatternA2(shot,0,4,-90+i*8,-4/60,0,0.5);
		ObjMove_AddPatternA2(shot,50,NO_CHANGE,NO_CHANGE,4/60,0,5);
		}
	}
	else if(type=="L"){
	PlaySound("Eff7",95,0);
		ascent(i in 0..24){
		let shot = OrbBullet(GetEnemyX,GetEnemyY,0,0,401,12);
		ObjMove_AddPatternA2(shot,0,4,-90-i*8,-4/60,0,0.5);
		ObjMove_AddPatternA2(shot,50,NO_CHANGE,NO_CHANGE,4/60,0,5);
		}
	}
}

function OrbBullet(x,y,spd,dir,color,delay){
	let obj = ShotA1(x,y,spd,dir,color,0,delay);
	ManageEffect;
	return obj;
	task ManageEffect{
		let c = 0;
		while(!Obj_IsDeleted(obj)){
		if(c%9==0){spongy(obj,0.04,4);}
		c++;
		yield;
		}
	}
}

task StaticLaser{
	StaticL("L1");
	StaticL("L2");
	StaticL("R1");
	StaticL("R2");
	
		task StaticL(type){
			let obj;
			let c = 0;
			if(type=="L1"){
			obj = CreateStraightLaserA1(stgx-100,100,180,0,10,999999,9,60);
				ascent(i in 0..60){ObjLaser_SetLength(obj,i*10);yield;}
			}
			if(type=="L2"){
			obj = CreateStraightLaserA1(stgx-75,150,165,0,10,999999,9,60);
				ascent(i in 0..60){ObjLaser_SetLength(obj,i*10);yield;}
			}
			if(type=="R1"){
			obj = CreateStraightLaserA1(stgx+100,100,0,0,10,999999,9,60);
				ascent(i in 0..60){ObjLaser_SetLength(obj,i*10);yield;}
			}
			if(type=="R2"){
			obj = CreateStraightLaserA1(stgx+75,150,15,0,10,999999,9,60);
				ascent(i in 0..60){ObjLaser_SetLength(obj,i*10);yield;}
			}
			while(!Obj_IsDeleted(obj)){
			ObjLaser_SetRenderWidth(obj,rand(14,16));
			let angle = ObjStLaser_GetAngle(obj);
			let point = GetLineBorderPoint(ObjMove_GetX(obj), ObjMove_GetY(obj), ObjMove_GetX(obj)+600*cos(angle), ObjMove_GetY(obj)+600*sin(angle));
			if(c%10==0){
			PlaySound("BulletWave",90,0);
			ShotA2(point[0],point[1],0,angle+180,0.05,5,114,0,8);
			}
			c++;
			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(0);
		TAccel(50,10,0.02,120);
		MapleExplode(180,1,2,5);
		PlaySound("enemoff",95,0);
		wait(105);
		PlaySound("enemoff",100,0);
		MapleExplode(20,100,1,8);
		Obj_Delete(objBoss);
		wait(30);
		SetAutoDeleteObject(true);
		loop(180){yield;}
		CloseScript(GetOwnScriptID());
		return;
	}