#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include"./functions.txt"

let koi;
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	TXY;
	
	TEnemyWork;
	ObjEnemy_RenderOku(objEnemy);
	ObjEnemy_SetGlow(objEnemy,false);
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(80);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
	case(EV_START_BOSS_SPELL)
		{
		SetScriptResult(true);
		}
}

task TEnemyWork
{
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	ObjMove_SetPosition(objEnemy,GetStgFrameWidth*0.5,-64);
	
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,15,5);
	loop(105){yield;}

	TSpellName("Not a Nonspell \"Sun Want to Destroy Moon\"");
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	loop(60){yield;}
	
	ObjEnemy_SetRender(objEnemy,OKU_SPELL);
	ObjEnemy_SetDamageRate(objEnemy,5,5);
	loop(15){yield;}
	
	let spread = [24];
	TSpam;
	loop(135){yield;}

	let koi = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(koi);
	ObjEnemy_RenderKoi(koi);
	ObjEnemy_SetGlow(koi,false);
	Obj_SetRenderPriority(koi,0.6);
	ObjMove_SetPosition(koi,GetStgFrameWidth-96,GetStgFrameHeight-240);
	ascent(i in 0..120)
		{
		ObjRender_SetAlpha2(koi,(i+1)*255/120);
		yield;
		}
	loop(30){yield;}
	ObjEnemy_SetGlow(koi,true);
	let timeM = 105;
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) > 0)
		{
		ObjEnemy_SetRender(koi,KOI_SPIN2);
		loop(60){yield;if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}
		let angleP = rand(-1,1)*30;
		ascent(i in -1..2)
			{	
			TLightning(i*12,angleP,0);		
			}
		loop(15){yield;if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}
		ObjEnemy_SetRender(koi,KOI_ARM);
		loop(floor(timeM*0.5)){yield;if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}
		ObjMove_SetDestAtWeight(koi,GetStgFrameWidth-96+rand(-1,1)*16,GetStgFrameHeight-240+rand(-1,1)*16,20,4);
		loop(ceil(timeM*0.5)){yield;if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0){break;}	
		yield;
		if(timeM > 0)
			{
			timeM-=20;
			}
		}
	loop(75){yield;}
	ObjEnemy_SetRender(koi,KOI_SPIN2);
	loop(60){yield;}
	ascent(i in -1..2)
		{	
		TLightning(i*12,0,1);		
		}
	loop(15){yield;}
	ObjEnemy_SetRender(koi,KOI_ARM);
	loop(50){yield;}
	ObjMove_SetAngle(objEnemy,atan2(enmy-(ObjMove_GetY(koi)-40),enmx-ObjMove_GetX(koi)));
	ObjMove_SetSpeed(objEnemy,175/10);
	PlaySound("se_PlayerDie",90);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(60){yield;}

	Obj_SetValue(objScene,"KOI_POS",[ObjMove_GetX(koi),ObjMove_GetY(koi)]);
	ObjEnemy_SetLife(objEnemy,0);
	Obj_Delete(objEnemy);
	Obj_Delete(koi);
	yield;
	CloseScript(GetOwnScriptID);

	task TLightning(y,angleP,type)
		{
		let dist = 175;
		let time = 10;
		let xM = ObjMove_GetX(koi)+dist*0.5*cos(angleP);
		let yM = ObjMove_GetY(koi)-40+dist*0.5*sin(angleP);
		PlaySound("se_LaserStart",80);
		let laser1 = CreateLaserA2(xM+y*cos(angleP+90),yM+y*sin(angleP+90),dist/time,angleP+180,dist,20,10^10,LIGHTNING,0);
		loop(45){yield;}
		PlaySound("se_LaserShort",80);
		let laser2 = CreateLaserA2(ObjMove_GetX(laser1),ObjMove_GetY(laser1),dist/time,0,dist,20,10^10,LIGHTNING,0);
		if(type == 0)
			{
			angleP = atan2(GetPlayerY-yM,GetPlayerX-xM);
			}	
		else
			{
			angleP = atan2(enmy-yM,enmx-xM);
			}
		ObjStLaser_SetAngle(laser2,angleP);
		ObjMove_SetAngle(laser2,angleP);
		Obj_Delete(laser1);
		loop(time){yield;}
		ObjMove_SetSpeed(laser2,dist/time);
		if(type != 0){return;}
		let counter = floor(rand(0,6));
		dist = rand(0.125,0.875)*dist;
		let t = 6;
		if(y == 0)
			{
			t = 3;
			}
		while(!Obj_IsDeleted(laser2))
			{
			counter+=1;
			if(counter%t == 0)
				{
				let shot = CreateShotA2(ObjMove_GetX(laser2)+dist*cos(angleP),ObjMove_GetY(laser2)+dist*sin(angleP),0,rand(0,360),1/(75+absolute(y/12)*90),2.75-absolute(y/12)*2,BL_SMALL_ORANGE,8);
				if(y == 0)
					{
					ObjMove_SetAngle(shot,ObjMove_GetAngle(laser2)+180+rand(-1,1)*3);
					ObjShot_SetGraphic(shot,BL_SMALL_YELLOW);
					}
				}
			yield;
			}
		}
	task TSpam
		{
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) > 0)
			{	
			ascent(i in 0..spread[0])
				{
				let angle = i*360/spread[0]+180/spread[0]+90;
				let shot = CreateShotA2(ObjMove_GetX(objEnemy)+8*cos(angle),ObjMove_GetY(objEnemy)-32+8*sin(angle),1.5,angle,1/7,10,LARGE_YELLOW,0);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				}
			loop(3){yield;}
			PlaySound("se_CuteShotLong",65);
			}
		}
}	