#東方弾幕風
#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/data.txt"

    let imgBoss =GetCurrentScriptDirectory ~ "..\lib/dot_utuho.png"; 
    let imgNuclear =GetCurrentScriptDirectory ~ "..\img/s_shot.png"; 

    // 初期位置
    let xIni    = GetCenterX;
    let yIni    = GetClipMinY + 96;


   @Initialize {
	LoadUserShotData(GetCurrentScriptDirectory~".\..\img\shot_All.txt");
	InitializeDoubleSpoiler;
	DrawSpellName(imgSpellName,6,274,291,300);
        LoadGraphic(imgBoss);
        LoadGraphic(imgNuclear);
        SetTexture(imgBoss);
	InitializeAction();
        TMain;
    }

    @MainLoop {
        SetCollisionA(GetX, GetY, 1);
        SetCollisionB(GetX, GetY, 16);
        yield;
    }

    @DrawLoop {
	DrawText(GetLife,120,352,12,255);//プレイ時間の表示
	DrawBoss( imgBoss );
    }

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

	}

    // メインタスク
task TMain 
{
	yield;
	standBy;
//	TMove;
	Shot;
//	Shot2;
}

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

task TMove// 浮揚と移動
{
	loop
	{
	wait(120);
	moveToPlayer(rand(40, 60), rand(-30, 30), 30,
                     GetClipMinX + 48, GetClipMinY +  64,
                     GetClipMaxX - 48, GetClipMinY + 144);
	MagicCircleEffectTask;
	}
}


task Shot
{
	while(!StepUp1)
	{
	SetAction(ACT_SUN,240);
	loop(3)
	{
		if(!StepUp1)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(60)
		{
			if(StepUp1){break;}
			wait(1);
		}
	}
		if(!StepUp1)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(300)
		{
			if(StepUp1){break;}
			wait(1);
		}
	}
	SetAction(ACT_STAND,31);
	wait(1);
	Concentration01(30);
	wait(30);
	SetAction(ACT_SUN,300);
	while(!StepUp2)
	{
	loop(4)
	{
		if(!StepUp2)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(60)
		{
			if(StepUp2){break;}
			wait(1);
		}
	}
		if(!StepUp2)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(300)
		{
			if(StepUp2){break;}
			wait(1);
		}
	}
	SetAction(ACT_STAND,31);
	wait(1);
	Concentration01(30);
	wait(30);
	SetAction(ACT_SUN,360);
	while(!StepUp3)
	{
	loop(5)
	{
		if(!StepUp3)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(60)
		{
			if(StepUp3){break;}
			wait(1);
		}
	}
		if(!StepUp3)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(240)
		{
			if(StepUp3){break;}
			wait(1);
		}
	}
	SetAction(ACT_STAND,30);
	wait(1);
	Concentration01(30);
	wait(30);
	SetAction(ACT_SUN,540);
	while(!StepUp4)
	{
	loop(9)
	{
		if(!StepUp4)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(30)
		{
			if(StepUp4){break;}
			wait(1);
		}
	}
		if(!StepUp4)
		{
			CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		}
		loop(240)
		{
			if(StepUp4){break;}
			wait(1);
		}
	}
	SetAction(ACT_STAND,30);
	wait(1);
	Concentration01(30);
	wait(30);
	SetAction(ACT_SUN,360);
	loop
	{
	loop(11)
	{

		CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		loop(30)
		{
			wait(1);
		}
	}
		CreateEnemyFromScript("Familiar", GetX, GetY,2, GetAngleToPlayer, 1);
		loop(240)
		{
			wait(1);
		}
	}
}


#include_function ".\..\lib/lib_anime_Utsuho.txt"
}


