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

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

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


@Event {

	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(2500);
	}
	
	case(EV_REQUEST_TIMER){
		SetScriptResult(35)
	}
	
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(50000);
	}
	
}

@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), "MAGENTA", "ROSE", "BRIGHT", 6);
	FWait(60);
	ObjEnemyBossScene_StartSpell(objScene);
	TDrawCutin01(objBoss, "Inversion \"Reality and Fantasy\"");
	TEnd();
	FWait(90);
	TShootCircles();
	FWait(30);
	TCharge01(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), "AQUA", "BLUE", "BRIGHT", 6);
	FWait(60);
	TShoot01();
	TShoot02();
	SetCommonData("bossAtkAnim", true);
}

task TShootCircles {
	let startSize = 0.25;
	let typeCircle = "outer";
	let amount = 5;
	
	loop (2) {
	
		ascent (i in 0..amount) {
			let circle = TCircle(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), startSize);
			circles = circles ~ [circle];
			TMoveCircle(circle, i, startSize, typeCircle);
		}
		
		typeCircle = "inner";
	}
	
	task TMoveCircle (obj, i, ss, type) {
		let spin = 0;
		let size = 0;
		let count = 0;
		let limit = 0;
		let modifySin = 0;
		
		if (type == "outer") {
			limit = 275;
			modifySin = 180;
		} else {
			limit = 150;
		}
		
		while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && !Obj_IsDeleted(obj)) {
			let position = (360 / amount) * i;
			
			if (type == "outer") {
				ObjRender_SetPosition(obj,
					GetCommonData("bossX", ObjMove_GetX(objBoss)) + size * cos(position + (spin / 2.75)),
					GetCommonData("bossY", ObjMove_GetY(objBoss)) + size * sin(position + (spin / 2.75)),
					0);
			} else {
				ObjRender_SetPosition(obj,
					GetCommonData("bossX", ObjMove_GetX(objBoss)) + size * cos(position - (spin / 2.75)),
					GetCommonData("bossY", ObjMove_GetY(objBoss)) + size * sin(position - (spin / 2.75)),
					0);
			}
			
			spin ++;
			
			if (size < limit) {
				size += limit / 60;
			} else {
				ObjEnemy_SetDamageRate(objBoss, 100, 100);
			}
			
			if (count < 16) {
				count ++;
			} else {
				let scale = 0.0875 * sin((spin * 1.11) + modifySin) + (ss);
				ObjRender_SetScaleXYZ(obj, scale, scale, 0);
			}
			
			yield;
		}
		
	}
	
	function TCircle (x, y, startSize) {
		let cImg = ObjPrim_Create(OBJ_SPRITE_2D);
		let path = "script/yukari_yakumo_fuchsia/img/circle.png";
		ObjPrim_SetTexture(cImg, path);
		ObjSprite2D_SetSourceRect(cImg, 0, 0, 512, 512);
		ObjSprite2D_SetDestCenter(cImg);
		ObjRender_SetScaleXYZ(cImg, 0, 0, 0);
		ObjRender_SetAlpha(cImg, 128);
		FSetColor(cImg, "INDIGO", "BRIGHT");
		Obj_SetRenderPriorityI(cImg, GetShotRenderPriorityI() - 3);
		ObjRender_SetPosition(cImg, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0);
		TSize();
		return cImg;
		
		task TSize () {
			
			let n = 15;
			ascent (i in 0..n) {
				let k = i / n;
				k = 1 - (1 - k)^2;
				let scale = (startSize * k) + (0 * (1 - k));
				ObjRender_SetScaleXYZ(cImg, scale, scale, 0);
				FBreakIfFalse(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0);
				yield;
			}
			
			while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) { yield; }
			
			let prevScale = ObjRender_GetScaleX(cImg);
			
			descent (i in 0..n) {
				let k = i / n;
				k = k^2;
				let scale = (prevScale * k) + (0 * (1 - k));
				ObjRender_SetScaleXYZ(cImg, scale, scale, 0);
				FBreakIfFalse(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0);
				yield;
			}
			
			Obj_Delete(cImg);
		}
		
	}
	
}

task TShoot01 () {
	let spin = 0;
	let waitTime = 3;
	
	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		
		let angle = 0;
		while (angle < 360) {
			let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 8, angle + spin * 3, -0.1, 6, PELLET_BLUE, 5);
			Obj_SetRenderPriorityI(obj, GetShotRenderPriorityI() - 2);
			TInvert(obj);
			angle += 360 / 14;
		}
		
		spin += waitTime;
		FWait(waitTime);
	}
	
}

task TShoot02 () {
	let spin = 0;
	let waitTime = 10;
	let count = 0;
	
	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	
		let angle = 0;
		while (angle < 360) {
			let sum = angle - spin * 1.93;
			CreateShotA2(ObjMove_GetX(objBoss) + 25 * cos(sum), ObjMove_GetY(objBoss) + 25 * sin(sum), 0, sum, 0.05, 1.5, BUTTERFLY_PURPLE, 15);
			angle += 360 / 5;
		}	
		
		FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/tan00.wav", 15);
		
		if (count % 12 == 0) {
			let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, GetAngleToPlayer(objBoss), -0.2, 1.5, BUBBLE_BLUE, 15);
			Obj_SetRenderPriorityI(obj, GetShotRenderPriorityI() - 1);
			obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, GetAngleToPlayer(objBoss) + 30, -0.2, 1.5, BUBBLE_BLUE, 15);
			Obj_SetRenderPriorityI(obj, GetShotRenderPriorityI() - 1);
			obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, GetAngleToPlayer(objBoss) - 30, -0.2, 1.5, BUBBLE_BLUE, 15);
			Obj_SetRenderPriorityI(obj, GetShotRenderPriorityI() - 1);
			FPlaySoundDelay(objBoss, "script/yukari_yakumo_fuchsia/se/tan01.wav", 15);
		}
		
		count ++;
		spin += waitTime;
		FWait(waitTime);
	}
	
}

task TInvert (obj) {
	let result = "negative";
		
	while (ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0 && !Obj_IsDeleted(obj)) {
		
		ascent (i in 0..length(circles)) {
		
			if (FGetGapLength(ObjRender_GetX(circles[i]), ObjRender_GetY(circles[i]), 
			ObjRender_GetX(obj), ObjRender_GetY(obj)
			) < (ObjRender_GetScaleX(circles[i]) * 250)) {
				ObjShot_SetIntersectionEnable(obj, true);
				ObjRender_SetAlpha(obj, 255);
				result = "positive";
				break;
			}
			
		}
		
		if (result != "positive") {
			ObjShot_SetIntersectionEnable(obj, false);
			ObjRender_SetAlpha(obj, 0);
		}
		
		result = "negative";
		yield;
	}
	
}

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;
}



	
	
	
	
	
	
	
	
	
	
	
	
	
	




