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

@Initialize
{
	SetShotAutoDeleteClip(128,128,128,128);
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(64,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に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
let HoleFlag=0;
let HolePosition=[0,0];

task BossWork
{
	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 128, 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;
	loop(90){yield;}
	DamageRate=[100,100];

	Hole;

	task Hole
	{
		wait(200-15);
		loop
		{

			let x=ObjRender_GetX(objEnemy);
			let y=ObjRender_GetY(objEnemy);
			SetAction(ACT_SHOT_B, 60);
			wait(15);
			BlackHole(x,y);
			wait(390);
		}

		task BlackHole(x,y)
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../../img/effect/WhiteHole.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
	//	ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	//	Obj_SetRenderPriorityI(obj,70);
		ObjRender_SetAngleZ(obj,0);
		ObjRender_SetPosition(obj, x, y,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, -64, 64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, -64, 0);
		ObjPrim_SetVertexPosition(obj, 3, 64,64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 128);
		ObjPrim_SetVertexUVT(obj, 2, 128, 0);
		ObjPrim_SetVertexUVT(obj, 3, 128, 128);
		let scale=0.5;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		let Alpha=1;
		ObjRender_SetColor(obj,150*Alpha,150*Alpha,150*Alpha);
		let angle=GetAngleToPlayer(obj);
		let px=GetPlayerX;
		let py=GetPlayerY;
		let dis=((py-y)^2+(px-x)^2)^0.5;
		let speed=dis/45;
		let Bspeed=speed;
		loop(90)
		{
			speed-=Bspeed/90;
			ObjRender_SetX(obj,ObjRender_GetX(obj)+speed*cos(angle));
			ObjRender_SetY(obj,ObjRender_GetY(obj)+speed*sin(angle));
			yield;
		}
		HolePosition=[ObjRender_GetX(obj),ObjRender_GetY(obj)];
		HoleFlag=1;
		loop(240)
		{
			if(scale<1)
			{
				scale+=0.02;
			}
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			let Gangle=GetAngleToPlayer(obj)+180;
			let dis=((HolePosition[1]-GetPlayerY)^2+(HolePosition[0]-GetPlayerX)^2)^0.5;
			speed=100/dis;
			if(dis<=32)
			{
				ObjRender_SetX(GetPlayerObjectID,HolePosition[0]);
				ObjRender_SetY(GetPlayerObjectID,HolePosition[1]);
			}
			else
			{
			ObjRender_SetX(GetPlayerObjectID,ObjRender_GetX(GetPlayerObjectID)+speed*cos(Gangle));
			ObjRender_SetY(GetPlayerObjectID,ObjRender_GetY(GetPlayerObjectID)+speed*sin(Gangle));
			}
			yield;
		}
		HoleFlag=0;
		loop(30)
		{
			scale-=1/30;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task C
	{

		S;

		task S
		{

		loop
		{
			FireBall;
			wait(210);
			Move;
			FireBall2;
			wait(210);
		}

			task FireBall
			{
			let angle=rand(0,360);
			let way=8;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3600){way=12;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1800){way=16;}
			SetAction(ACT_SHOT_A, way*5+30);
			ascent(i in 0..way)
			{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			Bomb(ex,ey,rand(1,3),angle+i*360/way,DS_BALL_M_RED);
			wait(5);
			}
			}

			task FireBall2
			{
			let angle=rand(0,360);
			let way=8;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3600){way=12;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1800){way=16;}
			ascent(i in 0..way)
			{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			Bomb(ex,ey,rand(1,3),angle-i*360/way,DS_BALL_M_BLUE);
			wait(5);
			}
			}
		}

		task Move
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			let mx= ex+rand(-150,150);
			let my= ey+rand(-30,30);
			while(mx<64 || mx>320){mx=ex+rand(-150,150);}
			while(my<64 || my>128){my=ey+rand(-30,30);}
			ObjMove_SetDestAtWeight(objEnemy,mx, my, 20,3.0);
			SetAction(ACT_MOVE, 75);
			wait(150);
		}

		task Bomb(x,y,speed,angle,gra)
		{
			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);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

			ShotEffect(obj);
			let Bspeed=speed;
			loop(120)
			{
				ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				speed-=Bspeed/120;
				ObjMove_SetSpeed(obj, speed);
				yield;
			}
			x=ObjRender_GetX(obj);
			y=ObjRender_GetY(obj);
			speed=1;
			angle=rand(0,360);
			ascent(i in 0..8)
			{
				ascent(j in 0..4)
				{
			Shot(x,y,speed+0.5*j,angle+i*45,gra);
				}
			}
			Obj_Delete(obj);
		}

		task Shot(x,y,speed,angle,gra)
		{
			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-8);
			//ObjShot_SetDelay(obj, delay);
		//	ObjShot_SetAutoDelete(obj,false);
		//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

			ShotEffect(obj);
			let Count=0;
			while(!Obj_IsDeleted(obj))
			{
				if(HoleFlag)
				{
					let Gangle=atan2(HolePosition[1]-ObjRender_GetY(obj),HolePosition[0]-ObjRender_GetX(obj));
					let dis=((HolePosition[1]-ObjRender_GetY(obj))^2+(HolePosition[0]-ObjRender_GetX(obj))^2)^0.5;
					speed=100/dis;
					if(dis<=32)
					{
						ObjRender_SetX(obj,HolePosition[0]);
						ObjRender_SetY(obj,HolePosition[1]);
					}
					else
					{
					ObjRender_SetX(obj,ObjRender_GetX(obj)+speed*cos(Gangle));
					ObjRender_SetY(obj,ObjRender_GetY(obj)+speed*sin(Gangle));
					}
				}
				yield;
			}
			Obj_Delete(obj);
		}

			task BladeShot(x,y,angle,gra)
			{
				let obj = CreateShotA2(x, y, 0, angle, 0, 0, gra, 0);//弾を発射
				ObjShot_SetSpellResist(obj,true);
				let scale=1.5;
				ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				let speed=0.75;
				loop(8)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=1.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-=1.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);
	}
}
