#include "./Default_EnemyConst.txt"

function CreateEnemy(graph,spellback)
{
	let objFam = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objFam);
	let imgFam = GetCurrentScriptDirectory ~ "img/enemy.png";
	alternative(graph)
		case(30,31,32,33,34)
			{
			imgFam = GetCurrentScriptDirectory ~ "img/Mami.png";
			}
	ObjPrim_SetTexture(objFam,imgFam);
	TEnemyRender;
	ObjMove_SetAngle(objFam,90);
	return objFam;
	task TEnemyRender
		{		
		alternative(graph)
			case(FAIRY_BLUE)
				{
				TFairyRender(256,288);
				}
			case(FAIRY_RED)
				{
				TFairyRender(288,320);
				}
			case(FAIRY_GREEN)
				{
				TFairyRender(320,352);
				}
			case(FAIRY_YELLOW)
				{
				TFairyRender(352,384);
				}
			case(MAID_BLUE)
				{
				TFairyRender(0,32);
				}
			case(MAID_RED)
				{
				TFairyRender(32,64);
				}
			case(MAID_GREEN)
				{
				TFairyRender(64,96);
				}
			case(MAID_YELLOW)
				{
				TFairyRender(96,128);
				}
			case(FAIRY_LARGE)
				{
				TFairyLargeRender;
				}
			case(SPIRIT_BLUE)
				{
				TSpiritRender(192,224);
				}
			case(SPIRIT_RED)
				{
				TSpiritRender(128,160);
				}
			case(SPIRIT_GREEN)
				{
				TSpiritRender(160,192);
				}
			case(SPIRIT_YELLOW)
				{
				TSpiritRender(224,256);
				}
			case(BUTTERFLY_BLUE)
				{
				TButterflyRender(192,224);
				}
			case(BUTTERFLY_RED)
				{
				TButterflyRender(128,160);
				}
			case(BUTTERFLY_GREEN)
				{
				TButterflyRender(160,192);
				}
			case(BUTTERFLY_YELLOW)
				{
				TButterflyRender(224,256);
				}
			case(FUZZ_BLUE)
				{
				TOtherRender(64,96,128,160);
				}
			case(FUZZ_RED)
				{
				TOtherRender(96,128,128,160);
				}
			case(FUZZ_GREEN)
				{
				TOtherRender(64,96,160,192);
				}
			case(FUZZ_YELLOW)
				{
				TOtherRender(96,128,160,192);
				}
			case(ORB_BLUE)
				{
				TOtherRender(64,96,192,224);
				}
			case(ORB_RED)
				{
				TOtherRender(0,32,192,224);
				}
			case(ORB_GREEN)
				{
				TOtherRender(32,64,192,224);
				}
			case(ORB_PURPLE)
				{
				TOtherRender(96,128,192,224);
				}
			case(MAGIC_BLUE)
				{
				TOtherRender(64,96,224,256);
				}
			case(MAGIC_RED)
				{
				TOtherRender(0,32,224,256);
				}
			case(MAGIC_GREEN)
				{
				TOtherRender(32,64,224,256);
				}
			case(MAGIC_PURPLE)
				{
				TOtherRender(96,128,224,256);
				}
			case(MAMI_MAN_BLUE)
				{
				TMamiManRender(256,288);
				}
			case(MAMI_DOG)
				{
				TMamiRender(288,320);
				}
			case(MAMI_BIRD)
				{
				TMamiRender(320,352);
				}
			case(MAMI_FROG)
				{
				TMamiRender(352,384);
				}
			case(MAMI_MAN_RED)
				{
				TMamiManRender(384,416);
				}
		}
	task TSpellBack(sizeM,color,dirA)
		{
		sizeM = (sizeM*2/64)*0.85;
		let dir = 0;
		let size = dirA*45;
		let objBack = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objBack,imgFam);
		Obj_SetRenderPriorityI(objBack,30);
		ObjRender_SetColorHSV(objBack,color[0],color[1],255);
		ObjRender_SetBlendType(objBack,BLEND_ADD_ARGB);
		while(!Obj_IsDeleted(objFam))
			{	
			let speed = ObjMove_GetSpeed(objFam);
			let angle = ObjMove_GetAngle(objFam);
			dir+=dirA;
			size+=1;
			let scale = sizeM+0.0125*sizeM*sin(size);
			ObjRender_SetAngleZ(objBack,dir);
			ObjSprite2D_SetSourceRect(objBack,0,128,63,195);
			ObjRender_SetScaleXYZ(objBack,scale,scale,0);
			ObjRender_SetX(objBack,ObjMove_GetX(objFam)+speed*cos(angle));
			ObjRender_SetY(objBack,ObjMove_GetY(objFam)+speed*sin(angle));
			ObjSprite2D_SetDestCenter(objBack);
			yield;
			}
		Obj_Delete(objBack);
		}

	task TOtherRender(xm,xM,ym,yM)
		{
		let anim = 0;
		if(spellback == true)
			{
			TSpellBack(xM-xm,[0,0],1);
			TSpellBack((xM-xm)*0.75,[0,200],-1);
			}
		let dir = cos(ObjMove_GetAngle(objFam))*2/3;
		if(absolute(cos(ObjMove_GetAngle(objFam))) < (2^(1/2))/2)
			{
			dir = sin(ObjMove_GetAngle(objFam))*2/3;
			}
		while(!Obj_IsDeleted(objFam))
			{	
			anim+=dir*(2+ObjMove_GetSpeed(objFam)+rand(-1,1));
			ObjRender_SetAngleZ(objFam,anim);
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}

	task TMamiRender(ym,yM)
		{
		let xm = 0;
		let xM = 0;
		let anim = 0;
		if(graph == MAMI_BIRD)
			{
			ObjRender_SetAngleZ(objFam,90);
			}
		if(spellback == true)
			{
			TSpellBack(yM-ym,[0,0],1);
			TSpellBack((yM-ym)*0.75,[0,200],-1);
			}
		if(graph != MAMI_BIRD)
			{
			ObjRender_SetAngleZ(objFam,0);		
			}
		while(!Obj_IsDeleted(objFam))
			{
			if(graph == MAMI_BIRD)
				{
				ObjRender_SetAngleZ(objFam,ObjMove_GetAngle(objFam));
				}
			if(graph == MAMI_DOG)
				{
				if(cos(ObjMove_GetAngle(objFam)) > 0)
					{
					ObjRender_SetScaleX(objFam,absolute(ObjRender_GetScaleX(objFam)));
					}	
				else
					{
					ObjRender_SetScaleX(objFam,-absolute(ObjRender_GetScaleX(objFam)));
					}
				}
			anim+=0.125;
			if(anim >= 4)
				{
				anim = 0;
				}
			alternative(floor(anim))
				case(0)
					{
					xm = 0;
					xM = 32;
					}
				case(1,3)	
					{
					xm = 32;
					xM = 64;
					}
				case(2)
					{
					xm = 64;
					xM = 96;
					}
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}

	task TMamiManRender(ym,yM)
		{
		let xm = 0;
		let xM = 0;
		let anim = 0;
		if(spellback == true)
			{
			TSpellBack(yM-ym,[0,0],1);
			TSpellBack((yM-ym)*0.75,[0,200],-1);
			}
		if(graph != MAMI_BIRD)
			{
			ObjRender_SetAngleZ(objFam,0);		
			}
		while(!Obj_IsDeleted(objFam))
			{
			anim+=0.125;
			if(anim >= 4)
				{
				anim = 0;
				}
			alternative(floor(anim))
				case(0,2)
					{
					xm = 0;
					xM = 32;
					}
				case(1)	
					{
					xm = 32;
					xM = 64;
					}
				case(3)
					{
					xm = 64;
					xM = 96;
					}
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}
	task TButterflyRender(ym,yM)
		{
		let xm = 0;
		let xM = 0;
		let anim = 0;
		ObjRender_SetBlendType(objFam,BLEND_ADD_ARGB);
		if(spellback == true)
			{
			TSpellBack(yM-ym,[0,0],1);
			TSpellBack((yM-ym)*0.75,[0,200],-1);
			}
		while(!Obj_IsDeleted(objFam))
			{
			ObjRender_SetAngleZ(objFam,ObjMove_GetAngle(objFam));
			anim+=0.1+ObjMove_GetSpeed(objFam)/20;
			if(anim >= 6)
				{
				anim = 0;
				}
			alternative(floor(anim))	
				case(0)		
					{
					xm = 128;
					xM = 160;
					}
				case(1,5)
					{
					xm = 160;
					xM = 192;
					}	
				case(2,4)
					{
					xm = 192;
					xM = 224;
					}
				case(3)
					{
					xm = 224;
					xM = 256;
					}	
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}
	task TSpiritRender(ym,yM)
		{
		ObjRender_SetScaleXYZ(objFam,1.25,1.25,0);
		ObjRender_SetBlendType(objFam,BLEND_ADD_ARGB);
		let xm = 0;
		let xM = 0;
		let anim = 0;
		let move = 0;
		if(spellback == true)
			{
			TSpellBack(yM-ym,[0,0],1);
			TSpellBack((yM-ym)*0.75,[0,200],-1);
			}
		while(!Obj_IsDeleted(objFam))
			{
			anim+=0.2;
			move+=1;
			if(anim >= 6)
				{
				anim = 0;
				}
			alternative(floor(anim))
				case(0)
					{
					xm = 256;
					xM = 288;
					}
				case(1,5)
					{
					xm = 288;
					xM = 320;
					}
				case(2,4)
					{
					xm = 320;
					xM = 352;
					}
				case(3)
					{
					xm = 352;
					xM = 384;
					}
			if(move == 4)
				{
				TSpiritAnim2(xm+128,xM+128);
				move = 0;
				}
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		task TSpiritAnim2(xmA,xMA)
			{
			let dist1 = rand(0,8);
			let dist2 = 1;
			let angle1 = rand(0,360);
			let angle2 = 270+rand(-15,15);
			let scale = 1.125;
			let x = ObjMove_GetX(objFam)+dist1*cos(angle1);
			let y = ObjMove_GetY(objFam)+dist1*sin(angle1);
			let objCut = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(objCut,imgFam);
			ObjSprite2D_SetSourceRect(objCut,xmA,ym,xMA,yM);
			Obj_SetRenderPriorityI(objCut,38);
			while(scale > 0)
				{
				ObjRender_SetBlendType(objCut,ObjRender_GetBlendType(objFam));
				if(ObjRender_GetBlendType(objFam) == BLEND_ALPHA)
					{
					ObjRender_SetAlpha(objCut,(scale/1.125)*150);
					}
				else
					{
					ObjRender_SetColorHSV(objCut,0,0,(scale/1.125)*150);
					}
				scale-=0.025*1.125;
				x+=dist2*cos(angle2);
				y+=dist2*sin(angle2);
				ObjRender_SetScaleXYZ(objCut,scale,scale,0);
				ObjRender_SetX(objCut,x);
				ObjRender_SetY(objCut,y);
				ObjSprite2D_SetDestCenter(objCut);
				yield;
				}
			Obj_Delete(objCut);
			}
		}
	task TFairyLargeRender
		{
		let ym = 0;
		let yM = 0;
		let xm = 0;
		let xM = 0;
		let anim = 0;
		let move = 0;
		if(spellback == true)
			{
			TSpellBack(448-382,[0,0],1);
			TSpellBack((448-382)*0.75,[0,200],-1);
			}
		while(!Obj_IsDeleted(objFam))
			{
			if(ObjMove_GetSpeed(objFam) > 0.1 && absolute(cos(ObjMove_GetAngle(objFam))) > (2^(1/2))/2)
				{
				move+=0.188*ObjMove_GetSpeed(objFam);
				if(move < 4)
					{
					anim = 0;
					ym = 382;
					yM = 448;
					}
				else
					{
					anim+=0.133;
					}
				if(anim >= 4)
					{
					anim = 0;
					}
				alternative(floor(move))
					case(0)
						{
						xm = 256;
						xM = 320;
						}
					case(1)
						{
						xm = 320;
						xM = 384;
						}
					case(2)
						{
						xm = 384;
						xM = 448;
						}
					case(3)
						{
						xm = 448;
						xM = 512;
						}	
					others
						{
						alternative(floor(anim))
							case(0)
								{
								ym = 256;
								yM = 320;
								xm = 384;
								xM = 448;
								}
							case(1)
								{
								ym = 256;
								yM = 321;
								xm = 448;
								xM = 512;
								}
							case(2)
								{
								ym = 320;
								yM = 384;
								xm = 384;
								xM = 448;
								}
							case(3)
								{
								ym = 320;
								yM = 384;
								xm = 448;
								xM = 512;
								}
						}
				ObjRender_SetScaleX(objFam,absolute(cos(ObjMove_GetAngle(objFam)))/cos(ObjMove_GetAngle(objFam)));
				}
			else
				{
				anim+=0.12;
				if(anim >= 4)
					{
					anim = 0;
					}
				ym = 382;
				yM = 449;
				alternative(floor(anim))	
					case(0)
						{
						xm = 0;
						xM = 64;
						}
					case(1)
						{
						xm = 64;
						xM = 128;
						}
					case(2)
						{
						xm = 128;
						xM = 192;
						}
					case(3)
						{
						xm = 192;
						xM = 256;
						}
				ObjRender_SetScaleX(objFam,1);
				}	
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}
	task TFairyRender(ym,yM)
		{
		let xm = 0;
		let xM = 0;
		let anim = 0;
		let move = 0;
		if(spellback == true)
			{
			TSpellBack(yM-ym,[0,0],1);
			TSpellBack((yM-ym)*0.75,[0,200],-1);
			}
		while(!Obj_IsDeleted(objFam))
			{
			if(ObjMove_GetSpeed(objFam) > 0.1)
				{
				move+=0.188*ObjMove_GetSpeed(objFam);
				if(move < 4)
					{
					anim = 0;
					}
				else
					{
					anim+=0.133;
					}
				if(anim >= 4)
					{
					anim = 0;
					}
				alternative(floor(move))
					case(0)
						{
						xm = 128;
						xM = 160;
						}
					case(1)
						{
						xm = 160;
						xM = 192;
						}
					case(2)
						{
						xm = 192;
						xM = 224;
						}
					case(3)
						{
						xm = 224;
						xM = 256;
						}	
					others
						{
						alternative(floor(anim))
							case(0)
								{
								xm = 256;
								xM = 288;
								}
							case(1)
								{
								xm = 288;
								xM = 320;
								}
							case(2)
								{
								xm = 320;
								xM = 352;
								}
							case(3)
								{
								xm = 353;
								xM = 384;
								}
						}
				ObjRender_SetScaleX(objFam,absolute(cos(ObjMove_GetAngle(objFam)))/cos(ObjMove_GetAngle(objFam)));
				}
			else
				{
				anim+=0.133;
				if(anim >= 4)
					{
					anim = 0;
					}
				alternative(floor(anim))	
					case(0)		
						{
						xm = 0;
						xM = 32;
						}
					case(1)
						{
						xm = 32;
						xM = 64;
						}	
					case(2)
						{
						xm = 64;
						xM = 96;
						}
					case(3)
						{
						xm = 96;
						xM = 128;
						}
				ObjRender_SetScaleX(objFam,1);
				}	
			ObjSprite2D_SetSourceRect(objFam,xm,ym,xM,yM);
			ObjSprite2D_SetDestCenter(objFam);
			yield;
			}
		}
}
