#include "../../system/functions.txt"

let type_fairy = 0;
let type_orb = 1;
let type_flame = 2;

let fairy_black = "black";
let fairy_blue = "blue";
let fairy_green = "green";
let fairy_red = "red";
let fairy_yellow = "yellow";
let fairy_white = "white";

let orb_red = 0;
let orb_orange = 1;
let orb_yellow = 2;
let orb_green = 3;
let orb_sky = 4;
let orb_blue = 5;
let orb_purple = 6;
let orb_white = 7;

let orb_a_red = 8;
let orb_a_orange = 9;
let orb_a_yellow = 10;
let orb_a_green = 11;
let orb_a_sky = 12;
let orb_a_blue = 13;
let orb_a_purple = 14;
let orb_a_white = 15;

let ring_red = 16;
let ring_orange = 17;
let ring_yellow = 18;
let ring_green = 19;
let ring_sky = 20;
let ring_blue = 21;
let ring_purple = 22;
let ring_white = 23;

let ring_a_red = 24;
let ring_a_orange = 25;
let ring_a_yellow = 26;
let ring_a_green = 27;
let ring_a_sky = 28;
let ring_a_blue = 29;
let ring_a_purple = 30;
let ring_a_white = 31;

let flame_red = 0;
let flame_orange = 30;
let falme_yellow = 60;
let flame_green = 120;
let flame_sky = 180;
let flame_blue = 240;
let flame_purple = 300;
let flame_white = -1;

function CreateBasicEnemyA1(x,y,speed,angle,life,damage,type,graph,size)
{
	let objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy,x,y);
	ObjMove_SetSpeed(objEnemy,speed);
	ObjMove_SetAngle(objEnemy,angle);
	ObjEnemy_SetLife(objEnemy,life);
	ObjEnemy_SetDamageRate(objEnemy,damage,damage);	
	SetCommonData("ENEMY_COUNT",GetCommonData("ENEMY_COUNT",0)+1);
	Obj_SetRenderPriority(objEnemy,0.38);
	alternative(type)
		case(type_fairy)
			{
			TFairyRender(objEnemy,graph,size);
			}
		case(type_orb)
			{
			TOrbRender(objEnemy,graph,size);
			}
		case(type_flame)
			{
			TFlameRender(objEnemy,graph,size);
			}
	return objEnemy;
}

task TFairyRender(objEnemy,color,size)
{
	ObjPrim_SetTexture(objEnemy,GetCurrentScriptDirectory ~ "./img/fairy_" ~ ToString(color) ~ ".png");
	let xm = 0;
	let xM = 0;
	let ym = 0;
	let yM = 0;
	alternative(size)
		case(1)
			{
			xm = 224;
			xM = 32;
			ym = 0;
			yM = 32;
			}
		case(2)
			{
			xm = 0;
			xM = 48;
			ym = 32;
			yM = 48;
			}
		case(3)
			{
			xm = 0;
			xM = 48;
			ym = 32;
			yM = 48;
			}
		case(4)
			{
			xm = 0;
			xM = 48;
			ym = 128;
			yM = 48;
			}
		case(5)
			{
			xm = 0;
			xM = 48;
			ym = 176;
			yM = 48;
			}
		case(6)
			{
			xm = 0;
			xM = 64;
			ym = 224;
			yM = 64;
			}
		others
			{
			xm = 0;
			xM = 32;
			ym = 0;
			yM = 32;
			}
	let anim = 0;
	let move = 0;
	Obj_SetRenderPriority(objEnemy,0.4);
	while(!Obj_IsDeleted(objEnemy))
		{
		if((ObjMove_GetSpeed(objEnemy) > 0.1 && absolute(cos(ObjMove_GetAngle(objEnemy))) > cos(65)) || move > 0)
			{
			anim = 0;
			if(ObjMove_GetSpeed(objEnemy) > 0.1 && absolute(cos(ObjMove_GetAngle(objEnemy))) > cos(65))
				{
				move+=0.15;
				}
			else
				{
				if(move > 2)
					{
					move = 2;
					}
				move-=0.125;
				}
			if(move >= 4)
				{
				move = 2;	
				}
			if(cos(ObjMove_GetAngle(objEnemy)) > 0)
				{
				ObjRender_SetScaleX(objEnemy,absolute(ObjRender_GetScaleX(objEnemy)));
				}
			else
				{
				ObjRender_SetScaleX(objEnemy,-absolute(ObjRender_GetScaleX(objEnemy)));
				}	
			ObjSprite2D_SetSourceRect(objEnemy,xm+xM*(3+floor(move)),ym,xm+xM*(4+floor(move)),ym+yM);	
			}
		else
			{
			move = 0;
			anim+=0.15;
			ObjSprite2D_SetSourceRect(objEnemy,xm+xM*(floor(anim)%3),ym,xm+xM*(1+floor(anim)%3),ym+yM);
			}
		ObjSprite2D_SetDestCenter(objEnemy);
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),xM*0.4);
		if(GetObjectDistance(objEnemy,GetPlayerObjectID) <= xM*0.4 && ObjEnemy_IsIntersect(objEnemy) == true)
			{
			let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE_RED,0,1);
			Obj_SetVisible(spam,false);
			Obj_DelayDelete(spam,2);
			}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			if(Effect < 3)
				{
				ObjEffect_Explosion(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy));
				}
			PlaySound("se_Die4",70);
			Obj_Delete(objEnemy);
			}
		yield;
		}
}

