#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#System["./../../system/Default_System.txt"]

#include "./../../system/functions/functions_world.txt"

let BossScene;
let MBossScene;
let EnmS = false;
let effectS = true;

@Event
{
	alternative(GetEventType())

		case(EV_PAUSE_ENTER)
			{
			PauseMusic;
			}
		case(EV_PAUSE_LEAVE)
			{
			ResumeMusic;
			}
		case(EV_PLAYER_DIE)
			{
			let shot = CreateShotA1(GetPlayerX,GetPlayerY,0,0,BUBBLE[SHOT_RED],0);
			Obj_SetValue(shot,"IS_BROKEN",true);
			Obj_SetVisible(shot,false);
			Obj_DelayDelete(shot,2);
			}
		case(666)
			{	
			TWing;
			}
		case(667)
			{	
			TArm;
			}
		case(668)
			{	
			TBody;
			}

}

task TMain
{
	yield;
	LoadBoss;
	SetCommonData("BGM_START",false);
	let Back = LoadScript(GetCurrentScriptDirectory ~ "boss/Background.txt");
	StartScript(Back);

	loop(0)
		{
		let Enemy = LoadScript(GetCurrentScriptDirectory ~ "Enemy.txt");
		StartScript(Enemy);	
		loop(20){yield;}
		PlayMusic("bgm_Stage");
		while(!IsCloseScript(Enemy)){yield;}
		Obj_Delete(Enemy);
		}

	ObjEnemyBossScene_Regist(BossScene);
	loop(0)
		{
		while(GetCommonData("BGM_START",false) == false){yield;}
		loop(120){yield;}
		}
	EndCurrentMusic;
	PlayMusic("bgm_Boss");
	while(!Obj_IsDeleted(BossScene)){yield;}

	effectS = false;
	SetCommonData("BACKGROUND_END",2);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	loop(150){yield;}
	EndCurrentMusic;
	CloseScript(Back);
	CloseStgScene;
}
task LoadBoss
{
	BossScene = ObjEnemyBossScene_Create();
	let path = GetCurrentScriptDirectory ~ "boss/";
	ObjEnemyBossScene_Add(BossScene, 0, path ~ "Init.txt");
	ObjEnemyBossScene_Add(BossScene, 0, path ~ "Base1.txt");
	ObjEnemyBossScene_Add(BossScene, 0, path ~ "Spell1.txt");
	ObjEnemyBossScene_Add(BossScene, 1, path ~ "Base2.txt");
	ObjEnemyBossScene_Add(BossScene, 1, path ~ "Spell2.txt");
	ObjEnemyBossScene_Add(BossScene, 2, path ~ "Base3.txt");
	ObjEnemyBossScene_Add(BossScene, 2, path ~ "Spell3.txt");
	ObjEnemyBossScene_Add(BossScene, 3, path ~ "Base4.txt");
	ObjEnemyBossScene_Add(BossScene, 3, path ~ "Spell4.txt");
	ObjEnemyBossScene_Add(BossScene, 3, path ~ "End.txt");
	ObjEnemyBossScene_LoadInThread(BossScene);
}

