// This has been heavily modified by me but the original version belongs to Kirbio. I also used Ozzy's AnimateBoss function as a reference. Thank you so much!

let GCSD = GetCurrentScriptDirectory;

let NormalFairySheet = GCSD ~ "../Img/Enm/NormalFairies.png";
let DarkFairySheet = GCSD ~ "../Img/Enm/DarkFairies.png";
let WispSheet = GCSD ~ "../Img/Enm/Wisps.png";
let AuraSheet = GCSD ~ "../Img/Enm/Aura.png";

function Obj_IsOutsideScreen(ID, Distance){ // From Kirbio, thanks! Calculates IF fairy is outside the screen. Distance adjusts the Stg Frame boundary.
	return ObjRender_GetX(ID) > GetStgFrameWidth + Distance || ObjRender_GetX(ID) < GetStgFrameLeft - Distance || 
		   ObjRender_GetY(ID) > GetStgFrameHeight + Distance || ObjRender_GetY(ID) < GetStgFrameTop - Distance;
}

function CreateFairyA1(Life){ // Creates Fairy
	let FairyObj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(FairyObj);
	ObjEnemy_SetLife(FairyObj, Life);
	ObjEnemy_SetDamageRate(FairyObj,100,50); // IMPORTANT.
	return FairyObj;
}

function GetX(ID){ // Gets X Location
	return ObjMove_GetX(ID);
}

function GetY(ID){ // Gets Y Location
	return ObjMove_GetY(ID);
}

function AliveEnemyA1(ID, Colour){ // Explodes enemy when shot dead.
	CheckLife;
	
	task CheckLife{
		while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0){yield;}
		ExplodeEnemy(ID, Colour);
	}
}
		
function AliveEnemyB1(ID1, ID2, Colour){ // Explodes ID2 enemy when ID1 enemy is shot dead.
	CheckLife;
	
	task CheckLife{
		while(ObjEnemy_GetInfo(ID1,INFO_LIFE) > 0){yield;}
		ExplodeEnemy(ID2, Colour);
	}
}
	
function ExplodeEnemy(ID, Colour){ // Plays the Explode Effect. INCOMPLETE, WORK ON IT.
	Obj_Delete(ID);
}

task SetEnemyTimerA1(ID, Time, Velocity){ // Sets a timer until the enemy flies off stage. Flies off in opposite direction to current angle.
	
	wait(Time);
	
	let Speed = 0;
	ObjMove_SetAngle(ID, ObjMove_GetAngle(ID)+180);
	
	loop(60){
		ObjMove_SetSpeed(ID, Speed);
		Speed+=Velocity/60;
		yield;
	}
	
	Delete(ID);
	Speed = 0;
}

task SetEnemyTimerB1(ID, Time, Angle, Velocity){ // Makes the enemy fly off stage. One can control the direction the enemy flies off in this function.
	
	wait(Time);
	
	let Speed = 0;
	ObjMove_SetAngle(ID, Angle);
	
	loop(60){
		ObjMove_SetSpeed(ID, Speed);
		Speed+=Velocity/60;
		yield;
	}
	Delete(ID);
	Speed = 0;
}

task Delete(ID){
	while(!Obj_IsOutsideScreen(ID, 100)){yield;}
	Obj_Delete(ID);
}
	