task TOrbRender(enemy,color,scale)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetPosition(obj,-128,-128,0);

	ObjPrim_SetTexture(enemy,GetCurrentScriptDirectory ~ "./img/orb.png");
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/orb.png");

	let xm = (color%4)*32+floor((color%32)/16)*128;
	let ym1 = floor((color%8)/4)*32;
	let ym2 = ym1+64+floor((color%16)/8)*128;

	ObjSprite2D_SetSourceRect(enemy,xm+1,ym1+1,xm+31,ym1+31);
	ObjSprite2D_SetSourceRect(obj,xm,ym2,xm+32,ym2+32);
	ObjSprite2D_SetDestRect(enemy,0.25,0.25,0.75,0.75);
	ObjSprite2D_SetDestRect(obj,0,0,1,1);
	Obj_SetRenderPriority(enemy,0.4);
	Obj_SetRenderPriority(obj,0.4);

	if((color >= orb_a_red && color < ring_red) || color >= ring_a_red)
		{
		ObjRender_SetBlendType(enemy,BLEND_ADD_ARGB);
		}

	let angle = ObjMove_GetAngle(enemy);
	let angleZ = 0;
	if(color >= 16)
		{
		angle = 0;
		}
	ObjEnemy_SetScale(enemy,scale);
	let xA = ObjMove_GetX(enemy);
	let yA = ObjMove_GetY(enemy);
	while(!Obj_IsDeleted(enemy))
		{
		scale = ObjEnemy_GetScale(enemy);
		ObjEnemy_SetIntersectionCircleToShot(enemy,ObjMove_GetX(enemy),ObjMove_GetY(enemy),scale*0.4);
		if(GetObjectDistance(enemy,GetPlayerObjectID) <= scale*0.4 && ObjEnemy_IsIntersect(enemy) == true)
			{
			let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE_RED,0,1);
			Obj_SetVisible(spam,false);
			Obj_DelayDelete(spam,2);
			}
		if(color < 16)
			{
			angle += 6*sin(ObjMove_GetAngle(enemy)-angle);
			angleZ+=1+ObjMove_GetSpeed(enemy)*0.5;
			}
		let speedE = GetDistance(ObjMove_GetX(enemy),ObjMove_GetY(enemy),xA,yA);
		let angleE = atan2(ObjMove_GetY(enemy)-yA,ObjMove_GetX(enemy)-xA);
		xA = ObjMove_GetX(enemy);
		yA = ObjMove_GetY(enemy);
		ObjRender_SetPosition(obj,xA+speedE*cos(angleE),yA+speedE*sin(angleE),0);
		ObjRender_SetAngleZ(enemy,angle-angleZ*0.5);
		ObjRender_SetAngleZ(obj,angle+angleZ);
		ObjRender_SetBlendType(obj,ObjRender_GetBlendType(enemy));
		Obj_SetRenderPriority(obj,Obj_GetRenderPriority(enemy));
		ObjRender_SetScaleXYZ(obj,scale,scale,0);
		ObjRender_SetScaleXYZ(enemy,scale,scale,0);
		if(ObjEnemy_GetInfo(enemy,INFO_LIFE) <= 0)
			{
			if(Effect < 3)
				{
				ObjEffect_Explosion(ObjMove_GetX(enemy),ObjMove_GetY(enemy));
				}
			PlaySound("se_Die4",70);
			Obj_Delete(obj);
			Obj_Delete(enemy);
			}
		if(Obj_IsDeleted(enemy))
			{
			Obj_Delete(obj);
			}
		yield;
		}
	Obj_Delete(obj);
}

