#東方弾幕風[Single]
#ScriptVersion[3]
#Title["Spell5L"]
#Text["Spell5L"]
#Background["./Background.txt"]
#BGM[DEFAULT]
#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,50,25);
	mainTask;
	LoadSoundEffects;
	TRender;
	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(4500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(47);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(54321);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true)
	}
}

//----------------------------------------------------
task mainTask{
	wait(60);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Wriggle Sign\"Forbidden Bug\"",15);
	wait(60);
	TShot;
	}

task TShot{
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,4,60,255,255,0);
	wait(70);
	PlaySound("stars",0);
	TExplosionA(GetEnemyX,GetEnemyY,5,0.3);
	TCircle(GetEnemyX,GetEnemyY,180,0,255,0);
	TCircle(GetEnemyX,GetEnemyY,180,255,255,0);
	TCircle(GetEnemyX,GetEnemyY,300,0,255,0);
	TCircle(GetEnemyX,GetEnemyY,420,255,255,0);
	TCircle(GetEnemyX,GetEnemyY,540,0,255,0);
	TCircle(GetEnemyX,GetEnemyY,660,255,255,0);
	TCircle(GetEnemyX,GetEnemyY,780,0,255,0);
	wait(120);
	TTest;
	}

task TTest{
	let obj;
	PlaySound("Shot2",0);
	ascent(i in 0..48){
	ascent(a in 0..6){
	obj = CreateShotA1(GetEnemyX,GetEnemyY,1.5+a/5,i*7.5,SMALL_GREEN,10);
	Manage(obj);
	}
	}
	wait(180);
	PlaySound("Shot2",0);
	ascent(i in 0..48){
	ascent(a in 0..6){
	obj = CreateShotA1(GetEnemyX,GetEnemyY,1.5+a/5,i*7.5,SMALL_YELLOW,10);
	Manage2(obj);
	}
	}
	wait(180);
	TFire1;
	}

task TFire1{
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,3,30,0,255,0);
	wait(30);
	TCharge01(GetEnemyX,GetEnemyY,3,30,255,255,0);
	wait(30);
	let obj;
	let angle = 0;
	let fault = 0;
	ascent(n in 0..24){
	PlaySound("Shot1",0);
	fault = rand(-2.5,2.5);
	ascent(i in 0..11){
	angle = i*3.5+n*5;
	obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angle+fault,PELLET_GREEN,10);
	Manage(obj);
	}
	ascent(i in 0..53){
	angle = i*5+n*5+60;
	obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angle+fault,PELLET_YELLOW,10);
	Manage2(obj);
	}
	wait(6);
	}
	wait(60);
	TFire2;
	}

task TFire2{
	let obj;
	let angle = 0;
	let fault = 0;
	ascent(n in 0..24){
	PlaySound("Shot1",0);
	fault = rand(-1,1);
	ascent(i in 0..11){
	angle = i*3.5-n*5+180;
	obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angle+fault,PELLET_YELLOW,10);
	Manage2(obj);
	}
	ascent(i in 0..57){
	angle = i*5-n*5+230;
	obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angle+fault,PELLET_GREEN,10);
	Manage(obj);
	}
	wait(6);
	}
	wait(60);
	TFire3;
	}

task TFire3{
	let obj;
	let angle = 0;
	let fault = 0;
	ascent(n in 0..2){
	fault = rand(-2.5,2.5);
	loop(2){
	PlaySound("Shot1",0);
	ascent(i in 0..90){
	obj = CreateShotA1(GetEnemyX,GetEnemyY,2,i*4+fault,LARGE_GREEN,10);
	Manage(obj);
	}
	wait(15);
	}
	wait(20-n*5);
	loop(2){
	PlaySound("Shot1",0);
	ascent(i in 0..90){
	obj = CreateShotA1(GetEnemyX,GetEnemyY,2,i*4+fault,LARGE_YELLOW,10);
	Manage2(obj);
	}
	wait(15);
	}
	wait(20-n*5);
	}
	TFire4;
	}

