
#ScriptVersion[3]
#Title["星符「ミッドナイトレヴァリエ」"]
#Text["Exルーミア最初のスペルカード：[r]星符「ミッドナイトレヴァリエ」"]
#Image["./ExRumia(星符「ミッドナイトレヴァリエ」).jpg"]
#Background["script/default_system/Default_Background_IceMountain.txt"]
#BGM[DEFAULT]
#Player[DEFAULT]

#include"script/default_system/Default_ShotConst.txt"
#include"script/default_system/Default_Effect.txt"

#include"./../../img/boss/lib_anime_Reisen.txt"
#include"./../../lib/BossCommon/BossCommon.txt"

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(384,160);
	SetItem(20,2,2);
}

@MainLoop
{
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, ex, ey, 32);//当たり判定(自弾)登録
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ex, ey, 12);//当たり判定(体当たり)登録
	SetCommonData("BossPosition",[ex,ey]);
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(5700);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 224, 10,5);
	SetAction(ACT_MOVE, 120);
//	loop(60){yield;}
	CallSpellCard("「星辰朦朧（アーミラリシェイク）」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(120){yield;}
	C;
	wait(90);
	DamageRate=[100,100];

	task C
	{
	let AzP=0;
	let ElP=0;
	let ChangeFlag=0;
	let ChangeRate=0;
	Change;
	let waittime=90;
	loop
	{
		let AngleSpeed=1*0;
		let Az=rand(0,360);
		let El=rand(0,360);

		let angle=rand(0,360);
		let way=60;
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1425){way=72;}
		ascent(i in 0..way)
		{
				Point(192,224,0,Az,El,angle+i*360/way,0,DS_MISSILE_RED);
		}
		wait(waittime);
		Az=rand(0,360);
		El=rand(0,360);
		ascent(i in 0..way)
		{
				Point(192,224,0,Az,El+90,angle+i*360/way,0,DS_MISSILE_BLUE);
		}
		wait(waittime/3);
		Az=rand(0,360);
		El=rand(0,360);
		ascent(i in 0..way)
		{
				Point(192,224,0,Az,El+90,angle+i*360/way,0,DS_MISSILE_PURPLE);
		}
		wait(waittime/3);

		Az=rand(0,360);
		El=rand(0,360);
		ascent(i in 0..way)
		{
				Point(192,224,0,Az,El+90,angle+i*360/way,0,DS_MISSILE_WHITE);
		}
		wait(waittime/3);
	}

	task Change
	{
		loop
		{
		wait(30);
		ChangeFlag=1;
		let dir=floor(rand(0,2-0.001))*2-1;
		let dir2=floor(rand(0,2-0.001))*2-1;
		AzP=rand(1,2)*dir/2;
		ElP=rand(1,2)*dir2/2;
		SetAction(ACT_SHOT_B, 60);
		loop(20)
		{
			ChangeRate+=1/20;
			yield;
		}
		loop(20)
		{
			yield;
		}
		loop(20)
		{
			ChangeRate-=1/20;
			yield;
		}
		ChangeFlag=0;
		AzP=0;
		ElP=0;
		wait(90);
		}
	}

	task Point(cx,cy,Radius,Az,El,a,b,gra)
	{
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjShot_SetAutoDelete(obj,false);
		ObjShot_SetGraphic(obj, gra);
		ObjShot_SetSpellResist(obj,true);

		let x0; let y0; let z0; let x; let y; let z;


	//	ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjShot_SetIntersectionEnable(obj,true);
		let count=0;
		Counter;
		let Scale=1.5;
	//	ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);

		let BulletFlag=1;
		while(!Obj_IsDeleted(obj))
		{
			x0=Radius*sin(a)*cos(b);
			y0=Radius*sin(a)*sin(b);
			z0=Radius*cos(a);
			x=x0;
			y=y0*cos(Az)-z0*sin(Az);
			z=y0*sin(Az)+z0*cos(Az);
			let tx=x;
			x=x*cos(El)+z*sin(El);
			z=-tx*sin(El)+z*cos(El);

			ObjMove_SetPosition(obj,cx+x,cy+y);

			if(ChangeFlag==0)
			{
				Radius+=2.5;
				BulletFlag=1;
			}
			else
			{
				if(count==3){break;}
				if(BulletFlag==1)
				{
			//		Shot(cx+x,cy+y,1,atan2(cy+y-144,cx+x-192))
				}
				BulletFlag=0;
			}
			Az+=AzP*ChangeRate;
			El+=ElP*ChangeRate;
			ObjMove_SetAngle(obj,atan2(cy+y-224,cx+x-192));
			yield;
		}
		Obj_Delete(obj);

		task Counter
		{
			while(!Obj_IsDeleted(obj))
			{
			while(ChangeFlag==0){yield;}
			if(Obj_IsDeleted(obj)){break;}
			ObjShot_SetIntersectionEnable(obj,false);
			ObjRender_SetAlpha(obj,50);
			count+=1;
			while(ChangeFlag==1){yield;}
			if(Obj_IsDeleted(obj)){break;}
			ObjShot_SetIntersectionEnable(obj,true);
			ObjRender_SetAlpha(obj,255);
			}
		}

		task Shot(x,y,speed,angle)
		{

		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj,speed);
		ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, gra);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

		let scale=4.0;
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=320;
		Counter;

		while(!Obj_IsDeleted(obj))
		{
			yield;
		}
		Obj_Delete(obj);

		task Counter
		{
			while(!Obj_IsDeleted(obj))
			{
			while(ChangeFlag==0){yield;}
			if(Obj_IsDeleted(obj)){break;}
			ObjMove_SetSpeed(obj,0);
			ObjShot_SetIntersectionEnable(obj,false);
			ObjRender_SetAlpha(obj,50);
			while(ChangeFlag==1){yield;}
			if(Obj_IsDeleted(obj)){break;}
			ObjMove_SetSpeed(obj,speed);
			ObjShot_SetIntersectionEnable(obj,true);
			ObjRender_SetAlpha(obj,255);
			}
		}

		}

	}

	}
