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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(160,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(5600);//ライフを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, 144, 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;}
	Aurora;
	wait(120);
	S;
	DamageRate=[100,100];

	task Aurora
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let dir=floor(rand(0,2-0.001))*2-1;
		loop
		{
		let angle=rand(-10,10);
		ascent(i in 0..2)
		{
			AuroraShot(ex,ey,0.5,i*180+angle,60,1.0*dir,0,floor(rand(9,16-0.001)),0);
		}
		dir*=-1;
		wait(150);
		}

		task AuroraShot(x,y,speed,angle,amp,om,arg,graphic,delay)
		{
/*
    		amp*=2;
  		speed*=2;
		arg*=2;
		om*=2;
*/
   		let t = 0;
  		let px = speed*t;
   		let py = amp*sin(om*t + arg);
    		let a = amp*om*3.1415926/(180*speed);
    
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj,x+px*cos(angle)-py*sin(angle),y+px*sin(angle)+py*cos(angle));

   		ObjMove_SetSpeed(obj, 0);
   		ObjMove_SetAngle(obj, angle + atan(a*cos(om*t + arg)));
  		ObjShot_SetGraphic(obj, graphic);
		Obj_SetVisible(obj,false);
		L;
		while(!Obj_IsDeleted(obj))
		{
       			px=speed*t;
       			py=amp*sin(om*t + arg);
        		ObjMove_SetPosition(obj,x+px*cos(angle)-py*sin(angle),y+px*sin(angle)+py*cos(angle));
       			ObjMove_SetAngle(obj, angle + atan(a*cos(om*t + arg)));
      			t++;

       			px=speed*t;
       			py=amp*sin(om*t + arg);
        		ObjMove_SetPosition(obj,x+px*cos(angle)-py*sin(angle),y+px*sin(angle)+py*cos(angle));
       			ObjMove_SetAngle(obj, angle + atan(a*cos(om*t + arg)));
      			t++;

       			yield;
    		}
		task L
		{
			while(!Obj_IsDeleted(obj))
			{
				Laser(ObjRender_GetX(obj),ObjRender_GetY(obj),ObjMove_GetAngle(obj)+90,1.5,8,96,72+graphic);
				wait(2);
				Laser(ObjRender_GetX(obj),ObjRender_GetY(obj),ObjMove_GetAngle(obj)-90,1.5,8,96,72+graphic);
				wait(2);
			}
		}

		}    


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

			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

		//	ObjRender_SetBlendType(obj, BLEND_ALPHA);
			ObjShot_SetIntersectionEnable(obj,true);
			ObjLaser_SetInvalidLength(obj,Slength*0.15,Slength*0.15);
			loop(Slength/Lspeed)
			{
				ObjLaser_SetLength(obj,length);
				length+=Lspeed;
				yield;
			}
			ObjStLaser_SetSource(obj,false);
			while(!Obj_IsDeleted(obj))
			{
				ObjRender_SetPosition(obj,x+r*cos(Langle),y+r*sin(Langle),0);
				r+=Lspeed;
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task S
	{
	let way=2;
	loop
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let angle=GetAngleToPlayer(objEnemy);

		ascent(j in -way..way+1)
		{
		ascent(i in 0..10)
		{
		Shot(ex,ey,1.0,angle+i*36+j*1.8);
		}
		}

		wait(120);
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=4200){way=3;}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=2800){way=4;}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1400){way=5;}
	}

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

		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

		ShotEffect(obj);
		while(!Obj_IsDeleted(obj))
		{
			ObjMove_SetAngle(obj, angle);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

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