task TFire4{
	PlaySound("Charge4",0);
	TCharge01(GetEnemyX,GetEnemyY,3,30,0,255,0);
	wait(30);
	TCharge01(GetEnemyX,GetEnemyY,3,30,255,255,0);
	wait(30);
	let obj;
	let angle = 0;
	let fault = 0;
	PlaySound("Shot5",0);
	ascent(i in 0..10){
	ascent(a in 0..8){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,1.5+a/3,i*36,LARGE_GREEN,10); // warning
	 Manage(obj);
	}
	}
	wait(30);
	ascent(n in 0..24){
	 PlaySound("Shot1",0);
	 ascent(i in 0..10){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,4.5,i*36+n,SMALL_GREEN,10);
	 Manage(obj);
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,4.5,i*36-n,SMALL_GREEN,10);
	 Manage(obj);
	 }
	 ascent(i in 0..7){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angleToPlayer-6+i*2,SMALL_YELLOW,10);
	 Manage2(obj);
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angleToPlayer-6+i*2,SMALL_GREEN,10);
	 Manage(obj);
	 }
	wait(5);
	}
	wait(30);
	PlaySound("Shot5",0);
	ascent(i in 0..10){
	ascent(a in 0..8){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,1.5+a/3,i*36+18,LARGE_YELLOW,10); // warning
	 Manage2(obj);
	}
	}
	wait(30);
	ascent(n in 0..36){
	 PlaySound("Shot1",0);
	 ascent(i in 0..10){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,4.5,i*36+18+n,SMALL_YELLOW,10);
	 Manage2(obj);
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,4.5,i*36+18-n,SMALL_YELLOW,10);
	 Manage2(obj);
	 }
	 ascent(i in 0..7){
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angleToPlayer-6+i*2,SMALL_GREEN,10);
	 Manage(obj);
	 obj = CreateShotA1(GetEnemyX,GetEnemyY,3,angleToPlayer-6+i*2,SMALL_YELLOW,10);
	 Manage2(obj);
	 }
	wait(3);
	}	
	wait(60);
	TFire5;
	}

task TFire5{
	PlaySound("Charge2",0);
	TCharge01(GetEnemyX,GetEnemyY,3,30,0,255,0);
	wait(30);
	TCharge01(GetEnemyX,GetEnemyY,3,30,255,255,0);
	wait(30);
	let obj;
	let angle = 0;
	let fault = 0;
	ascent(n in 0..48){
	 PlaySound("Shot1",0);
	 ascent(i in 0..9){
		obj = CreateShotA1(GetEnemyX,GetEnemyY,3,i*40-n*2.5,PELLET_GREEN,7);
		Manage(obj);
	 }
	 ascent(i in 0..9){
	 	obj = CreateShotA1(GetEnemyX,GetEnemyY,3,i*40+n*2.5+20,PELLET_YELLOW,7);
		Manage2(obj);
	 }
	wait(4);
	}
	wait(60);
	ascent(n in 0..48){
	 PlaySound("Shot1",0);
	 ascent(i in 0..9){
		obj = CreateShotA1(GetEnemyX,GetEnemyY,2.5,i*40+n*2.5,PELLET_GREEN,7);
		Manage(obj);
	 }
	 ascent(i in 0..9){
	 	obj = CreateShotA1(GetEnemyX,GetEnemyY,2.5,i*40-n*2.5+20,PELLET_YELLOW,7);
		Manage2(obj);
	 }
	wait(3);
	}
	wait(60);
	ascent(n in 0..72){
	 PlaySound("Shot1",0);
	 ascent(i in 0..9){
		obj = CreateShotA1(GetEnemyX,GetEnemyY,2,i*40-n*1.5,PELLET_GREEN,7);
		Manage(obj);
	 }
	 ascent(i in 0..9){
	 	obj = CreateShotA1(GetEnemyX,GetEnemyY,2,i*40+n*1.5+20,PELLET_YELLOW,7);
		Manage2(obj);
	 }
	wait(2);
	}
	wait(60);
	TFire6;
	}

