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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(256,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;
}

let EnemyMaxLife=9000;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(EnemyMaxLife);//ライフを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, 96, 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;}
	SetAction(ACT_SHOT_B, 60);
	C;
	loop(120){yield;}
	DamageRate=[100,100];
	task C
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let PA=90;
		let A=0;
		ascent(i in 0..7)
		{
		Shot(ex,ey,A+360/7*i,1,i);
		}

		wait(660);
		loop(3)
		{
			HomingShot(ex,ey,0);
			wait(60);
			HomingShot(ex,ey,180);
			wait(60);
		}
			HomingShot(ex,ey,0);
			wait(60);

		wait(920);
		loop
		{
			S4(ex,ey,1,GetAngleToPlayer(objEnemy),0);
			wait(45);
		}

		task Shot(cx,cy,Angle,dir,t)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, cx, cy);
	//	ObjMove_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, 16);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);

		let x0; let y0; let z0; let x; let y; let z;

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

		//ObjMove_SetAngle(obj, angle);
		T_Shot;
		//T_Laser;

		let a=Angle;
		let b=0;
		let Az=24;
		let El=0;

		loop(96)
		{
			a=Angle;
			x0=Radius*sin(a)*cos(b);
			y0=Radius*sin(a)*sin(b);
			z0=Radius*cos(a);
			x=x0;
			y=y0*cos(Az)-z0*sin(Az);
			z=y0*sin(Az)+z0*cos(Az);
			let tx=x;
			x=x*cos(El)+z*sin(El);
			z=-tx*sin(El)+z*cos(El);

			ObjMove_SetPosition(obj,cx+x,cy+y);
			BlurEffect(cx+x,cy+y);

			if(Radius<192){
			Radius+=2;}
			Angle+=3*dir;
		//	Az+=0.8;
		//	El+=1.1;
			yield;
		}

		while(!Obj_IsDeleted(obj))
		{
			a=Angle;
			x0=Radius*sin(a)*cos(b);
			y0=Radius*sin(a)*sin(b);
			z0=Radius*cos(a);
			x=x0;
			y=y0*cos(Az)-z0*sin(Az);
			z=y0*sin(Az)+z0*cos(Az);
			let tx=x;
			x=x*cos(El)+z*sin(El);
			z=-tx*sin(El)+z*cos(El);

			ObjMove_SetPosition(obj,cx+x,cy+y);
			BlurEffect(cx+x,cy+y);

			if(Radius<192){
			Radius+=2;}
			Angle+=3*dir;
			Az+=0.9;
			El+=0.7;
			yield;
		}
		Obj_Delete(obj);

		task T_Shot
		{
			wait(96);
			let speed=1.5;
			let angle=90;
			let angletheta=0;

			loop(120)
			{
				S(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,angle+180,1);		
				S(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,angle,1);
				angle-=7;
				wait(3);
			}
			speed=2.5;
			wait(150+t*3);

			loop(21)
			{
				let SA=(360+GetAngleToPlayer(objEnemy)*0)/4;
				loop(3)
				{
					ascent(i in 0..1)
					{
					S(ObjRender_GetX(obj),ObjRender_GetY(obj),speed,SA+i*45,3);
					}
					wait(1);
				}
				wait(18);
			}

			wait(120);
			loop(30)
			{
				let SX=ObjRender_GetX(obj);
				let SY=ObjRender_GetY(obj);
				let SA=GetAngleToPlayer(objEnemy)+rand(-15,15);
				loop(8)
				{
					ascent(i in 0..1)
					{
					S2(SX,SY,5,SA,6);
					}
					wait(1);
				}
				wait(12);
			}
			wait(90);
			loop
			{
				let aaa=atan2(ObjRender_GetY(objEnemy)-ObjRender_GetY(obj),ObjRender_GetX(objEnemy)-ObjRender_GetX(obj));
				let dis=((ObjRender_GetY(objEnemy)-ObjRender_GetY(obj))^2+(ObjRender_GetX(objEnemy)-ObjRender_GetX(obj))^2)^0.5;
				let sss=dis/90+1;
				if(dis>80*0)
				{
				S3(ObjRender_GetX(obj),ObjRender_GetY(obj),sss,aaa+angle,0);
				S3(ObjRender_GetX(obj),ObjRender_GetY(obj),sss,aaa-angle,0);
				}
				angle=18;
				wait(3);
			}
		}

		}

		task S(x,y,speed,angle,t)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, speed);
		ObjShot_SetGraphic(obj, 9+t);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		ShotEffect(obj);
		ObjMove_SetAngle(obj, angle);
		loop(180)
		{
			yield;
		}
		ObjShot_SetAutoDelete(obj,true);
		while(!Obj_IsDeleted(obj))
		{
			yield;
		}
		Obj_Delete(obj);
		}

		task S2(x,y,speed,angle,t)
		{
		let time=0;
		let Bspeed=speed;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, speed);
		ObjShot_SetGraphic(obj, 9+t);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);
		ShotEffect(obj);
		ObjMove_SetAngle(obj, angle);

		loop(45)
		{
			speed-=Bspeed/45;
			ObjMove_SetSpeed(obj, speed);
			yield;
		}

		while(!Obj_IsDeleted(obj))
		{
			speed+=Bspeed/60;
			ObjMove_SetSpeed(obj, speed);
			yield;
		}
		Obj_Delete(obj);
		}

		task S3(x,y,speed,angle,t)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, speed);
		ObjShot_SetGraphic(obj, 9+t);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		ShotEffect(obj);
		ObjMove_SetAngle(obj, angle);
		loop(300)
		{
			yield;
		}
		ObjShot_SetAutoDelete(obj,true);
		while(!Obj_IsDeleted(obj))
		{
			yield;
		}
		Obj_Delete(obj);
		}


		task S4(x,y,speed,angle,t)
		{
		let time=0;
		let Bspeed=speed;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, speed);
		ObjShot_SetGraphic(obj, DS_BALL_L_PURPLE);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);
		ShotEffect(obj);
		ObjMove_SetAngle(obj, angle);

		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			speed+=0.05;
			ObjMove_SetSpeed(obj, speed);
			yield;
		}
		Obj_Delete(obj);
		}

		task HomingShot(x,y,angle)
		{
			let LaserSpeed=3;
			loop(30)
			{
				Homing;
				Homing;
				Homing;

				x+=LaserSpeed*cos(angle);
				y+=LaserSpeed*sin(angle);
				if(x<-64){break;}
				if(x>448){break;}
				LaserSpeed-=2.5/30;
				BladeShot(x,y,angle,83);
				yield;
			}

			loop(75)
			{
				Homing;
				Homing;
				Homing;

				x+=LaserSpeed*cos(angle);
				y+=LaserSpeed*sin(angle);
				if(x<-64){break;}
				if(x>448){break;}
				if(y<-32){break;}
				LaserSpeed+=9/60;
				BladeShot(x,y,angle,83);
				yield;
			}
			loop(75)
			{
				x+=LaserSpeed*cos(angle);
				y+=LaserSpeed*sin(angle);
				if(x<-64){break;}
				if(x>448){break;}
				if(y<-32){break;}
				LaserSpeed+=6/60;
				BladeShot(x,y,angle,83);
				yield;
			}
			function Homing
			{
				let ToPlayerAngle=atan2(GetPlayerY-y,GetPlayerX-x);
				let judgangle=sin(ToPlayerAngle-angle);
				let ShiftAngle=absolute(ToPlayerAngle-angle);
				if(judgangle>0)
				{
					angle+=1.0;
				}
				else
				{
					angle-=1.0;
				}
			}

			task BladeShot(x,y,angle,gra)
			{
				let obj = CreateShotA2(x, y, 0, angle, 0, 0, gra, 0);//弾を発射
				ObjShot_SetSpellResist(obj,true);
				let scale=2.5;
				ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				let speed=0.75;
				loop(10)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/15;
					ObjRender_SetScaleXYZ(obj,scale,scale,scale);
					ObjShot_SetIntersectionCircleA1(obj,scale*3);
					yield;
				}
				ObjShot_SetIntersectionEnable(obj,false);
				loop(5)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/15;
					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 BlurEffect(x,y)
	{
		let obj = CreateShotA2(x, y, 0, 0, 0, 0, 16, 0);//弾を発射
		ObjShot_SetSpellResist(obj,true);
		let scale=2.5;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let speed=0.75;
		loop(10)
		{
			if(Obj_IsDeleted(obj)){break;}
			scale-=2.5/15;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			ObjShot_SetIntersectionCircleA1(obj,scale*3);
			yield;
		}
		ObjShot_SetIntersectionEnable(obj,false);
		loop(5)
		{
			if(Obj_IsDeleted(obj)){break;}
			scale-=2.5/15;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			yield;
		}
		Obj_Delete(obj);
	}

}