#東方弾幕風[Single]
#ScriptVersion[3]
#Title["LastWord"]
#Text["LastWord"]
#Background["./Background.txt"]
#BGM["./LastWord.mp3"]
#Player[DEFAULT]

#include"./Functions/Index.txt"
#include"script/Wriggle Nightbug/ZUNShot_Const.txt"
#include"script/Default_system/Default_Effect.Txt"

let objBoss;
let enemy;
let bSpell=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,25,12);
	mainTask;
	LoadSoundEffects;
	TRender;
	TEnd;
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,5);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);//出現と同時に敵弾を全て削除
	SetPlayerLife(0);
	SetPlayerSpell(0);
}

@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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
	case(EV_REQUEST_IS_LAST_SPELL)
	{
		SetScriptResult(true);
	}
}

//----------------------------------------------------
task mainTask{
	wait(60);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Wriggle Sign\"Rebirth of mysterious fireflies\"",15);
	TTest;
	wait(300);
	TFire;
	}

task TFire{
	let c = 0;
	let dir = 0;
	let more = 0;
	while(!Obj_IsDeleted(objBoss)){
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,3,60,0,255,0);
	wait(60);
	loop(6){
	 ascent(n in 0..3){
	 ascent(i in 0..18){
	 CreateShotOA1(objBoss,3.5,i*20+dir,SMALL_YELLOW,12);
	 }
	 PlaySound("Shot5",0);
	 wait(5);
	 }
	 dir = rand(0,19);
	 wait(10-more);
	 ascent(n in 0..3){
	 ascent(i in 0..18){
	 CreateShotOA1(objBoss,3.5,i*20+dir,SMALL_GREEN,12);
	 }
	 PlaySound("Shot5",0);
	 wait(5);
	 }
	 dir = rand(0,19);
	 wait(10-more);
	 if(c%5==0){TMovement(30,5,5);}
	 c++;
	}
	//////////////////////////
	c = 0;
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,3,60,0,255,0);
	wait(60);
	loop(6){
	 ascent(n in 0..3){
	 ascent(i in 0..18){
	 CreateShotOA1(objBoss,3.5,i*20+dir+n*2,SMALL_YELLOW,12);
	 }
	 PlaySound("Shot5",0);
	 }
	 dir = rand(0,19);
	 wait(20-more*2);
	 ascent(n in 0..3){
	 ascent(i in 0..18){
	 CreateShotOA1(objBoss,3.5,i*20+dir+n*2,SMALL_GREEN,12);
	 }
	 PlaySound("Shot5",0);
	 }
	 dir = rand(0,19);
	 wait(20-more*2);
	 if(c%5==0){TMovement(30,15,5);}
	 c++;
	}
	ObjEnemy_SetDamageRate(objBoss,50,25);
	more++;
	}
	}

task TTest{
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,4,60,255,255,0);
	wait(60);
	PlaySound("fam",0);
	ascent(i in 0..3){
	enemy = fam(GetEnemyX,GetEnemyY,5000);
	TEnemy(enemy,i,0,i);
	enemy = fam(GetEnemyX,GetEnemyY,5000);
	TEnemy(enemy,i,1,i+3);
	}
	}

task TEnemy(obj,way,type,value){
	let c = 0;
	EnemyMove;
	wait(value*10+60);
	EnemyFire;
		task EnemyFire{
		let t = 0;
		let dir = 0;
		while(!Obj_IsDeleted(obj)){
		let shot;
		 if(t%55==0){
		  loop(2){
		  dir = getAtanAngle(obj,objPlayer);
		  ascent(i in 0..24){
		  shot = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),6,i*15+dir,-0.15,1,SMALL_YELLOW,12);
		  ObjMove_AddPatternA3(shot,5/0.15,NO_CHANGE,NO_CHANGE,0,0,0,DOT_YELLOW);
		  }
		  PlaySound("Shot1",0);
		  wait(2);
		  }
		 }
		t++;
		yield;
		}
		}
	task EnemyMove{
	while(!Obj_IsDeleted(obj)){
	if(type==0){ObjMove_SetDestAtWeight(obj,(GetEnemyX+120)+50*cos(c+way*120),(GetEnemyY)+50*sin(c+way*120),10,5);}
	if(type==1){ObjMove_SetDestAtWeight(obj,(GetEnemyX-120)+50*cos(c+way*120),(GetEnemyY)+50*sin(c+way*120),10,5);}
	if(type==0){c-=2;}
	if(type==1){c+=2;}
	yield;
	}
	}
	}

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

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

task TRender
{
	magiccircle(objBoss,1.5,0,255,0,true);
	let imgWriggle = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	ObjPrim_SetTexture(objBoss, imgWriggle);
	ObjRender_SetScaleXYZ(objBoss,1.2,1.2,1.2);
	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, 48, 64);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(objBoss, 48, 0, 96, 64);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(objBoss, 96, 0, 144, 64);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(objBoss, 144, 0, 192, 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);
		SetAutoDeleteObject(true);
		loop(10){
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		yield;
		}
		CloseScript(GetOwnScriptID());
		return;

	}

