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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(224,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(4000);//ライフを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, 144, 10,3);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「ミスティーリング」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(75){yield;}
	C;
	loop(120){yield;}
	DamageRate=[100,100];
	task C
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy)-64;
		let PA=90;
		let A=0;
		SetAction(ACT_SHOT_A, 60);
		ascent(i in 0..5)
		{
		Shot(ex,ey,PA,A+72*i,1,0);
		Shot(ex,ey,PA,A+72*i,-1,5);
		}

		task Shot(x,y,PAngle,Angle,dir,t)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, 9+t);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

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

		let angle=atan2(1/3*sin(PAngle)*cos(Angle)+1*cos(PAngle)*sin(Angle),1/3*cos(PAngle)*cos(Angle)-1*sin(PAngle)*sin(Angle));
		ObjMove_SetAngle(obj, angle);
		T_Shot;
		T_Laser;
		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetX(obj,x+R/3*cos(PAngle)*cos(Angle)-R*sin(PAngle)*sin(Angle));
			ObjRender_SetY(obj,y+R/3*sin(PAngle)*cos(Angle)+R*cos(PAngle)*sin(Angle));
			if(R<150){
			R+=1;}
		//	PAngle+=0.5;
			Angle+=3.5*dir;
			yield;
		}
		Obj_Delete(obj);

		task T_Shot
		{
			wait(60);
			let speed=1.25+0.1*t;
			let angle=90;
			let angletheta=0;
			R;
			loop
			{
			S(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,Angle+angle+90+dir*90+GetAngleToPlayer(objEnemy)-90,t);
			wait(2);
			S(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,Angle+angle+90+dir*90+GetAngleToPlayer(objEnemy)-90,t);
			wait(3);
			}
			
			task R
			{
			loop
			{
				angle-=1.0;
				yield;
			}
			}
		}

		task T_Laser
		{
			wait(240);
			let wt=180;
			wtc;
			loop
			{
				if(t==0){
				Laser(obj,t);}
				wait(wt);
				if(t==5){
				Laser(obj,t);}
				wait(wt);
			}

			task wtc
			{
			loop
			{
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3000){wt=150;}
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=2000){wt=120;}
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1000){wt=90;}
				yield;
			}
			}
		}

		}

		task Laser(Cobj,t)
		{
			let x=GetPlayerX;
			let y=GetPlayerY;
			let LAngle=0;
			let width=3;
			let length=0;
			let obj=ObjShot_Create(OBJ_STRAIGHT_LASER);
			ObjShot_Regist(obj);
			ObjShot_SetGraphic(obj,153+t);
			ObjLaser_SetLength(obj,length);
			ObjLaser_SetRenderWidth(obj,24);
			ObjStLaser_SetAngle(obj,LAngle);
			ObjRender_SetPosition(obj,ObjMove_GetX(Cobj),ObjMove_GetY(Cobj),0);
			ObjShot_SetIntersectionEnable(obj,false);
			loop(30)
			{
				LAngle=atan2(y-ObjRender_GetY(Cobj),x-ObjRender_GetX(Cobj));
			//	width+=12/30;
				length+=500/30;
				ObjLaser_SetRenderWidth(obj,width);
				ObjRender_SetPosition(obj,ObjMove_GetX(Cobj),ObjMove_GetY(Cobj),0);
				ObjLaser_SetLength(obj,length);
				ObjStLaser_SetAngle(obj,LAngle);
				yield;
			}
			loop(30)
			{
				LAngle=atan2(y-ObjRender_GetY(Cobj),x-ObjRender_GetX(Cobj));
				width+=12/30;
			//	length+=500/30;
				ObjLaser_SetRenderWidth(obj,width);
				ObjRender_SetPosition(obj,ObjMove_GetX(Cobj),ObjMove_GetY(Cobj),0);
				ObjLaser_SetLength(obj,length);
				ObjStLaser_SetAngle(obj,LAngle);
				yield;
			}
			ObjShot_SetIntersectionEnable(obj,true);
			loop(75)
			{
				LAngle=atan2(y-ObjRender_GetY(Cobj),x-ObjRender_GetX(Cobj));
				ObjRender_SetPosition(obj,ObjMove_GetX(Cobj),ObjMove_GetY(Cobj),0);
				ObjStLaser_SetAngle(obj,LAngle);
				yield;
			}
			ObjShot_SetIntersectionEnable(obj,false);
			loop(30)
			{
				LAngle=atan2(y-ObjRender_GetY(Cobj),x-ObjRender_GetX(Cobj));
				width-=12/30;
				ObjLaser_SetRenderWidth(obj,width);
				ObjRender_SetPosition(obj,ObjMove_GetX(Cobj),ObjMove_GetY(Cobj),0);
				ObjStLaser_SetAngle(obj,LAngle);
				yield;
			}
			Obj_Delete(obj);
		}


		task S(x,y,speed,angle,t)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, speed);
		ObjShot_SetGraphic(obj, 9+t);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);
		ShotEffect(obj);
		ObjMove_SetAngle(obj, angle);

		while(!Obj_IsDeleted(obj))
		{
			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);
	}
}