task Aura(ID, Scale, R, G, B){ // Sets enemy aura
	yield;
	let AuraObj = ObjPrim_Create(OBJ_SPRITE_2D);
	let inc = 0;
	ObjPrim_SetTexture(AuraObj, AuraSheet);
	Obj_SetRenderPriority(AuraObj, 0.2);
	ObjRender_SetScaleXYZ(AuraObj, Scale, Scale, Scale);
	ObjRender_SetBlendType(AuraObj,BLEND_ALPHA);
	ObjRender_SetAlpha(AuraObj,255);
	ObjRender_SetColor(AuraObj,R, G, B);
	
	while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0 && !Obj_IsDeleted(ID) && !Obj_IsOutsideScreen(ID, 50)){
	
		ObjRender_SetPosition(AuraObj,ObjRender_GetX(ID),ObjRender_GetY(ID),0);
		
		if(inc == 0){ObjSprite2D_SetSourceRect(AuraObj, 0, 0, 48, 48);}
		if(inc == 4){ObjSprite2D_SetSourceRect(AuraObj, 48, 0, 96, 48);}
		if(inc == 8){ObjSprite2D_SetSourceRect(AuraObj, 96, 0, 144, 48);}
		if(inc == 12){ObjSprite2D_SetSourceRect(AuraObj, 144, 0, 192, 48);}
		if(inc == 16){ObjSprite2D_SetSourceRect(AuraObj, 192, 0, 240, 48);}
		if(inc == 20){ObjSprite2D_SetSourceRect(AuraObj, 240, 0, 288, 48);}
		if(inc == 24){ObjSprite2D_SetSourceRect(AuraObj, 288, 0, 336, 48);}
		if(inc == 28){ObjSprite2D_SetSourceRect(AuraObj, 336, 0, 384, 48);}
		if(inc == 31){inc = 3;}
		
		inc++;
		ObjSprite2D_SetDestRect(AuraObj,-22,-29,26,19);
		yield;
	}
	Obj_Delete(AuraObj);
}

task AnimateSmallFairy(ID, Type){ // Animates small fairies

	ObjPrim_SetTexture(ID, NormalFairySheet); 
	
	let AnimationTimer = 12;
	let AnimationIsMove = false;
	
	let YOffset = 0;
	let XWidth = 32;
	let YLength = 32;
	
	
	alternative(Type)
	case( "Blue" || 1){ YOffset = 320; Aura(ID, 1,0,0,255);}
	case( "Red" || 2){ YOffset = 352; Aura(ID, 1,255,0,0);}
	case( "Green" || 3){ YOffset = 384; Aura(ID, 1,0,255,0);}
	case( "Yellow" || 4){ YOffset = 416; Aura(ID, 1,255,255,0);}
	
	while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0 && !Obj_IsDeleted(ID) && !Obj_IsOutsideScreen(ID, 50)){
	
	ObjEnemy_SetIntersectionCircleToShot(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),11);
	ObjEnemy_SetIntersectionCircleToPlayer(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),10);

			if(ObjMove_GetSpeed(ID) == 0){
				if(AnimationIsMove == true){
					AnimationIsMove = false;
					AnimationTimer = 0;
				}
			}
			
			else if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.3 || ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.3){
				if(AnimationIsMove == false){
					AnimationIsMove = true;
					AnimationTimer = 0;
					
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.1) { ObjRender_SetScaleXYZ(ID, 1, 1, 1); }
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.1){ ObjRender_SetScaleXYZ(ID, -1, 1, 1); }
				}
			}
		
		if(AnimationIsMove == false){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, XWidth*7, YOffset, XWidth*8, YOffset+YLength);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth*6, YOffset, XWidth*7, YOffset+YLength);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*5, YOffset, XWidth*6, YOffset+YLength);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*4, YOffset, XWidth*5, YOffset+YLength);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, 0, YOffset, XWidth, YOffset+YLength);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset, XWidth*2, YOffset+YLength);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset, XWidth*3, YOffset+YLength);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset, XWidth*4, YOffset+YLength);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
		
		if(AnimationIsMove == true){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, XWidth*4, YOffset, XWidth*5, YOffset+YLength);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth*5, YOffset, XWidth*6, YOffset+YLength);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*6, YOffset, XWidth*7, YOffset+YLength);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*7, YOffset, XWidth*8, YOffset+YLength);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, XWidth*8, YOffset, XWidth*9, YOffset+YLength);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth*9, YOffset, XWidth*10, YOffset+YLength);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*10, YOffset, XWidth*11, YOffset+YLength);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*11, YOffset, XWidth*12, YOffset+YLength);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
	
		ObjSprite2D_SetDestCenter(ID);
		AnimationTimer++;
		yield;
	}
	Obj_Delete(ID);
}
	
