
#ScriptVersion[3]
#Title["YuyukoS6"]
#Text["YuyukoS6"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]
#Player["./MoF_Reimu/Reimu.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let action = false;
let objScene=GetEnemyBossSceneObjectID();
let soularray = [];
let count = 0;
let a_count = 0;
let totalangle = 0;
let mainangle = 0;
let cangle = 0;
let dienumber = 0;
let plusarray = [12,18,30,60,180];
let killerappear = 0;
let plusangle = 0;
let r = 0;
@Initialize
{
Sound(GCSD ~ "Little.mp3");
SetBoss("Yuyuko",true);
SetCommonData("Life",1);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	if(GetCommonData("IsFan",false)==true){
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss)-90,ObjMove_GetY(objBoss),26*3.5);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss)+90,ObjMove_GetY(objBoss),26*3.5);
	}
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(4500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(105);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(35000000);
	}
		case(EV_PLAYER_REBIRTH)
	{
		if(GetPlayerLife>=0)
		{
			dienumber = 0;
			ascent(i in 0..length(soularray))
			{
			Obj_SetValue(soularray[i],"alive",1);
			}
			count = 0;
			r = 0;
		}
	}
}

//----------------------------------------------------
task mainTask{
	magiccircle(objBoss,1,-1);
	DamageRateAtFrame(47,10,660);
	ObjMove_SetDestAtWeight(objBoss,stgx,120,10,7);
	wait(60);
	PlaySound("Charge3",100,0);
	RayCharge("RED",2,60);
	RayCharge("BLUE",2,60);
	wait(120);
	Cutin("Eternal Sign \"Butterfly Dream\"",15,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	Sound(GCSD ~ "Butterfly.mp3");
	SetCommonData("FanAtYuyuko",1);
	if(GetCommonData("IsFan",false)==true)
	{
		CallFan(0);
	}
	else
	{
		CallFan(1);
	}
	wait(60);
	TShot1;
	wait(2400);
	DamageRateAtFrame(57,10,180);
	}
//----------------------------------------------------

task TShot1
{	
	/*PlaySound("Charge3",100,0);
	RayCharge("WHITE",2,60);
	RayCharge("WHITE",2,60);
	wait(120);
	FlashScreen;
	FixedPlayer;
	MagicCirclePlayer;
	wait(60);
	ShinkiCharge(45,"WHITE");
	PlaySound("Charge5",95,0);
	wait(60);
	FireEffect;
	wait(150);*/
	CreateSoul;
	//FallingPellet;
}

task FallingPellet
{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		PlaySound("Shot6",95,0);
		let px = rand(-10,stgx+10);
		let py = rand(-80,40)+GetEnemyY;
		let dir = rand(88,92);
		TShotA2(stgx+px,py,0.5,dir,0.01,rand(1.5,2.5),53,8);
		TShotA2(stgx+px,py,0.5,dir+180,0.01,rand(1.5,2.5),53,8);
		TShotA2(stgx-px,py,0.5,dir,0.01,rand(1.5,2.5),53,8);
		TShotA2(stgx-px,py,0.5,dir+180,0.01,rand(1.5,2.5),53,8);
		wait(10);
	}
}

