let ang = 0;
let idle = true;
let atk = false;
	
task SetBoss(tspell){
	if(GetCommonData("ItemStart",0)==0){StartItemScript(GCSD ~"Item.txt");}
	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);
	LoadTexture(GCSD~"Shot/ZUNShot.png");
	LoadTexture(GCSD~"system/img/front01.png");
	LoadTexture(GCSD~"system/img/front00.png");
	LoadTexture(GCSD~"img/Dot.png");
	LoadTexture(GCSD~"img/Cutin.png");
	LoadTexture(GCSD~"img/cdbg05a00.png");
	LoadTexture(GCSD~"img/cdbg05b00.png");
	LoadTexture(GCSD~"img/stage01b.png");
	LoadTexture(GCSD~"img/stg3bg4.png");
	LoadTexture(GCSD~"img/stage03e.png");
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	mainTask;
	TRender;
	TEnd;
	DrawAll;
	if(tspell==true){
	SetCommonData("Score",40);
	//BombShield;
	}
	else{
	//SetFogParam(1250,1300, 0, 0, 0);
	}
	SetAutoDeleteObject(true);
	SetAutoDeleteObject(false);
	}
	
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){
		Set2DCameraRatioX(1);
		Set2DCameraRatioY(1);
		let dis = 0;
		DeleteCommonData("End");
		SetCommonData("End",type);
		SetCommonData("BossX",GetEnemyX);
		SetCommonData("BossY",GetEnemyY);
		let px = GetEnemyX;
		let py = GetEnemyY;
		Obj_Delete(objBoss);
			if(type==1){
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
			{
				SetCommonData("Piece",GetCommonData("Piece",0)+1);
				if(GetCommonData("Piece",0)==4){
				SetPlayerLife(GetPlayerLife+1);
				SetCommonData("Piece",0);
				PlaySound("extend",100,0);
				}
			AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));
			}
			else{
			PlaySound("Shot2",95,0);
			}
			}
		}
		
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 ClearBullet2(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 && ObjShot_IsSpellResist(arr[i]) == false){
			effbreak(arr[i],0,10);
			Obj_Delete(arr[i]);
			}
		}
		dis += speed;
		yield;
			}
}

task DrawBoss{
	let imgMiko = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	let px = 64;
	let py = 64;
	let c = 0;
	ObjPrim_SetTexture(objBoss, imgMiko);
	ObjRender_SetScaleXYZ(objBoss,1.1,1.1,1);
	while(!Obj_IsDeleted(objBoss))
	{
		let angle = ObjMove_GetAngle(objBoss);
		let speed = ObjMove_GetSpeed(objBoss);
		if(speed>0 && idle == true){
		f=0;
		MarisaTail(objBoss,0,192,64,256,ang);
		}
		if(speed <= 0 && idle == true && atk == false){
		ObjRender_SetAngleY(objBoss,ang);
		if(f<10){SetImageAndTail(objBoss, px*0, 0, px*1, 80,ang);
		}
		if(f>=10 && f<20){SetImageAndTail(objBoss, px*1, 0, px*2, 80,ang);
		}
		if(f>=20 && f<30){SetImageAndTail(objBoss, px*2, 0, px*3, 80,ang);
		}
		if(f>=30){SetImageAndTail(objBoss, px*3, 0, px*4, 80,ang);
		}
		if(f>=40){f=0;}
		f++;
		c++;
		ObjRender_SetY(objBoss,ObjMove_GetY(objBoss)+0.1*sin(c*5));
		t = 0;
		}
		else if(cos(angle) > 0 || cos(angle) < 0 && t == 0){
		idle = false;
		if(cos(angle)>0){ang=180;}
		else{ang=0;}
		manageSprite(ang);
		t = 1;
		}
		ObjSprite2D_SetDestCenter(objBoss);
		yield;
	}
}

task manageSprite(ang){ // Manage movement sprite
		let f = 0;
		let px = 64;
		idle = false;
			ObjRender_SetAngleY(objBoss,ang);
			while(f<=25 && idle == false  && atk == false){
			if(f<4){SetImageAndTail(objBoss, px*0, 160, px*1, 240,ang);}
			if(f>=4 && f<8){SetImageAndTail(objBoss, px*1, 160, px*2, 240,ang);}
			if(f>=8 && f<12){SetImageAndTail(objBoss, px*2, 160, px*3, 240,ang);}
			if(f>=12 && f<16){SetImageAndTail(objBoss, px*3, 160, px*4, 240,ang);}
			if(f>=16 && f<20){SetImageAndTail(objBoss, px*4, 160, px*5, 240,ang);}
			if(f>=20 && f<24){SetImageAndTail(objBoss, px*5, 160, px*6, 240,ang);}
			f+=1;
			yield;
			}
			while(ObjMove_GetSpeed(objBoss)>0.5){
			SetImageAndTail(objBoss, px*6, 160, px*7, 240,ang);
			yield;
			}
			while(f>=0 && idle == false  && atk == false){
			if(f<4){SetImageAndTail(objBoss, px*0, 160, px*1, 240,ang);}
			if(f>=4 && f<8){SetImageAndTail(objBoss, px*1, 160, px*2, 240,ang);}
			if(f>=8 && f<12){SetImageAndTail(objBoss, px*2, 160, px*3, 240,ang);}
			if(f>=12 && f<16){SetImageAndTail(objBoss, px*3, 160, px*4, 240,ang);}
			if(f>=16 && f<20){SetImageAndTail(objBoss, px*4, 160, px*5, 240,ang);}
			if(f>=20 && f<24){SetImageAndTail(objBoss, px*5, 160, px*6, 240,ang);}
			f-=1;
			yield;
			}
			idle = true;
	}
	
task ReverseAng{
	let px = 64;
	idle = false;
	atk = true;
	ObjRender_SetAngleY(objBoss,ang);
	loop(6){SetImageAndTail(objBoss, px*0, 80, px*1, 160,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*1, 80, px*2, 160,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*2, 80, px*3, 160,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*3, 80, px*4, 160,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*4, 80, px*5, 160,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*5, 80, px*6, 160,ang);yield;}
	ang += 180;
	ObjRender_SetAngleY(objBoss,ang);
	loop(6){SetImageAndTail(objBoss, px*1, 160, px*2, 240,ang);yield;}
	loop(6){SetImageAndTail(objBoss, px*0, 160, px*1, 240,ang);yield;}
	idle = true;
	atk = false;
	}