let ang = 0;
#include"./Functions/EventList.Txt"
task SetBoss(name,tspell){
	SetShotAutoDeleteClip(64,64,64,64);
	SetStgFrame(0, 0, 640, 480, 0, 200);
	SetPlayerClip( 0, 0, 640, 450 );
	if(GetCommonData("ItemStart",0)==0){StartItemScript(GCSD ~"Item.txt");}
	SetCommonData("BossName","Fujiwara no Mokou");
	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);
	mainTask;
	DrawBoss(objBoss,name);
	BulletHitEffect;
	TEnd;
	DrawAll;
	if(tspell==true){
	SetCommonData("Score",40);
	}
	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 BombShield{
		while(!Obj_IsDeleted(objBoss)){
			if(IsPlayerSpellActive){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 ){ wait( 190 ); AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));}
			if( ObjEnemyBossScene_GetInfo( objScene, INFO_PLAYER_SHOOTDOWN_COUNT) != 0 || ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0 ){ wait( 190 ); 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/stg8enm2.png";
	let f = 0;
	let t = 0;
	let px = 64;
	let py = 82;
	let c = 0;
		ObjPrim_SetTexture(obj, img);
		ObjRender_SetAlpha(obj,255);
		ObjRender_SetScaleXYZ(obj,1.2,1.2,0);
	while(!Obj_IsDeleted(obj))
	{
		let angle = ObjMove_GetAngle(obj);
		let speed = ObjMove_GetSpeed(obj);
		if(speed>0 && idle == true){}
		if(speed <= 1 && idle == true && atk == false){
		if(f<12){		  SetImageAndTail(obj, px*0, py*0, px*1, py*1);}
		if(f>=12 && f<24) {SetImageAndTail(obj,px*1, py*0, px*2, py*1);}
		if(f>=24 && f<36){SetImageAndTail(obj,px*2, py*0, px*3, py*1);}
		if(f>=36 && f<48){SetImageAndTail(obj,px*3, py*0, px*4, py*1);}
		if(f>=48){f=0;}
		f++;
		c++;
		//ObjRender_SetY(obj,ObjMove_GetY(obj)+0.1*sin(c*5));
		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 mx = 0;
		let px = 64; 
		let py = 82;
		let plusy = 0;
		idle = false;  
		//if(ang>=0){plusy+=py;}
		while(ang>0 && f<16 && idle == false  && atk == false){
		if(f<16){		SetImageAndTail(obj,px*0, py*1, px*1, py*2);}
		
		f+=1;
		yield;
		}
		while(ang<0 && f<16 && idle == false  && atk == false){
		if(f<16){		SetImageAndTail(obj,px*0, py*2, px*1, py*3);}
		
		f+=1;
		yield;
		}
		idle = true;
	}
	
task attack(delay){ // Manage attack pose
	let type = 1;
	let f2 = 0;
	let px = 64;
	let py = 81;
	atk = true;
	idle = false;
		if(type==1)
		{
			loop(6+delay){SetImageAndTail(objBoss,px*0, py*3, px*1, py*4);yield;}
			atk = false;
			idle = true;
		}
	}
	