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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(224,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(6700);//ライフを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,2.0);
	SetAction(ACT_MOVE, 105);
//	loop(60){yield;}
	CallSpellCard("「ドリーミングジュエル」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(105){yield;}
	let t=0;
	loop
	{
	SetAction(ACT_SHOT_A, 60);
	C(t%2);
	DamageRate=[100,100];
	wait(450-60);
	t++;
	}
//	thunder;

	task C(t)
	{
		let cx=ObjMove_GetX(objEnemy);
		let cy=ObjMove_GetY(objEnemy);
		let array=[];
		ascent(i in 0..120)
		{
			LightShot(cx,cy,i*3,i);
			array=array~[i*3];
		}
		let TempArray=[];
		loop(120)
		{
			let RandE=floor(rand(0,length(array)-0.001));
			TempArray=TempArray~[array[RandE]];
			array=erase(array,RandE);		
		}
		array=TempArray;

		task LightShot(x,y,angle,number)
		{
		let R=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, speed);
	//	ObjMove_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, DS_BALL_M_PURPLE-t);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);
	//	ShotEffect(obj);
		let AlphaAngle=rand(0,360);
		let Alpha=absolute(cos(AlphaAngle));
		let Alpha2=absolute(cos(AlphaAngle));
	//	LightShot2(obj,AlphaAngle+90);
	//	ObjRender_SetColor(obj,255*Alpha,255*Alpha,255*Alpha);
		let speed=0;
loop(3)
{
		speed=0;
		loop(30)
		{
			speed+=1.5/30;
			R+=speed;
			ObjMove_SetPosition(obj, ObjMove_GetX(objEnemy)+R*cos(angle),y+R*sin(angle));
			yield;
		}
		loop(30)
		{
			R+=speed;
			ObjMove_SetPosition(obj, ObjMove_GetX(objEnemy)+R*cos(angle),y+R*sin(angle));
			yield;
		}
		loop(30)
		{
			speed-=1.5/30;
			R+=speed;
			ObjMove_SetPosition(obj, ObjMove_GetX(objEnemy)+R*cos(angle),y+R*sin(angle));
			yield;
		}
	//	wait(30);
		R+=42;
		let dis=((y+R*sin(TempArray[number])-ObjMove_GetY(obj))^2+(x+R*cos(TempArray[number])-ObjMove_GetX(obj))^2)^0.5;
		let Tangle=atan2(y+R*sin(TempArray[number])-ObjMove_GetY(obj),x+R*cos(TempArray[number])-ObjMove_GetX(obj));

		speed=dis/240;
		let SR=0;
		loop(60)
		{
			SR+=1/60;
			ObjMove_SetPosition(obj, ObjMove_GetX(obj)+speed*SR*cos(Tangle),ObjMove_GetY(obj)+speed*SR*sin(Tangle));
			yield;
		}
		loop(180)
		{
			ObjMove_SetPosition(obj, ObjMove_GetX(obj)+speed*SR*cos(Tangle),ObjMove_GetY(obj)+speed*SR*sin(Tangle));
			yield;
		}
		loop(60)
		{
			SR-=1/60;
			ObjMove_SetPosition(obj, ObjMove_GetX(obj)+speed*SR*cos(Tangle),ObjMove_GetY(obj)+speed*SR*sin(Tangle));
			yield;
		}

	//	wait(30);
		}
	Obj_Delete(obj);
	}
}

	task thunder
	{
		let x=0;
		let y=0;
		let cx=ObjMove_GetX(objEnemy);
		let cy=-64;
		let length=30;

		loop(360)
		{
			let x=cx+rand(-8,8);
			let y=cy+rand(-8,8);
			loop(8)
			{
			IceEffect(x, y, rand(0, 180), length, 50, 30);
			}
			length+=0.3;
			yield;
		}

//length+=0.25*420;
		loop(15)
		{
			let x=cx+rand(-8,8);
			let y=cy+rand(-8,8);
			loop(8)
			{
			IceEffect(x, y, rand(0, 180), length, 50, 30);
			}
			length-=0.3*24;
			yield;
		}
		wait(30);
		Lightning(cx);
	}

	task IceEffect(x, y, angle, leng, wid, time)
	{
		let obj1 = CreateStraightLaserA1(x, y, angle, 0, 2*wid, 30, DS_ICE_BLUE, 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 Lightning(bx)
	{
		loop(20)
		{
			let X=192+rand(-16,16);
			let A=90+rand(-45,45);
			Light(X,0,A,0);
			LightEffect(X,0,A,0);
		}

		task Light(x,y,angle,c)
		{
			let obj1 = CreateStraightLaserA1(x, y, angle, 0, 5, 30, DS_BEAM_BLUE, 0);
		//	ObjShot_SetIntersectionEnable(obj1,false);
			ObjStLaser_SetSource(obj1, false);
			ObjShot_SetAutoDelete(obj1,false);
			ObjLaser_SetLength(obj1,32);
			ObjLaser_SetRenderWidth(obj1, 10);
			ObjRender_SetBlendType(obj1, BLEND_ADD_RGB);
			let color = [255,255,255];

			x+=30*cos(angle);
			y+=30*sin(angle);

			loop(10)
			{
			loop
			{
				let angleP=rand(-45,45);
				angle+=angleP;
				if(angle>=45 && angle<=135){break;}
				angle-=angleP;
			}
			let jx=x+30*cos(angle);
			let jy=y+30*sin(angle);

			let Xrange=16+128*jy/512;
			if(bx-Xrange<=jx && bx+Xrange>=jx){break;}
			jx-=30*cos(angle);
			jy-=30*sin(angle);
			}
			if(c%2==0)
			{
			yield;
			}
			if(y<=512 && x>=-64 && y>=-64 && x<=512 && c<20)
			{
			Light(x,y,angle,c+1);
			LightEffect(x,y,angle,c);
			}
			wait(30);
			let time=15;
			ascent(i in 1 .. time+1)
			{
			ObjRender_SetColor(obj1, color[0]*(time-i)/time, color[1]*(time-i)/time, color[2]*(time-i)/time);
			yield;
			}
			Obj_Delete(obj1);
		}

		task LightEffect(x,y,angle,c)
		{
		let obj1 = CreateStraightLaserA1(x, y, angle, 0, 10, 32, DS_BEAM_BLUE, 0);
		ObjShot_SetIntersectionEnable(obj1,false);
		ObjStLaser_SetSource(obj1, false);
		let color = [255,255,255];
		let time=30;
		ascent(i in 1 .. time+1)
		{
			ObjLaser_SetLength(obj1,32);
			ObjLaser_SetRenderWidth(obj1, 40*(time-i)/time);
			ObjRender_SetColor(obj1, color[0]*(time-i)/time, color[1]*(time-i)/time, color[2]*(time-i)/time);
			yield;
		}
		wait(5);
		ascent(i in 1 .. time+1)
		{
			ObjLaser_SetLength(obj1,32);
			ObjLaser_SetRenderWidth(obj1, 10*(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 FreezeShot(x,y,dir,waittime)
	{
	let speed=0;
	let angle=atan2(ObjMove_GetY(objEnemy)-y,ObjMove_GetX(objEnemy)-x)+15*dir;
	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, 14);
	ObjShot_SetAutoDelete(obj,false);
	let gradangle=80+rand(0,8);
	ShotEffect(obj);
	loop(waittime){yield;}
	loop(50)
	{
		speed+=0.04;
		ObjMove_SetSpeed(obj, speed);
		yield;
	}
	loop(60){yield;}
	ObjShot_SetAutoDelete(obj,true);
	}

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

	task B
	{
		let grad=0;
		let gra=0;
		GradChange;
		loop
		{
		ascent(i in 0..10)
		{
		Shot(i*80,241+(gra+i)%7,1,0);
		Shot(i*80,241+(gra-i+8)%7,-1,0);
		Shot(i*80,241+(gra+i)%7,1,90);
		Shot(i*80,241+(gra-i+8)%7,-1,90);
		}
		//gra+=1;
		loop(50*2){yield}
		}
		
		task GradChange
		{
		wait(300*1.5);
		let graddir=1;
		if(GetPlayerX>192){graddir=-1;}
		let gradplus=0.1;
		loop
		{
			grad+=gradplus*graddir;
			if(gradplus<0.5)
			{
			gradplus+=0.0001;
			}
			yield;
		}
		}

		task Shot(height,gra,dir,grad2)
		{
		let time=0;
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);

		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, gra);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

		let scale=4.0;
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		let R=320;
		//while(!Obj_IsDeleted(obj))
		loop(360*2)
		{
			x=196+(R*cos(grad+grad2)+(R-time)*cos(grad+grad2+90)-((time*2)*0+height)*cos(grad+grad2))*dir;
			y=224+(R*sin(grad+grad2)+(R-time)*sin(grad+grad2+90)-((time*2)*0+height)*sin(grad+grad2))*dir;
			ObjMove_SetPosition(obj, x, y);
			time+=64/40/2;
			yield;
		}
		Obj_Delete(obj);
		}

	}

	task A
	{
		let angle=-90;
		ascent(j in 0..7)
		{
		ascent(i in 0..36-2*j)
		{
		Shot(1.5+j*0.3*0+1.5,i*360/(36-2*j),247-j,j%2*2-1);
		}

		wait(4);
		}

		task Shot(speed,angle,gra,dir)
		{
		let R=0;
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);

		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, gra);
		//ObjShot_SetDelay(obj, delay);
		ObjShot_SetAutoDelete(obj,false);
		let gradangle=80+rand(0,8);

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

		let sangle=atan2(ObjRender_GetY(objEnemy)-y,ObjRender_GetX(objEnemy)-x)+180;
		ObjMove_SetAngle(obj, sangle+45*((gra%2)*2-1));
		while(!Obj_IsDeleted(obj))
		{
			ObjMove_SetPosition(obj, x+R*cos(angle), y+R*sin(angle));
			R+=speed;
			angle+=0.5*dir;
			yield;
		}
		}
	}

}