task TFire6{
	let obj;
	let angle = 0;
	let fault = 0;
	TExplosionA(GetEnemyX,GetEnemyY,5,0.4);
	PlaySound("Shot4",0);
	ascent(a in 1..12){
	 fault = rand(0,6);
	 ascent(i in 0..90){
		 angle = i*4+fault;
		 if(i%2==0){
		 obj = CreateShotA1(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),a/6,angle,ARROWHEAD_GREEN,10);
		 Manage(obj);
		 }
		 else{
		 obj = CreateShotA1(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),a/6,angle,ARROWHEAD_YELLOW,10);
		 Manage2(obj);
		 }
	 }
	}
	}

task TCircle(x,y,size,r,g,b){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/charge.png";
	let s = size/128;
	let ra = 0;
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetAlpha(obj,32);
	ObjRender_SetColor(obj,r,g,b);
	while(ra<size/128){
	ra+= size/12800;
	ObjRender_SetScaleXYZ(obj,ra,ra,0);
	yield;
	}
	}


task Manage(obj){
	let dis = 0;
	let shot;
	while(!Obj_IsDeleted(obj)){
	dis = (((ObjMove_GetX(objBoss)-ObjMove_GetX(obj))^2+(ObjMove_GetY(objBoss)-ObjMove_GetY(obj))^2)^(1/2));
	if((dis>=90 && dis<=150)||(dis>=210 && dis<=270) || (dis>=330 && dis<=390)){
	ObjRender_SetAlpha(obj,60);
	ObjShot_SetIntersectionEnable(obj,false);
	}
	else{
	ObjRender_SetAlpha(obj,255);
	ObjShot_SetIntersectionEnable(obj,true);
	}
	yield;
	}
	}

task Manage2(obj){
	let dis = 0;
	while(!Obj_IsDeleted(obj)){
	dis = (((ObjMove_GetX(objBoss)-ObjMove_GetX(obj))^2+(ObjMove_GetY(objBoss)-ObjMove_GetY(obj))^2)^(1/2));
	if((dis>=150 && dis<=210)||(dis>=270 && dis<=330)){
	ObjRender_SetAlpha(obj,60);
	ObjShot_SetIntersectionEnable(obj,false);
	}
	else{
	ObjRender_SetAlpha(obj,255);
	ObjShot_SetIntersectionEnable(obj,true);
	}
	yield;
	}
	}

task TFake{
	let x = 0;
	let y = 0;
	let c = 0;
	let color;
	let shot;
	let spd = 0;
	ascent(i in 0..120){
	ascent(a in 1..5){
	if(a%2!=0){color=DOT_GREEN;}
	else{color=DOT_YELLOW;}
	x = GetEnemyX+(90+60*a)*cos(i*3);
	y = GetEnemyY+(90+60*a)*sin(i*3);
	shot = CreateShotA1(x,y,0,0,color,5);
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,64);
	x = GetEnemyX+(90+60*a)*cos(i*3);
	y = GetEnemyY+(90+60*a)*sin(i*3);
	shot = CreateShotA1(x,y,0,0,color,5);
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,64);
	}
	}
	ascent(i in 0..36){
	x = GetEnemyX+(90)*cos(i*10);
	y = GetEnemyY+(90)*sin(i*10);
	shot = CreateShotA1(x,y,0,0,DOT_GREEN,5);
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,64);
	x = GetEnemyX+(90)*cos(i*10);
	y = GetEnemyY+(90)*sin(i*10);
	shot = CreateShotA1(x,y,0,0,DOT_YELLOW,5);
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,64);
	}
	}
//----------------------------------------------------

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

task TRender
{
	magiccircle(objBoss,1.5,0,255,0,true);
	let imgWriggle = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	let c = 0;
	ObjPrim_SetTexture(objBoss, imgWriggle);
	ObjRender_SetScaleXYZ(objBoss,1.2,1.2,1.2);
	while(!Obj_IsDeleted(objBoss))
	{
		c++;
		if(c==70){TExplosionA(GetEnemyX,GetEnemyY,10,0.4)};
		if(c>=70){ObjRender_SetAlpha(objBoss,0)};
		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;

	}

