let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let FAIRY_SMALL_A1 = 1.1;
let FAIRY_SMALL_A2 = 1.2;
let FAIRY_SMALL_A3 = 1.3;
let FAIRY_SMALL_A4 = 1.4;

let FAIRY_MEDIUM_A1 = 2.1;
let FAIRY_MEDIUM_A2 = 2.2;
let FAIRY_MEDIUM_A3 = 2.3;

let FAIRY_LARGE_A1 = 3.1;
let FAIRY_LARGE_A2 = 3.2;
let FAIRY_LARGE_B1 = 3.3;

let KOSUKE_LEAVE = 99.1;

let objEnemy;

#include "../tricky_system/Event_Const.dnh"
#include "../lib/includes/EnemyFunctions.dnh"
#include "../lib/includes/EffectFunctions.dnh"
#include "../lib/includes/Function_Audio.dnh"
#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "../lib/animation/AnimLib.h"
#include "./../lib/includes/ItemFunctions.dnh"

@MainLoop {
	yield;
}

@Event {
	alternative(GetEventType)
	case(EV_SPAWN_ENEMY) {
		let eventParam = GetEventArgument(0);
		let type = eventParam[0];
		let enemyParam = eventParam[1..length(eventParam)];
		
		alternative(type)
		case(FAIRY_SMALL_A1) { CreateFairy_SmallA1(enemyParam); }
		case(FAIRY_SMALL_A2) { CreateFairy_SmallA2(enemyParam); }
		case(FAIRY_SMALL_A3) { CreateFairy_SmallA3(enemyParam); }
		case(FAIRY_SMALL_A4) { CreateFairy_SmallA4(enemyParam); }
		case(FAIRY_MEDIUM_A1) { CreateFairy_MediumA1(enemyParam); }
		case(FAIRY_MEDIUM_A2) { CreateFairy_MediumA2(enemyParam); }
		case(FAIRY_MEDIUM_A3) { CreateFairy_MediumA3(enemyParam); }
		
		case(KOSUKE_LEAVE) { CreateKosuke_Leave(enemyParam); }
	}
}

@Initialize{
	LoadShotdataBasic;
}