task TFlameRender(objEnemy,color,scale)
{
	ObjPrim_SetTexture(objEnemy,GetCurrentScriptDirectory ~ "./img/flame.png");
	ObjRender_SetBlendType(objEnemy,BLEND_ADD_ARGB);
	ObjRender_SetColorHSV(objEnemy,color,175,255);

	let anim = 0;

	ObjEnemy_SetScale(objEnemy,scale);
	ObjSprite2D_SetDestRect(objEnemy,0,0,1,1);
	Obj_SetRenderPriority(objEnemy,0.4);


	while(!Obj_IsDeleted(objEnemy))
		{
		let scale = ObjEnemy_GetScale(objEnemy);
		anim+=1/6;
		ObjSprite2D_SetSourceRect(objEnemy,(floor(anim)%8)*64,0,(1+floor(anim)%8)*64,64);
		ObjRender_SetScaleXYZ(objEnemy,scale,scale,0);
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),scale*0.4);
		if(GetObjectDistance(objEnemy,GetPlayerObjectID) <= scale*0.4 && ObjEnemy_IsIntersect(objEnemy) == true)
			{
			let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE_RED,0,1);
			Obj_SetVisible(spam,false);
			Obj_DelayDelete(spam,2);
			}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			if(Effect < 3)
				{
				ObjEffect_Explosion(ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy));
				}
			PlaySound("se_Die4",70);
			Obj_Delete(objEnemy);
			}
		yield;
		}
}

task ObjEnemy_SetScale(obj,scale)
	{
	Obj_SetValue(obj,"ENEMY_SCALE",scale);
	}
function ObjEnemy_GetScale(obj)
	{
	return Obj_GetValueD(obj,"ENEMY_SCALE",1);
	}

task ObjEnemy_SetIntersect(obj,val)
	{
	Obj_SetValue(obj,"ENEMY_INTERSECT",val);
	}
function ObjEnemy_IsIntersect(obj)
	{
	return Obj_GetValueD(obj,"ENEMY_INTERSECT",true);
	}

function ClearStage
{
	let array = GetAllEnemyID;
	ascent(i in 0..length(array))
		{
		let obj = array[i];
		if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"STAGE_SKIP_DELETE",false) == false)
			{
			Obj_Delete(obj);
			}
		}
	array = GetShotIdInCircleA2(GetStgFrameWidth/2,GetStgFrameHeight/2,GetStgFrameHeight,TARGET_ENEMY);
	ascent(i in 0..length(array))
		{
		let obj = array[i];
		if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"STAGE_SKIP_DELETE",false) == false)
			{
			ObjShot_ToItem(obj);
			}
		}
	PlaySound("se_Die",80);
}