// For Hec's Yukari
let ang = 0;
#include"./Functions/EventList.Txt"
task SetBoss(name,tspell){
	LoadAll;
	SetShotAutoDeleteClip(64,64,64,64);
	SetStgFrame(0, 0, 640, 480, 0, 200);
	SetPlayerClip( 0, 0, 640, 450 );
	//SetCommonData("Difficulty","Normal"); // test only
	//SetCommonData("Difficulty","Hard"); // test only
	//SetCommonData("Difficulty","Lunatic"); // test only
	if(GetCommonData("ItemStart",0)==0){StartItemScript(GCSD ~"Item.txt");}
	if(name=="Yukari"){SetCommonData("BossName","Yukari Yakumo");}
	SetCommonData("IsSpell",tspell);
	SetCommonData("Bomb",0);
	SetCommonData("Life",0);
	SetCommonData("Fight",1);
	SetCommonData("Score",0);
	SetCommonData("shot",0);
	SetCommonData("bomb",0);
	SetCommonData("Effect",2);
	SetShotAutoDeleteClip(64,64,64,64);
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	DrawBoss(objBoss,name);
	BulletHitEffect;
	OccultHitEffect;
	TEnd;

	DrawAll;
	if(tspell==true){
	SetCommonData("Score",40);
	BombShield;
	}
	SetAutoDeleteObject(true);
	yield;
	SetAutoDeleteObject(false);
	}
	
task BulletHitEffect
{
	let startlife = ObjEnemy_GetInfo(objBoss, INFO_LIFE);
	let countshot = 0;
	while(!Obj_IsDeleted(objBoss)){
		if(ObjEnemy_GetInfo(objBoss, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE)>startlife/5)
		{
				if(countshot%2==0 && GetCommonData("Occult",0)==0)
				{
					PlaySound("hitlife",100,0);
				}
				countshot++;
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_SHOT_HIT_COUNT) > 0 && ObjEnemy_GetInfo(objBoss, INFO_LIFE)<startlife/5)
		{
				if(countshot%2==0 && GetCommonData("Occult",0)==0)
				{
					PlaySound("hitlesslife",100,0);
				}
				countshot++;
		}
		wait( 3 );
	}
}

task OccultHitEffect
{
	let countshot = 0;
		while(!Obj_IsDeleted(objBoss)){
			if(ObjEnemy_GetInfo(objBoss, INFO_SHOT_HIT_COUNT) > 0)
			{
				if(countshot%2==0 && GetCommonData("Occult",0)==1)
				{
					PlaySound("Shot5",70,0);
				}
				countshot++;
			}
			wait( 3 );
			}
}
	
task BombShield{
		while(!Obj_IsDeleted(objBoss)){
			if(IsPlayerSpellActive || GetCommonData("Occult",0)==1){ObjEnemy_SetDamageRate(objBoss, 0,0);}
			else{ObjEnemy_SetDamageRate(objBoss, GetCommonData("shot",0),GetCommonData("bomb",0));}
			yield;
		}
	}

task EndBoss(let type){
		let dis = 0;
		SetCommonData("ItemEnd",type);
		SetCommonData("BossX",GetEnemyX);
		SetCommonData("BossY",GetEnemyY);
		NotifyEventAll(EV_FAM_DIE,objBoss);
			if(type==1){
			if( ObjEnemyBossScene_GetInfo( objScene, INFO_PLAYER_SHOOTDOWN_COUNT) == 0 && ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0 ){ AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));}
			if( ObjEnemyBossScene_GetInfo( objScene, INFO_PLAYER_SHOOTDOWN_COUNT) != 0 || ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0 ){ NotifyEventAll( EV_SPELL_FAILED, 0); }	
			if( ObjEnemyBossScene_GetInfo( objScene, INFO_TIMER )  == 0 ){ NotifyEventAll( EV_SPELL_FAILED, 0); }		
			}
			if(type==2){
		 	if( ObjEnemyBossScene_GetInfo( objScene, INFO_PLAYER_SHOOTDOWN_COUNT) == 0 && ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0 ){ AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));}	
		 	if( ObjEnemyBossScene_GetInfo( objScene, INFO_PLAYER_SHOOTDOWN_COUNT) != 0 || ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0  ){ NotifyEventAll( EV_SPELL_FAILED, 0); }		
			}
			//-----------------Delete Sound-------------------------//
			DeleteAllSound;
			Obj_Delete( objBoss );
	}
	