task AnimateMediumFairy(ID, Type){ // Animates medium fairies

	ObjPrim_SetTexture(ID, NormalFairySheet); 
	
	let AnimationTimer = 12;
	let AnimationIsMove = false;
	
	let YOffset = 0;
	let XWidth = 48;
	let YLength = 32;
	
	
	alternative(Type)
	case( "Blue" || 1){ YOffset = 0; Aura(ID, 1.1,0,0,255);}
	case( "Red" || 2){ YOffset = 96; Aura(ID, 1.1,255,0,0);}
	
	while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0 && !Obj_IsDeleted(ID) && !Obj_IsOutsideScreen(ID, 50)){
	
	ObjEnemy_SetIntersectionCircleToShot(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),11);
	ObjEnemy_SetIntersectionCircleToPlayer(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),10);

			if(ObjMove_GetSpeed(ID) == 0){
				if(AnimationIsMove == true){
					AnimationIsMove = false;
					AnimationTimer = 0;
				}
			}
			
			else if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.3 || ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.3){
				if(AnimationIsMove == false){
					AnimationIsMove = true;
					AnimationTimer = 0;
					
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.1) { ObjRender_SetScaleXYZ(ID, 1, 1, 1); }
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.1){ ObjRender_SetScaleXYZ(ID, -1, 1, 1); }
				}
			}
		
		if(AnimationIsMove == false){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset+YLength, XWidth*4, YOffset+YLength*2);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset+YLength, XWidth*3, YOffset+YLength*2);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset+YLength, XWidth*2, YOffset+YLength*2);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, 0, YOffset+YLength, XWidth, YOffset+YLength*2);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, 0, YOffset, XWidth, YOffset+YLength);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset, XWidth*2, YOffset+YLength);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset, XWidth*3, YOffset+YLength);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset, XWidth*4, YOffset+YLength);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
		
		if(AnimationIsMove == true){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, 0, YOffset+YLength, XWidth, YOffset+YLength*2);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset+YLength, XWidth*2, YOffset+YLength*2);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset+YLength, XWidth*3, YOffset+YLength*2);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset+YLength, XWidth*4, YOffset+YLength*2);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, 0, YOffset+YLength*2, XWidth, YOffset+YLength*3);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset+YLength*2, XWidth*2, YOffset+YLength*3);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset+YLength*2, XWidth*3, YOffset+YLength*3);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset+YLength*2, XWidth*4, YOffset+YLength*3);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
	
		ObjSprite2D_SetDestCenter(ID);
		AnimationTimer++;
		yield;
	}
	Obj_Delete(ID);
}

task AnimateDarkFairy(ID, Type){ // Animates dark fairies

	ObjPrim_SetTexture(ID, DarkFairySheet); 
	
	let AnimationTimer = 12;
	let AnimationIsMove = false;
	
	let YOffset = 0;
	let XWidth = 32;
	let YLength = 32;
	
	
	alternative(Type)
	case( "Blue" || 1){ YOffset = 0; Aura(ID, 1,0,0,128);}
	case( "Red" || 2){ YOffset = 32; Aura(ID, 1,128,0,0);}
	case( "Yellow" || 4){ YOffset = 64; Aura(ID, 1,128,128,0);}
	case( "Purple" || 5){ YOffset = 96; Aura(ID, 1,128,0,128);}
	
	while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0 && !Obj_IsDeleted(ID) && !Obj_IsOutsideScreen(ID, 50)){
	
	ObjEnemy_SetIntersectionCircleToShot(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),11);
	ObjEnemy_SetIntersectionCircleToPlayer(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),10);

			if(ObjMove_GetSpeed(ID) == 0){
				if(AnimationIsMove == true){
					AnimationIsMove = false;
					AnimationTimer = 0;
				}
			}
			
			else if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.3 || ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.3){
				if(AnimationIsMove == false){
					AnimationIsMove = true;
					AnimationTimer = 0;
					
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.1) { ObjRender_SetScaleXYZ(ID, 1, 1, 1); }
					if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.1){ ObjRender_SetScaleXYZ(ID, -1, 1, 1); }
				}
			}
		
		if(AnimationIsMove == false){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, XWidth*7, YOffset, XWidth*8, YOffset+YLength);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth*6, YOffset, XWidth*7, YOffset+YLength);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*5, YOffset, XWidth*6, YOffset+YLength);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*4, YOffset, XWidth*5, YOffset+YLength);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, 0, YOffset, XWidth, YOffset+YLength);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset, XWidth*2, YOffset+YLength);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset, XWidth*3, YOffset+YLength);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset, XWidth*4, YOffset+YLength);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
		
		if(AnimationIsMove == true){
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, XWidth*4, YOffset, XWidth*5, YOffset+YLength);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth*5, YOffset, XWidth*6, YOffset+YLength);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*6, YOffset, XWidth*7, YOffset+YLength);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*7, YOffset, XWidth*8, YOffset+YLength);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, XWidth*8, YOffset, XWidth*9, YOffset+YLength);}
			if(AnimationTimer == 13){ObjSprite2D_SetSourceRect(ID, XWidth*9, YOffset, XWidth*10, YOffset+YLength);}
			if(AnimationTimer == 18){ObjSprite2D_SetSourceRect(ID, XWidth*10, YOffset, XWidth*11, YOffset+YLength);}
			if(AnimationTimer == 23){ObjSprite2D_SetSourceRect(ID, XWidth*11, YOffset, XWidth*12, YOffset+YLength);}
			if(AnimationTimer == 27){AnimationTimer = 12;}
		}
	
		ObjSprite2D_SetDestCenter(ID);
		AnimationTimer++;
		yield;
	}
	Obj_Delete(ID);
}