CreateShotFromScript( "shot1",GetX, GetY, 2, GetAngleToPlayer, 0, RED01);
///////////////////////////////////////////////////////////////////////////////////////
script_enemy Familiar{//CreateShot01と同等の弾

       let out=false;
       let gr=GetArgument;
    let imgNuclear =GetCurrentScriptDirectory ~ "..\img/s_shot.png"; 

	let exp=2.71828;
	let size=0;
	let VanishFlag=false;
       @Initialize{
	SetLife(2000);
	//SetDefault(GetArgument);
	Tmain;
       }
       @MainLoop {
AddLife(1);
        SetCollisionB(GetX, GetY, size);
		yield;
          }
       @DrawLoop {

       }
       @Finalize{

	}

task Tmain
{
	effect;
	move;
	shot;
	eraze;
}

task effect
{
		let sc=0.5;
  		let objE = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(objE,GetX,GetY);
		ObjEffect_SetTexture(objE,imgNuclear);
		ObjEffect_SetPrimitiveType(objE, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(objE, ADD);
		ObjEffect_SetScale(objE, sc,sc);
		ObjEffect_CreateVertex(objE, 4);
		ObjEffect_SetVertexXY(objE, 0, -128, -128);
		ObjEffect_SetVertexXY(objE, 1,  128, -128);
		ObjEffect_SetVertexXY(objE, 2,  128,  128);
		ObjEffect_SetVertexXY(objE, 3, -128,  128);
		
		ObjEffect_SetVertexUV(objE, 0,  0,  0);
		ObjEffect_SetVertexUV(objE, 1,  256, 0);
		ObjEffect_SetVertexUV(objE, 2, 256,  256);
		ObjEffect_SetVertexUV(objE, 3, 0, 256);

		ObjEffect_SetVertexColor(objE,0,100,255,250,250);
		ObjEffect_SetVertexColor(objE,1,100,255,250,250);
		ObjEffect_SetVertexColor(objE,2,100,255,250,250);
		ObjEffect_SetVertexColor(objE,3,100,255,250,250);
	
		ObjEffect_SetLayer(objE,5);
		while((!Obj_BeDeleted(objE) && GetSpeed>0.1) || GetSpeed==0)
		{	
			Obj_SetPosition(objE,GetX,GetY);
			wait(1);
		}
		Obj_Delete(objE);
}

task move
{
let PX=GetX;
let PY=GetY;
let GPX=GetPlayerX;
let GPY=GetPlayerY;
let Frame=0;
let speed=0;

while(GetX!=GPX && GetY!=GPY)
{

if(CsdDistanceToPlayer(PX,PY,GPX,GPY)>10*8)
{
	if(CsdDistanceToPlayer(GetX,GetY,GPX,GPY)>10*8)
	{
		speed=8;
	}
	else
	{
Frame++;
		speed=8*exp^(-Frame/10);
	}
}
else
{
Frame++;
	speed=CsdDistanceToPlayer(PX,PY,GPX,GPY)/10*exp^(-Frame/10);
} 
SetSpeed(speed);
SetAngle(GetAngle);
wait(1);

}

}

task shot
{
NuClear;

//	CreateShot01(GetX, GetY,GetSpeed,GetAngle,GetArgument,GetDelayTime);
}


task NuClear
{
		let sc=0.0;

  		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj,GetX,GetY);
		ObjEffect_SetTexture(obj,imgNuclear);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetScale(obj, sc,sc);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -128, -128);
		ObjEffect_SetVertexXY(obj, 1,  128, -128);
		ObjEffect_SetVertexXY(obj, 2,  128,  128);
		ObjEffect_SetVertexXY(obj, 3, -128,  128);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  256, 0);
		ObjEffect_SetVertexUV(obj, 2, 256,  256);
		ObjEffect_SetVertexUV(obj, 3, 0, 256);

	/*	ObjEffect_SetVertexColor(obj,0,100,255,250,250);
		ObjEffect_SetVertexColor(obj,1,100,255,250,250);
		ObjEffect_SetVertexColor(obj,2,100,255,250,250);
		ObjEffect_SetVertexColor(obj,3,100,255,250,250);
	*/

  		let objE = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(objE,GetX,GetY);
		ObjEffect_SetTexture(objE,imgNuclear);
		ObjEffect_SetPrimitiveType(objE, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(objE, ADD);
		ObjEffect_SetScale(objE, sc,sc);
		ObjEffect_CreateVertex(objE, 4);
		ObjEffect_SetVertexXY(objE, 0, -128, -128);
		ObjEffect_SetVertexXY(objE, 1,  128, -128);
		ObjEffect_SetVertexXY(objE, 2,  128,  128);
		ObjEffect_SetVertexXY(objE, 3, -128,  128);
		
		ObjEffect_SetVertexUV(objE, 0,  256,  0);
		ObjEffect_SetVertexUV(objE, 1,  512, 0);
		ObjEffect_SetVertexUV(objE, 2, 512,  256);
		ObjEffect_SetVertexUV(objE, 3, 256, 256);

	/*	ObjEffect_SetVertexColor(objE,0,100,255,250,250);
		ObjEffect_SetVertexColor(objE,1,100,255,250,250);
		ObjEffect_SetVertexColor(objE,2,100,255,250,250);
		ObjEffect_SetVertexColor(objE,3,100,255,250,250);
	*/

		ObjEffect_SetLayer(obj,5);
		ObjEffect_SetLayer(objE,5);

ascent(let i in 0..12)
{
RoundShot(50*1.5,i*360/12);
RoundShot(25*1.5,i*360/12);
}
		while((!Obj_BeDeleted(obj) && GetSpeed>0.1) || GetSpeed==0)
		{	
			Obj_SetPosition(obj,GetX,GetY);
			Obj_SetPosition(objE,GetX,GetY);
			wait(1);
		}
		while(!Obj_BeDeleted(obj))
		{
			if(GetLife!=2000)
			{
				Obj_Delete(obj);
				Obj_Delete(objE);
				size=0;
			}
			if(sc<1.0*5/6)
			{
			size+=50*1.5/15;
			sc+=5/6*0.10*2/3;
			}
			else{break;}
			ObjEffect_SetScale(obj, sc,sc);
			ObjEffect_SetScale(objE, sc,sc);
			Obj_SetPosition(obj,GetX,GetY);
			Obj_SetPosition(objE,GetX,GetY);
			wait(1);
		}
		loop(10)
		{
			if(GetLife!=2000)
			{
				Obj_Delete(obj);
				Obj_Delete(objE);
				size=0;
			}
		wait(1)
		}
		BlueShot(obj);
		while(!Obj_BeDeleted(obj) && GetLife==2000)
		{	
			if(VanishFlag){break;}
			wait(1);
		}
		if(!VanishFlag)
		{
		BasePoint(2000);
		Obj_Delete(obj);
		Obj_Delete(objE);
		size=0;
		}
		else
		{
		loop(10)
		{	
			size-=60*1.5/15;
			sc-=5/6*0.10*2/3;
			ObjEffect_SetScale(obj, sc,sc);
			ObjEffect_SetScale(objE, sc,sc);
			wait(1);
		}
	//	VanishEnemy;
		size=0;
		Obj_Delete(obj);
		Obj_Delete(objE);
		}
}

