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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(384,128);
	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(4500);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	let hallucinationFlag=0;
	let hallucinationCount=0;

	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 112, 10,5);
	SetAction(ACT_MOVE, 75);
//	loop(60){yield;}
	CallSpellCard("「テンダーハルシネイション」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(75){yield;}
	C;
	H;
	loop(90){yield;}
	DamageRate=[100,100];

	task H
	{
	loop
	{
		if(hallucinationCount>0)
		{
			hallucinationFlag=1;
			hallucinationCount-=1;
		}
		else
		{
		hallucinationFlag=0
		}
		yield;
	}
	}

	task C
	{
		SetAction(ACT_SHOT_B, 240);
		S1;
		wait(60);
		P1;
		wait(240);
		SetAction(ACT_SHOT_B, 900);
		S2;
		wait(60);
		P2;

		wait(900);
		S3;
		wait(60);
		P3;

		task S3
		{
			S3_1;
			S3_2;
			task S3_1
			{
			let angle=rand(0,360);
			loop
			{
				let x=ObjMove_GetX(objEnemy);
				let y=ObjMove_GetY(objEnemy);
				loop(3)
				{
				ascent(i in 0..7)			
				{
				Shot(x,y,2.5,angle+i*360/7,15);
				}
				wait(1);
				angle+=1.6;
				}
				angle+=4.8;
				wait(3);
			}
			}

			task S3_2
			{
			let angle=rand(0,360);
			loop
			{
				let x=ObjMove_GetX(objEnemy);
				let y=ObjMove_GetY(objEnemy);
				loop(12)
				{
				ascent(i in 0..5)			
				{
				Shot(x,y,3.5,angle+i*72,15);
				}
				angle-=1;
				wait(2);
				}
				angle-=12;
				wait(24);
			}
			}
		}

		task P3
		{
			loop
			{
				let angle=rand(0,180)*0+GetAngleToPlayer(objEnemy);
				Poison(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),rand(1.5,3.5)*0+3,angle);
				wait(15);
			}
		}


		task P2
		{
			loop(12)
			{
			ascent(i in -3..4)
			{
				Poison(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),1.75,GetAngleToPlayer(objEnemy)+i*6.0);
			}
			wait(60);
			}
		}

		task S2
		{
			let dir=floor(rand(0,2-0.001))*2-1;
			let angle=rand(0,360);
			loop(50)
			{
				let x=ObjMove_GetX(objEnemy);
				let y=ObjMove_GetY(objEnemy);
				ascent(i in 0..18)			
				{
				Shot2(x,y,2.5,angle+i*20,15);
				}
			wait(15);
			angle+=3*dir;
			}
		}

		task P1
		{
			loop(5)
			{
			ascent(i in -2..3)
			{
				Poison(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),2,GetAngleToPlayer(objEnemy)+i*20);
			}
			wait(30);
			}
		}

		task S1
		{
			loop(16)
			{
				let x=ObjMove_GetX(objEnemy)+rand(-80,80);
				let y=ObjMove_GetY(objEnemy)+rand(-30,30);
				loop(30)			
				{
				Shot(x,y,rand(1,3),rand(0,360),41);
				}
			wait(15);
			}
		}
