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

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

//----------------------------------------------------
//敵動作
//----------------------------------------------------
let HoleFlag=0;

let HoleX=[48,144,240,336];
let HoleY=[120,196,196,120];


let HoleX2=[16,368,368,16];
let HoleY2=[16,16,432,432];


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

	task C
	{
		ascent(i in 0..4)
		{
		Enemy(i);
		}
		wait(120);
		HoleFlag=1;
		S;

		task S
		{
			let angle=rand(0,360);
			let Sangle=0;
		loop
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			ascent(i in 0..8)
			{
			Shot(ex+45*cos(6*angle+i*45),ey+45*sin(5*angle+i*45),1.5,Sangle+i*45);
			Shot(ex+45*cos(5*angle+i*45),ey+45*sin(6*angle+i*45),1.5,Sangle+i*45);
			}
			angle+=2;
			Sangle+=6;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3800){break;}
			wait(7);
		}
		DamageRate=[5,5];
		wait(90);
		Move;
		let AP=0;
		Sangle=0;
		angle=0;
		loop
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			ascent(i in 0..9)
			{
			Shot(ex+36*cos(angle+i*45),ey+36*sin(angle+i*45),0.875,angle+i*40);
			}
			angle+=AP;
			AP+=1;
			wait(8);
		}
		}

		task Move
		{
			wait(120);
			DamageRate=[100,100];
			loop
			{
			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(210);
			}
		}

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

			ShotEffect(obj);
			let Count=0;
			while(!Obj_IsDeleted(obj))
			{
				if(Count==0 &&	HoleFlag==1)
				{
					ascent(i in 0..4)
					{
						let x=ObjRender_GetX(obj);
						let y=ObjRender_GetY(obj);
						let HX=HoleX[i];
						let HY=HoleY[i];
						let dis=((HY-y)^2+(HX-x)^2)^0.5;
						let radius=48;
						if(dis<=radius)
						{
							let ax=0;
							let ay=0;
							if(i==0)
							{
								ax=HoleX[2]-HoleX[0];
								ay=HoleY[2]-HoleY[0];
							}
							if(i==1)
							{
								ax=HoleX[3]-HoleX[1];
								ay=HoleY[3]-HoleY[1];
							}
							if(i==2)
							{
								ax=HoleX[0]-HoleX[2];
								ay=HoleY[0]-HoleY[2];
							}
							if(i==3)
							{
								ax=HoleX[1]-HoleX[3];
								ay=HoleY[1]-HoleY[3];
							}
							ObjRender_SetX(obj,x+ax);
							ObjRender_SetY(obj,y+ay);
							ObjShot_SetGraphic(obj, DS_BALL_S_RED);
							Count+=1;
							break;
						}
					}
				}
				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);
	}
}

task Enemy(num)
{

	let X=HoleX[num];
	let Y=HoleY[num];
	#include"./../../lib/ZakoCommon/ZakoCommon.txt"
	let dis=((ObjRender_GetY(objEnemy)-Y)^2+(ObjRender_GetX(objEnemy)-X)^2)^0.5;
	let speed=dis/45;
	let Bspeed=speed;
	let angle=atan2(Y-ObjRender_GetY(objEnemy),X-ObjRender_GetX(objEnemy));
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	SetLife(obj,10000);
        SetDamageRate(obj,0,0);
//	DrawEnemy(obj,RedFairy,1);
	DrawCircle(obj,0.20,255,255,255);
	EnemyResist(obj,ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy));
//	ObjRender_SetMovePositionBezier(obj,[x+rand(-200,200)],[y+rand(-200,200)],x+R*cos(angle),y+R*sin(angle),120);
//	ObjMove_SetDestAtFrame(obj,ObjRender_GetX(obj)+R*cos(angle),ObjRender_GetY(obj)+R*sin(angle),30);

	loop(90)
	{	
		speed-=Bspeed/90;
		ObjMove_SetX(obj,ObjMove_GetX(obj)+speed*cos(angle));
		ObjMove_SetY(obj,ObjMove_GetY(obj)+speed*sin(angle));
		yield;
	}

	let anglePlus=0;
	let Radius=0;

	Eater(ObjMove_GetX(obj),ObjMove_GetY(obj),0,0);

	loop
	{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3800){break;}
		yield;
	}

	dis=((HoleY[num]-HoleY2[num])^2+(HoleX[num]-HoleX2[num])^2)^0.5;
	speed=dis/45;
	Bspeed=speed;
	angle=atan2(HoleY[num]-HoleY2[num],HoleX[num]-HoleX2[num])+180;

	loop(90)
	{	
		speed-=Bspeed/90;
		ObjMove_SetX(obj,ObjMove_GetX(obj)+speed*cos(angle));
		ObjMove_SetY(obj,ObjMove_GetY(obj)+speed*sin(angle));
		HoleX[num]=ObjMove_GetX(obj);
		HoleY[num]=ObjMove_GetY(obj);
		yield;
	}

	loop
	{
		HoleX=HoleX2;
		HoleY=HoleY2;
		yield;
	}

	Obj_Delete(obj);

	task Eater(x,y,ax,ay)
	{
		let objE = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(objE, GetCurrentScriptDirectory~"../../img/effect/WhiteHole.png");
		ObjPrim_SetPrimitiveType(objE, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(objE, 4);
		ObjRender_SetBlendType(objE, BLEND_ADD_RGB);
		ObjRender_SetAngleZ(objE,0);

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

		ObjPrim_SetVertexUVT(objE, 0, 0, 0);
		ObjPrim_SetVertexUVT(objE, 1, 0, 128);
		ObjPrim_SetVertexUVT(objE, 2, 128, 0);
		ObjPrim_SetVertexUVT(objE, 3, 128, 128);
		let scale=0;
		ObjRender_SetScaleXYZ(objE,scale,scale,scale);
		let Alpha=1;
		ObjRender_SetColor(objE,50*Alpha,50*Alpha,50*Alpha);
		let speed=1.5;
		loop(30)
		{
			scale+=1.2*0.66/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			ObjRender_SetPosition(objE,x,y,0);
			yield;
		}
		loop
		{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3800){break;}
			ObjRender_SetPosition(objE,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		loop(30)
		{
			ObjRender_SetPosition(objE,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			scale-=1.2*0.66/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			yield;
		}
		loop(60)
		{
			ObjRender_SetPosition(objE,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		loop(30)
		{
			ObjRender_SetPosition(objE,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			scale+=1.2*0.66/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			yield;
		}
		loop
		{
			ObjRender_SetPosition(objE,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		Obj_Delete(objE);
	}

}