task BlueShot(obj)
{
let r=140*2/3;
while(!Obj_BeDeleted(obj) && !VanishFlag)
{
	let angle=rand(0,360);
	CreateBlueShot(GetX+r*cos(angle),GetY+r*sin(angle),angle);
	wait(5);
}
}

task CreateBlueShot(let x,let y,let angle)
{
	let obj = Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	ObjShot_SetGraphic(obj, BLUE01);
	Obj_SetSpeed(obj, 0);
	Obj_SetAngle(obj, angle);
	ObjShot_SetDelay(obj, 10);
	//ObjShot_SetBombResist(obj,true);
	//Obj_SetCollisionToPlayer(obj,false);
	Obj_SetAutoDelete(obj,false);
	//Obj_SetAlpha(obj,0);

	let X=Obj_GetX(obj);//毎フレーム座標を記録するための値
	let Y=Obj_GetY(obj);//毎フレーム座標を記録するための値
	let VanishJudg=false;//画面外にいって消えたかを判定。

	let speed=0;
	while(!Obj_BeDeleted(obj))
	{
		if(speed<1.0)
		{
			speed+=0.01;
		}
		Obj_SetSpeed(obj, speed);
		if(Obj_GetX(obj)<GetClipMinX-0)//左端での挙動
		{
			Obj_Delete(obj);
			VanishJudg=true;
        	}
		if(Obj_GetX(obj)>GetClipMaxX+0)//右端での挙動
		{
			Obj_Delete(obj);
			VanishJudg=true;
        	}
		if(Obj_GetY(obj)<GetClipMinY-0)//上端での挙動
		{
			Obj_Delete(obj);
			VanishJudg=true;
        	}
		if(Obj_GetY(obj)>GetClipMaxY+0)//下端での挙動
		{
			Obj_Delete(obj);
			VanishJudg=true;
        	}
		X=Obj_GetX(obj);
		Y=Obj_GetY(obj);
		wait(1);
	}
	if(VanishJudg==false)
	{
		if(DistanceToPlayer(X,Y)<=50)
		{
			RiskShot(120);
		}
		if(DistanceToBoss(X,Y)<=50)
		{
			ClearShot;
		}
		BasePoint(120);
		BulletColor("BLUE");
	}
}

