using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Unity.Simulation.VehicleControllers
{
///
/// A helper class for fetching the various interfaces with specific message types.
///
public static class InterfaceFetchingExtensions
{
///
/// Return the controller interface for the provided type of message attahced to this object.
///
public static IRobotController FetchControllerWithMessageType(this MonoBehaviour script) where T : IControlMessage
{
var components = script.GetComponents();
if (components == null || components.Length == 0)
Debug.LogError("No MonoBehaviours found on the GameObject!");
var validComponents = components.OfType>();
if (validComponents == null || validComponents.Count() != 1)
Debug.LogError($"Couldn't find the {typeof(IRobotController).Name} controller!");
return validComponents.First();
}
}
}