task CreateSoul
{
	/*ascent(i in 0..6)
	{
		let core = CreateShotA1(GetPlayerX+0*cos(i*60),GetPlayerY+0*sin(i*60),0,0,701,12);
		ObjShot_SetAutoDelete(core,false);
		ObjShot_SetSpellResist(core,true);
		Obj_SetValue(core,"num",i);
		Obj_SetValue(core,"alive",1);
		soularray = soularray ~ [core];
	}*/
	FamiliarHandler;
	/*while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
	{
		a_count = 0;
		ascent(i in 0..length(soularray))
		{
			if(Obj_GetValueD(soularray[i],"alive",0)==1)
			{
				a_count++;
			}
		}
		totalangle = 60;
		ascent(i in 0..length(soularray))
		{
			if(count<90)
			{
				r = 75*sin(count);
			}
			mainangle = i*totalangle+cangle+plusangle;
			ObjMove_SetPosition(soularray[i],GetPlayerX+r*cos(mainangle),GetPlayerY+r*sin(mainangle));
			if(Obj_GetValueD(soularray[i],"alive",0)==0)
			{
				Obj_SetVisible(soularray[i],0);
			}
			else
			{
				Obj_SetVisible(soularray[i],1);
				if(count%2==0){TrailBullet(ObjMove_GetX(soularray[i]),ObjMove_GetY(soularray[i]),701);}
			}
		}
		cangle += 2;
		if(killerappear==0 && GetPlayerState==STATE_NORMAL){count++;}
		if(count%90==0 && killerappear==0 && dienumber<6 && GetPlayerLife>=0)
		{
			SoulExplosion;
		}
		yield;
	}*/
	
	task FamiliarHandler
	{
		let color = [540,543];
		/*wait(180);
		PlaySound("Charge5",95,0);
		ShinkiCharge(45,"WHITE");
		wait(60);
		PlaySound("Eff2",100,0);
		MapleExplode(GetEnemyX,GetEnemyY,1.25,"WHITE");
		ascent(i in 0..9)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,1.5,i*40+10);}
		}
		*/
		
		wait(60);
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		PlaySound("Charge5",95,0);
		ShinkiCharge(45,"PURPLE");
		wait(60);
		PlaySound("Eff2",100,0);
		MapleExplode(GetEnemyX,GetEnemyY,1.25,"WHITE");
		ascent(i in 0..12)
		{
			YuyukoLaser(i*360/(12)+angleToPlayer,60);
		}
		wait(120);
		ShinkiCharge(45,"BLUE");
		PlaySound("Charge5",95,0);
		YuyukoAttack("BLUE");
		wait(60);
		PlaySound("Eff2",100,0);
		MapleExplode(GetEnemyX,GetEnemyY,1.25,"BLUE");
		ascent(i in 0..9+Level*3)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,1.5,i*40+10);}
		}
		wait(150);
		PlaySound("Charge5",95,0);
		ShinkiCharge(45,"PURPLE");
		let fixangle = angleToPlayer;
		ascent(i in 2..10)
		{
			YuyukoLaser(i*30+fixangle-180,75);
			wait(10);
		}
		wait(60);
		fixangle = angleToPlayer;
		descent(i in 2..10)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,2,i*30+fixangle-120);}
			YuyukoLaser(i*30+fixangle-180,75);
			wait(10);
		}			
		
		wait(120);
		ShinkiCharge(45,"RED");
		PlaySound("Charge5",95,0);
		YuyukoAttack("RED");
		wait(60);
		PlaySound("Eff2",100,0);
		MapleExplode(GetEnemyX,GetEnemyY,1.25,"RED");
		
		ascent(i in 0..9)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,1.5,i*40+10);}
		}
		wait(150);
		PlaySound("Charge5",95,0);
		ShinkiCharge(60,"AQUA");
		fixangle = angleToPlayer;
		ascent(i in 2..10)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,2,i*30+fixangle-120);}
			YuyukoLaser(i*30+fixangle-180,75);
			wait(10);
		}
		wait(60);
		fixangle = angleToPlayer;
		descent(i in 2..10)
		{
			if(i%3==0){SpawnFam(GetEnemyX,GetEnemyY,2,i*30+fixangle-120);}
			YuyukoLaser(i*30+fixangle-180,75);
			wait(10);
		}
		wait(120);
		}
		task YuyukoAttack(shotcolor)
		{
			let scolor = [292,823,1003];
			let fixangle = angleToPlayer;
			if(shotcolor=="BLUE")
			{
				scolor = [294,824,1007];
			}
			ascent(i in 0..20+Level*4)
			{
				Prelaser(i*360/(20+Level*4)+fixangle);
			}
			wait(60);
			PlaySound("Eff1",100,75);
			ascent(i in 0..20+Level*4)
			{
				ascent(j in 0..15)
				{
					let v = 1.5+j/2;
					CreateShotA2(GetEnemyX,GetEnemyY,v,i*360/(20+Level*4)+fixangle,-v/90,v/2,scolor[0],8);
				}
			}
			ascent(i in 0..16+Level*4)
			{
					ascent(j in 0..7)
					{
						let bubble = ShotA1(GetEnemyX,GetEnemyY,1,i*360/(16+Level*4),scolor[1],0,8);
						ObjMove_AddPatternA2(bubble,75,NO_CHANGE,NO_CHANGE,0.0025+j/150,0,10);
					}
			}
			task Prelaser(dir)
			{
				let obj = CreateStraightLaserA1(GetEnemyX,GetEnemyY,dir,0,30,0,scolor[2],60);
				ascent(i in 0..60)
				{
					ObjLaser_SetLength(obj,i*10);
					yield;
				}
				descent(i in 0..15)
				{
					ObjLaser_SetRenderWidth(obj,i);
					wait(1);
				}	
				Obj_Delete(obj);			
			}
		}
		task YuyukoLaser(dir,delay)
		{
			Prelaser;
			wait(delay);
			PlaySound("Laser4",100,0);
			CreateLooseLaserA1(GetEnemyX,GetEnemyY,15,dir,350,30,1053,0);
			ascent(i in 0..30+Level*10)
			{
				CreateShotA1(GetEnemyX,GetEnemyY,5.5+Level*1.5-i/6,dir-rand(-i/1.75,i/1.75),color[i%2],0);
			}
			task Prelaser
			{
				let obj = CreateStraightLaserA1(GetEnemyX,GetEnemyY,dir,0,60,0,1045,delay);
				ascent(i in 0..delay)
				{
					ObjLaser_SetLength(obj,i*10);
					yield;
				}
				descent(i in 0..30)
				{
					ObjLaser_SetRenderWidth(obj,i/2);
					wait(1);
				}				
			}
		}
		task SpawnFam(fx,fy,v,angle)
		{
			/*let ene = fam(fx,fy,125,0.7,0,true);
			let fcount = 0;
			let checkshot = 1;
			ObjMove_AddPatternA1(ene,0,v,angle);
			CheckBounce;
			while(ObjEnemy_GetInfo(ene,INFO_LIFE)>0)
			{
				fcount++;
				if(fcount%3==0)
				{
					TrailBullet(ObjMove_GetX(ene),ObjMove_GetY(ene),855);
				}
				if(ObjMove_GetX(ene)<-60 || ObjMove_GetX(ene)>384+60 || ObjMove_GetY(ene)<-60 || ObjMove_GetY(ene)>448+60)
				{
					checkshot = 0;
					Obj_Delete(ene);
				}
				yield;
			}
			if(dienumber>0 && checkshot==1)
			{
				//ObjEnemy_SetLife(objBoss,ObjEnemy_GetInfo(objBoss,INFO_LIFE)-40);
				dienumber--;
				SoulReborn(soularray[dienumber],dienumber);
			}
			
			task CheckBounce // 2 times
			{
				while(ObjMove_GetX(ene)>0 && ObjMove_GetX(ene)<384 && ObjMove_GetY(ene)>0 && ObjMove_GetY(ene)<448)
				{
					yield;
				}
				if(ObjMove_GetX(ene)<0 || ObjMove_GetX(ene)>384){ObjMove_SetAngle(ene,180-ObjMove_GetAngle(ene));}
				if(ObjMove_GetY(ene)<0 || ObjMove_GetY(ene)>448){ObjMove_SetAngle(ene,-ObjMove_GetAngle(ene));}
				yield;
				while(ObjMove_GetX(ene)>0 && ObjMove_GetX(ene)<384 && ObjMove_GetY(ene)>0 && ObjMove_GetY(ene)<448)
				{
					yield;
				}
				if(ObjMove_GetX(ene)<0 || ObjMove_GetX(ene)>384){ObjMove_SetAngle(ene,180-ObjMove_GetAngle(ene));}
				if(ObjMove_GetY(ene)<0 || ObjMove_GetY(ene)>448){ObjMove_SetAngle(ene,-ObjMove_GetAngle(ene));}
			}*/
		}
	}
	
	task SoulExplosion
	{
		SCharge02(3,60,128,0,128);
		wait(60);
		Obj_SetValue(soularray[dienumber],"alive",0);
		PlaySound("Enep2",100,0);
		TExpan(ObjMove_GetX(soularray[dienumber]),ObjMove_GetY(soularray[dienumber]),6,30,255,255,255);
		dienumber++;
		if(dienumber==6)
		{
				KillPlayer;
		}
	}
	
	task SoulReborn(target,round)
	{
		TExpan(ObjMove_GetX(target),ObjMove_GetY(target),6,30,0,255,255);
		Obj_SetValue(target,"alive",1);
		PlaySound("Eff5",100,0);
	}
	
	task TrailBullet(tx,ty,color)
	{
		let obj = TShotA1(tx,ty,0,0,color,0);
		ObjShot_SetAutoDelete(obj,false);
		ObjShot_SetSpellResist(obj,true);
		ObjShot_SetIntersectionEnable(obj,false);
		descent(i in 0..15)
		{
			ObjRender_SetScaleXYZ(obj,i/15,i/15,i/15);
			yield;
		}
		Obj_Delete(obj);
	}
	
	task KillPlayer
	{
		let killer = CreateShotA1(GetPlayerX,GetPlayerY,0,0,201,0);
		killerappear = 1;
		Obj_SetVisible(killer,false);
		ObjShot_SetAutoDelete(killer,false);
		ObjShot_SetSpellResist(killer,true);
		while(GetPlayerState==STATE_NORMAL || GetPlayerState==STATE_HIT)
		{
			ObjMove_SetPosition(killer,GetPlayerX,GetPlayerY);
			yield;
		}
		dienumber = 0;
		wait(GetPlayerDownStateFrame);
		ascent(i in 0..length(soularray))
		{
			Obj_SetValue(soularray[i],"alive",1);
		}
		Obj_Delete(killer);
		killerappear = 0;
		count = 0;
		r = 0;
	}
	function SCharge02(size,frame,r,g,b){ //Draw a charge circle that has no acceleration on shrinking
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/charge.png";
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	Obj_SetRenderPriorityI(obj,25);
	TAlpha;
	TShrink;
	return obj;
	task TAlpha{
		let l=frame;
		let alpha=255/l;
		let ra = r/l;
		let ga = g/l;
		let ba = b/l;
		loop(l){
			if(Obj_IsDeleted(objBoss)){break;}
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetColor(obj,ra,ga,ba);
			alpha+=255/(l);
			ra += r*2/l;
			ga += g*2/l;
			ba += b*2/l;
			yield;
			}
			Obj_Delete(obj);
		}
	task TShrink{
			let a = size/frame;
			loop(frame){
			ObjRender_SetScaleXYZ(obj,size,size,0);
			ObjRender_SetPosition(obj,ObjMove_GetX(soularray[dienumber]),ObjMove_GetY(soularray[dienumber]),0);
			size = size-a;
			if(Obj_IsDeleted(objBoss)){Obj_Delete(obj);}
			yield;
			}
		Obj_Delete(obj);
	}
}
}

