
script_enemy_main
{
	#include_script".\..\..\EnemyBossSystem.txt";
	#include_script".\Youmu_AnimeLib.txt";
	
	let tex_bg=filedir~"Youmu_SpellBG_.png";
	
	tex_enemy=filedir~"Youmu_Main.png";
	tex_spellbg=filedir~"Youmu_SpellBG.png";
	
	bg_rect=[0,0,512,768];
	
	@Initialize
	{
		point=450;
		SetLife(800);
		SetTimer(49);
		EnemyBossInitialize;
		LoadGraphic(tex_bg);
		SetAnimetion(tex_enemy);
	}
	@MainLoop{EnemyBossMainLoopSystem(50,invincible);yield;}
	@DrawLoop{EnemyBossDrawLoop;}
	@Finalize{EnemyBossFinalize;}
	
	
	
	task MainThread_Standard
	{
		yield;
		SetMovePositionHermite(cenX,minY+150,0,0,0,0,100);
		SetAction(ACT_MOVE,100);
		SetSpellCard("神道剣「新神陰一円」",51);
		wait(100);
		
		let r=1;
		if(rand_int(0,1)){r=-r;}
		loop
		{
			let wt=70;
			SetAction(ACT_CHARGE,wt);
			Concentration01(wt);
			Slow(1);
			MotionBlurEx(SHOT,wt,120,ADD);
			wait(wt);
			Slow(0);
			SetAction(ACT_SLASH_H,60);
			local
			{
				let i=rand(0,360);
				let leng=60;
				let L=40;
				loop(L)
				{
					let ag=i;
					ascent(a in 0..2)
					{
						let way=16;
						let sx=GetX+leng*cos(ag);
						let sy=GetY+leng*sin(ag);
						let p=[atan2(GetPlayerY-sy,GetPlayerX-sx),rand(0,360)];
						let speed=[3,1.8];
						let sch=[5,4];
						let angle=p[a];
						let gra=[113,118];
						let delay=10;
						while(angle<p[a]+360)
						{
							CreateShotA(1,sx,sy,delay);
							SetShotDataA(1,0,speed[a],angle,sch[a],0,0,gra[a]);
							SetShotDataA(1,360/sch[a],NULL,NULL,0,0,0,gra[a]);
							FireShot(1);
							angle+=360/way;
						}
						ag+=180;
					}
					leng+=2.5;
					i+=(540/L)*r;
					wait(1);
				}
			}
			wait(60);
			let wt=80;
			SetMovePositionHermite(cenX+rand(-80,80),minY+rand(130,150),0,0,0,0,wt);
			SetAction(ACT_MOVE,wt);
			wait(wt);
		}
		
	}
	
	
	
	
	task MainThread_Advanced
	{
		yield;
		SetMovePositionHermite(cenX,minY+120,0,0,0,0,100);
		SetAction(ACT_MOVE,100);
		SetSpellCard("天縛剣「無明無間の地」",52);
		wait(100);
		let bindflg=false;
		PlayerState;
		let r=1;
		if(rand_int(0,1)){r=-r;}
		loop
		{
			local
			{
				let wt=50;
				let stx=cenX+200*r;let sty=cenY-60;
				let edx=cenX-160*r;let edy=cenY-140;
				SetAction(ACT_CHARGE,wt);
				SetMovePositionHermite(stx,sty,0,0,0,0,wt);
				wait(wt);
				SetAction(ACT_SLASH_H,180);
				SetMovePositionHermite(edx,edy,0,0,0,0,10);
				let ag=atan2(edy-sty,edx-stx);
				let leng=GetGapLength(stx,sty,edx,edy);
				SlashEffect(stx,sty,ag,leng,180);
				SlashShot(stx,sty);
				
				task SlashShot(let bx,let by)
				{
					let m=10;
					let p=90-(leng/m)*r;
					let lp=20;
					let l=0;
					while(l<leng*1.4)
					{
						let sx=bx+l*cos(ag);
						let sy=by+l*sin(ag);
						let speed=4;
						let gra=119;
						let delay=10;
						while(speed>=0.4)
						{
							let angle=[p,-p];
							ascent(a in 0..length(angle))
							{
								CreateShot01(sx,sy,speed,angle[a],gra,delay);
							}
							speed/=1.2;
						}
						loop(6){CherryEffect(sx,sy);}
						wait(1);
						p+=lp/(m/2)*r;
						l+=lp;
					}
				}
				
				Bind;
				wait(180);
			}
			
			local
			{
				SetAction(ACT_MOVE,100);
				SetMovePositionHermite(cenX,minY+120,0,0,0,0,100);
				wait(100);
				let lp=0;
				loop(4)
				{
					SetAction(ACT_CHARGE,10);
					wait(10);
					SetAction(ACT_SLASH_H,30);
					let px=GetPlayerX;
					let py=GetPlayerY;
					let p=90+(90*r)+rand(-20,-40)*r;
					let i=-250;
					let Lp=15;
					loop(Lp)
					{
						let way=3+(lp%2);
						let wide=100;
						let sx=GetGapX(GetX,i,p);
						let sy=GetGapY(GetY,i,p);
						let ag=atan2(py-sy,px-sx);
						let gra=[113,118];
						let delay=15;
						let speed=5;
						while(speed>=1.4)
						{
							let angle=ag-wide/2;
							while(angle<=ag+wide/2+1)
							{
								CreateShot01(sx,sy,speed,angle,gra[lp%2],delay);
								angle+=wide/(way-1);
							}
							speed=speed/1.3;
						}
						i+=500/Lp;
						wait(2);
					}
					lp++;
					r=-r;
					wait(10);
				}
				wait(80);
			}
			
			local
			{
				Move;
				let wide=0;
				let way=1;
				loop(10)
				{
					let p=GetAngleToPlayer;
					let speed=[3,1.9];
					let gra=[180,183];
					let delay=10;
					ascent(a in 0..2)
					{
						if(wide==0)
						{
							let angle=p;
							CreateShot01(GetX,GetY,speed[a],angle,gra[a],delay);
						}
						else
						{
							let angle=p-wide/2;
							while(angle<=p+wide/2)
							{
								CreateShot01(GetX,GetY,speed[a],angle,gra[a],delay);
								angle+=wide/(way-1);
							}
						}
					}
					way++;
					wide+=15;
					wait(30);
				}
				
				task Move
				{
					wait(20);
					let r=1;
					loop(2)
					{
						let wt=100;
						let sx=cenX+rand(30,100)*r;
						let sy=rand(100,140);
						SetMovePositionHermite(sx,sy,0,0,0,0,wt);
						SetAction(ACT_MOVE,wt);
						wait(wt);
						wait(40);
						r=-r;
					}
				}
				
				wait(100);
			}
			
			
			local
			{
				let wt=60;
				SetAction(ACT_CHARGE,wt);
				SetMovePositionHermite(cenX,cenY-140,0,0,0,0,wt);
				wait(wt);
				
				wt=10;
				let sx=cenX+80*r;
				let sy=cenY-60;
				let ag=atan2(sy-GetY,sx-GetX);
				let leng=GetGapLength(GetX,GetY,sx,sy)*1.5;
				SetAction(ACT_SLASH_V,wt);
				SetMovePositionHermite(sx,sy,0,0,0,0,wt);
				SlashEffect(GetX,GetY,ag,leng,180);
				wait(wt);
				
				wt=20;
				SetAction(ACT_CHARGE,wt);
				wait(wt);
				
				local
				{
					let p=GetAngleToPlayer;
					let way=12;
					let wide=12;
					let angleA=p;
					let gra=116;
					let delay=30;
					while(angleA<p+360)
					{
						let angleB=angleA-wide/2;
						while(angleB<=angleA+wide/2)
						{
							let j=0;
							let speed=4.5;
							while(speed>=1)
							{
								let angleC=angleB+j;
								CreateShot01(GetX,GetY,speed,angleC,gra,delay);
								speed/=1.25;
								j+=360/way/2;
							}
							angleB+=2;
						}
						angleA+=360/way;
					}
				}
				loop(60){CherryEffect(GetX,GetY);}
				
				wt=10;
				let sx=cenX-180*r;
				let sy=cenY-140;
				let ag=atan2(sy-GetY,sx-GetX);
				let leng=GetGapLength(GetX,GetY,sx,sy)*2;
				let lx=GetGapX(GetX,leng/4,ag+180);
				let ly=GetGapY(GetY,leng/4,ag+180);
				SetAction(ACT_SLASH_H,90);
				SetMovePositionHermite(sx,sy,0,0,0,0,wt);
				SlashEffect(lx,ly,ag,leng,180);
				wait(wt);
				
				wait(160);
			}
			
			local
			{
				let wt=120;
				SetAction(ACT_MOVE,wt);
				SetMovePositionHermite(cenX,cenY-120,0,0,0,0,wt);
				let p=GetAngleToPlayer;
				let wide=0;
				let way=1;
				loop(15)
				{
					let roundway=8;
					let speed=[3,2.2];
					let gra=[177,182];
					let delay=10;
					let a=0;
					let angleA=p;
					while(angleA<p+360)
					{
						if(wide==0)
						{
							let angleB=angleA;
							CreateShot01(GetX,GetY,speed[a],angleB,gra[a],delay);
						}
						else
						{
							let angleB=angleA-wide/2;
							while(angleB<=angleA+wide/2)
							{
								CreateShot01(GetX,GetY,speed[a],angleB,gra[a],delay);
								angleB+=wide/(way-1);
							}
						}
						a=(a+1)%2;
						angleA+=360/roundway;
					}
					way++;
					wide+=4.8;
					wait(13);
				}
				wait(100);
			}
		}
		
		task Bind
		{
			let i=0;
			let leng=240;
			while(leng>0)
			{
				let ag=i;
				while(ag<i+360)
				{
					let sx=GetPlayerX+leng*cos(ag);
					let sy=GetPlayerY+leng*sin(ag);
					Set(sx,sy,ag+90);
					ag+=90;
				}
				i+=6;
				leng-=4;
				wait(2);
			}
			bindflg=true;
			Explosion01(GetPlayerX,GetPlayerY,2,0.4,120);
			BGEffect;
			wait(960);
			bindflg=false;
			
			task Set(let sx,let sy,let ag)
			{
				loop
				{
					let spd=10;
					if(sx<minX||sx>maxX||sy<minY||sy>maxY){break;}
					sx+=spd*cos(ag);
					sy+=spd*sin(ag);
				}
				let angle=ag-180;
				let leng=580;
				let alpha=92;
				let del=12;
				SetLaserObject(sx,sy,leng,30,angle,alpha,7,30,del);
			}
		}
		
		task PlayerState
		{
			let playerx=GetPlayerX;
			let playery=GetPlayerY;
			loop
			{
				if(!bindflg)
				{
					playerx=GetPlayerX;
					playery=GetPlayerY;
				}
				else
				{
					let v=14;
					if(playerx>maxX-v){playerx=maxX-v-1;}
					if(playery>maxY-v){playery=maxY-v-1;}
					if(playerx<minX+v){playerx=minX+v+1;}
					if(playery<minY+v){playery=minY+v+1;}
					SetPlayerX(playerx);
					SetPlayerY(playery);
					Rag;
				}
				yield;
			}
			

			
			task Rag
			{
				let vx=0;
				let vy=0;
				let v=GetPlayerInfo(PLAYER_SPEED_HIGH);
				if(GetKeyState(VK_SLOWMOVE)==KEY_HOLD){v=GetPlayerInfo(PLAYER_SPEED_LOW);}
				if(GetKeyState(VK_LEFT)==KEY_HOLD){vx=-v;}
				if(GetKeyState(VK_RIGHT)==KEY_HOLD){vx=v;}
				if(GetKeyState(VK_UP)==KEY_HOLD){vy=-v;}
				if(GetKeyState(VK_DOWN)==KEY_HOLD){vy=v;}
				
				if(vx!=0&&vy!=0){vx/=1.4;vy/=1.4;}
				if(playerx+vx>maxX||playerx+vx<minX){vx=0;}
				if(playery+vy>maxY||playery+vy<minY){vy=0;}
				
				loop(15){yield;}
				
				playerx+=vx;
				playery+=vy;
			}
		}
		
		task BGEffect
		{
			let texture_rect=bg_rect;
			let leng_h=texture_rect[2]-texture_rect[0];
			let leng_v=texture_rect[3]-texture_rect[1];
			let posx=boss_bgposx;let posy=boss_bgposy;
			
			let base = Obj_Create(OBJ_EFFECT);
			ObjEffect_SetTexture(base,tex_bg);
			ObjEffect_SetPrimitiveType(base,PRIMITIVE_TRIANGLESTRIP);
			ObjEffect_CreateVertex(base,4);
			ObjEffect_SetLayer(base,0);
			Obj_SetPosition(base,posx,posy);
		
			ObjEffect_SetVertexXY(base,0,-leng_h/2,-leng_v/2);
			ObjEffect_SetVertexXY(base,1, leng_h/2,-leng_v/2);
			ObjEffect_SetVertexXY(base,2,-leng_h/2, leng_v/2);
			ObjEffect_SetVertexXY(base,3, leng_h/2, leng_v/2);
			ObjEffect_SetVertexUV(base,0,texture_rect[0],texture_rect[1]);
			ObjEffect_SetVertexUV(base,1,texture_rect[2],texture_rect[1]);
			ObjEffect_SetVertexUV(base,2,texture_rect[0],texture_rect[3]);
			ObjEffect_SetVertexUV(base,3,texture_rect[2],texture_rect[3]);
			Effect;
			
			let alpha=192;
			while(!Obj_BeDeleted(base))
			{
				let color_r=255;
				let color_g=255;
				let color_b=255;
				if(IsBGStopping)
				{
					color_g=32;
					color_b=32;
				}
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(base,a,alpha,color_r,color_g,color_b);
				}
				if(!bindflg){alpha-=6;}
				if(alpha<4){Obj_Delete(base);}
				yield;
			}
			
			task Effect
			{
				let tuning=GetCommonData("Memory_ConfigData");
				if(!tuning[1])
				{
					let scp=0.01;
					let limit=15;
					loop(30)
					{
						SpellBGEffect(0,40,scp,limit);
						scp+=0.005;
						limit+=1;
						yield;
					}
					/*
					loop
					{
						wait(120);
						let scp=0.01;
						let limit=240;
						local
						{
							SpellBGEffect(1,128,scp,limit);
							wait(2);
						}
					}*/
				}
			}
			
			task SpellBGEffect(let mode,let base,let scp,let limit)
			{
				let obj=SetEffectObject(tex_bg,1);
				SetEffectObjectRect(obj,texture_rect[0],texture_rect[1],texture_rect[2],texture_rect[3]);
				if(mode==0){ObjEffect_SetRenderState(obj,ADD);}
				else{ObjEffect_SetRenderState(obj,ALPHA);}
				
				let scale=1.0;
				let alpha=base;
				while(!Obj_BeDeleted(obj))
				{
					let color=255;
					ascent(a in 0..4)
					{
						ObjEffect_SetVertexColor(obj,a,alpha,color,color,color);
					}
					Obj_SetPosition(obj,posx,posy);
					ObjEffect_SetScale(obj,scale,scale);
					
					alpha-=base/limit;
					scale+=scp;
					if(alpha<=0){Obj_Delete(obj);}
					
					yield;
				}
			}
		}
	}
	
	
	
	
	task MainThread_Unlimited{}
	
}