task CreateFairy_SmallA1(enemyParam) {
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	ObjEnemy_SetLife(objEnemy, 12);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,12,4);
	Const;
	Movement;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-20 && GetY<GetStgFrameHeight+20);
			yield;
		}
	}
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+175,5,4);
		wait(60);
		ObjMove_SetDestAtWeight(objEnemy, GetX+rand(-40, 40), GetY+rand(5, -10), 10, 0.5);
		wait(120);
		ObjMove_SetDestAtWeight(objEnemy, GetX+rand(-20, 20), GetY-200, 10, 2);
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			
			if(randInt(0, 2)==0){
				CreateItems02(1, ITEM_POINT_S, GetX, GetY, 0, -50, 0, 0, 10000);
			}
		}
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_SmallA2(enemyParam) {
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	ObjEnemy_SetLife(objEnemy, 12);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let spd = 0;
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,12,4);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-20 && GetY<GetStgFrameHeight+20);
			yield;
		}
	}
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+175,5,4);
		wait(60);
		ObjMove_SetDestAtWeight(objEnemy, GetX+rand(-40, 40), GetY+rand(5, -10), 10, 0.5);
		wait(120);
		ObjMove_SetDestAtWeight(objEnemy, GetX+rand(-20, 20), GetY-200, 10, 2);
	}
	
	task Fire {
		wait(45);
		while(!Obj_IsDeleted(objEnemy)){
			PlaySoundEffect("shot2", 90, 384/2);
			spd = rand(0.9, 1.2);
			angle = rand(90-2, 90+2);
			CreateDNHShotA1(GetX, GetY, spd, angle, DOT[GRAY], 10);
			wait(60);
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}

			if(randInt(0, 2)==0){
				CreateItems02(1, ITEM_POINT_S, GetX, GetY, 0, -50, 0, 0, 10000);
			}
		}
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_SmallA3(enemyParam) {

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
	ObjPrim_SetTexture(obj2, familiar);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, 1);
	
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	let circleangle = 0;
	
	ObjEnemy_SetLife(objEnemy, 150);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let itemx = 0;
	let itemy = 0;
	
	let shotgraphic = [CIRCLE_S[RED], CIRCLE_S[BLUE], CIRCLE_S[GREEN]][enemyParam[3]];
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,12,4);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			ObjRender_SetAngleZ(obj2, circleangle);
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			ObjRender_SetPosition(obj2, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-40 && GetY<GetStgFrameHeight+20);
			circleangle+=2;
			yield;
		}
	}
	
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+150,5,4);
		wait(60);
		if(ObjMove_GetX(objEnemy)>GetCenterX){ ObjMove_SetAngle(objEnemy, 180); }
		else if(ObjMove_GetX(objEnemy)<GetCenterX){ ObjMove_SetAngle(objEnemy, 0); }
		else{ ObjMove_SetAngle(objEnemy, 270); }
		
		ObjMove_SetAcceleration(objEnemy, 2/120);
		ObjMove_SetMaxSpeed(objEnemy, 2);
	}
	
	task Fire {
		wait(45);
		if(!Obj_IsDeleted(objEnemy)){
			PlaySoundEffect("shot3", 100, GetX);
			angle = GetAngleToPlayer(objEnemy);
			loop(7){
				ascent(i in -2..3){
					ascent(j in 0..4){
						CreateDNHShotA2(GetX, GetY, 2+(j*1.5), (i*1.95)+angle, -(2+(j*1.5))/60, 2+(j*1.45)/2, shotgraphic, 5);
					}
				}
				angle+=360/7;
			}
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			CreateItems02(3, ITEM_POINT_S, GetX, GetY, 0, -50, 25, 25, 10000);
		}
		Obj_Delete(obj2);
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_SmallA4(enemyParam) {

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
	ObjPrim_SetTexture(obj2, familiar);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, 1);
	
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	let circleangle = 0;
	
	ObjEnemy_SetLife(objEnemy, 125);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let itemx = 0;
	let itemy = 0;
	
	let shotgraphic = [CIRCLE_S[RED], CIRCLE_S[BLUE], CIRCLE_S[GREEN]][enemyParam[3]];
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,12,4);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			ObjRender_SetAngleZ(obj2, circleangle);
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			ObjRender_SetPosition(obj2, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-40 && GetY<GetStgFrameHeight+20);
			circleangle+=2;
			yield;
		}
	}
	
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+150,5,4);
		wait(60);
		if(ObjMove_GetX(objEnemy)>GetCenterX){ ObjMove_SetAngle(objEnemy, 180); }
		else if(ObjMove_GetX(objEnemy)<GetCenterX){ ObjMove_SetAngle(objEnemy, 0); }
		else{ ObjMove_SetAngle(objEnemy, 270); }
		
		ObjMove_SetAcceleration(objEnemy, 2/120);
		ObjMove_SetMaxSpeed(objEnemy, 2);
	}
	
	task Fire {
		wait(45);
		if(!Obj_IsDeleted(objEnemy)){
			PlaySoundEffect("laser", 100, GetX);
			angle = rand(0, 360);
			loop(14){
				let obj = CreateLooseLaserA1(GetX, GetY, 3.25, angle, 250, 10, shotgraphic, 0);
				ObjCrLaser_SetTipDecrement(obj, 0.25);
				angle+=360/14;
			}
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			CreateItems02(4, ITEM_POINT_S, GetX, GetY, 0, -50, 25, 25, 10000);
		}
		Obj_Delete(obj2);
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_MediumA1(enemyParam) {

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
	ObjPrim_SetTexture(obj2, familiar);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, 1);
	
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	let dir = enemyParam[2];
	
	let dirincrement = 0;
	
	let circleangle = 0;
	
	ObjEnemy_SetLife(objEnemy, 125);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let itemx = 0;
	let itemy = 0;
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,17,6);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			ObjRender_SetAngleZ(obj2, circleangle);
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			ObjRender_SetPosition(obj2, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-40 && GetY<GetStgFrameHeight+20);
			circleangle+=2;
			dirincrement+=dir;
			yield;
		}
	}
	
	task Movement {
		ObjMove_SetSpeed(objEnemy, 1.5);
		if(ObjMove_GetX(objEnemy)>=GetCenterX){ ObjMove_SetAngle(objEnemy, 170); }
		else{ ObjMove_SetAngle(objEnemy, 10); }
		while(!Obj_IsDeleted(objEnemy)){
			ObjMove_SetAngle(objEnemy, ObjMove_GetAngle(objEnemy)+dir);
			yield;
		}
	}
	
	task Fire {
		wait(5);
		while(!Obj_IsDeleted(objEnemy)){
			PlaySoundEffect("shot2", 75, GetX);
			CreateDNHShotA2(GetX, GetY, 0, 90+(dirincrement*0.5), 0.01, 1.25, RICE[GREEN], 10);
			wait(17);
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			
			CreateItems02(3, ITEM_POINT_S, GetX, GetY, 0, -50, 25, 25, 10000);
		}
		Obj_Delete(obj2);
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_MediumA2(enemyParam) {

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
	ObjPrim_SetTexture(obj2, familiar);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, 1);
	
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	let dir = 90;
	
	let circleangle = 0;
	
	ObjEnemy_SetLife(objEnemy, enemyParam[2]);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 15, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	let speed = 0;
	
	
	let itemx = 0;
	let itemy = 0;
	
	let shotgraphic = [CIRCLE_S[RED], CIRCLE_S[GREEN], CIRCLE_S[BLUE]][enemyParam[3]];
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,17,6);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			ObjRender_SetAngleZ(obj2, circleangle);
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			ObjRender_SetPosition(obj2, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-40 && GetY<GetStgFrameHeight+20);
			circleangle+=2;
			yield;
		}
	}
	
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+150,5,4);
		wait(360);
		if(ObjMove_GetX(objEnemy)>GetCenterX){ ObjMove_SetAngle(objEnemy, 180); }
		else if(ObjMove_GetX(objEnemy)<GetCenterX){ ObjMove_SetAngle(objEnemy, 0); }
		else{ ObjMove_SetAngle(objEnemy, 270); }
		
		ObjMove_SetAcceleration(objEnemy, 2/120);
		ObjMove_SetMaxSpeed(objEnemy, 2);
	}
	
	task Fire {
		wait(45);
		while(!Obj_IsDeleted(objEnemy)){
			if(ObjMove_GetSpeed(objEnemy)<0.2){
				PlaySoundEffect("shot5", 100, GetX);
				loop(6){
					CreateDNHShotA2(GetX, GetY, 4, angle+rand(-2, 2), -(3/30), 2.25-speed+rand(0.1, -0.1), PELLET[BLUE], 10);
					CreateDNHShotA2(GetX, GetY, 4, -angle+rand(-2, 2), -(3/30), 2.25-speed+rand(0.1, -0.1), PELLET[AZURE], 10);
					angle+=360/6;
				}
			}
				speed+=0.01;
				angle+=9.45;
				wait(10);
			
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			
			CreateItems02(4, ITEM_POINT_S, GetX, GetY, 0, -50, 25, 25, 10000);
		}
		Obj_Delete(obj2);
		Obj_Delete(objEnemy);
	}
}