task DeleteAllSound
{
			ascent(i in 0..length(soundarr))
			{
				Obj_Delete(soundarr[i]);
			}
}
		
		
task ClearBullet(time,speed){
		let dis = 0;
		let px = GetEnemyX;
		let py = GetEnemyY;
		loop(time){
		let arr = GetShotIdInCircleA2(stgx,stgy,1000,TARGET_ENEMY);
		ascent(i in 0..length(arr)){
			if(!Obj_IsDeleted(arr[i]) && GetDistance(ObjMove_GetX(arr[i]),ObjMove_GetY(arr[i]),px,py) <dis && IsPlayerSpellActive == false){
			effbreak(arr[i],0,10);
			Obj_Delete(arr[i]);
			}
		}
		dis += speed;
		yield;
			}
}

task DrawBoss(obj,name)
{
	let img;
	img = GetCurrentScriptDirectory~"img/boss23.png";
	let f = 0;
	let t = 0;
	let px = 80;
	let py = 96;
	let c = 0;
	ObjPrim_SetTexture(obj, img);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetScaleXYZ(obj,1.1,1.1,1.1);
	while(!Obj_IsDeleted(obj))
	{
		let angle = ObjMove_GetAngle(obj);
		let speed = ObjMove_GetSpeed(obj);
		if(speed>0 && idle == true){SetImageAndTail(obj, 0, 192, px, 192+py);}
		if(speed <= 0 && idle == true && atk == false)
		{
			SetImageAndTail(obj,0, 192, px, 192+py);
			f++;
			c++;
			t = 0;
		}
		else if(cos(angle) > 0 || cos(angle) < 0 && t==0)
		{
			idle = false;
			manageSprite(obj,cos(angle),name);
			t = 1;
		}
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
}

task manageSprite(obj,ang,name){ // Manage movement sprite
		let f = 0;
		let px = 80; 
		let py = 96;
		let plusy = 288;
		if(ang<0){plusy = 384;}
		idle = false;  
		while(f<18 && idle == false  && atk == false){
		if(f<6){				SetImageAndTail(obj, px*0, py*0+plusy, px*1, py*1+plusy);}
		if(f>=6 && f<12){SetImageAndTail(obj,px*1, py*0+plusy, px*2, py*1+plusy);}
		if(f>=12 && f<18){SetImageAndTail(obj,px*2, py*0+plusy, px*3, py*1+plusy);}
		f+=1;
		yield;
		}
		while(ObjMove_GetSpeed(obj)>0.5){
		SetImageAndTail(obj, px*2, py*0+plusy, px*3, py*1+plusy);
		yield;
		}
		while(f>=0 && idle == false  && atk == false){
		if(f<6){				SetImageAndTail(obj, px*0, py*0+plusy, px*1, py*1+plusy);}
		if(f>=6 && f<12){SetImageAndTail(obj,px*1, py*0+plusy, px*2, py*1+plusy);}
		if(f>=12 && f<18){SetImageAndTail(obj,px*2, py*0+plusy, px*3, py*1+plusy);}
		f-=1;
		yield;
		}
		idle = true;
	}
	
task attack(delay)
{
	let f2 = 0;
	let px = 80;
	let py = 96;
	atk = true;
	idle = false;
	loop(8){SetImageAndTail(objBoss, px, 192, px*2, 192+py);yield;}
	loop(8+delay){SetImageAndTail(objBoss,px*2, 192, px*3, 192+py);yield;}
	loop(8){SetImageAndTail(objBoss,px, 192, px*2, 192+py);yield;}
	atk = false;
	idle = true;
}
	
task LoadAll{

}