task RoundShot(let r,let angle)
{
	let obj = Obj_Create(OBJ_SHOT);
	ObjShot_SetGraphic(obj, RED05);
	ObjShot_SetDelay(obj, 0);
	ObjShot_SetBombResist(obj,true);
	Obj_SetCollisionToPlayer(obj,false);
	Obj_SetAutoDelete(obj,false);
	Obj_SetAlpha(obj,0);
	let X;
	let Y;
	let VanishJudg=false;
	let sc=0;
	while((!Obj_BeDeleted(obj) && GetSpeed>0.1) || GetSpeed==0)
	{
		Obj_SetPosition(obj, GetX,GetY);
		wait(1);
	}
	ObjShot_SetBombResist(obj,false);
	loop(15)
	{
		if(Obj_BeDeleted(obj)){break;}
		sc+=1/15;
		Obj_SetPosition(obj, GetX+r*sc*cos(angle),GetY+r*sc*sin(angle));	
		wait(1);
	}
	while(!Obj_BeDeleted(obj) && GetLife==2000)
	{
	if(VanishFlag){Obj_Delete(obj);}
	Obj_SetPosition(obj, GetX+r*cos(angle),GetY+r*sin(angle));
	wait(1);
	}
	if(!VanishFlag)
	{
	AddLife(-1);
	Obj_Delete(obj);
	}
}

task eraze
{
	wait(480);
	VanishFlag=true;
}

function wait(w)//10Fの時止め対応版
{
    loop(w) {
	if(GetCommonDataDefault("TimeStop",false)==true)
	{
	SetSpeed(0);
		loop(30){yield;}
	}

	if(GetCommonDataDefault("DefeatBoss",false)==true)
	{
		loop{yield;}
	}

	 yield; 
	}
}

function CsdDistanceToPlayer(X,Y,EX,EY)
{//距離を返す
	return(((X-EX)^2+(Y-EY)^2)^0.5);
}

function DistanceToPlayer(X,Y)
{//プレイヤーまでの距離を返す
	return(((X-GetPlayerX)^2+(Y-GetPlayerY)^2)^0.5);
}

function DistanceToBoss(X,Y)
{//敵までの距離を返す。IDが１なので注意
	return(((X-GetEnemyInfo(1,ENEMY_X))^2+(Y-GetEnemyInfo(1,ENEMY_Y))^2)^0.5);
}

function RiskShot(RiskScore)
{//リスクショットを更新。上限２０００
	SetCommonData("RiskPoint",GetCommonDataDefault("RiskPoint",0)+RiskScore);
	if(GetCommonData("RiskPoint")>=2000)
	{
		SetCommonData("RiskPoint",2000);
	}
}
function ClearShot
{//クリアショットを更新。0ならクリアショットになる。
	SetCommonData("ClearShot",GetCommonDataDefault("ClearShot",0)+1);
}

function BasePoint(Base)
{//ベースポイントを更新
	SetCommonData("BasePoint",GetCommonDataDefault("BasePoint",0)+Base);
}

function BulletColor(color)
{//色により共通データを更新
	alternative(color)
	case("RED"){SetCommonData("RedShot",GetCommonDataDefault("RedShot",0)+1);}
	case("BLUE"){SetCommonData("BlueShot",GetCommonDataDefault("BlueShot",0)+1)}
	case("GREEN"){SetCommonData("GreenShot",GetCommonDataDefault("GreenShot",0)+1);}
	case("YELLOW"){SetCommonData("YellowShot",GetCommonDataDefault("YellowShot",0)+1);}
	case("PURPLE"){SetCommonData("PurpleShot",GetCommonDataDefault("PurpleShot",0)+1);}
	case("CYAN"){SetCommonData("CyanShot",GetCommonDataDefault("CyanShot",0)+1);}
	case("ORANGE"){SetCommonData("OrangeShot",GetCommonDataDefault("OrangeShot",0)+1);}
	case("WHITE"){SetCommonData("WhiteShot",GetCommonDataDefault("WhiteShot",0)+1);}
}
    } 