task ObjEnemy_SetIdependance(obj,obj2){
	while(! Obj_IsDeleted(obj2) && ObjEnemy_GetInfo(obj2,INFO_LIFE) > 0){
		yield;
	}
	ObjEnemy_SetLife(obj,-1);
}

function ObjEnemy_CreateCasualFamiliar(x,y,obj2,color){
	let obj = ObjEnemy_CreateFamiliar(x,y,obj2,color);
	ObjRender_SetAngleX(obj,60);
	ObjRender_SetScaleXYZ(obj,0.5,0.5,0);
	return obj;
}

function ObjEnemy_CreateFamiliar(x,y,obj2,color){
	
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjEnemy_SetLife(obj,200);
	ObjEnemy_SetCircle(obj,color);
	ObjEnemy_SetIdependance(obj,obj2);
	ObjMove_SetPosition(obj,x,y);
	
	Control(obj,x,y,color);
	
	return obj;
	
	task Control(obj,x,y,color){
		
		let xa = x;
		let ya = y;
		
		while(! Obj_IsDeleted(obj) && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0){
			xa = ObjMove_GetX(obj);
			ya = ObjMove_GetY(obj);
			
			if(GetX(obj) > GetStgFrameWidth+120 || GetX(obj) < -120 || GetY(obj) > GetStgFrameHeight+120 || GetY(obj) < -120){
				ObjEnemy_SetLife(obj,0);
				Obj_Delete(obj);
				return;
			}
			
			yield;
		}
		
		if(!Obj_IsDeleted(obj)){NotifyEventAll(EV_USER_STAGE + 4, [xa,ya,color[0],color[1],color[2]]);
		
		Obj_Delete(obj);}
	}
}

task ObjEnemy_SetCircle(parent,color){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetMainStgScriptDirectory ~ "img/enemy/familiar.png");
	Obj_SetRenderPriority(obj, 0.49);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj,color[0],color[1],color[2]);
	ObjRender_SetAlpha(obj,0);
	
	while(! Obj_IsDeleted(parent) && ObjEnemy_GetInfo(parent,INFO_LIFE) > 0){
		
		ObjRender_SetPosition(obj,ObjMove_GetX(parent),ObjMove_GetY(parent),0);
		ObjRender_SetAngleXYZ(obj,ObjRender_GetAngleX(parent),ObjRender_GetAngleY(parent),ObjRender_GetAngleZ(obj)+4);
		ObjRender_SetScaleXYZ(obj,ObjRender_GetScaleX(parent),ObjRender_GetScaleY(parent),0);
		
		yield;
		
		ObjRender_SetAlpha(obj,255);
	}
	
	Obj_Delete(obj);
}