
#ScriptVersion[3]
#Title["Spell6V"]
#Text["Spell6V"]
#Background["./Background.txt"]
#BGM[DEFAULT]
#Player[DEFAULT]

#include"./Functions/Index.txt"
#include"script/Letty/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();
@Initialize
{LoadTexture(GCSD~"shot/ZUNShot.png");
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
	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(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
}

//----------------------------------------------------
task mainTask{
	wait(60);
	maple(1);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss,10,0);
	PlaySound("cardcall",100,0);
	Cutin("Medi","\"Mystical Blizzard\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	TCloud;
	}
//----------------------------------------------------

task TCloud{
	maple(3);
	wait(120);
	PlaySound("piss",100,0);
	ascent(i in 0..4){
	let obj = CreateShotOA1(objBoss,0,0,SMALL_BLUE,0);
	ObjShot_SetSpellResist(obj,true);
	Manage(obj,i*90,2.5,60,0);
	}
	wait(600);
	maple(3);
	wait(120);
	PlaySound("piss",100,0);
	ObjEnemy_SetDamageRate(objBoss,20,0);
	ascent(i in 0..4){
	let obj = CreateShotOA1(objBoss,0,0,SMALL_BLUE,0);
	ObjShot_SetSpellResist(obj,true);
	Manage(obj,i*90+15,-2.5,100,1);
	}
	wait(600);
	maple(3);
	wait(120);
	PlaySound("piss",100,0);
	ObjEnemy_SetDamageRate(objBoss,30,0);
	ascent(i in 0..4){
	let obj = CreateShotOA1(objBoss,0,0,SMALL_BLUE,0);
	ObjShot_SetSpellResist(obj,true);
	Manage(obj,i*90+30,2.4,140,2);
	}
	wait(600);
	maple(3);
	wait(120);
	PlaySound("piss",100,0);
	ObjEnemy_SetDamageRate(objBoss,40,0);
	ascent(i in 0..4){
	let obj = CreateShotOA1(objBoss,0,0,SMALL_BLUE,0);
	ObjShot_SetSpellResist(obj,true);
	Manage(obj,i*90+45,-2.3,180,3);
	}
	wait(300);
	ObjEnemy_SetDamageRate(objBoss,100,0);
	}

task Manage(obj,dir,spd,maxr,type){
	let r = 0;
	let c = 0;
	let t = 0;
	ObjRender_SetAlpha(obj,0);
	FakeBullet;
	Bullet;
	task Bullet{
	wait(60);
	while(!Obj_IsDeleted(obj)){
	 if(type==0){
	 PlaySound("Shot1",95,0);
	 t+=spd*5;
	 ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),3,dir+t,-0.1,1.5,PELLET_BLUE,12);
	 wait(4);
	 }
	 if(type==1){
	 PlaySound("Shot1",95,0);
	 t+=rand(0,90);
	 ascent(i in 0..2){
	 ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),3,dir+t+i*180,-0.1,1.5,SMALL_BLUE,12);
	 }
	 wait(8);
	 }
	 if(type==2){
	 PlaySound("Shot5",95,0);
	 t+=rand(0,90);
	 ascent(i in 0..3){
	 CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,dir+t+i*120,0.03,1.5,LARGE_BLUE,12);
	 }
	 wait(32);
	 }
	 if(type==3){
	 PlaySound("BulletWave",95,0);
	 t+=rand(0,110);
	 ascent(i in 0..3){
	 ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,dir+t+i*120,0.03,1.5,BUBBLE_BLUE,12);
	 }
	 wait(60);
	 }
	}
	}
	while(!Obj_IsDeleted(obj)){
	if(r<maxr){r++;}
	ObjMove_SetPosition(obj,GetEnemyX+r*cos(dir+c),GetEnemyY+r*sin(dir+c));
	c+=spd;
	yield;
	}
	 task FakeBullet{
	 let shot;
	 while(!Obj_IsDeleted(obj)){
	 shot = CreateShotOA1(obj,0,0,LARGE_BLUE,30);
	 ObjShot_SetDeleteFrame(shot,0);
	 wait(2);
	 }
	 }
	}
//----------------------------------------------------

task TRender
{
	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){
		ObjRender_SetAngleY(objBoss,180);
		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,0);}
		else{ObjRender_SetAngleY(objBoss,180);}
		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(5);
		}
		TExplosionA(GetEnemyX,GetEnemyY,2,0.6);
		Obj_Delete(objBoss);
		SetAutoDeleteObject(true);
		//PlaySound("enemoff",100,0);
		StopSlow(TARGET_ALL);
		loop(60){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
		}
		CloseScript(GetOwnScriptID());
		return;
	}
