let gay = GetCurrentScriptDirectory ~ "./Render/img/fly.png";
LoadTexture(gay);

function OVNI(x, y, num, sc){
	let orkut = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(orkut);
	
	setpos(orkut, x, y);
	renderUFO(orkut, num, sc);
	hitbox(orkut, 20);
	
	return orkut;
}

task renderUFO(obj, num, sc){

	let wi = 64;
	let he = 64;
	let n = min(num, 3);
	
	let dir;
	let speed;
	let whichway = 0;
	let idleframe = 0;
	let frame = 0;

	ObjPrim_SetTexture(obj, gay);
	ObjSprite2D_SetSourceRect(obj, 0, 0, wi, he);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, sc, sc, 0);
	ObjRender_SetAngleXYZ(obj, 0, 180, 0);
	
	while(!Obj_IsDeleted(obj)){
		
		dir = ObjMove_GetAngle(obj);
		speed = ObjMove_GetSpeed(obj);

		if(speed == 0 && frame > 0){whichway = -1;}
		else if(speed == 0 && frame < 0){whichway = 1;}
		else if(speed == 0 && frame == 0){whichway = 0;}

		else if(cos(dir) < 0){whichway=-1;}
		else if(cos(dir) > 0){whichway=1;}
		
		if(speed == 0 && frame == 0){

			frame = 0;
			if(idleframe >= 00 && idleframe < 10){ObjSprite2D_SetSourceRect(obj, 000, 64*n, 063, 64*n+63);}
			else if(idleframe >= 10 && idleframe < 20){ObjSprite2D_SetSourceRect(obj, 064, 64*n, 127, 64*n+63);}
			else if(idleframe >= 20 && idleframe < 30){ObjSprite2D_SetSourceRect(obj, 128, 64*n, 191, 64*n+63);}
			else if(idleframe >= 30 && idleframe < 40){ObjSprite2D_SetSourceRect(obj, 192, 64*n, 255, 64*n+63);}
			if(idleframe < 40){idleframe += 2;}
			else{idleframe = 0;}
		}
		
		else if(speed != 0 || frame != 0){
		
			if(frame < -00 && frame >= -05){ObjSprite2D_SetSourceRect(obj, 000, 64*n, 063, 64*n+63); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -05 && frame >= -10){ObjSprite2D_SetSourceRect(obj, 064, 64*n, 127, 64*n+63); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -10 && frame >= -15){ObjSprite2D_SetSourceRect(obj, 128, 64*n, 191, 64*n+63); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -15 && frame >= -20){ObjSprite2D_SetSourceRect(obj, 192, 64*n, 255, 64*n+63); ObjRender_SetAngleXYZ(obj,0,0,0);}
			
			if(frame > 00 && frame <= 05){ObjSprite2D_SetSourceRect(obj, 000, 64*n, 063, 64*n+63); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame > 05 && frame <= 10){ObjSprite2D_SetSourceRect(obj, 064, 64*n, 127, 64*n+63); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame > 10 && frame <= 15){ObjSprite2D_SetSourceRect(obj, 128, 64*n, 191, 64*n+63); ObjRender_SetAngleXYZ(obj,0,180,0);}
			if(frame > 15 && frame <= 20){ObjSprite2D_SetSourceRect(obj, 192, 64*n, 255, 64*n+63); ObjRender_SetAngleXYZ(obj,0,180,0);}
			idleframe = 0;
			
		}
		
		frame = frame + whichway*2;
		if(frame > +20){frame = +20;}
		if(frame < -20){frame = -20;}

		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
	
	Obj_Delete(obj);
}

task hitbox(obj, rad){
	while(!Obj_IsDeleted(obj)){		
		ObjEnemy_SetIntersectionCircleToShot(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), rad); 
		ObjEnemy_SetIntersectionCircleToPlayer(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), rad);
		yield;
	}
	
	Obj_Delete(obj);
}