#東方弾幕風
#Title[陰陽「博麗反夢結界」]
#Text[]
#Player[FREE]
#Image[.]
#BackGround[User]
#ScriptVersion[2]
script_enemy_main {

let Norma=5;//撮影枚数の設定
let SpellTimer=9000;//制限時間の設定（フレーム単位）
let StepUp=[1,2,3,4];//段階移行する枚数を設定（五つまで設定可能）。StepUp1〜5（boolean）まであり、入力枚数撮るとtrueになる

#include_function ".\..\txt/DoubleSpoilerData.txt"

    let imgBoss =GetCurrentScriptDirectory ~ "..\lib/dot_reimu.png"; 

    // 初期位置
    let xIni    = GetCenterX;
    let yIni    = GetCenterY-150;


    @Initialize {
	SetShotAutoDeleteClip(0,0,0,0);
	SetBulletScorePoint(12,150);
       LoadUserShotData(GetCurrentScriptDirectory~".\..\img\shot_All.txt");
	InitializeDoubleSpoiler;
	DrawSpellName(imgSpellName,6,441,278,467);
        LoadGraphic(imgBoss);
	  LoadGraphic(GetCurrentScriptDirectory ~ "..\img/SampleEC01a.png");
        SetTexture(imgBoss);
	SetX(GetCenterX-0.1);
	InitializeAction();
        TMain;
    }

    @MainLoop {
	SetSpeed(0);
        SetCollisionA(GetX, GetY, 32);
	DoubleSpoilerYield;
    }

    @DrawLoop {
	//SetAlpha(150);
	DrawBoss( imgBoss );
    }

    @Finalize {
        DeleteGraphic(imgBoss);
	FinalizeDoubleSpoiler;
    }
	@BackGround
	{


	}

    // メインタスク
task TMain 
{
	yield;
	Border;
	BossShadowAnimation(imgBoss,0,255,255,255);
	standBy;
//	SetAction(ACT_PRAY,30);
	Shot;
	Shot3;
	Move;
	step;
}

task Border
{
let r1=400;
let r2=450;
let angle=0;
	loop
	{
		BorderEffect(r1,r2,angle);
		if(r1>100)
		{
			r1-=5;
			r2-=5;
		}
		angle+=2;
	yield;
	}
}

task BorderEffect(r1,r2,angle)
{
		let randvertex=rand_int(0,5);
		let VertexNum=48;
		let Radius=[r1,r2];
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,GetCurrentScriptDirectory ~ "..\img/SampleEC01a.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj,VertexNum);
		ascent(let i in 0+randvertex..VertexNum+randvertex)
		{
			ObjEffect_SetVertexXY(obj, i-randvertex, Radius[i%2]*cos(360/(VertexNum-2)*i), Radius[i%2]*sin(360/(VertexNum-2)*i));
		}
		ascent(let i in 0+randvertex..VertexNum+randvertex)
		{	
			ObjEffect_SetVertexUV(obj, i-randvertex,  256/(VertexNum)*i,  16*(i%2));
		}


		ObjEffect_SetLayer(obj,3);

		let Scale=1;
		let Zangle=0;
		ObjEffect_SetScale(obj,Scale,Scale);
		Obj_SetPosition(obj,GetX,GetY);
		let Alpha=100;
		//let angle=0;

		let StandardX=GetCenterX;
		let StandardY=GetCenterY+32;

		loop(5)
		{
		if(r1>100)
		{
			r1-=5;
			r2-=5;
			Radius=[r1,r2];
		}
		ascent(let i in 0..VertexNum)
		{
			ObjEffect_SetVertexXY(obj, i, Radius[i%2]*cos(360/(VertexNum-2)*i+angle), Radius[i%2]*sin(360/(VertexNum-2)*i+angle));
		}
		ascent(let i in 0..VertexNum)
		{			
			ObjEffect_SetVertexColor(obj, i,Alpha,255,0,255*(i%3));
		}
		Obj_SetPosition(obj,StandardX,StandardY);
		ObjEffect_SetScale(obj,Scale,Scale);
		angle+=2;
		Alpha-=20;
		Scale+=0.01;
		yield;
		}
		Obj_Delete(obj);
}



task BorderEffectuu(r1,r2,angle)
{
		let randrect=rand(-2,2);
		let randvertex=32;
		let VertexNum=64+randvertex;
		let Radius=[r1,r2];
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,GetCurrentScriptDirectory ~ "..\img/SampleEC01a.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj,VertexNum);
		ascent(let i in 0..VertexNum)
		{
			ObjEffect_SetVertexXY(obj, i, Radius[i%2]*cos(360/(VertexNum-2)*i), Radius[i%2]*sin(360/(VertexNum-2)*i));
		}
		ascent(let i in 0..VertexNum)
		{	
			ObjEffect_SetVertexUV(obj, i,  256/(VertexNum)*i,  16*(i%2));
		}


		ObjEffect_SetLayer(obj,3);

		let Scale=1;
		let Zangle=0;
		ObjEffect_SetScale(obj,Scale,Scale);
		Obj_SetPosition(obj,GetX,GetY);
		let Alpha=100;
		//let angle=0;

		let StandardX=GetCenterX;
		let StandardY=GetCenterY+32;

		loop(5)
		{
		if(r1>100)
		{
			r1-=5;
			r2-=5;
			Radius=[r1,r2];
		}
		ascent(let i in 0..VertexNum)
		{
			ObjEffect_SetVertexXY(obj, i, Radius[i%2]*cos(360/(VertexNum-2)*i+angle)+randrect, Radius[i%2]*sin(360/(VertexNum-2)*i+angle)+randrect);
		}
		ascent(let i in 0..VertexNum)
		{			
			ObjEffect_SetVertexColor(obj, i,Alpha,255,0,255*(i%3));
		}
		Obj_SetPosition(obj,StandardX,StandardY);
		angle+=3;
		Alpha-=20;
		yield;
		}
		Obj_Delete(obj);
}


sub standBy//初期位置へ
{
	SetAction(ACT_MOVE, 30);
	let wIni = 30;
	SetMovePosition03(xIni, yIni, 5,8);
	//SetMovePosition02(xIni, yIni, wIni);
	SetInvincibility(wIni);
	wait(wIni);
}

let WorldShotWay=0;

task step
{
	while(!StepUp1){yield;}
	WorldShotWay=104;
	while(!StepUp2){yield;}
	WorldShotWay=112;
	while(!StepUp3){yield;}
	WorldShotWay=120;
	while(!StepUp4){yield;}
	WorldShotWay=128;
}

task Move
{
wait(30);
loop
{
/*	DoubleSpoilermoveToPlayer(40, 0, 30,
                     GetClipMinX + 48, GetClipMinY +  64,
                     GetClipMaxX - 48, GetClipMinY + 102);
*/		DoubleSpoilermoveToPlayer03(40,0, 15,2,
               		 GetClipMinX + 48, GetCenterY-80,
               		 GetClipMaxX - 48, GetCenterY-40);
	MagicCircleEffectTask;
	wait(120);
}
}

task Shot3
{
loop
{
	if(WorldShotWay>0)
	{
	SetAction(ACT_SHOT_A,30);
	}
	ascent(let i in 0..WorldShotWay)
	{
		WorldShot(GetX,GetY,1,i*360/WorldShotWay,95);
	}
wait(240);
}
}


task Shot
{
Shotsource(GetClipMinX+16, GetClipMinY+16,1.5,3,89);
Shotsource(GetClipMaxX-16, GetClipMinY+16,2.5,3.5,94);
}

task SerVantShot(Cobj,speed,graphic)
{
let angle=0;
let angleplus=1;
	loop
	{
		ascent(let i in 0..3)
		{
			WorldShot(Obj_GetX(Cobj),Obj_GetY(Cobj),speed,Obj_GetAngle(Cobj)+i*90,graphic);
		}
		angle+=angleplus;
		wait(2);
	}
}

task Shotsource(x,y,MoveSpeed,speed,graphic)
{
	let obj = Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	ObjShot_SetGraphic(obj, graphic);
	ObjShot_SetDelay(obj, 0);
	ObjShot_SetBombResist(obj,true);
	Obj_SetAutoDelete(obj,false);

	SerVantShot(obj,speed,graphic);
	while(!Obj_BeDeleted(obj))
	{
		while(Obj_GetX(obj)<=GetClipMaxX-16)
		{
			Obj_SetSpeed(obj,MoveSpeed);
			Obj_SetAngle(obj,0);
			yield;
		}
		while(Obj_GetY(obj)<=GetClipMaxY-16)
		{
			Obj_SetSpeed(obj,MoveSpeed);
			Obj_SetAngle(obj,90);
			yield;
		}
		while(Obj_GetX(obj)>=GetClipMinX+16)
		{
			Obj_SetSpeed(obj,MoveSpeed);
			Obj_SetAngle(obj,180);
			yield;
		}
		while(Obj_GetY(obj)>=GetClipMinY+16)
		{
			Obj_SetSpeed(obj,MoveSpeed);
			Obj_SetAngle(obj,270);
			yield;
		}
		yield;
	}
}

task WorldShot(let x,let y,let speed,let angle,let graphic)
{
	let obj = Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	ObjShot_SetGraphic(obj, graphic);
	ObjShot_SetDelay(obj, 0);
	//ObjShot_SetBombResist(obj,true);
	//Obj_SetAutoDelete(obj,false);

	let Standardgraphic=graphic;
	let angleplus=6;

	let r=0;
	let pangle=90+GetAngleToPlayer-90;
	let GPAngle=GetAngleToPlayer;
	let t=0;
	let Border=false;

	Obj_SetSpeed(obj,speed);
	Obj_SetAngle(obj,angle);

	let X=Obj_GetX(obj);
	let Y=Obj_GetY(obj);

	let StandardX=GetCenterX;
	let StandardY=GetCenterY+32;

	while(!Obj_BeDeleted(obj))
	{
		X=Obj_GetX(obj);
		Y=Obj_GetY(obj);
		if(((StandardY-Y)^2+(StandardX-X)^2)^0.5<=125 && !Border)
		{
			graphic=96;
			Border=true;
			Obj_SetX(obj,StandardX+(StandardX-X));
			Obj_SetY(obj,StandardY+(StandardY-Y));
			ObjShot_SetGraphic(obj, graphic);
			Obj_SetAngle(obj,180+Obj_GetAngle(obj));
		}
		if(((StandardY-Y)^2+(StandardX-X)^2)^0.5>125 && Border)
		{
			graphic=Standardgraphic;
			Border=false;
			Obj_SetX(obj,StandardX+(StandardX-X));
			Obj_SetY(obj,StandardY+(StandardY-Y));
			ObjShot_SetGraphic(obj, graphic);
			Obj_SetAngle(obj,180+Obj_GetAngle(obj));
		}
		wait(1);
	}
	if(GetCommonDataDefault("Shooting",false))
	{
		if(X<GetClipMaxX && X>GetClipMinX && Y<GetClipMaxY && Y>GetClipMinY)
		{
			if(graphic==89)
			{
				EffectSet(X,Y,"RED");
				BulletColor("RED");
			}
			if(graphic==94)
			{
				EffectSet(X,Y,"BLUE");
				BulletColor("BLUE");
			}
			if(graphic==95)
			{
				EffectSet(X,Y,"PURPLE");
				BulletColor("PURPLE");
			}
			if(graphic==96)
			{
				EffectSet(X,Y,"WHITE");
				BulletColor("WHITE");
			}
		}
	}
	if(GetCommonDataDefault("EndDefeatBullet",false))
	{
		if(graphic==89)
		{
			EffectSet(X,Y,"RED");
		}
		if(graphic==94)
		{
			EffectSet(X,Y,"BLUE");
		}
		if(graphic==95)
		{
			EffectSet(X,Y,"PURPLE");
		}
		if(graphic==96)
		{
			EffectSet(X,Y,"WHITE");
		}
	}
}

#include_function ".\..\txt/Data.txt"
#include_function ".\..\lib/lib_anime_Reimu.txt"
}
