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

@Initialize
{
	BossInit(384,128);
}

@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(4800);//ライフを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, 352, 80, 10,7);
	SetAction(ACT_MOVE, 60);

	CallSpellCard("「一刀散雷撃」");
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(60){yield;}
	Shoot;
	loop(60){yield;}
	DamageRate=[100,100];

	task Shoot
	{
		let way=8;
		Setway;
		Slash(32,80,0);
		wait(180);
		loop
		{
			Slash(352,144,1);
			wait(180);
			Slash(32,144,1);
			wait(180);
			Slash(352,80,1);
			wait(180);
			Slash(32,80,1);
			wait(180);
		}

		function Slash(ex,ey,delay)
		{
			SetAction(ACT_CHARGE, 60);
			NoticeLaser(GetX,GetY,0,atan2(ey-GetY,ex-GetX),600,5,158,60);
			if(delay)
			{
				StartSlow(TARGET_ALL,30);
			}
			wait(60);
			StartSlow(TARGET_ALL,60);
			SetAction(ACT_SLASH_H, 30);
			ObjMove_SetDestAtFrame(objEnemy,ex,ey,10);
			let gx=GetX;
			let gy=GetY;
			let sw=way;
			let count=0;
			ascent(i in 0..sw+1)
			{
				let angle=rand(60,120);
				Thunder(gx+(ex-gx)*i/sw,gy+(ey-gy)*i/sw,angle);
				count+=20/way;
				while(count>=1)
				{
					wait(1);
					count-=1;
				}
			}
		}

		task Setway
		{
			loop
			{
				if(GetLife<=3600){way=12;}
				if(GetLife<=2400){way=16;}
				if(GetLife<=1200){way=20;}
				yield;
			}
		}

		task Thunder(x,y,angle)
		{
			ascent(i in -1..2)
			{
				Laser(x,y,angle+60*i,8,7,150);
			}
			let o=CreateShotA1(x,y,0,0,DS_SCALE_YELLOW,60);
			wait(60);
			Obj_Delete(o);

			SquareShot(x,y);

			task SquareShot(x,y)
			{
				let theta=rand(0,360);
				ascent(i in 0..11)
				{
					ascent(j in 0..4)
					{
						Shot(x,y,2,i,theta+j*90);
					}
				}

				task Shot(x,y,speed,num,theta)
				{
					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_SCALE_YELLOW);
				//	ObjShot_SetDelay(obj, 30);

				//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

					ShotEffect(obj);
					let count=-30;
					let dx;
					let dy;
					while(!Obj_IsDeleted(obj))
					{
						let tx=x+count*speed;
						let ty=y+count*(speed-2*speed*num/10);
						tx-=x;
						ty-=y;

						dx=tx*cos(theta)-ty*sin(theta)+x;
						dy=tx*sin(theta)+ty*cos(theta)+y;

						ObjRender_SetX(obj,dx);
						ObjRender_SetY(obj,dy);

						ObjMove_SetAngle(obj, atan2(dy-y,dx-x));

						count++;
						yield;
					}
					Obj_Delete(obj);
				}
			}
		}

		task NoticeLaser(x,y,Lspeed,Langle,Slength,width,graphic,delay)
		{
			let obj = CreateStraightLaserA1(x, y, Langle, Lspeed, width, Slength, graphic, 0);
			ObjLaser_SetLength(obj,Slength);
			ObjShot_SetIntersectionEnable(obj,false);
			ObjShot_SetAutoDelete(obj,false);
			wait(delay);
			Obj_Delete(obj);
		}

		task Laser(x,y,Langle,Lspeed,width,Slength)
		{
			let length=0;
			let obj = CreateStraightLaserA1(x, y, Langle, Lspeed, width, 100, DS_SCALE_YELLOW, 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);
			loop(5)
			{
				if(Obj_IsDeleted(obj)){break;}
				scale+=2.625/5;
				ObjRender_SetScaleXYZ(obj,scale,1,1);
				ObjLaser_SetLength(obj,Slength*scale);
				ObjLaser_SetRenderWidth(obj,width);
				yield;
			}
			loop(30)
			{
				if(Obj_IsDeleted(obj)){break;}
				scale-=2.625/30;
				ObjRender_SetScaleXYZ(obj,scale,1,1);
				ObjLaser_SetLength(obj,Slength*scale);
				ObjLaser_SetRenderWidth(obj,width);
				yield;
			}
		//	ObjRender_SetBlendType(obj, BLEND_ALPHA);
			ObjShot_SetIntersectionEnable(obj,true);

			loop(Slength/Lspeed)
			{
				ObjLaser_SetLength(obj,length);
				length+=Lspeed;
				Lspeed+=0.05;
				yield;
			}
			while(!Obj_IsDeleted(obj))
			{
				ObjRender_SetPosition(obj,x+r*cos(Langle),y+r*sin(Langle),0);
				r+=Lspeed;
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task ShotEffect(obj)
	{
		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjShot_SetIntersectionEnable(obj,false);
		loop(15)
		{
			if(Obj_IsDeleted(obj)){break;}
			scale-=3.0/15;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			yield;
		}
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjShot_SetIntersectionEnable(obj,true);
	}
}