
#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"
let AddDamageRate=1;
@Initialize
{
	BossInit(0,0);
}

@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;
}

let EnemyMaxLife=3500;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2800);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 144, 10,7);
	SetAction(ACT_MOVE, 60);
	CallSpellCard("「インフェルナルグリモア」");

	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(60){yield;}
	OpenGate;
	loop(75){yield;}
	Shoot;

	task OpenGate
	{
		ObjRender_SetAlpha(objEnemy,150);
		ObjEnemy_SetDamageRate(objEnemy,0,0);
		Circle;
		SetAction(ACT_SHOT_B, 90);
		task Circle
		{
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../../img/zako/Circle.png");
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			Obj_SetRenderPriorityI(obj,30);
			ObjRender_SetAngleZ(obj,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, 256, 0);
			ObjPrim_SetVertexUVT(obj, 2, 256, 256);
			ObjPrim_SetVertexUVT(obj, 3, 0, 256);

			let Alpha=1;
			let scale=0;

			let Xangle=0;
			let Yangle=0;
			let Zangle=rand(0,360);
			let R=255;
			let G=0;
			let B=255;
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			loop(1920+75)
			{
				ObjRender_SetPosition(obj,GetX,GetY,0);
				ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				CircleBlur(Zangle,scale,R,G,B);
				if(scale<1){scale+=1/60;}
				Zangle+=3;
				yield;
			}
			AddDamageRate=1.5;
			loop
			{
				ObjRender_SetPosition(obj,GetX,GetY,0);
				ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);
				Zangle+=3;
				yield;
			}
			Obj_Delete(obj);
		}
		task CircleBlur(Zangle,scale,R,G,B)
		{
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../../img/zako/Circle.png");
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			Obj_SetRenderPriorityI(obj,30);
			ObjRender_SetAngleZ(obj,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, 256, 0);
			ObjPrim_SetVertexUVT(obj, 2, 256, 256);
			ObjPrim_SetVertexUVT(obj, 3, 0, 256);

			let Bscale=scale;
			let Alpha=0.2;
			ObjRender_SetAngleXYZ(obj,0,0,Zangle);

			loop(15)
			{
				ObjRender_SetPosition(obj,ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),0);
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
				scale+=Bscale/15;
				Alpha-=0.2/15;
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task Shoot
	{
		loop
		{
			ascent(i in -1..2)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy)+i*10);
			}
			wait(120);
			ascent(i in -1..2)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy)+i*10);
			}
			wait(180);
			ascent(i in -2..3)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy)+i*30);
			}
			wait(240);
			ascent(i in -2..3)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy)+i*30);
			}
			wait(240);
			loop(8)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy),);
				wait(15);
			}
			wait(120);
			loop(8)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy),);
				wait(15);
			}
			wait(240);
			ascent(i in 0..12)
			{
				Bullet(GetX,GetY,GetAngleToPlayer(objEnemy)+i*30);
			}
			wait(540);
		}
		task Bullet(x,y,angle)
		{
			let time=0;
			let obj = ObjShot_Create(OBJ_SHOT);
			ObjShot_Regist(obj);
			ObjMove_SetPosition(obj, x, y);
			ObjMove_SetSpeed(obj, 3);
			ObjShot_SetGraphic(obj, DS_BALL_M_PURPLE);
			//ObjShot_SetDelay(obj, delay);
			//ObjShot_SetAutoDelete(obj,false);

			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

			ObjMove_SetAngle(obj, angle);

			let Rcount=0;
			while(!Obj_IsDeleted(obj))
			{
				if(ObjRender_GetX(obj)>=388 && Rcount<2)
				{
					ObjRender_SetX(obj,388+(388-ObjRender_GetX(obj)));
					ObjMove_SetAngle(obj, 180-ObjMove_GetAngle(obj));
					Rcount++;
				}
				if(ObjRender_GetX(obj)<=0 && Rcount<2)
				{
					ObjRender_SetX(obj,0+(0-ObjRender_GetX(obj)));
					ObjMove_SetAngle(obj, 180-ObjMove_GetAngle(obj));
					Rcount++;
				}
				if(ObjRender_GetY(obj)<=0 && Rcount<2)
				{
					ObjRender_SetY(obj,0+(0-ObjRender_GetY(obj)));
					ObjMove_SetAngle(obj, 360-ObjMove_GetAngle(obj));
					Rcount++;
				}
				if(ObjRender_GetY(obj)>=448)
				{
					Enemy(ObjRender_GetX(obj),ObjRender_GetY(obj));
					break;
				}
				yield;
			}
			Obj_Delete(obj);
		}
	}
}

task Enemy(x,y)
{
	#include"./../../lib/ZakoCommon/ZakoCommon.txt"
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	SetLife(obj,250);
	SetDamageRate(obj,100,100);

	DrawCircle(obj,0.20,255,255,255);
	EnemyResist(obj,x,y);
	SetCollisionA(obj,0,0,32);
	SetCollisionB(obj,0,0,16);

	let ey=rand(16,160);
	ObjMove_SetDestAtWeight(obj, x, ey, 10,7);

	loop(120)
	{		
		yield;
	}

	let type=floor(rand(0,3-0.001));

	alternative(type)
	case(0){S0;}
	case(1){S1;}
	case(2){S2;}
	Erase;

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

	if(ObjEnemy_GetInfo(obj,INFO_LIFE)<=0)
	{
		ObjEnemy_AddLife(objEnemy,-100*AddDamageRate);
	}

	task Erase
	{
		loop(600)
		{
			yield;
		}
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)>0)
		{
			ObjEnemy_AddLife(objEnemy,100*AddDamageRate);
		}
		Obj_Delete(obj);
	}

	task S0
	{
		while(!Obj_IsDeleted(obj))
		{
			let angle=GetAngleToPlayer(obj);
			loop(3)
			{
				if(Obj_IsDeleted(obj)){break;}
				if(GetObjectDistance(obj,GetPlayerObjectID)>30)
				{
					let obj2=CreateShotA1(ObjRender_GetX(obj),ObjRender_GetY(obj),3,angle,DS_KUNAI_PURPLE,0);
				}
				wait(3);
			}
			wait(60);
		}
	}

	task S1
	{
		while(!Obj_IsDeleted(obj))
		{
			let angle=rand(0,360);
			if(GetObjectDistance(obj,GetPlayerObjectID)>30)
			{
				ascent(i in 0..20)
				{
					let obj2=CreateShotA1(ObjRender_GetX(obj),ObjRender_GetY(obj),1.0,angle+i*18,DS_BALL_BS_BLUE,0);
				}
			}
			wait(90);
		}
	}

	task S2
	{
		while(!Obj_IsDeleted(obj))
		{
			let angle=rand(0,360);
			if(Obj_IsDeleted(obj)){break;}
			if(GetObjectDistance(obj,GetPlayerObjectID)>30)
			{
				let obj2=CreateShotA1(ObjRender_GetX(obj),ObjRender_GetY(obj),2,angle,DS_KUNAI_BLUE,0);
			}
			wait(3);
		}
	}

	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);
	}
}