#東方弾幕風[Single]
#ScriptVersion[3]
#Title["\"Desire Cycle\""]
#Text["Spell7"]
#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 s = 1;
let color = [119,121,123,124,126,129,131];
let orbcolor = [315,316,319,320,317,318,321];
@Initialize
{
LoadTexture(GCSD~"shot/ZUNShot.png");
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjMove_SetDestAtWeight(objBoss,stgx,180,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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(90);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(50000);
	}
}

//----------------------------------------------------
task mainTask{
	SetShotAutoDeleteClip(32,32,32,32);
	wingset(2);
	wait(60);
	set(60);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss,10,0);
	PlaySound("cardcall",100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Miko","\"Desire Cycle\"",15);
	wait(60);
	TBall;
	}
//----------------------------------------------------

task TBall{
	set(6000);
	wait(60);
	let obj;
	let shot;
	ascent(n in 0..100){
	if(Obj_IsDeleted(objBoss)){break;}
	Ball1(n);
	if(n>6){
	if(n==7){
	TEffect;
	}
	ObjEnemy_SetDamageRate(objBoss,20,0);
	Ball2(n);
	}
	if(n>13){
	ObjEnemy_SetDamageRate(objBoss,30,0);
	if(n==14){TEffect;}
	Ball3(n);
	}
	if(n>20){
	ObjEnemy_SetDamageRate(objBoss,50,0);
	if(n==21){TEffect;}
	Ball4(n);
	}
	wait(179);
	s = -s;
	yield;
	}
		task Ball1(n){
			ascent(i in 0..8){
			if(Obj_IsDeleted(objBoss)){break;}
			PlaySound("Eff2",90,0);
			obj = LargeBullet(GetEnemyX,GetEnemyY-70,1,i*45+n*97,0.02,4,243+n%7,20);
			shot = ShotA1(GetEnemyX,GetEnemyY-70,0,0,orbcolor[n%7],15);
			ObjShot_SetDeleteFrame(shot,0);
			Manage(obj,1.35*s,n%7,0);
			wait(8);
			}
		}
		task Ball2(n){
			wait(56);
			ascent(i in 0..5){
			if(Obj_IsDeleted(objBoss)){break;}
			PlaySound("Eff2",90,0);
			obj = LargeBullet(GetEnemyX,GetEnemyY-70,1,i*72+n*133,0.02,4,243+n%7,20);
				shot = ShotA1(GetEnemyX,GetEnemyY-70,0,0,orbcolor[n%7],15);
			ObjShot_SetDeleteFrame(shot,0);
			Manage(obj,0.9*s*-1,n%7,0);
			wait(8);
			}
		}
		task Ball3(n){
			wait(96);
			ascent(i in 0..4){
			if(Obj_IsDeleted(objBoss)){break;}
			PlaySound("Eff2",90,0);
			obj = LargeBullet(GetEnemyX,GetEnemyY-70,1,i*90+n*153,0.02,4,243+n%7,20);
			shot = ShotA1(GetEnemyX,GetEnemyY-70,0,0,orbcolor[n%7],15);
			ObjShot_SetDeleteFrame(shot,0);
			Manage(obj,0.45*s*1,n%7,0);
			obj = LargeBullet(GetEnemyX,GetEnemyY-70,1,i*90+n*153,0.02,4,243+n%7,20);
			Manage(obj,0.45*s*-1,n%7,0);
			wait(8);
			}
		}
		task Ball4(n){
			wait(120);
			ascent(i in 0..6){
			if(Obj_IsDeleted(objBoss)){break;}
			PlaySound("Eff2",90,0);
			obj = LargeBullet(GetEnemyX,GetEnemyY-70,1,i*30+n*17,0.02,4,243+n%7,20);
			shot = ShotA1(GetEnemyX,GetEnemyY-70,0,0,orbcolor[n%7],15);
			ObjShot_SetDeleteFrame(shot,0);
			Manage(obj,0,n%7,0);
			wait(8);
			}
		}
	}
	
task Manage(obj,v,n,time){
	let dir = 0;
	let c = 0;
	Angular(v);
	while(!Obj_IsDeleted(obj)){
	let x = ObjMove_GetX(obj);
	let y = ObjMove_GetY(obj);
	c++;
	dir = getAtanAngle(obj,objBoss);
	if(c%70==time && c>70 && c<=240){
	PlaySound("BulletWave",95,0);
	ascent(i in 0..8){
	ascent(j in 0..3){
	ShotA2(x,y,0,i*72+dir+j*2,0.02,1,color[n],18);
	}
	}
	}
	yield;
	}
	task Angular(v){
	ObjMove_SetAngularVelocity(obj,v);
	wait(160);
	ObjMove_SetAngularVelocity(obj,0);
	}
	}
	
task TEffect{
	let r = 150;
	let dir;
	PlaySound("Charge2",100,0);
	ascent(n in 0..20){
	ascent(i in 0..9){
	dir = i*40+n*17;
	let shot = ShotA1(GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),0,0,48+n%15,20);
	ObjShot_SetDeleteFrame(shot,0);
	dir = i*40-n*17;
	shot = ShotA1(GetEnemyX+r*cos(dir),GetEnemyY+r*sin(dir),0,0,48+n%15,20);
	ObjShot_SetDeleteFrame(shot,0);
	}
	r -= 7.5;
	wait(2);
	}
	}
	

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

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;}
		StartSlow(TARGET_ALL,30);
		SetAutoDeleteObject(true);
		loop(10){
		TExplosionA(GetEnemyX+rand(-50,50),GetEnemyY+rand(-50,50),10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		wait(10);
		}
		TExplosionA(GetEnemyX,GetEnemyY,1.1,0.6);
		Obj_Delete(objBoss);
		SetAutoDeleteObject(true);
		PlaySound("enemoff",100,0);
		StopSlow(TARGET_ALL);
		loop(240){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
		}
		CloseScript(GetOwnScriptID());
		return;
	}
