//Enemy Management Script

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;

let FAIRY_SMALL_1A1 = 1.1;
let FAIRY_LARGE_1A1 = 1.2;

#include"System/LIB/Event_Const.dnh"
#include"Script/LIB/Function_Misc.dnh"
#include"Script/LIB/Function_Audio.dnh"
#include"Script/LIB/Function_Effect.dnh"
#include"Script/LIB/Function_Enemy.dnh"
#include"Script/LIB/Function_Shot.dnh"
#include"Script/LIB/SE_Const.dnh"
#include"Script/LIB/FairyAnimation.dnh"
#include"Shot/ZUNShot_Const.dnh"

@Initialize {
}

@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_1A1) { CreateFairy_Small1A1(enemyParam); }
		case(FAIRY_LARGE_1A1) { CreateFairy_Large1A1(enemyParam); }
	}
}

task CreateFairy_Small1A1(enemyParam) {
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	
	ObjEnemy_SetLife(objEnemy,10);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let diff = GetAreaCommonData("PlayState","Difficulty","Hard");
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	PlayEnemyHitSound(objEnemy,1);
	AnimateEnemyA1(objEnemy,"Red");
	EnemyCollision(objEnemy,24,16);
	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>-40 && GetX<GetStgFrameWidth+40 && GetY>-40 && GetY<GetStgFrameHeight+40);
			yield;
		}
	}
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+200,5,4);
		wait(120);
		if(GetX<GetCenterX) {
			ObjMove_SetDestAtWeight(objEnemy,GetX+400,GetY+90,10,1);
		}
		if(GetX>=GetCenterX) {
			ObjMove_SetDestAtWeight(objEnemy,GetX-400,GetY+90,10,1);
		}
	}
	task Fire {
		let graphic = BL_SMALL_RED;
		let delay = 10;
		wait(60);
		loop(4) {
			if(!Obj_IsDeleted(objEnemy)) {
				PlaySoundEffect(shot,90);
				if(diff=="Normal") {
					CreateShot02(GetX,GetY,5,GetAngleToPlayer(objEnemy),-4/20,1,graphic,delay);
				}
				if(diff=="Hard") {
					CreateShot02(GetX,GetY,6,GetAngleToPlayer(objEnemy),-4.5/20,1.5,graphic,delay);
					CreateShot02(GetX,GetY,6,GetAngleToPlayer(objEnemy)-1.5,-4.5/20,1.5,graphic,delay);
					CreateShot02(GetX,GetY,6,GetAngleToPlayer(objEnemy)+1.5,-4.5/20,1.5,graphic,delay);
				}
				if(diff=="Lunatic") {
					CreateShot02(GetX,GetY,7,GetAngleToPlayer(objEnemy),-5/20,2,graphic,delay);
					CreateShot02(GetX,GetY,7,GetAngleToPlayer(objEnemy)-3,-5/20,2,graphic,delay);
					CreateShot02(GetX,GetY,7,GetAngleToPlayer(objEnemy)+3,-5/20,2,graphic,delay);
				}
			}
			if(diff=="Normal") { wait(75); }
			if(diff=="Hard") { wait(50); }
			if(diff=="Lunatic") { wait(25); }
		}
	}
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			CreateItems01(1,1,GetX,GetY,0);
			ExplosionA1(objEnemy,[255,96,96],[255,96,96]);
		}
		else { Obj_Delete(objEnemy); }
	}
}
task CreateFairy_Large1A1(enemyParam) {
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	let x = enemyParam[0];
	let y = enemyParam[1];
	let rot = enemyParam[2];
	
	ObjEnemy_SetLife(objEnemy,130);
	ObjEnemy_SetDamageRate(objEnemy,100,100);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	
	let diff = GetAreaCommonData("PlayState","Difficulty","Hard");
	let GetX = 0;
	let GetY = 0;
	let GetLife = 0;
	let OnScreen = false;
	
	PlayEnemyHitSound(objEnemy,1);
	AnimateEnemyA4(objEnemy);
	EnemyCollision(objEnemy,32,24);
	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>-40 && GetX<GetStgFrameWidth+40 && GetY>-40 && GetY<GetStgFrameHeight+40);
			yield;
		}
	}
	task Movement {
		ObjMove_SetDestAtWeight(objEnemy,GetX,GetY+150,8,3);
		wait(300);
		ObjMove_SetDestAtWeight(objEnemy,GetX+300*rot,GetY,8,1.2);
	}
	task Fire {
		let graphic = BL_SMALL_GREEN;
		let delay = 10;
		let r = 10;
		let t = 0;
		let dir1 = 0;
		let dir2 = 0;
		wait(60);
		loop {
			PlaySoundEffect(shot,90);
			CreateShot01(GetX+r*cos(dir2),GetY+r*sin(dir2),1.3,dir1,graphic,delay);
			CreateShot01(GetX+r*cos(dir2+120),GetY+r*sin(dir2+120),1.3,dir1+120,graphic+2,delay);
			CreateShot01(GetX+r*cos(dir2+240),GetY+r*sin(dir2+240),1.3,dir1+240,graphic+4,delay);
			dir1+=360/15*rot;
			dir2+=360/45*rot;
			r+=1.5*sin(8*t);
			t++;
			waitEx(objEnemy,7);
		}
	}
	task End {
		while(!OnScreen) { yield; }
		while(GetLife>0 && OnScreen) { yield; }
		if(OnScreen) {
			CreateItems01(5,1,GetX,GetY,40);
			CreateItems01(5,3,GetX,GetY,40);
			ExplosionA2(objEnemy,[96,255,96],[96,255,96]);
		}
		else { Obj_Delete(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;
	}
}