task TWing
	{
	let objEnemy = GetEnemyBossObjectID[0];
	let x = ObjMove_GetX(objEnemy);
	let y = ObjMove_GetY(objEnemy);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "boss/img/sprite_wing.png");
	Obj_SetRenderPriority(obj, 0.32);
	ObjSpriteList2D_SetSourceRect(obj,256,1,512,127);
	ObjSpriteList2D_SetDestRect(obj,0,-64,256,64);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory ~ "boss/img/sprite_wing.png");
	Obj_SetRenderPriority(obj2, 0.32);
	ObjSpriteList2D_SetSourceRect(obj2,256,1,512,127);
	ObjSpriteList2D_SetDestRect(obj2,0,-64,256,64);
	ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);
	ObjRender_SetColorHSV(obj2,60,45,255);
	

	TFlap;

	while(effectS == true)
		{
		while(!Obj_IsDeleted(objEnemy))
			{
			x = ObjMove_GetX(objEnemy);
			y = ObjMove_GetY(objEnemy);
			if(effectS == false){break;}
			yield;
			}
		while(length(GetEnemyBossObjectID) == 0){yield;if(effectS == false){break;}}
		if(length(GetEnemyBossObjectID) > 0)
			{
			objEnemy = GetEnemyBossObjectID[0];
			}
		}
	Obj_Delete(obj);
	Obj_Delete(obj2);
	task TFlap
		{
		let t = -1;
		while(!Obj_IsDeleted(BossScene))
			{
			t+=1;
			ObjSpriteList2D_ClearVertexCount(obj);
			ObjSpriteList2D_ClearVertexCount(obj2);

			let tA = 1-((1-min(t,45)/45)^2);

			descent(s in 0..2)
				{
				let sc = 1.25+s*0.25;

				ObjRender_SetScaleY(obj2,-tA*sc);
				ObjRender_SetPosition(obj2,x,y-17,0);
				ObjRender_SetAngleZ(obj2,270-tA*90-5*sin(t+15-s*75));
				ObjRender_SetScaleX(obj2,-sc);
				ObjRender_SetAlpha(obj2,75*sc+15*sin(t+s*30));
				ObjSpriteList2D_AddVertex(obj2);

				ObjRender_SetScaleY(obj2,-tA*sc);
				ObjRender_SetPosition(obj2,x,y-17,0);
				ObjRender_SetAngleZ(obj2,90+tA*90+5*sin(t+15-s*75));
				ObjRender_SetScaleX(obj2,sc);
				ObjRender_SetAlpha(obj2,75*sc+15*sin(t+s*30));
				ObjSpriteList2D_AddVertex(obj2);
				}

			ObjRender_SetScaleY(obj,-tA);
			ObjRender_SetPosition(obj,x,y-17,0);

			ObjRender_SetAngleZ(obj,270-tA*90-5*sin(t));
			ObjRender_SetScaleX(obj,-1);
			ObjSpriteList2D_AddVertex(obj);

			ObjRender_SetAngleZ(obj,90+tA*90+5*sin(t));
			ObjRender_SetScaleX(obj,1);
			ObjSpriteList2D_AddVertex(obj);
			yield;
			}
		}
	}

task TArm
	{
	let objEnemy = GetEnemyBossObjectID[0];
	let x = ObjMove_GetX(objEnemy);
	let y = ObjMove_GetY(objEnemy);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory ~ "boss/img/fist.png");
	Obj_SetRenderPriority(obj2, 0.36);
	ObjSpriteList2D_SetSourceRect(obj2,0,0,128,128);
	ObjSpriteList2D_SetDestRect(obj2,-64,0,64,128);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "boss/img/arm.png");
	Obj_SetRenderPriority(obj, 0.36);
	ObjSpriteList2D_SetSourceRect(obj,0,0,256,256);
	ObjSpriteList2D_SetDestRect(obj,-128,0,128,256);

	TFlap;

	while(effectS == true)
		{
		while(!Obj_IsDeleted(objEnemy))
			{
			x = ObjMove_GetX(objEnemy);
			y = ObjMove_GetY(objEnemy);
			if(effectS == false){break;}
			yield;
			}
		while(length(GetEnemyBossObjectID) == 0){yield;if(effectS == false){break;}}
		if(length(GetEnemyBossObjectID) > 0)
			{
			objEnemy = GetEnemyBossObjectID[0];
			}
		}
	task TFlap
		{
		let t = -1;
		while(!Obj_IsDeleted(BossScene))
			{
			t+=1;
			ObjSpriteList2D_ClearVertexCount(obj);
			ObjSpriteList2D_ClearVertexCount(obj2);
			let tA = max(0,1-t/20);
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				let xA = x+dir*tA*64-2+dir*3;
				let yA = y-14;
				let dA = 256*0.55;
				let aA = -dir*50+dir*5*sin(t);

				let xB = xA+dA*cos(aA+90)-dir*24;
				let yB = yA+dA*sin(aA+90);
				let dB = 256*0.5;
				let aB = aA-dir*105+dir*5*sin(t*1.25);

				ObjRender_SetPosition(obj,xA,yA,0);
				ObjRender_SetAngleZ(obj,aA);
				ObjRender_SetScaleXYZ(obj,-dir*0.225,0.55,0);
				ObjSpriteList2D_AddVertex(obj);

				ObjRender_SetPosition(obj2,xB+dB*cos(aB+90)-dir*28,yB+dB*sin(aB+90)+28,0);
				ObjRender_SetAngleZ(obj2,aB-dir*5);
				ObjRender_SetScaleXYZ(obj2,dir*0.5,0.5,0);
				ObjSpriteList2D_AddVertex(obj2);

				ObjRender_SetPosition(obj,xB,yB,0);
				ObjRender_SetAngleZ(obj,aB);
				ObjRender_SetScaleXYZ(obj,dir*0.4,0.5,0);
				ObjSpriteList2D_AddVertex(obj);
				}
			yield;
			}
		}
	Obj_Delete(obj);
	Obj_Delete(obj2);
	}

