#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["yy_s3"]

#include"./shot/ZUNShot_Const.txt"
#include"./function/index.txt"

//----------------------------------------------------
//global variables
//----------------------------------------------------
let objBoss;
let objScene = GetEnemyBossSceneObjectID();
let boundary;


@Event {

	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(3500);
	}
	
	case(EV_REQUEST_TIMER){
		SetScriptResult(35)
	}
	
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(100000);
	}
	
}

@Initialize {
	TMainTask();
}

@MainLoop {
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 26);
	
	yield;
}

task TMainTask () {
	objBoss = TDrawBoss();
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjMove_SetDestAtWeight(objBoss, halfStageFrameWidth, GetStgFrameHeight() / 3, 10, 15);
	while (IsPlayerSpellActive == true || ObjMove_GetSpeed(objBoss) > 0) { yield; }
	TCharge01(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), "RED", "VIOLET", "BRIGHT", 6);
	FWait(60);
	ObjEnemyBossScene_StartSpell(objScene);
	TDrawCutin01(objBoss, "Yakumo Spirit \"Her Playful Psyche\"");
	TEnd();
	FWait(180);
	ObjEnemy_SetDamageRate(objBoss, 100, 100);
	TShoot01();
}

task TShoot01 () {
	let sound = FPlaySoundNR("script/yukari_yakumo_fuchsia/se/bigBoundary.wav");
	ObjSound_SetVolumeRate(sound, 100);
	boundary = TDrawBoundarySideways(halfStageFrameWidth + 8, 50, 2, 3.25, 30);
	FWait(30);
	TCharge01(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), "PURPLE", "INDIGO", "BRIGHT", 6);
	FWait(60);
	let laserWaitTime = 7;
	let bulletWaitTime = 4;
	let roseBulletTotal = 270;
	let n = 2;
	let d = 5;
	let kRose = n / d;
	TLasers();
	
	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		let spin = 0;
		let radius = 100;
	
		FWait(90);
		SMovement(15, 5);
		
		loop (90 / bulletWaitTime) {
			
			let angle = 0;
			while (angle < 360) {
				let calc = angle + spin * 7;
				CreateShotA2(ObjMove_GetX(objBoss) + radius * cos(calc),
							ObjMove_GetY(objBoss) + radius * sin(calc),
							6, calc, -0.25, 1.5, BUTTERFLY_PURPLE, 5);
				angle += 360 / 5;
			}
			
			FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/tan00.wav", 5);
			radius -= 3;
			spin += bulletWaitTime;
			FWait(bulletWaitTime);
			FBreakIfFalse(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0);
		}
		
		let amount = 0;
		
		ascent (i in 0..roseBulletTotal) {
			let result = (i * (d + 1)) * 1.5;
			let posX = ObjMove_GetX(objBoss) + 100 * sin(kRose * result) * cos(result);
			let posY = ObjMove_GetY(objBoss) + 100 * sin(kRose * result) * sin(result);
			
			let angle = FGetGapAngle(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), posX, posY);
			let dist = FGetGapLength(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), posX, posY);
			
			CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), (dist / 100) + 0.5, angle, DOT_PURPLE, 15);
		}
		
		FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/kira03.wav", 15);
		FWait(120);
	}
	
	task TLasers () {
	
		while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
			let obj = CreateLooseLaserA1(halfStageFrameWidth + 10 + rand(-150, 150), 50 + rand(-10, 10), rand(8, 12), 90 + rand(-5, 5), 75, 10, A_LARGE_AQUA, 5);
			ObjMove_SetAcceleration(obj, -0.275);
			ObjMove_SetMaxSpeed(obj, 0.5);
			ObjMove_AddPatternA2(obj, 60, NO_CHANGE, NO_CHANGE, 0.2, 0, rand(3, 5));
			FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/lazer00.wav", 5);
			FWait(laserWaitTime);
		}
	
	}
	
}

task SMovement (weight, speed) {
	let resultx = rand(0, 1);
	resultx = round(resultx);
	let resulty = rand(0, 1);
	resulty = round(resulty);
	let posx = rand(150, -150);
	
	if (posx < 75 && posx > -75) {
	
		if (resultx == 1) {
			SetCommonData("toRight", GetCommonData("toRight", 0) + 1);
			
			if (GetCommonData("toRight", 0) % 4 == 0) {
				posx = -75;
			} else {
				SetCommonData("countForRight", GetCommonData("countForRight", 0) + 1);
				posx = 75;
				
				if (GetCommonData("countForRight", 0) % 2 == 0 || GetCommonData("countForRight", 0) % 7 == 0) {
					posx = -75;
				}
				
			}
			
		} else {
			posx = -75;
		}
		
	}
	
	if ((ObjMove_GetX(objBoss) + posx) <= 50 || (ObjMove_GetX(objBoss) + posx) >= (GetStgFrameWidth() - 50)) {
		posx= - posx;
	}
	
	let posy = rand(25, -25);
	
	if (posy < 25 && posy > -25) {
	
		if (resulty == 1) {
			SetCommonData("toDown", GetCommonData("toDown", 0) + 1);
			
			if (GetCommonData("toRight", 0) % 4 == 0) {
				posy = -15;
			} else {
				SetCommonData("countForDown", GetCommonData("countForDown", 0) + 1);
				posy = 15;
				
				if (GetCommonData("countForDown", 0) % 2 == 0 || GetCommonData("countForDown", 0) % 6 == 0) {
					posy = -15;
				}
				
			}
		
		}
		
	}
	
	if ((ObjMove_GetY(objBoss) + posy) <= 32 || (ObjMove_GetY(objBoss) + posy) >= GetStgFrameHeight() / 3) {
		posy = -posy;
	}
	
	ObjMove_SetDestAtWeight(objBoss, ObjMove_GetX(objBoss) + posx, ObjMove_GetY(objBoss) + posy, weight, speed);
}

task TEnd () {

	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) { yield; }
	
	TCloseBoundary(boundary, 30);
	TExplosionA(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, 0.5);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	loop (30) {
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	
	TExplosionA(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, 0.5);
	Obj_Delete(objBoss);
	FPlaySoundNR("script/yukari_yakumo_fuchsia/se/enep01.wav");
	
	loop (90) {
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	
	yield;
	CloseScript(GetOwnScriptID());
	return;
}



	
	
	
	
	
	
	
	
	
	
	
	
	
	




