#東方弾幕風[Single]
#ScriptVersion[3]
#Title["Non-Spell1"]
#Text["Non-Spell1"]
#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 GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	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(4000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
}

//----------------------------------------------------
task mainTask{
	wait(120);
	TShot;
	}
//----------------------------------------------------

task TShot{
	let acc = 0;
	let v = 0;
	let fault = 0;
	let obj;
	PlaySound("Shot1",0);
	ascent(a in 0..10){
	PlaySound("BulletWave",120-a*5);
	v = 1.5+a/3;
	acc = -v/60;
	fault = rand(-1,1);
	ascent(i in 0..13){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,v,270+i*15+fault,acc,0,PELLET_YELLOW,10);
	ObjMove_AddPatternA2(obj,120-a*5,0,NO_CHANGE+a*22.5+140,0.05,3,4-a/3);
	ObjMove_AddPatternA2(obj,140-a*5,NO_CHANGE,NO_CHANGE,0.1,0,4-a/3);
	}
	}
	wait(60);
	PlaySound("Shot1",0);
	ascent(a in 0..10){
	PlaySound("BulletWave",120-a*5);
	v = 1.5+a/3;
	acc = -v/60;
	fault = rand(-1,1);
	ascent(i in 0..13){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,v,90+i*15+fault,acc,0,PELLET_GREEN,10);
	ObjMove_AddPatternA2(obj,120-a*5,0,NO_CHANGE-a*22.5+220,0.05,3,4-a/3);
	ObjMove_AddPatternA2(obj,140-a*5,NO_CHANGE,NO_CHANGE,0.1,0,4-a/3);
	}
	}
	wait(120);
	PlaySound("Shot1",0);
	ascent(a in 0..10){
	PlaySound("BulletWave",120-a*5);
	v = 1.5+a/3;
	acc = -v/60;
	fault = rand(-1,1);
	ascent(i in 0..13){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,v,270+i*15+fault,acc,0,PELLET_YELLOW,10);
	ObjMove_AddPatternA2(obj,120-a*5,0,NO_CHANGE+a*22.5+140,-0.05,3,4-a/3);
	ObjMove_AddPatternA2(obj,140-a*5,NO_CHANGE,NO_CHANGE,-0.1,0,4-a/3);
	}
	}
	wait(60);
	PlaySound("Shot1",0);
	ascent(a in 0..10){
	PlaySound("BulletWave",120-a*5);
	v = 1.5+a/3;
	acc = -v/60;
	fault = rand(-1,1);
	ascent(i in 0..13){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,v,90+i*15+fault,acc,0,PELLET_GREEN,10);
	ObjMove_AddPatternA2(obj,120-a*5,0,NO_CHANGE-a*22.5+220,-0.05,3,4-a/3);
	ObjMove_AddPatternA2(obj,140-a*5,NO_CHANGE,NO_CHANGE,-0.1,0,4-a/3);
	}
	}
	wait(180);
	TShot2;
	}

task TShot2{
	let color;
	let x = 0;
	let y = 0;
	let angle = 0;
	loop(3){
	PlaySound("Shot1",0);
	ascent(i in 0..24){
	ascent(a in 0..9){
	if(a%2==0){color = PELLET_YELLOW;}
	else{color = PELLET_GREEN;}
	CreateShotA1(GetEnemyX,GetEnemyY,0.5+a/4,i*15,color,10);
	}
	}
	TMovement(30,10,5);
	wait(60);
	}
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,5);
	wait(60);
	enemy = fam(GetEnemyX,GetEnemyY,100);
	TEnemy(enemy,1);
	enemy = fam(GetEnemyX,GetEnemyY,100);
	TEnemy(enemy,-1);
	wait(360);
	TShot;
	}

task TEnemy(obj,way){
	let r = 0;
	let c = 0;
	while(!Obj_IsDeleted(obj)){
	if(r<120){r += 0.75;}
	if(c>300){ObjEnemy_SetLife(obj,0);}
	c += 1;
	ObjMove_SetPosition(obj,GetEnemyX+r*cos(c*way+90),GetEnemyY+r*sin(c*way+90));
	if(c<=240 && c>=60 && c%4 ==0){
	PlaySound("Shot1",0);
	loop(2){
	CreateShotOA1(obj,rand(0.5,1.5),rand(0,359),SMALL_YELLOW,12);
	}
	}
	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_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}
