#東方弾幕風[Single]
#ScriptVersion[3]
#Title["Non-Spell3L"]
#Text["Non-Spell3L"]
#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(4250);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
}

//----------------------------------------------------
task mainTask{
	wait(60);
	TMain;
	}
//----------------------------------------------------

task TMain{
	let obj;
	while(!Obj_IsDeleted(objBoss)){
	ascent(i in 0..10){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,5,i*36,0,0,LARGE_YELLOW,60);
	Manage(obj,1);
	}
	wait(330);
	ascent(i in 0..10){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,5,i*36+18,0,5,LARGE_GREEN,60);
	Manage(obj,1);
	}
	wait(195);
	TMovement(40,15,3);
	wait(195);
	ascent(i in 0..10){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,5,i*36+9,0,0,LARGE_YELLOW,60);
	Manage(obj,-1);
	}
	wait(330);
	ascent(i in 0..10){
	obj = CreateShotA2(GetEnemyX,GetEnemyY,5,i*36+27,0,5,LARGE_GREEN,60);
	Manage(obj,-1);
	}
	wait(195);
	TMovement(40,15,3);
	wait(195);
	}
	}

task Manage(obj,way){
	let c = 0;
	let x = 0;
	let y = 0;
	let shot;
	 ObjRender_SetAlpha(obj,0);
	 ObjShot_SetIntersectionEnable(obj,false);
	 wait(10);
	 while(!Obj_IsDeleted(obj)){
	 c++;
		if(c%2==0){
		PlaySound("Shot1",0);
		PlaySound("BulletWave",30);
		x = ObjMove_GetX(obj)+(35*way)*cos(c*10);
		y = ObjMove_GetY(obj)+(35*way)*sin(c*10);
		shot = CreateShotA2(x,y,0,c*10,0.1,0,PELLET_GREEN,25);
		ObjMove_AddPatternA2(shot,30,0,NO_CHANGE,0.01,0,1);
		x = ObjMove_GetX(obj)-(35*way)*cos(c*10);
		y = ObjMove_GetY(obj)-(35*way)*sin(c*10);
		shot = CreateShotA2(x,y,0,c*10,0.1,0,PELLET_YELLOW,25);
		ObjMove_AddPatternA2(shot,30,0,NO_CHANGE,0.01,0,1);
	 	}
	 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;

	}
