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

@Initialize
{
	BossInit(224,0);
}

@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(6550);//ライフを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, cx, 116, 10,3);
	SetAction(ACT_MOVE, 60);
	CallSpellCard("「レイジングミスト」");

	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	let waterline=60;
	water;
	wait(120);
	Fire;
	wait(60);
	DamageRate=[100,100];

	task Fire
	{
		loop
		{
			SetAction(ACT_SHOT_A, 180);
			ascent(i in 0..8)
			{
				Concentrate(GetX,GetY,i*45);
			}
			wait(60);
			let way=12;
			ascent(i in 0..way)
			{
				Flare(i*360/way,1);
			}
			wait(360);
		}

		task Concentrate(x,y,angle)
		{
			let length=30;
			let width=15;
			let obj = CreateCurveLaserA1(x, y, 0,angle,length,width, DS_BALL_S_RED, 0);

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

			ObjShot_SetAutoDelete(obj,false);
			let R=0;
			loop(30)
			{
				ObjRender_SetX(obj,x+R*cos(angle));
				ObjRender_SetY(obj,y+R*sin(angle));
				ObjMove_SetAngle(obj,angle);
				y-=108/60;
				R+=3;
				angle+=6;
				yield;
			}
			loop(30)
			{
				ObjRender_SetX(obj,x+R*cos(angle));
				ObjRender_SetY(obj,y+R*sin(angle));
				ObjMove_SetAngle(obj,angle);
				y-=108/60;
				R-=3;
				angle+=6;
				yield;
			}
			wait(30);
			Obj_Delete(obj);
		}

	}

	task Flare(angle,dir)
	{
		let rc=floor(rand(0,5-0.001));
		let x=GetX;
		let y=GetY-108;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
		ObjMove_SetSpeed(obj, 5);
		ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, DS_RICE_S_RED);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		//Obj_SetValue(obj,"Fire",false);
		//ObjShot_SetIntersectionEnable(obj,false);
		Obj_SetVisible(obj,false);

		let count=0;
		let R=0;
		let Rmax=36;
		let BaseAngleP=0.8;

		loop(120)
		{
			ObjShot_SetIntersectionCircleA1(obj,32);
			ObjMove_SetPosition(obj,x+R*cos(angle),y+R*sin(angle));
		//	if(count%3==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);}
			if((rc+count)%4==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);}
			if(R<Rmax){R+=Rmax/90;}
			angle+=BaseAngleP*dir;
			count++;
			yield;
		}
		let ex=GetPlayerX;
		let ey=480;
		let ShotAngle=atan2(ey-y,ex-x);
		let speed=0;
		while(y<464)
		{
			ObjShot_SetIntersectionCircleA1(obj,32);
			ObjMove_SetPosition(obj,x+R*cos(angle),y+R*sin(angle));
		//	if(count%3==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);}
			if((rc+count)%4==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);}
			if(R<Rmax){R+=Rmax/90;}
			if(speed<10){speed+=0.1;}
			x+=speed*cos(ShotAngle);
			y+=speed*sin(ShotAngle);
			angle+=BaseAngleP*dir;
			count++;
			if(y>=464-waterline/2)
			{
				ascent(i in 0..8)
				{
					MistShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);
				}
			}
			yield;
		}
		loop(30)
		{
			ObjMove_SetPosition(obj,x+R*cos(angle),y+R*sin(angle));
		//	if(count%3==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),rand(0,360));}
			if((rc+count)%4==0){EffectShot(ObjRender_GetX(obj),ObjRender_GetY(obj),angle);}
			R-=Rmax/30;
			angle+=BaseAngleP*dir;
			count++;
			yield;
		}
		Obj_Delete(obj);


		task MistShot(x,y,angle)
		{
			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, DS_FIRE_RED);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			ObjShot_SetAutoDelete(obj,false);
			let yspeed=rand(0,3);
			let xspeed=rand(-2.5,2.5);
			loop
			{
				let x=ObjRender_GetX(obj);
				let y=ObjRender_GetY(obj);
				if(y<-8 || x<-8 || x>416+8){break;}
				ObjRender_SetX(obj,x+xspeed);
				ObjRender_SetY(obj,y+yspeed);
				ObjMove_SetAngle(obj, atan2(yspeed,xspeed));
				yspeed-=0.01;
				yield;
			}
			Obj_Delete(obj);
		}
		task EffectShot(x,y,angle)
		{
			let obj = CreateShotA2(x, y, 0, angle, 0, 0, DS_FIRE_RED, 0);//弾を発射
			let scale=3.5;
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			ObjShot_SetSpellResist(obj,true);
		//	ObjShot_SetIntersectionEnable(obj,false);
			let speed=0.75;
			loop(10)
			{
				if(Obj_IsDeleted(obj)){break;}
				scale-=3.5/60;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				ObjShot_SetIntersectionCircleA1(obj,10*32/35*scale);
				speed-=0.75/60;
				ObjMove_SetSpeed(obj,speed);
				yield;
			}
			ObjShot_SetIntersectionEnable(obj,false);
			loop(2)
			{
				if(Obj_IsDeleted(obj)){break;}
				scale-=2.5/60;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				speed-=0.75/60;
				ObjMove_SetSpeed(obj,speed);
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task water
	{
		w1;
		w2;
		loop
		{
			let wc=1-ObjEnemy_GetInfo(objEnemy,INFO_LIFE)/6000;
			waterline=60+180*wc;
			if(waterline>180){waterline=180;}
			yield;
		}

		task w1
		{
			loop
			{
				wave(1);
				wait(floor(rand(100,300)));
			}
		}
		task w2
		{
			loop
			{
				wave(-1);
				wait(floor(rand(100,300)));
			}
		}

		task wave(dir)
		{
			let x=224-224*dir;
			loop
			{
				wlaser(x);
				x+=10*dir;
				wait(10);
			}

			task wlaser(dx)
			{
				let length=0;
				let obj=ObjShot_Create(OBJ_STRAIGHT_LASER);
				ObjShot_Regist(obj);
				ObjShot_SetGraphic(obj,DS_SCALE_SKY);
				ObjLaser_SetLength(obj,length);
				ObjLaser_SetRenderWidth(obj,12);
				ObjStLaser_SetAngle(obj,90);
				ObjRender_SetPosition(obj,dx,480-length,0);
			//	ObjShot_SetIntersectionEnable(obj,false);
			//	ObjStLaser_SetSource(obj,false);
				let langle=0;
				loop(360)
				{
					ObjLaser_SetLength(obj,length);
					ObjRender_SetPosition(obj,dx,464-length,0);
					length=waterline*sin(langle);
					langle+=0.5;
					yield;
				}
				Obj_Delete(obj);
			}
		}
	}
}