
#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_Sakuya.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]);
	SetCommonData("uuu",HoleObject);
	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に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
let HoleObject=[];
let SubHoleObject=[];

task BossWork
{
	//座標(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;
	loop(90){yield;}
	DamageRate=[100,100];

	task C
	{
		S;
		wait(60);
		E;


		task E
		{
			loop
			{
			ascent(i in 0..3)
			{
				Enemy(GetAngleToPlayer(objEnemy)+i*120);
			}
			wait(420);
			}
		}
		task S
		{
		let way=15;
		loop
		{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=3000){way=18;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1500){way=24;}
			loop(2)
			{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			let angle=GetAngleToPlayer(objEnemy);
			loop(3)
			{
			ascent(i in 0..way)
			{
				Shot(ex,ey,1.75,angle+360/way*i);
			}
			wait(10);
			}
			wait(60);
			}
			local
			{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
			let angle=GetAngleToPlayer(objEnemy);
			loop(3)
			{
			ascent(i in 0..way)
			{
				Shot(ex,ey,1.75,angle+360/way*i);
			}
			wait(10);
			}
			}

			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<80 || my>160){my=ey+rand(-30,30);}
			ObjMove_SetDestAtWeight(objEnemy,mx, my, 20,3.0);
			SetAction(ACT_MOVE, 75);
			wait(60);
			ex = ObjMove_GetX(objEnemy);
			ey = ObjMove_GetY(objEnemy);
			let angle=GetAngleToPlayer(objEnemy);
			ascent(k in -1..3)
			{
			ascent(j in 0..4)
			{
			ascent(i in -2..3)
			{
				Shot(ex,ey,3.0+0.5*j,angle+2*i+k*90);
			}
			}
			}
			wait(150);
		}
		}

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

			ShotEffect(obj);
			let ReflectCount=0;
			while(!Obj_IsDeleted(obj))
			{
				if(ReflectCount==0)
				{
					if(ObjRender_GetY(obj)<0)//上端での挙動
					{
					ObjRender_SetY(obj,ObjRender_GetY(obj)+448);
					ReflectCount=1;
        				}
					if(ObjRender_GetY(obj)>GetStgFrameHeight)//上端での挙動
					{
					ObjRender_SetY(obj,ObjRender_GetY(obj)-448);
					ReflectCount=1;
        				}
					if(ObjRender_GetX(obj)<0)//左端での挙動
					{
					ObjRender_SetX(obj,ObjRender_GetX(obj)+384);
					ReflectCount=1;
        				}
					if(ObjRender_GetX(obj)>GetStgFrameWidth)//右端での挙動
					{
					ObjRender_SetX(obj,ObjRender_GetX(obj)-384);
					ReflectCount=1;
					}
				}
				else
				{
				ObjShot_SetGraphic(obj, DS_KNIFE_KOUMA_RED);
				}
				if(length(HoleObject)>0)
				{
					ascent(i in 0..length(HoleObject))
					{
						let x=ObjRender_GetX(obj);
						let y=ObjRender_GetY(obj);
						let HoleX=ObjRender_GetX(HoleObject[i]);
						let HoleY=ObjRender_GetY(HoleObject[i]);
						let dis=((HoleY-y)^2+(HoleX-x)^2)^0.5;
						let radius=Obj_GetValueD(HoleObject[i],"Radius",0);
						if(dis<=radius)
						{
							let ox=x;
							let oy=y;
							let d=((HoleY-y)^2+(HoleX-x)^2)^0.5;
							while(d<radius)
							{
								x+=cos(angle);
								y+=sin(angle);
								d=((HoleY-y)^2+(HoleX-x)^2)^0.5;
							}
							Laser(ox,oy,x,y,ObjShot_GetImageID(obj));
							ObjRender_SetX(obj,ox);
							ObjRender_SetY(obj,oy);
							wait(1);
							ObjRender_SetX(obj,x);
							ObjRender_SetY(obj,y);
						}
					}
				}
				yield;
			}
			Obj_Delete(obj);
		}

			task Laser(ox,oy,x,y,gra)
			{
			let length=((y-oy)^2+(x-ox)^2)^0.5;
			let width=12;
			let obj = CreateStraightLaserA1(x, y, atan2(y-oy,x-ox)+180, 0, 0, 900, gra, 0);
			ObjLaser_SetLength(obj,length);
			ObjStLaser_SetSource(obj,false);
			let r=0;
			let scale=0;

			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			ObjShot_SetIntersectionEnable(obj,false);
			ObjLaser_SetRenderWidth(obj, width);
			wait(2);

			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(angle)
{
	#include"./../../lib/ZakoCommon/ZakoCommon.txt"
	let dis=((ObjRender_GetY(objEnemy)-GetPlayerY)^2+(ObjRender_GetX(objEnemy)-GetPlayerX)^2)^0.5;
	let speed=dis/60;
	let Bspeed=speed;
	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(120)
	{	
		speed-=Bspeed/120;
		ObjMove_SetX(obj,ObjMove_GetX(obj)+speed*cos(angle));
		ObjMove_SetY(obj,ObjMove_GetY(obj)+speed*sin(angle));
		if(ObjRender_GetY(obj)<0)//上端での挙動
		{
			ObjRender_SetY(obj,ObjRender_GetY(obj)+448);
        	}
		if(ObjRender_GetY(obj)>GetStgFrameHeight)//上端での挙動
		{
			ObjRender_SetY(obj,ObjRender_GetY(obj)-448);
        	}
		if(ObjRender_GetX(obj)<0)//左端での挙動
		{
			ObjRender_SetX(obj,ObjRender_GetX(obj)+384);
        	}
		if(ObjRender_GetX(obj)>GetStgFrameWidth)//右端での挙動
		{
			ObjRender_SetX(obj,ObjRender_GetX(obj)-384);
		}
		yield;
	}
	HoleObject=HoleObject~[obj];
	let anglePlus=0;
	let Radius=0;

	if(ObjMove_GetX(obj)<=72)
	{
		Enemy2(ObjMove_GetX(obj)+384,ObjMove_GetY(obj));
	}
	if(ObjMove_GetX(obj)>=312)
	{
		Enemy2(ObjMove_GetX(obj)-384,ObjMove_GetY(obj));
	}
	if(ObjMove_GetY(obj)<=72)
	{
		Enemy2(ObjMove_GetX(obj),ObjMove_GetY(obj)+448);
	}
	if(ObjMove_GetY(obj)>=376)
	{
		Enemy2(ObjMove_GetX(obj),ObjMove_GetY(obj)-448);
	}
	if(ObjMove_GetX(obj)<=72 && ObjMove_GetY(obj)<=72)
	{
		Enemy2(ObjMove_GetX(obj+384),ObjMove_GetY(obj)+448);
	}
	if(ObjMove_GetX(obj)>=312 && ObjMove_GetY(obj)>=376)
	{
		Enemy2(ObjMove_GetX(obj-384),ObjMove_GetY(obj)-448);
	}

	Eater(ObjMove_GetX(obj),ObjMove_GetY(obj),0,0);
	loop(300)
	{
		yield;
	}
	HoleObject=erase(HoleObject,0);
	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/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			ObjRender_SetPosition(objE,x,y,0);
			Obj_SetValue(obj,"Radius",72*scale/1.2);
			SetCollisionB(obj,ax,ay,72*scale/1.2);
			yield;
		}
		wait(240);
		loop(30)
		{
			scale-=1.2/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			ObjRender_SetPosition(objE,x,y,0);
			Obj_SetValue(obj,"Radius",72*scale/1.2);
			SetCollisionB(obj,ax,ay,72*scale/1.2);
			yield;
		}
		Obj_Delete(objE);
	}



}

	task Enemy2(x,y)
	{

	let obj2 = ObjEnemy_Create(OBJ_ENEMY);

	HoleObject=HoleObject~[obj2];
	ObjEnemy_Regist(obj2);
	//EnemyResist(obj2,x,y);
	ObjMove_SetX(obj2,x);
	ObjMove_SetY(obj2,y);
	Eater(x,y);
	loop(300)
	{
		yield;
	}
	HoleObject=erase(HoleObject,0);
	Obj_Delete(obj2);

	task Eater(x,y)
	{
		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/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			ObjRender_SetPosition(objE,x,y,0);
			Obj_SetValue(obj2,"Radius",72*scale/1.2);
			ObjEnemy_SetIntersectionCircleToPlayer(obj2,ObjMove_GetX(obj2),ObjMove_GetY(obj2),72*scale/1.2);
			yield;
		}
		loop(240)
		{
			ObjEnemy_SetIntersectionCircleToPlayer(obj2,ObjMove_GetX(obj2),ObjMove_GetY(obj2),72*scale/1.2);
			yield;
		}
		loop(30)
		{
			scale-=1.5/30;
			ObjRender_SetScaleXYZ(objE,scale,scale,scale);
			ObjRender_SetPosition(objE,x,y,0);
			Obj_SetValue(obj2,"Radius",72*scale/1.2);
			ObjEnemy_SetIntersectionCircleToPlayer(obj2,ObjMove_GetX(obj2),ObjMove_GetY(obj2),72*scale/1.2);
			yield;
		}
		Obj_Delete(objE);
	}
	}