task TBody
	{
	let objEnemy = GetEnemyBossObjectID[0];
	let x = ObjMove_GetX(objEnemy);
	let y = ObjMove_GetY(objEnemy);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "boss/img/hisou.png");
	let sc = 1.25;	
	ObjRender_SetScaleXYZ(obj,sc,sc,1);
	Obj_SetRenderPriority(obj, 0.35);

	TFlap;

	while(effectS == true)
		{
		while(!Obj_IsDeleted(objEnemy))
			{
			x = ObjMove_GetX(objEnemy);
			y = ObjMove_GetY(objEnemy);
			if(effectS == false){break;}
			yield;
			}
		while(length(GetEnemyBossObjectID) == 0){yield;if(effectS == false){break;}}
		if(length(GetEnemyBossObjectID) > 0)
			{
			objEnemy = GetEnemyBossObjectID[0];
			}
		}
	task TFlap
		{
		let t = -1;
		let xA = 13;	
		while(!Obj_IsDeleted(BossScene))
			{
			t+=1;
			ObjSpriteList2D_ClearVertexCount(obj);

			local
				{
				ascent(dir in 0..2)
					{
					dir = dir*2-1;
					let tA = 1-min(max(0,t-120),20)/20;
					ObjSpriteList2D_SetSourceRect(obj,54,68,84,108);
					ObjSpriteList2D_SetDestRect(obj,-20,-30,20,30);
					ObjRender_SetScaleXYZ(obj,1,1.5,0);
					ObjRender_SetPosition(obj,x+dir*xA*sc+dir*4,y-2+tA*GetStgFrameHeight+sc*(4+28+98+15),0);
					ObjSpriteList2D_AddVertex(obj);
					}
				}
			local
				{
				ascent(dir in 0..2)
					{
					dir = dir*2-1;
					let tA = 1-min(max(0,t-140),20)/20;
					ObjSpriteList2D_SetSourceRect(obj,124+dir*20-16,166,124+dir*20+16,190);
					ObjSpriteList2D_SetDestRect(obj,-16,-12,16,12);
					ObjRender_SetScaleXYZ(obj,1.25,1,0);
					ObjRender_SetPosition(obj,x+dir*xA*sc+2+dir*8,y+tA*GetStgFrameHeight+sc*(6+28+108+15+20),0);
					ObjSpriteList2D_AddVertex(obj);
					}
				}
			local
				{
				ascent(dir in 0..2)
					{
					dir = dir*2-1;
					let tA = 1-min(max(0,t-90),20)/20;
					ObjSpriteList2D_SetSourceRect(obj,124+dir*60-20,144,124+dir*60+20,204);
					ObjSpriteList2D_SetDestRect(obj,-15,-20,15,20);
					ObjRender_SetScaleXYZ(obj,1,1.55,0);
					ObjRender_SetPosition(obj,x+dir*xA*sc+1,y+tA*GetStgFrameHeight+4*sc+48*sc+38*sc,0);
					ObjSpriteList2D_AddVertex(obj);
					}
				}
			local
				{
				let tA = 1-min(max(0,t-50),20)/20;
				ObjSpriteList2D_SetSourceRect(obj,96,64,190,132);
				ObjSpriteList2D_SetDestRect(obj,-52,-34,52,34);
				ObjRender_SetScaleXYZ(obj,1,1.4,0);
				ObjRender_SetPosition(obj,x,y+tA*GetStgFrameHeight+6*sc+48*sc,0);
				ObjSpriteList2D_AddVertex(obj);
				}
			local
				{
				let tA = 1-min(t,20)/20;
				ObjSpriteList2D_SetSourceRect(obj,96,0,190,64);
				ObjSpriteList2D_SetDestRect(obj,-52,-32,52,32);
				ObjRender_SetScaleXYZ(obj,1.25,1.25,0);
				ObjRender_SetPosition(obj,x,y+tA*GetStgFrameHeight+6*sc,0);
				ObjSpriteList2D_AddVertex(obj);
				}
			yield;
			}
		}
	Obj_Delete(obj);
	}