/*
	wait(120);
	let dir=floor(rand(0,2-0.001))*2-1;
	loop
	{
		T_S(dir);
		wait(120);
		wait(90);
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let mx= ex+rand(-120,120);
		let my= ey+rand(-20,20);
		while(mx<64 || mx>320){mx=ex+rand(-120,120);}
		while(my<80 || my>160){my=ey+rand(-20,20);}
		ObjMove_SetDestAtWeight(objEnemy,mx, my, 30,2.0);
		SetAction(ACT_MOVE, 75);
		dir*=-1;
	}
*/
/*
	task T_S(dir)
	{
		let PAngle=rand(0,360);
		let angle=rand(0,360);
		loop(20)
		{
		ascent(i in 0..20)
		{
		Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),2,angle+18*i);
		}
		wait(6);
		PAngle+=23*dir;
		}
	}
*/
	task Shot(x,y,speed,angle,gra)
	{
		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, gra);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

		let obj2 = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj2);
		ObjMove_SetPosition(obj2, x, y);
		ObjMove_SetSpeed(obj2, speed);
		ObjMove_SetAngle(obj2, angle);
		ObjShot_SetGraphic(obj2, gra);
		ObjShot_SetIntersectionEnable(obj2,false);

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

		ObjRender_SetScaleXYZ(obj2,scale,scale,scale);
		ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
		ObjRender_SetColor(obj2,20,20,20);

		while(!Obj_IsDeleted(obj))
		{
			if(hallucinationFlag)
			{
				scale=4;
			}
			else
			{
				scale=1;
			}
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			let Rect=GetShotDataInfoA1(gra,TARGET_ENEMY,INFO_RECT);
			let R=Rect[2]-Rect[0];
			R=floor(R/3);
			SetShotIntersectionCircle(ObjRender_GetX(obj),ObjRender_GetY(obj),R*scale);

			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj2);
	}

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

		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=0;
		let yspeed=0;

		let obj2 = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj2);
		ObjMove_SetPosition(obj2, x, y);
		ObjMove_SetSpeed(obj2, speed);
		ObjMove_SetAngle(obj2, angle);
		ObjShot_SetGraphic(obj2, gra);
		ObjShot_SetIntersectionEnable(obj2,false);

		ObjRender_SetScaleXYZ(obj2,scale,scale,scale);
		ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
		ObjRender_SetColor(obj2,20,20,20);


		while(!Obj_IsDeleted(obj))
		{
			if(hallucinationFlag)
			{
				scale=4;
			}
			else
			{
				scale=1;
			}
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			let Rect=GetShotDataInfoA1(gra,TARGET_ENEMY,INFO_RECT);
			let R=Rect[2]-Rect[0];
			R=floor(R/3);
			SetShotIntersectionCircle(ObjRender_GetX(obj),ObjRender_GetY(obj),R*scale);
			ObjRender_SetY(obj,ObjRender_GetY(obj)+yspeed);
			ObjRender_SetY(obj2,ObjRender_GetY(obj2)+yspeed);
			yspeed+=0.02;
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj2);
	}
	}

	task Poison(x,y,speed,angle)
	{
		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_ADD_RGB);
	//	Obj_SetRenderPriorityI(obj,70);
		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, 0, 128);
		ObjPrim_SetVertexUVT(obj, 2, 128, 0);
		ObjPrim_SetVertexUVT(obj, 3, 128, 128);
		let scale=0.4;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		let Alpha=1;
		ObjRender_SetColor(obj,255*Alpha,50*Alpha,255*Alpha);

		ObjRender_SetPosition(obj,x,y,0);

		let s_count=0;
		loop(300)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			let dis=((GetPlayerY-ObjRender_GetY(obj))^2+(GetPlayerX-ObjRender_GetX(obj))^2)^0.5;
			if(dis<=24)
			{
				hallucinationCount+=1;
			}
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task A
	{
		let angle=-90;
		ascent(j in 0..7)
		{
		ascent(i in 0..36-2*j)
		{
		Shot(1.5+j*0.3*0+1.5,i*360/(36-2*j),247-j,j%2*2-1);
		}

		wait(4);
		}

		task Shot(speed,angle,gra,dir)
		{
		let R=0;
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);

		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, 0);
		ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, gra);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

		let sangle=atan2(ObjRender_GetY(objEnemy)-y,ObjRender_GetX(objEnemy)-x)+180;
		ObjMove_SetAngle(obj, sangle+45*((gra%2)*2-1));
		while(!Obj_IsDeleted(obj))
		{
			ObjMove_SetPosition(obj, x+R*cos(angle), y+R*sin(angle));
			R+=speed;
			angle+=0.5*dir;
			yield;
		}
		
	}

}