task FireEffect
{
	ascent(i in 0..15)
	{
		PlaySound("Eff2",95,0);
		FEffect(GetEnemyX, GetEnemyY, GetPlayerX, GetPlayerY, rand(0,360), rand(0,360),32,80);
		wait(5);
	}
}

task FixedPlayer
{
	loop(270)
	{
		ObjMove_SetPosition(GetPlayerObjectID,stgx,416-30);
		yield;
	}
}

task FlashScreen
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img;
	img = GCSD~"img/5.png";
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,50);
	ObjRender_SetPosition(obj,stgx,stgy,0);
	ObjRender_SetScaleXYZ(obj,2,2,0);
	PlaySound("Slash",100,0);
	descent(i in 0..60)
	{
		ObjRender_SetAlpha(obj,i*4);
		yield;
	}
	Obj_Delete(obj);
}

task MagicCirclePlayer
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img;
	let angle = 0;
	img = GCSD~"img/eff_flower.png";
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 128, 128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj,21);
	loop(120)
	{
		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		ObjRender_SetScaleXYZ(obj,2,2,2);
		ObjRender_SetColor(obj,255,255,255);
		ObjRender_SetAngleXYZ(obj,0,0,0);
		yield;
	}
	ascent(i in 0..150)
	{
		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		ObjRender_SetScaleXYZ(obj,2-i/75,2-i/75,2-i/75);
		ObjRender_SetColor(obj,255-i*255/150,255-i*255/150,255-i*255/150);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle += i/10;
		yield;
	}
	let ratio = angle/30;
	PlaySound("Boon1",100,0);
	ascent(i in 0..60)
	{
		ObjRender_SetScaleXYZ(obj,i/10,i/10,i/10);
		ObjRender_SetColor(obj,255-i*255/60,255-i*255/60,255-i*255/60);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle -= ratio;
		yield;
	}
	Obj_Delete(obj);
}

//----------------------------------------------------

task FEffect(x, y, x2, y2, angle, angle2,color,time){
				let vx=(x2-x)/time;
				let vy=(y2-y)/time;
				let r=0;
				let a=0;
					loop(time){
					//angle+=1; angle2+=1;
					x+=vx; y+=vy;
					a+=360/time; r=150-150*cos(a);
					let sangle = GetPointAngle(x,y,x+r*cos(angle),y+r*sin(angle2));
					let dummy = TShotA1(x+r*cos(angle), y+r*sin(angle2),0,sangle,701,12);
					ObjShot_SetDeleteFrame(dummy,0);
					yield;
					}
}

//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}