task CreateFairy_MediumA3(enemyParam) {

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
	ObjPrim_SetTexture(obj2, familiar);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 0.75, 0.75, 1);
	
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	let dir = enemyParam[2];
	
	let dir2 = 90;
	
	let dirincrement = 0;
	
	let circleangle = 0;
	let switch = 1;
	if(x>GetCenterX){ switch = -1; }
	
	ObjEnemy_SetLife(objEnemy, 150);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	Invuln(objEnemy, 0, 100, 100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let itemx = 0;
	let itemy = 0;
	
	let time = 0;
	
	AnimateEnemyA1(objEnemy, enemyParam[3]);
	EnemyCollision(objEnemy,17,6);
	Const;
	Movement;
	Fire;
	End;
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			ObjRender_SetAngleZ(obj2, circleangle);
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			ObjRender_SetPosition(obj2, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-40 && GetY<GetStgFrameHeight+20);
			circleangle+=2;
			dirincrement+=dir;
			yield;
		}
	}
	
	task Movement {
		ObjMove_SetSpeed(objEnemy, 1.5);
		if(ObjMove_GetX(objEnemy)>=GetCenterX){ ObjMove_SetAngle(objEnemy, 170); }
		else{ ObjMove_SetAngle(objEnemy, 10); }
		while(!Obj_IsDeleted(objEnemy)){
			ObjMove_SetAngle(objEnemy, ObjMove_GetAngle(objEnemy)+dir);
			yield;
		}
	}
	
	task Fire {
		wait(5);
		while(!Obj_IsDeleted(objEnemy)){
			//PlaySoundEffect("shot2", 75, GetX);
			loop(2){
			CreateDNHShotA2(GetX, GetY, 0, dir2, 0.01, 2, CIRCLE_S[GRAY], 10);
			CreateShotA2(GetX, GetY, 0, dir2, 0.0095, 2, DOT[GRAY], 10);
			dir2+=360/2;
			}
			dir2+=25.25*switch;
			time++;
			if(time%2==0){ PlaySoundEffect("shot2", 75, GetX); }
			wait(4);
		}
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			PlaySoundEffect("se_enep00", 80, GetX);
			if(enemyParam[3]==0){
				TExpan(GetX, GetY, 2, 15, 255, 0, 0, 100);
			}
			else if(enemyParam[3]==1){
				TExpan(GetX, GetY, 2, 15, 0, 0, 255, 100);
			}
			else if(enemyParam[3]==2){
				TExpan(GetX, GetY, 2, 15, 0, 255, 0, 100);
			}
			
			CreateItems02(3, ITEM_POINT_S, GetX, GetY, 0, -50, 25, 25, 10000);
		}
		Obj_Delete(obj2);
		Obj_Delete(objEnemy);
	}
}

