
#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_Koakuma.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(4400);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	let AbyssPos=[192,224];
	let AbyssAngle=0;
	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 144, 10,7);
	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;
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>=EnemyMaxLife/2){yield;}
*/
	D;
	AbyssChange;
	C;
	wait(210);
	DamageRate=[100,100];
	wait(210);
	A;
	task AbyssChange
	{
		AngleChange;
		wait(184);
		loop
		{
			Change;
			loop(360){yield;}
		}

		task Change
		{
			let ex=rand(192-64,192+64);
			let ey=rand(366-32,366+32);
			ex=(ex*3+GetPlayerX)/4;
			ey=(ey*3+GetPlayerY)/4;
			let sx=AbyssPos[0];
			let sy=AbyssPos[1];
			let AA=atan2(ey-sy,ex-sx);
			let dis=((ey-sy)^2+(ex-sx)^2)^0.5;
			let speed=dis/180;
			loop(180)
			{
				AbyssPos[0]=AbyssPos[0]+speed*cos(AA);
				AbyssPos[1]=AbyssPos[1]+speed*sin(AA);
				yield;
			}
		}

		task AngleChange
		{
			wait(184);
			let dir=floor(rand(0,2-0.001))*2-1;
			if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{
				dir=1;
			}
			else
			{
				dir=-1;
			}
			let AAP=0;
			loop
			{
				AbyssAngle+=AAP*dir;
				if(AAP<0.2){AAP+=0.2/60;}
				yield;
			}
		}
	}

	task D
	{
		ascent(j in -18..19)
		{	
		ascent(i in 0..5)
		{
		RingShot(0,0+j*24,i*72,1);
		RingShot(0,0+j*24,i*72+36,-1);
		}
		}

		task RingShot(x,y,Angle,dir)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, 27.5-dir*2.5);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);

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

		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetX(obj,(x+R*cos(Angle)+256*dir)*cos(AbyssAngle)-(y+AbyssPos[1]*0)*sin(AbyssAngle)+AbyssPos[0]);
			ObjRender_SetY(obj,(x+R*cos(Angle)+256*dir)*sin(AbyssAngle)+(y+AbyssPos[1]*0)*cos(AbyssAngle)+AbyssPos[1]);
			Angle+=2*dir;
			if(R<184){R+=1;}
		//	BlurEffect(ObjRender_GetX(obj),ObjRender_GetY(obj), 27.5-dir*2.5);
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task C
	{
	wait(210);
	let R=30;
	let Speed=1.5/2;
	let Speed2=0.75*1.5;
	let Angle=0;
	let AnglePlus=1;
	loop
	{
//		Shot(ObjRender_GetX(objEnemy)+R*cos(Angle),ObjRender_GetY(objEnemy)+R*sin(Angle),Speed,Angle);
//		Shot(ObjRender_GetX(objEnemy)+R*cos(Angle+180),ObjRender_GetY(objEnemy)+R*sin(Angle+180),Speed,Angle+180);
		Shot(ObjRender_GetX(objEnemy)+R*cos(-Angle),ObjRender_GetY(objEnemy)+R*sin(-Angle),Speed2,-Angle);
		Shot(ObjRender_GetX(objEnemy)+R*cos(-Angle+180),ObjRender_GetY(objEnemy)+R*sin(-Angle+180),Speed2,-Angle+180);
		Angle+=AnglePlus;
		AnglePlus+=0.5;
if(AnglePlus>5){AnglePlus=5;}
		let life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		Speed=2.0-1.25*(life/4400);
		Speed2=Speed*1.5;
		wait(8);
	//	Shot(ObjRender_GetX(objEnemy)+R*cos(Angle),ObjRender_GetY(objEnemy)+R*sin(Angle),Speed,Angle);
	//	Shot(ObjRender_GetX(objEnemy)+R*cos(Angle+180),ObjRender_GetY(objEnemy)+R*sin(Angle+180),Speed,Angle+180);
		Shot(ObjRender_GetX(objEnemy)+R*cos(-Angle),ObjRender_GetY(objEnemy)+R*sin(-Angle),Speed2,-Angle);
		Shot(ObjRender_GetX(objEnemy)+R*cos(-Angle+180),ObjRender_GetY(objEnemy)+R*sin(-Angle+180),Speed2,-Angle+180);
		Angle+=AnglePlus;
		AnglePlus+=0.5;
if(AnglePlus>5){AnglePlus=5;}
		life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		Speed=2.0-1.25*(life/4400);
		Speed2=Speed*1.5;
		wait(5);
		if(life>=3520){wait(1);}
		if(life>=2640){wait(1);}
		if(life>=1760){wait(1);}
		
	}

		task Shot(x,y,speed,Angle)
		{
		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, 79);
	//	ObjShot_SetDelay(obj,10);
		//ObjShot_SetAutoDelete(obj,false);

		ShotEffect(obj);

		while(!Obj_IsDeleted(obj))
		{
			if(ObjRender_GetX(obj)<0)
			{
				Shot_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,GetAngleToPlayer(obj));
				break;
			}
			if(ObjRender_GetX(obj)>384)
			{
				Shot_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,GetAngleToPlayer(obj));
				break;
			}
			if(ObjRender_GetY(obj)<0)
			{
				Shot_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,GetAngleToPlayer(obj));
				break;
			}
			if(ObjRender_GetY(obj)>448)
			{
				Shot_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,GetAngleToPlayer(obj));
				break;
			}
			yield;
		}
		Obj_Delete(obj);
		}

		task Shot_A(x,y,speed,Angle)
		{
		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, 47-8);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

		ShotEffect(obj);

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

		task Shot2(x,y,speed,Angle)
		{
		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, 79);
	//	ObjShot_SetDelay(obj, 10);
		//ObjShot_SetAutoDelete(obj,false);

		ShotEffect(obj);
		while(!Obj_IsDeleted(obj))
		{
			if(ObjRender_GetX(obj)<0)
			{
				Shot2_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,0);
yield;
//				break;
			}
			if(ObjRender_GetX(obj)>384)
			{
				Shot2_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,180);
yield;
//				break;
			}
			if(ObjRender_GetY(obj)<0)
			{
				Shot2_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,90);
yield;
//				break;
			}
			if(ObjRender_GetY(obj)>448)
			{
				Shot2_A(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,270);
yield;
//				break;
			}
			yield;
		}
		Obj_Delete(obj);
		}

		task Shot2_A(x,y,speed,Angle)
		{
		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, 47-8);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

		ShotEffect(obj);

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

	task A
	{
		loop
		{
			ascent(i in 0..9)
			{
			Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),1,GetAngleToPlayer(objEnemy)+i*40);
			}
			wait(120);
		}
		task Shot(x,y,speed,Angle)
		{
		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, 171);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

		ShotEffect(obj);

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


}