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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(384,96);
	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(3000);//ライフを1500に設定
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(100);//時間制限を60秒に設定
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);//スペルカードボーナスを30000に設定
	}
}

//----------------------------------------------------
//敵動作
//----------------------------------------------------

let CosmoRadius=224;
task BossWork
{
	//座標(cx, 60)へ60フレームかけて移動する
	let cx = GetStgFrameWidth() / 2;//STGシーンの中心x座標を取得
	ObjMove_SetDestAtWeight(objEnemy, cx, 224, 10,4);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「星天破砕弓」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(75){yield;}
	SetAction(ACT_SHOT_B, 90);
	ascent(i in 0..12)
	{
		Enemy(i,1);
		Enemy(i,-1);
	}
	wait(180);
	C;
	DamageRate=[100,100];

	task C
	{
		loop
		{
			Con;
			wait(30);
			Allow;
			wait(60);

			task Allow
			{
			let num=1;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=2250){num=2;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=1500){num=3;}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=750){num=4;}
			loop(num){
			H2(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),GetAngleToPlayer(objEnemy));
			wait(10);
			}
			}
		}
		task H2(hx,hy,angle)
		{
			let t=0;
			let gra=88;
			let LaserSpeed=8;
			let angleAdd=0;
			let time=CosmoRadius/LaserSpeed;
			loop(time)
			{
				hx+=LaserSpeed*cos(angle);
				hy+=LaserSpeed*sin(angle);
				BladeShot(hx,hy,angle+angleAdd,gra+t%7);
				yield;
			}
			loop(30)
			{
				let obj=CreateShotA1(hx,hy,rand(0.5,2.5),rand(0,360),16,0);
				ShotEffect(obj);
			}
			loop(time)
			{
				hx+=LaserSpeed*cos(angle);
				hy+=LaserSpeed*sin(angle);
				BladeShot(hx,hy,angle+angleAdd,gra+t%7);
				yield;
			}


		}


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

		//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);

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

			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(8)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/12;
					ObjRender_SetScaleXYZ(obj,scale,scale,scale);
					ObjShot_SetIntersectionCircleA1(obj,scale*6);
					yield;
				}
				ObjShot_SetIntersectionEnable(obj,false);
				loop(4)
				{
					if(Obj_IsDeleted(obj)){break;}
					scale-=2.5/12;
					ObjRender_SetScaleXYZ(obj,scale,scale,scale);
					yield;
				}
				Obj_Delete(obj);
			}
	}

	task Con
	{
	loop(15)
	{
		let rangle=rand(0,360);
		PrismLaserShot(0,0);
		loop(1){yield;}
	}
	let R=0;
/*
	loop(30)
	{
		R+=4;
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
	loop(60)
	{
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
	loop(30)
	{
		R-=4;
		let rangle=rand(0,360);
		PrismLaserShot(1,R);
		loop(2){yield;}
	}
*/
	task PrismLaserShot(type,R)
	{
		let ra=rand(0,360);
		let x=ObjRender_GetX(objEnemy)+R*cos(ra)*type;
		let y=ObjRender_GetY(objEnemy)+R/2*sin(ra)*type;
		let speed=rand(5,10);
		let angle=rand(0,360);
		let angleplus=rand(-10,10);
		if(type==0)
		{
		x+=80*cos(angle+180);
		y+=80*sin(angle+180);
		}
		loop(12)
		{
			x+=speed*cos(angle);
			y+=speed*sin(angle);
			Lightning(x,y,rand(-1.5,1.5),angle+180,rand(10,15),rand(6,10), [150,150,0]);
			angle+=angleplus*type;
			yield;
		}

		task Lightning(x,y,speed,angle,leng,width,color)
		{
		//	x+=cos(angle)*leng*0.5;
		//	y+=sin(angle)*leng*0.5;

			let imgBer = GetCurrentScriptDirectory() ~ "../../img/effect/thunder.png";
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, imgBer);
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		//	Obj_SetRenderPriorityI(obj,90);
			ObjPrim_SetVertexPosition(obj, 0, -width/2, -leng/2, -0);
			ObjPrim_SetVertexPosition(obj, 1, width/2, -leng/2, -0);
			ObjPrim_SetVertexPosition(obj, 2, width/2, leng/2, 0);
			ObjPrim_SetVertexPosition(obj, 3, -width/2, leng/2, 0);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 48, 0);
			ObjPrim_SetVertexUVT(obj, 2, 48, 48);
			ObjPrim_SetVertexUVT(obj, 3, 0, 48);

			ObjRender_SetPosition(obj,x,y,0);

			let Alpha=0;
			let frame=0;
			let graphic=0;

			loop(5)
			{
			LightSet;
			Alpha+=1/5;
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,255*Alpha);
			yield;
			}

			loop(30)
			{
			LightSet;
			Alpha-=1/30;
			ObjRender_SetColor(obj,255*Alpha,255*Alpha,255*Alpha);
			yield;
			}
			Obj_Delete(obj);
	
			sub LightSet
			{
				if(frame%3==0)
				{
				graphic=floor(rand(0,256));
				ObjRender_SetAngleZ(obj,angle-90*(floor(rand(0,2))*2-1));

				ObjPrim_SetVertexUVT(obj, 0, 0, 0+graphic);
				ObjPrim_SetVertexUVT(obj, 1, 48, 0+graphic);
				ObjPrim_SetVertexUVT(obj, 2, 48, 48+graphic);
				ObjPrim_SetVertexUVT(obj, 3, 0, 48+graphic);
				ObjRender_SetScaleXYZ(obj,(floor(rand(0,2))*2-1)/2*Alpha,(floor(rand(0,2))*2-1)*Alpha,1);
				}
				frame++;
				ObjRender_SetPosition(obj,x,y,0);
				x+=speed*cos(angle);
				y+=speed*sin(angle);
			}
		}

	}
	}

	task IceEffect(x, y, angle, leng, wid, time)
	{
		let obj1 = CreateStraightLaserA1(x, y, angle, 0, 2*wid, 30, DS_ICE_WHITE, 0);
		ObjShot_SetIntersectionEnable(obj1,false);
		ObjStLaser_SetSource(obj1, false);
		let color = [255,255,255];
		ascent(i in 1 .. time+1)
		{
			ObjLaser_SetLength(obj1,leng+2*leng*i/time);
			ObjLaser_SetRenderWidth(obj1, 2*wid*(time-i)/time);
			ObjRender_SetColor(obj1, color[0]*(time-i)/time, color[1]*(time-i)/time, color[2]*(time-i)/time);
			yield;
		}
		Obj_Delete(obj1);
	}

	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 Enemy(num,dir)
{
	#include"./../../lib/ZakoCommon/ZakoCommon.txt"
	let R=CosmoRadius;
	let angle=num*30+7.5*dir;
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	SetLife(obj,10000);
        SetDamageRate(obj,0,0);
//	DrawEnemy(obj,RedFairy,1);
	DrawCircle(obj,0.20,255,255,255);
	EnemyResist(obj,ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy));
	let x=192;
	let y=224;
	ObjRender_SetMovePositionBezier(obj,[x+rand(-200,200)],[y+rand(-200,200)],x+R*cos(angle),y+R*sin(angle),120);
//	ObjMove_SetDestAtFrame(obj,ObjRender_GetX(obj)+R*cos(angle),ObjRender_GetY(obj)+R*sin(angle),30);

	loop(120)
	{		
		yield;
	}
//	SetCollisionA(obj,0,0,24);
//	SetCollisionB(obj,0,0,8);
	let anglePlus=0;
	S;
	while(!Obj_IsDeleted(obj))
	{
		ObjMove_SetPosition(obj,192+R*cos(angle),224+R*sin(angle));
		angle+=anglePlus*dir;
		if(anglePlus<3)
		{
		anglePlus+=0.01;
		}
		yield;
	}

	task S
	{
	while(!Obj_IsDeleted(obj))
	{
		let obj2=CreateShotA1(ObjRender_GetX(obj),ObjRender_GetY(obj),3,angle,DS_STAR_S_WHITE,0);
		ShotEffect(obj2);
		wait(6);
	}
	}

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