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

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

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


@Event {
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(2500);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(30)
	}
}

@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);
	TEnd();
	
	while (ObjMove_GetSpeed(objBoss) > 0) { yield; }
	
	FWait(60);
	TShoot01();
	FWait(90);
	ObjEnemy_SetDamageRate(objBoss, 100, 100);
}

task TShoot01 () {
	let n = 5;
	let waitTime = 5;
	let spin = 0;
	let angleBullets = 3;
	let angleRange = 45;
	let count = 0;
	
	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		
		loop (60) {
		
			ascent (i in 1..(n + 1)) {
				let calc = (360 / n) * i;
				let resultCalc = calc + 180 * sin(spin);
				
				let angle = -(angleRange / 2);
				while (angle <= (angleRange / 2)) {
					
					let xy = [ObjMove_GetX(objBoss) + 50 * cos(resultCalc),
							  ObjMove_GetY(objBoss) + 50 * sin(resultCalc)];
							  
					CreateShotA2(xy[0], xy[1], 1, resultCalc + angle, 0.025, 2.875, KUNAI_AQUA, 5);
					angle += angleRange / (angleBullets - 1);
				}
				
			}
			
			FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/tan00.wav", 5);
			spin += waitTime;
			count ++;
			
			if (count % 30 == 0) { SMovement(10, 7.5); }
			
			FWait(waitTime);
			FBreakIfFalse(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0);
		}
		
		FBreakIfFalse(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0);
		
		let angle = 0;
		while (angle < 360) {
			CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 1, angle + GetAngleToPlayer(objBoss), 0.05, 2.25, KUNAI_BLUE, 5);
			angle += 360 / 60;
		}
		
		FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/tan01.wav", 5);
		FWait(75);
	}
	
}

task SMovement (weight, speed) {
	let resultx = rand(0, 1);
	resultx = round(resultx);
	let resulty = rand(0, 1);
	resulty = round(resulty);
	let posx = rand(100, -100);
	
	if (posx < 50 && posx > -50) {
	
		if (resultx == 1) {
			SetCommonData("toRight", GetCommonData("toRight", 0) + 1);
			
			if (GetCommonData("toRight", 0) % 4 == 0) {
				posx = -50;
			} else {
				SetCommonData("countForRight", GetCommonData("countForRight", 0) + 1);
				posx = 50;
				
				if (GetCommonData("countForRight", 0) % 2 == 0 || GetCommonData("countForRight", 0) % 7 == 0) {
					posx = -50;
				}
				
			}
			
		} else {
			posx = -50;
		}
		
	}
	
	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; }
	
	SetCommonData("bossAtkAnim", false);
	TExplosionA(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 10, 0.5);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(objBoss);
	
	loop (60) {
		yield;
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	}
	
	CloseScript(GetOwnScriptID());
	return;
}









	
	
	
	
	
	
	
	
	
	
	
	
	
	




