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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(80,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(4800);//ライフを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, 112, 10,4);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「フェイタルフリージア」");

//	SetAction(ACT_SPELL, 60);
//	loop(12){yield;}
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	wait(90);
	C;
	DamageRate=[100,100];

	task C
	{
loop
{
		SetAction(ACT_SHOT_A, 60);
		let angle=rand(0,360);
		ascent(i in 0..24)
		{
		Freeze(angle+i*15,1);
		}
		loop(300){yield;}
		SetAction(ACT_SHOT_A, 60);
		angle=rand(0,360);
		ascent(i in 0..24)
		{
		Freeze(angle+i*15,-1);
		}
		loop(300){yield;}
}
	}

	task Freeze(angle,dir)
	{
		let x;
		let y;
		let cx=ObjMove_GetX(objEnemy);
		let cy=ObjMove_GetY(objEnemy);
		let R=0;
		loop(30)
		{
			R+=2.0;
			angle+=6*dir;
			x=cx+R*cos(angle);
			y=cy+R*sin(angle);
			IceEffect(x, y, angle-90+rand(-10, 10), 30, 15, 20);
			yield;
		}
		loop(15)
		{
			angle+=6*dir;
			x=cx+R*cos(angle);
			y=cy+R*sin(angle);
			IceEffect(x, y, angle-90+rand(-10, 10), 30, 15, 20);
			yield;
		}
		angle+=90*dir;
		let speed=3;
		let delay=65;
		let count=0;
		loop(15)
		{
			x=x+speed*cos(angle);
			y=y+speed*sin(angle);
			IceEffect2(x, y, angle+180+rand(-10, 10), 30, 15, 20);
		//	FreezeShot(x,y,floor(count%2)*2-1,delay);
			count+=0.2;
			delay-=1;
			yield;
		}
		loop(20)
		{
			angle-=3.0*dir;
			speed+=0.2;
			x=x+speed*cos(angle);
			y=y+speed*sin(angle);
			IceEffect2(x, y, angle+180+rand(-10, 10), 30, 15, 20);
			FreezeShot(x,y,floor(count%2)*2-1,delay);
			count+=0.2;
			delay-=1;
			yield;
		}
		loop(30)
		{
			angle-=1.5*dir;
			speed+=0.2;
			x=x+speed*cos(angle);
			y=y+speed*sin(angle);
			IceEffect2(x, y, angle+180+rand(-10, 10), 30, 15, 20);
			FreezeShot(x,y,floor(count%2)*2-1,delay);
			count+=0.2;
			delay-=1;
			yield;
		}
	}

	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 IceEffect2(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 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;
		}
		}
	}

}