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

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

//----------------------------------------------------
//敵動作
//----------------------------------------------------
task BossWork
{
	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 80, 10,5);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「トゥルゴヴィシュテの紅き夜」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(60){yield;}
	let dir=floor(rand(0,2-0.001))*2-1;

	A(dir);
	wait(90);
	DamageRate=[100,100];
	loop
	{
		C;
		wait(150);
		Move;
		wait(150);
		B;
		wait(150);
		Move;
		wait(180);
		D;
		wait(150);
		Move;
		wait(180);
	}

	task A(dir)
	{
		let waittime=5;
		loop
		{
			let kind=floor(rand(0,4-0.001));
			alternative(kind)
			case(0)
			{
			Laser(rand(0,384+32),-64,rand(80,100),2,15,90);
			}
			case(1)
			{
			Laser(-64,rand(0,448+32),rand(-10,10),2,15,90);
			}
			case(2)
			{
			Laser(rand(0,384+32),448+64,rand(80,100),2,15,90);
			}
			case(3)
			{
			Laser(384+64,rand(0,448+32),rand(170,190),2,15,90);
			}
			wait(waittime);
			let life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			if(life<4400){waittime=4;}
			if(life<2200){waittime=3;}
		}

		task Laser(x,y,Langle,Lspeed,width,Slength)
		{
			Lspeed=24;
			Slength=600;
			let length=0;
			let obj = CreateStraightLaserA1(x, y, Langle, Lspeed, width, 900, DS_SCALE_RED, 0);
			ObjLaser_SetLength(obj,Slength);
			ObjStLaser_SetSource(obj,false);
			let r=0;
			let scale=0;

		//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			ObjShot_SetIntersectionEnable(obj,false);

			if(!Obj_IsDeleted(obj)){//ObjRender_SetBlendType(obj, BLEND_ALPHA);
			}
			ObjShot_SetIntersectionEnable(obj,true);
			loop(Slength/Lspeed)
			{
				let lx=x+length*cos(Langle);
				let ly=y+length*sin(Langle);
				let dis=((ly-GetPlayerY)^2+(lx-GetPlayerX)^2)^0.5;
				if(dis<=80)
				{
					let objs=CreateShotA1(lx,ly,0.5,atan2(ly-GetPlayerY,lx-GetPlayerX)+180,9,0);
					ShotEffect(objs);
					break;
				}
				ObjLaser_SetLength(obj,length);
				length+=Lspeed;
				Lspeed+=0.05;
				yield;
			}
			while(length>0)
			{
				let lx=x+length*cos(Langle);
				let ly=y+length*cos(Langle);
				let dis=((ly-GetPlayerY)^2+(lx-GetPlayerX)^2)^0.5;
				ObjLaser_SetLength(obj,length);
				length-=Lspeed;
				Lspeed-=0.05;
				yield;
			}

			Obj_Delete(obj);
		}
	}

	task Move
	{
		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);
		wait(90);
	}

	task B
	{
		let angle=rand(0,360);
		let way=8;
		let life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		if(life<3300){way=12;}
		loop(3)
		{
		ascent(i in 0..way)
		{
			Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),1.5,angle+i*360/way);
		}
		wait(60);
		angle+=11.25;
		}

		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_L_RED);
	//	ObjShot_SetDelay(obj, delay);


		ShotEffect(obj);
		let ReflectCount=0;
		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			if(ReflectCount==0)
			{
			if(ObjRender_GetY(obj)<0)//上端での挙動
			{
			ObjRender_SetY(obj,ObjRender_GetY(obj));
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
			ReflectCount=1;
        		}
			if(ObjRender_GetY(obj)>GetStgFrameHeight)//上端での挙動
			{
			ObjRender_SetY(obj,GetStgFrameHeight*2-ObjRender_GetY(obj));
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
			ReflectCount=1;
        		}
			if(ObjRender_GetX(obj)<0)//左端での挙動
			{
			ObjRender_SetX(obj,ObjRender_GetX(obj));
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
			ReflectCount=1;
        		}
			if(ObjRender_GetX(obj)>GetStgFrameWidth)//右端での挙動
			{
			ObjRender_SetX(obj,GetStgFrameWidth*2-ObjRender_GetX(obj));
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
			ReflectCount=1;
			}
			}
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task C
	{
		let speed=1.8;
		let angle=GetAngleToPlayer(objEnemy);
		let way=36;
		let life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		if(life<3300){way=48;}
		loop(30)
		{
		ascent(i in 0..way)
		{
			Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),speed,angle+i*360/way);
		}
		wait(5);
		speed+=0.2;
		}

		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_RED);
	//	ObjShot_SetDelay(obj, delay);


		ShotEffect(obj);

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

	task D
	{
		let angle=GetAngleToPlayer(objEnemy);
		let dir=1;
		let way=30;
		let life=ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		if(life<3300){way=45;}
		loop(12)
		{
		angle=rand(0,360);
		ascent(i in 0..way)
		{
			Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),3.0,angle+i*360/way,dir);
		}
		dir*=-1;
		wait(12);
		}

		task Shot(x,y,speed,angle,dir)
		{
			let Bangle=angle;
			let obj = ObjShot_Create(OBJ_SHOT);
			ObjShot_Regist(obj);
			ObjMove_SetPosition(obj, x, y);
			ObjMove_SetSpeed(obj, speed*0);
			ObjMove_SetAngle(obj, angle);
			ObjShot_SetGraphic(obj, DS_RICE_S_RED);
			//ObjShot_SetDelay(obj, delay);

		//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

			ShotEffect(obj);
			let ay=0;
			let ZAngle=rand(0,360);
			loop(60)
			{
				ObjMove_SetX(obj,ObjRender_GetX(obj)+speed*cos(Bangle));
				ObjMove_SetY(obj,ObjRender_GetY(obj)+speed*sin(Bangle));
				ObjMove_SetAngle(obj, Bangle);
			//	ZAngle+=6;
				Bangle+=1.5*dir;
				speed-=1.5/60;
				yield;
			}
			while(!Obj_IsDeleted(obj))
			{
				ObjMove_SetX(obj,ObjRender_GetX(obj)+speed*cos(Bangle));
				ObjMove_SetY(obj,ObjRender_GetY(obj)+speed*sin(Bangle));
				ObjMove_SetAngle(obj, Bangle);
			//	ZAngle+=6;
				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);
	}
}