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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(192,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(5100);//ライフを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, 96, 10,3);
//	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「阿求乙女舞」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(60){yield;}
	C;
	loop(120){yield;}
	S;
	loop(60){yield;}
	DamageRate=[100,100];

	task S
	{
		let AP=0;
		let Sangle=0;
		let angle=45;
		let wt=8;
		loop
		{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3400){wt=6;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1700){wt=4;}
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			ascent(i in 0..4)
			{
			Shot(ex+36*cos(angle+i*90),ey+36*sin(angle+i*90),1.5,angle+i*90);
			}
			angle+=AP;
			AP+=1;
			wait(wt);
		}

		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, DS_BALL_S_YELLOW);
			//ObjShot_SetDelay(obj, delay);
		//	ObjShot_SetAutoDelete(obj,false);
		//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

			ShotEffect(obj);
			let Count=0;
			while(!Obj_IsDeleted(obj))
			{
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task C
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let dir=floor(rand(0,2-0.001))*2-1;
		let gra=0;
		SetAction(ACT_SHOT_A, 90);
		loop(7)
		{
		let angle=rand(0,360);
		ascent(i in 0..9)
		{
		S(ex,ey,angle+i*45,dir,gra);
		}
		dir*=-1;
		gra+=1;
		wait(10);
		}
		wait(30);

		loop(7)
		{
		Ring(gra%7);
		wait(105);
		gra+=1;
		}
		wait(120);
		C;

		task Ring(gra)
		{
			let cx=192;
			let cy=224;
			let cr=384;
			let ca=rand(0,360);
			let RingX=cx+cr*cos(ca);
			let RingY=cy+cr*sin(ca);
			let RingAngle=atan2(GetPlayerY-RingY,GetPlayerX-RingX);
			let RingRadius=128;
			let RingBitAngle=rand(0,360);
			let speed=2.0;
			let oldX=[0,0,0,0,0,0,0,0,0];
			let oldY=[0,0,0,0,0,0,0,0,0];
			loop(360)
			{
				RingX+=speed*cos(RingAngle);
				RingY+=speed*sin(RingAngle);
				ascent(i in 0..9)
				{
				let DAngle=atan2(RingY+RingRadius*sin(RingBitAngle+i*40)-oldY[i],RingX+RingRadius*cos(RingBitAngle+i*40)-oldX[i]);
				BladeShot(RingX+RingRadius*cos(RingBitAngle+i*40),RingY+RingRadius*sin(RingBitAngle+i*40),DAngle,81+gra);
				oldX[i]=RingX+RingRadius*cos(RingBitAngle+i*40);
				oldY[i]=RingY+RingRadius*sin(RingBitAngle+i*40);
				}
				RingBitAngle+=1.0;
				yield;
			}
		}

		task S(hx,hy,angle,dir,t)
		{
			let LaserSpeed=5;
			let angleAdd=0;
			loop(30)
			{
				hx+=LaserSpeed*cos(angle);
				hy+=LaserSpeed*sin(angle);
				BladeShot(hx,hy,angle+angleAdd,81+t%7);
				angle+=3*dir;
				LaserSpeed-=4/30;
				yield;
			}
			loop(120)
			{
				hx+=LaserSpeed*cos(angle);
				hy+=LaserSpeed*sin(angle);
				LaserSpeed+=5/30;
				BladeShot(hx,hy,angle+angleAdd,81+t%7);
				yield;
			}
		}

			task BladeShot(x,y,angle,gra)
			{
				let obj = CreateShotA2(x, y, 0, angle, 0, 0, gra, 0);//弾を発射
				ObjShot_SetSpellResist(obj,true);
				ObjShot_SetAutoDelete(obj,false);
				let scale=2.5;
				ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				let speed=0.75;
				loop(8)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/12;
					ObjRender_SetScaleXYZ(obj,scale,scale,scale);
					ObjShot_SetIntersectionCircleA1(obj,scale*6);
					yield;
				}
				ObjShot_SetIntersectionEnable(obj,false);
				loop(4)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/12;
					ObjRender_SetScaleXYZ(obj,scale,scale,scale);
					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);
	}

}