task AnimateWisp(ID, Type){ // Animates wisps

	ObjPrim_SetTexture(ID, WispSheet); 
	
	let AnimationTimer = 0;
	
	let YOffset = 0;
	let XWidth = 32;
	let YLength = 32;
	
	
	alternative(Type)
	case( "Red" || 2){ YOffset = 0; Aura(ID, 0.8,255,0,0);}
	case( "Green" || 3){ YOffset = 32; Aura(ID, 0.8,0,255,0);}
	case( "Blue" || 1){ YOffset = 64; Aura(ID, 0.8,0,0,255);}
	case( "Yellow" || 4){ YOffset = 96; Aura(ID, 0.8,255,255,0);}
	
	while(ObjEnemy_GetInfo(ID,INFO_LIFE) > 0 && !Obj_IsDeleted(ID) && !Obj_IsOutsideScreen(ID, 50)){
	
		ObjEnemy_SetIntersectionCircleToShot(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),11);
		ObjEnemy_SetIntersectionCircleToPlayer(ID,ObjMove_GetX(ID),ObjMove_GetY(ID),10);

			
		if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) >= 0.1) { ObjRender_SetScaleXYZ(ID, 1, 1, 1); }
		if(ObjMove_GetSpeed(ID)*cos(ObjMove_GetAngle(ID)) <= -0.1){ ObjRender_SetScaleXYZ(ID, -1, 1, 1); }
					
			if(AnimationTimer == 0){ObjSprite2D_SetSourceRect(ID, 0, YOffset, XWidth, YOffset+YLength);}
			if(AnimationTimer == 2){ObjSprite2D_SetSourceRect(ID, XWidth, YOffset, XWidth*2, YOffset+YLength);}
			if(AnimationTimer == 4){ObjSprite2D_SetSourceRect(ID, XWidth*2, YOffset, XWidth*3, YOffset+YLength);}
			if(AnimationTimer == 6){ObjSprite2D_SetSourceRect(ID, XWidth*3, YOffset, XWidth*4, YOffset+YLength);}
			if(AnimationTimer == 8){ObjSprite2D_SetSourceRect(ID, XWidth*4, YOffset, XWidth*5, YOffset+YLength);}
			if(AnimationTimer == 10){ObjSprite2D_SetSourceRect(ID, XWidth*5, YOffset, XWidth*6, YOffset+YLength);}
			if(AnimationTimer == 12){ObjSprite2D_SetSourceRect(ID, XWidth*6, YOffset, XWidth*7, YOffset+YLength);}
			if(AnimationTimer == 14){ObjSprite2D_SetSourceRect(ID, XWidth*7, YOffset, XWidth*8, YOffset+YLength);}
			if(AnimationTimer == 15){AnimationTimer = 1;}
	
		ObjSprite2D_SetDestCenter(ID);
		AnimationTimer++;
		yield;
	}
	Obj_Delete(ID);
}