#東方弾幕風[Single]
#ScriptVersion[3]
#Title["Discernment\"Twelve Articles\""]
#Text["Spell2"]
#Background["./Background.txt"]
#BGM[DEFAULT]
#Player[DEFAULT]

#include"./Functions/Index.txt"
#include"script/Miko/Shot/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;
let idle = true;
let objScene=GetEnemyBossSceneObjectID();
let s2 = 1;
let count = 0;
let color = ["PURPLE","GREEN","ORANGE","RED","BLUE","AQUA","GRAY"];
@Initialize
{
LoadTexture(GCSD~"shot/ZUNShot.png");
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	mainTask;
	TRender;
	TEnd;
	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(3000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(50);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
}

//----------------------------------------------------
task mainTask{
	ObjEnemy_SetDamageRate(objBoss,50,20);
	PlaySound("cardcall",100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Miko","Discernment\"Tenth Articles\"",15);
	wait(60);
	TStart;
	}
//----------------------------------------------------

task TStart{
	let dis = 0;
	let py = stgy*2;
	let c = 0;
	let px = GetEnemyX;
	let px2 = 0;
	let obj;
	let angle = 0;
	let s = 1;
	let m = 0;
	while(!Obj_IsDeleted(objBoss)){
	px = GetEnemyX;
	px2 = stgx;
	py = stgy*2;
	set(180);
	dis = stgy*2-(GetEnemyY-70);
	loop(70){
	obj = CreateShotA1(px+(px2)*sin(c*5+90),py,0,0,63+c%15,30);
	Manage(obj);
	obj = CreateShotA1(px+(px2)*sin(c*5+270),py,0,0,63+c%15,30);
	Manage(obj);
	c += 1;
	py -= dis/70;
	px2 -= stgx/70;
	yield;
	}
	if(m%2==0){
	ascent(i in 0..5){
	PlaySound("BulletWave",100,0);
	angle = getDotAngle(0,(20*s)+i*90,GetEnemyX,GetEnemyY-70);
	obj = ShotA2(GetEnemyX,GetEnemyY-70,0,angle,0.01,5,243+i,12);
	ObjMove_AddPatternA2(obj,20,NO_CHANGE,NO_CHANGE,0.15,0,7);
	Manage2(obj,1);
	angle = getDotAngle(stgx*2,(-5-20*s)+i*90,GetEnemyX,GetEnemyY-70);
	obj = ShotA2(GetEnemyX,GetEnemyY-70,0,angle,0.01,5,243+i,12);
	ObjMove_AddPatternA2(obj,20,NO_CHANGE,NO_CHANGE,0.15,0,7);
	Manage2(obj,-1);
	}
	}
	if(m%2==1){
	ascent(i in 0..6){
	PlaySound("BulletWave",100,0);
	angle = getDotAngle(0,(20*s)+i*75,GetEnemyX,GetEnemyY-70);
	obj = ShotA2(GetEnemyX,GetEnemyY-70,0,angle,0.01,5,243+i,12);
	ObjMove_AddPatternA2(obj,20,NO_CHANGE,NO_CHANGE,0.15,0,7);
	Manage2(obj,1);
	angle = getDotAngle(stgx*2,(-5-20*s)+i*75,GetEnemyX,GetEnemyY-70);
	obj = ShotA2(GetEnemyX,GetEnemyY-70,0,angle,0.01,5,243+i,12);
	ObjMove_AddPatternA2(obj,20,NO_CHANGE,NO_CHANGE,0.15,0,7);
	Manage2(obj,-1);
	wait(10);
	}
	}
	wait(300);
	TMovement(40,5,4);
	wait(300);
	DeleteBullet;
	s = -s;
	m++;
	}
	}

task DeleteBullet{
	wait(60);
	loop(100){
	count+=5;
	yield;
	}
	count = 0;
	}

//----------------------------------------------------
	task Manage(obj){
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	wait(29);
	Obj_Delete(obj);
	}
	
	task Manage2(obj,type){
	let far = 0;
	let cp = 0;
	CheckWall;
	wait(180);
	PlaySound("Laser2",95,0);
	TRainbow;
		task TRainbow{
		let c = 0;
		let co = rand(0,1);
		let dir = rand(5,13);
		let p = 0;
		dir = round(dir)*1.7;
		loop(45){
		PlaySound("Shot1",95,0);
		cp = GetDistance(GetPlayerX,GetPlayerY,ObjMove_GetX(obj)+c,ObjMove_GetY(obj));
		if(cp>30 || cp<-30){
		let shot = CreateShotA2(ObjMove_GetX(obj)+c,ObjMove_GetY(obj),0,c*dir,0.0015,0.5,33+(p+co)%15,30);
		ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,0.008,0,rand(0.7,0.8));
		Delete(shot);
		p++;
		}
		c+=10*type;
		yield;
		}
		loop(30){
		PlaySound("Shot1",95,0);
		yield;
		}
			task Delete(shot){
			let result = rand(0,6);
			result = round(result);
			while(!Obj_IsDeleted(shot)){
			far = GetDistance(ObjMove_GetX(shot),ObjMove_GetY(shot),GetEnemyX,GetEnemyY);
			if(far < count){
			effbreak(shot,color[result]);
			Obj_Delete(shot);
			}
			yield;
			}
			}
		}
		task CheckWall{
		let t = 0;
		while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj)<=0 || ObjMove_GetX(obj)>=stgx*2){
		ObjMove_SetSpeed(obj,0);
		if(t==0){
		PlaySound("Shot5",95,0);
		}
		t = 1;
		}
		yield;
		}
		}
	}

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

task TRender
{
Aura;
TWaveCircle();
	magiccircle(objBoss,1.75,true);
	let imgMiko = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	ObjPrim_SetTexture(objBoss, imgMiko);
	ObjRender_SetScaleXYZ(objBoss,1.05,1.05,1);
	while(!Obj_IsDeleted(objBoss))
	{
		let angle = ObjMove_GetAngle(objBoss);
		let speed = ObjMove_GetSpeed(objBoss);
		if(speed == 0 && idle == true){
		if(f<8){ObjSprite2D_SetSourceRect(objBoss, 0, 0, 64, 98);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(objBoss, 64, 0, 128, 98);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(objBoss, 128, 0, 192, 98);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(objBoss, 192, 0, 256, 98);}
		if(f>=32){f=0;}
		f++;
		t = 0;
		}
		else if(cos(angle) > 0 || cos(angle) < 0 && t == 0 && idle ==true){
		if(cos(angle)<0){point = 1;}
		else{point = -1;}
		manageSprite;
		t = 1;
		}
		ObjSprite2D_SetDestCenter(objBoss);
		yield;
	}
}

//----------------------------------------------------
task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		let arr = GetShotIdInCircleA2(stgx,stgy,600,TARGET_ENEMY);
		Obj_Delete(objBoss);
		SetAutoDeleteObject(true);
		yield;
		SetAutoDeleteObject(false);
		ascent(i in 0..length(arr)){
		effbreak(arr[i],"PURPLE");
		Obj_Delete(arr[i]);
		}
		wait(30);
		SetAutoDeleteObject(true);
		CloseScript(GetOwnScriptID());
		return;
	}
