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

@Initialize
{
//	BossInit(GetCommonData("BossPosition",[0,0])[0],GetCommonData("BossPosition",[0,0])[1]);
	BossInit(0,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;
}

let EnemyMaxLife=3500;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(EnemyMaxLife);//ライフを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,7);
	SetAction(ACT_MOVE, 60);
//	loop(60){yield;}
	CallSpellCard("「ソウルスティーラー」");
/*
	SetAction(ACT_SPELL, 60);
	loop(12){yield;}
*/
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	loop(60){yield;}
	C;
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>=EnemyMaxLife/2){yield;}
	D;

	task D
	{
		loop
		{
		let PAngle=rand(0,360);
		let angle=rand(0,360);
		ascent(i in 0..18)
		{
		Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),PAngle,angle+20*i);
		Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),PAngle+90,angle+20*i);
		}
		wait(30);
		}

		task Shot(x,y,PAngle,Angle)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, 47);
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

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

		let angle=atan2(1/3*sin(PAngle)*cos(Angle)+1*cos(PAngle)*sin(Angle),1/3*cos(PAngle)*cos(Angle)-1*sin(PAngle)*sin(Angle));
		ObjMove_SetAngle(obj, angle);

		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetX(obj,x+R/3*cos(PAngle)*cos(Angle)-R*sin(PAngle)*sin(Angle));
			ObjRender_SetY(obj,y+R/3*sin(PAngle)*cos(Angle)+R*cos(PAngle)*sin(Angle));
			R+=3;
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task C
	{
	SetAction(ACT_SHOT_B, 60);
	S;
	wait(120);
	DamageRate=[100,100];
	let dir=floor(rand(0,2-0.001))*2-1;
	loop
	{
		T_S(dir);
		wait(120);
		SetAction(ACT_SHOT_B, 60);
		S;
		wait(90);
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);
		let mx= ex+rand(-120,120);
		let my= ey+rand(-20,20);
		while(mx<64 || mx>320){mx=ex+rand(-120,120);}
		while(my<80 || my>160){my=ey+rand(-20,20);}
		ObjMove_SetDestAtWeight(objEnemy,mx, my, 30,2.0);
		SetAction(ACT_MOVE, 75);
		dir*=-1;
	}

		task T_S(dir)
		{
		let PAngle=rand(0,360);
		let angle=rand(0,360);
		loop(20)
		{
		ascent(i in 0..20)
		{
		Shot(ObjRender_GetX(objEnemy),ObjRender_GetY(objEnemy),PAngle,angle+18*i,dir);
		}
		wait(6);
		PAngle+=23*dir;
		}
		}

		task Shot(x,y,PAngle,Angle,dir)
		{
		let time=0;
		let obj = ObjShot_Create(OBJ_SHOT);
		ObjShot_Regist(obj);
		ObjMove_SetPosition(obj, x, y);
	//	ObjMove_SetSpeed(obj, 0);
		ObjShot_SetGraphic(obj, 41);
		if(dir==-1){ObjShot_SetGraphic(obj, 46);}
		//ObjShot_SetDelay(obj, delay);
		//ObjShot_SetAutoDelete(obj,false);

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

		let angle=atan2(1/3*sin(PAngle)*cos(Angle)+1*cos(PAngle)*sin(Angle),1/3*cos(PAngle)*cos(Angle)-1*sin(PAngle)*sin(Angle));
		ObjMove_SetAngle(obj, angle);

		while(!Obj_IsDeleted(obj))
		{
			ObjRender_SetX(obj,x+R/3*cos(PAngle)*cos(Angle)-R*sin(PAngle)*sin(Angle));
			ObjRender_SetY(obj,y+R/3*sin(PAngle)*cos(Angle)+R*cos(PAngle)*sin(Angle));
			R+=3;
			yield;
		}
		Obj_Delete(obj);
		}
	}

	task S
	{
		SoulSteal;
		task SoulSteal
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../../img/effect/WhiteHole.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	//	Obj_SetRenderPriorityI(obj,70);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, -64, 64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, -64, 0);
		ObjPrim_SetVertexPosition(obj, 3, 64,64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 128);
		ObjPrim_SetVertexUVT(obj, 2, 128, 0);
		ObjPrim_SetVertexUVT(obj, 3, 128, 128);
		let scale=0;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		let Alpha=1;
		ObjRender_SetColor(obj,255*Alpha,50*Alpha,50*Alpha);
		let angle=GetAngleToPlayer(objEnemy);
		let x=ObjRender_GetX(objEnemy);
		let y=ObjRender_GetY(objEnemy);
		let speed=1.5;
		let MaxScale=1.5+(EnemyMaxLife-ObjEnemy_GetInfo(objEnemy,INFO_LIFE))/EnemyMaxLife;
		loop(30)
		{
			scale+=MaxScale/30;
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			ObjRender_SetPosition(obj,x,y,0);
			yield;
		}
		let s_count=0;
		loop(600)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			let dis=((GetPlayerY-ObjRender_GetY(obj))^2+(GetPlayerX-ObjRender_GetX(obj))^2)^0.5;
			if(dis<=80*MaxScale/1.5)
			{
				s_count+=1;
				if(s_count%2==0)
				{
				Steal;
				}
			}
			yield;
		}
		Obj_Delete(obj);
		}

		task Steal
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../../img/effect/WhiteHole.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, -64, 64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, -64, 0);
		ObjPrim_SetVertexPosition(obj, 3, 64,64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 128);
		ObjPrim_SetVertexUVT(obj, 2, 128, 0);
		ObjPrim_SetVertexUVT(obj, 3, 128, 128);
		let scale=0.05;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		let Alpha=1;
		ObjRender_SetColor(obj,255*Alpha,50*Alpha,50*Alpha);
		let angle=rand(0,360);
		let x=GetPlayerX;
		let y=GetPlayerY;
		let speed=5;
		ObjRender_SetPosition(obj,x,y,0);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		let Arate=5;
		loop(600)
		{
			let ToEnemyAngle=atan2(ObjRender_GetY(objEnemy)-ObjRender_GetY(obj),ObjRender_GetX(objEnemy)-ObjRender_GetX(obj));
			let judgangle=sin(ToEnemyAngle-angle);
			let ShiftAngle=absolute(ToEnemyAngle-angle);
			if(judgangle>0)
			{
				angle+=Arate;
			}
			else
			{
				angle-=Arate;
			}
			Arate+=0.01;
			//speed+=0.02;
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			let dis=((ObjRender_GetY(objEnemy)-ObjRender_GetY(obj))^2+(ObjRender_GetX(objEnemy)-ObjRender_GetX(obj))^2)^0.5;
			if(dis<=24)
			{
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<EnemyMaxLife)
				{
				ObjEnemy_AddLife(objEnemy,10);
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>EnemyMaxLife)
				{
					ObjEnemy_SetLife(objEnemy,EnemyMaxLife);
				}
				}
				break;
			}
			yield;
		}
		Obj_Delete(obj);		
		}
	}

}