#東方弾幕風[Single]
#ScriptVersion[3]
#Title["信etal Sign"]
#Text["Untitle"]
#Image["./ExRumia(星符「ミッドナイトレヴァリエ」).jpg"]
#Background["script/default_system/Default_Background_IceMountain.txt"]
#BGM[DEFAULT]
#Player[DEFAULT]

#include"./Functions/Index.txt"
#include"script/Rumia Boss/ZUNShot_Const.txt"
#include"script/Default_system/Default_Effect.Txt"

let objBoss;
let bSpell=true;
let value = 420;
let frame = 0;
let aim = "false";
let objScene=GetEnemyBossSceneObjectID();
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,20,10);
	mainTask;
	TRender;
	LoadSoundEffects;
	TEnd;
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,5);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);//出現と同時に敵弾を全て削除	
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(850);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
}

//----------------------------------------------------
task mainTask{
	wait(60);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Metal Sign\"Magnet Ball\"",15);
	wait(60);
	TBall;
	}
//----------------------------------------------------

task TBall{
	let obj = CreateShotA2(GetEnemyX,GetEnemyY,0,0,-0.1,0,ORB_GRAY,30);
	Manage(obj);
	wait(240);
	TCharge01(GetEnemyX,GetEnemyY,3,3,60,255,255,255);
	PlaySound("Charge4",0);
	TMovement(35,10,2);
	wait(60);
	PlaySound("Eff4",0);
	aim = "true";
	wait(60);
	PlaySound("Charge1",0);
	wait(210);
	TCharge01(GetEnemyX,GetEnemyY,3,3,30,255,255,255);
	}

task Manage(obj){
	let c = 0;
	let knife;
	let start = rand(0,359);
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetSpellResist(obj,true);
	while(!Obj_IsDeleted(obj)){
	ObjMove_SetPosition(obj,GetPlayerX+(100)*cos(c*0.75+start),GetPlayerY+(100)*sin(c*0.75+start));
	c++;
	if(c%5==0 && aim == "false"){
	PlaySound("Shot1",0);
	knife = CreateShotA1(0,rand(-60,524),rand(1.2,2),0,KNIFE_GRAY,10);
	ManageKnife(knife,c/5);
	knife = CreateShotA1(stgx*2,rand(-60,524),rand(1.2,2),180,KNIFE_GRAY,10);
	ManageKnife(knife,c/5);
	}
	yield;
	}
	 task ManageKnife(obj2,dir){
	 let angle = 0;
	 let c2 = "false";
	 ObjShot_SetAutoDelete(obj2,false);
	 let obj3;
	 while(!Obj_IsDeleted(obj2)){
	 angle = Obj_GetAngleToObj(obj2,ObjMove_GetX(obj),ObjMove_GetY(obj));
	 if(aim=="true"){
	 obj3 = CreateShotA2(ObjMove_GetX(obj2),ObjMove_GetY(obj2),0,angle,0.005,4,D_PELLET_GRAY,15);
	 ManageWay(obj3,dir);
	 Obj_Delete(obj2);
	 }
	 yield;
	 }
	  task ManageWay(obj4,c){
	  ObjShot_SetAutoDelete(obj4,false);
	  let angle = 0;
	  loop(300){
	  angle = Obj_GetAngleToObj(obj4,ObjMove_GetX(obj),ObjMove_GetY(obj));
	  ObjMove_SetAngle(obj4,angle);
	  yield;
	  }
		if(c==1){
		ascent(i in 0..18){
		ascent(a in 0..8){
		CreateShotOA1(obj,0.6+a/4,i*20+a*2,PELLET_GRAY,10);
		}
		}
		Obj_Delete(obj);
		aim = "false";
		}
		ObjShot_SetAutoDelete(obj4,true);
	 }
	 }
	 PlaySound("Shot4",0);
	 wait(90);
	 aim = "false";
	 TBall;
	}


//----------------------------------------------------
//描画タスク
//----------------------------------------------------

task TRender
{
	magiccircle(objBoss);
	let imgExRumia = GetCurrentScriptDirectory~"boss11.png";
	let f = 0;
	let t = 0;
	ObjPrim_SetTexture(objBoss, imgExRumia);
	while(!Obj_IsDeleted(objBoss))
	{
		let angle = ObjMove_GetAngle(objBoss);
		let speed = ObjMove_GetSpeed(objBoss);
		if(speed == 0){
		if(f<8){ObjSprite2D_SetSourceRect(objBoss, 0, 0, 64, 64);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(objBoss, 64, 0, 128, 64);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(objBoss, 128, 0, 192, 64);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(objBoss, 192, 0, 256, 64);}
		if(f>=32){f=0;}
		f++;
		t = 0;
		}
		else if(cos(angle) > 0 || cos(angle) < 0 && t == 0){
		if(cos(angle)<0){ObjRender_SetAngleY(objBoss,180);}
		else{ObjRender_SetAngleY(objBoss,0);}
		manageSprite;
		t = 1;
		}
		ObjSprite2D_SetDestCenter(objBoss);
		yield;
	}
}

//----------------------------------------------------

function TMovement(x,y,speed){
		let resultx=rand(0,1);
		resultx=round(resultx);
		let resulty=rand(0,1);
		resulty=round(resulty);
		let posx=rand(10,-10);
		if(posx<=10&&posx>=-10){
			if(resultx==1){
				posx=x;
			}else{
				posx=-x;
			}
		}
		if((GetEnemyX+posx)<=96||(GetEnemyX+posx)>=(GetStgFrameWidth()-96)){
			posx=-posx;
		}
		
		let posy=rand(20,-20);
		if(posy<=7.5&&posy>=-7.5){
			if(resulty==1){
				posy=y;
			}else{
				posy=-y;
			}
		}
		if((GetEnemyY+posy)<=32||(GetEnemyY+posy)>=GetStgFrameHeight()/3){
			posy=-posy;
		}
		ObjMove_SetDestAtWeight(objBoss,GetEnemyX+posx,GetEnemyY+posy,10,speed);
}
	
//----------------------------------------------------
task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		TExplosionA(GetEnemyX,GetEnemyY,10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(30){
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		yield;
		}
		CloseScript(GetOwnScriptID());
	}