/*
	task B
	{
		let grad=0;
		let gra=0;
		GradChange;
		loop
		{
		ascent(i in 0..10)
		{
		Shot(i*80,241+(gra+i)%7,1,0);
		Shot(i*80,241+(gra-i+8)%7,-1,0);
		Shot(i*80,241+(gra+i)%7,1,90);
		Shot(i*80,241+(gra-i+8)%7,-1,90);
		}
		//gra+=1;
		loop(50*2){yield}
		}
		
		task GradChange
		{
		wait(300*1.5);
		let graddir=1;
		if(GetPlayerX>192){graddir=-1;}
		let gradplus=0.1;
		loop
		{
			grad+=gradplus*graddir;
			if(gradplus<0.5)
			{
			gradplus+=0.0001;
			}
			yield;
		}
		}

		task Shot(height,gra,dir,grad2)
		{
		let time=0;
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);

		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, 0);
	//	ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, gra);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=320;
		//while(!Obj_IsDeleted(obj))
		loop(360*2)
		{
			x=196+(R*cos(grad+grad2)+(R-time)*cos(grad+grad2+90)-((time*2)*0+height)*cos(grad+grad2))*dir;
			y=224+(R*sin(grad+grad2)+(R-time)*sin(grad+grad2+90)-((time*2)*0+height)*sin(grad+grad2))*dir;
			ObjMove_SetPosition(obj, x, y);
			time+=64/40/2;
			yield;
		}
		Obj_Delete(obj);
		}

	}

	task A
	{
		let angle=-90;
		ascent(j in 0..7)
		{
		ascent(i in 0..36-2*j)
		{
		Shot(1.5+j*0.3*0+1.5,i*360/(36-2*j),247-j,j%2*2-1);
		}

		wait(4);
		}

		task Shot(speed,angle,gra,dir)
		{
		let R=0;
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);

		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, 0);
		ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, gra);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

		let sangle=atan2(ObjRender_GetY(objEnemy)-y,ObjRender_GetX(objEnemy)-x)+180;
		ObjMove_SetAngle(obj, sangle+45*((gra%2)*2-1));
		while(!Obj_IsDeleted(obj))
		{
			ObjMove_SetPosition(obj, x+R*cos(angle), y+R*sin(angle));
			R+=speed;
			angle+=0.5*dir;
			yield;
		}
		}
	}
*/
}