task CreateKosuke_Leave(enemyParam) {
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	ObjEnemy_SetLife(objEnemy, 10000000000);
	ObjEnemy_Regist(objEnemy);
	
	ObjMove_SetPosition(objEnemy,x,y);
	
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	let angle = 0;
	
	let itemx = 0;
	let itemy = 0;
	
	EnemyCollision(objEnemy,0,0);
	
	let Youmu = GetCurrentScriptDirectory~"sprites/Youmu.png";
	ObjPrim_SetTexture(objEnemy, Youmu); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128);
	ascent(x in 0..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128);}
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*7, 128*6, 128*7+128); }
	descent(x in 2..4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*4, 128*x, 128*5, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 3);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	Const;
	Movement;
	End;
	
	
	loop(12){
		CreateItems02(1, ITEM_POINT_S, GetX+50*cos(angle), GetY+50*sin(angle), 0, -25, 0, 0, 10000);
		angle+=360/12;
	}
	CreateItems02(1, ITEM_SPELL, GetX, GetY, 0, -25, 0, 0, 69);
	
	task Const {
		while(!Obj_IsDeleted(objEnemy)) {
			GetX = ObjMove_GetX(objEnemy);
			GetY = ObjMove_GetY(objEnemy);
			GetLife = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			OnScreen = (GetX>-20 && GetX<GetStgFrameWidth+20 && GetY>-20 && GetY<GetStgFrameHeight+20);
			yield;
		}
	}
	
	task Movement {
		wait(100);
		ObjMove_SetDestAtWeight(objEnemy, ObjMove_GetX(objEnemy)+rand(-20, 20), -100, 10, 2);
	}
	
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		Obj_Delete(objEnemy);
	}
}

task AnimateEnemyA1(objEnemy, color){
	let graphic = GetCurrentScriptDirectory~"sprites/zako_yousei.png";
	let yoffset = [0, 64, 128][color%3];
	ObjPrim_SetTexture(objEnemy, graphic); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 32, 32);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 32*x, yoffset, 32*x+32, yoffset+32);}
	ascent(x in 4..8){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE, 32*x, yoffset+32, 32*x+32, yoffset+64);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND, 32, 32+yoffset, 64, 64+yoffset);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE, 6);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE, 1);
	ObjAnim_StartAnimationA1(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
}

task EnemyCollision(objEnemy,hitboxA,hitboxB) {
	while(!Obj_IsDeleted(objEnemy)) {
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hitboxA);
		ObjEnemy_SetIntersectionCircleToPlayer(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hitboxB);
		yield;
	}
}