
#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_Muse.txt"
#include"./../../lib/BossCommon/BossCommon.txt"

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(0,0);
	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(5400);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	let hallucinationFlag=0;
	let hallucinationCount=0;

	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 112, 10,7);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「アルスコンビナトリア」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

//	C;
	loop(60){yield;}
	SetAction(ACT_SHOT_C, 12000);
	loop(15){yield;}
	H;
	loop(90){yield;}
	DamageRate=[100,100];

	task H
	{
	S1;
	S2;

	task S1
	{
	loop
	{
		Shot(rand(0-16,384+16),0,rand(0.5,1.5),rand(88,92),9);
		wait(4-1);
	}
	}
	task S2
	{
	loop
	{
		Shot2(rand(0-16,384+16),448,rand(0.5,1.5),rand(268,272),14);
		wait(4-1);
	}
	}

	task Shot(x,y,speed,angle,gra)
	{
		let time=0;
		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);
		ShotEffect(obj);

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

		while(!Obj_IsDeleted(obj))
		{
			if(length(GetShotIdInCircleA2(ObjRender_GetX(obj),ObjRender_GetY(obj),10,TARGET_ENEMY)))
			{
				let BulletArray=GetShotIdInCircleA2(ObjRender_GetX(obj),ObjRender_GetY(obj),10,TARGET_ENEMY);
				let nearObj=obj;
				let nearDis=100;
				ascent(i in 0..length(BulletArray))
				{
					let dis=(ObjRender_GetY(obj)-ObjRender_GetY(BulletArray[i]))^2+(ObjRender_GetX(obj)-ObjRender_GetX(BulletArray[i]))^2;
					if(dis<nearDis && Obj_GetValueD(BulletArray[i],"type",0))
					{
						nearDis=dis;
						nearObj=BulletArray[i];
					}
				}
				if(nearObj!=obj)
				{
					speed=nearDis^0.5/2/30;
					angle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
					Obj_SetValue(nearObj,"com",[1,speed,angle+180]);
					ObjMove_SetSpeed(obj, speed);
					ObjMove_SetAngle(obj, angle);
					loop(30)
					{
						yield;
					}
					Shot3(ObjRender_GetX(obj),ObjRender_GetY(obj),0,GetAngleToPlayer(obj),15);
					break;
				}
			}
			yield;
		}
		Obj_Delete(obj);
	}

	task Shot2(x,y,speed,angle,gra)
	{
		let time=0;
		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);
		ShotEffect(obj);

		let scale=4.0;
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=0;
		Obj_SetValue(obj,"type",1);
		while(!Obj_IsDeleted(obj))
		{
			if(Obj_GetValueD(obj,"com",[0,0,0])[0])
			{
				Obj_SetValue(obj,"type",0);
				ObjMove_SetSpeed(obj, Obj_GetValueD(obj,"com",[0,0,0])[1]);
				ObjMove_SetAngle(obj, Obj_GetValueD(obj,"com",[0,0,0])[2]);
				loop(30)
				{
					yield;
				}
				break;
			}
			yield;
		}
		Obj_Delete(obj);
	}

	task Shot3(x,y,speed,angle,gra)
	{
		let time=0;
		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);
		ShotEffect(obj);
		let scale=4.0;
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=0;
		while(!Obj_IsDeleted(obj))
		{
			if(speed<1.0)
			{
				speed+=1.0/120;
			}
			ObjMove_SetSpeed(obj, speed);
			yield;
		}
		Obj_Delete(obj);
	}

	}

	task ShotEffect(obj)
	{
		let scale=2.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjShot_SetIntersectionEnable(obj,false);
		loop(15)
		{
			if(Obj_IsDeleted(obj)){break;}
			scale-=1.0/15;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			yield;
		}
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjShot_SetIntersectionEnable(obj,true);
	}
}