private List <IInteraction> GetViableInteractions(Ray ray, out InteractionEvent interactionEvent) { IInteractionSource source = GetActiveInteractionSource(); if (source == null) { interactionEvent = null; return(new List <IInteraction>()); } IInteractionTarget target = null; Vector3 point = Vector3.zero; if (Physics.Raycast(ray, out RaycastHit hit, float.PositiveInfinity, selectionMask)) { point = hit.point; GameObject targetGo = hit.transform.gameObject; target = targetGo.GetComponent <IInteractionTarget>() ?? new InteractionTargetGameObject(targetGo); if (!source.CanInteractWithTarget(target)) { interactionEvent = null; return(new List <IInteraction>()); } } interactionEvent = new InteractionEvent(source, target, point); IInteraction[] availableInteractions = source.GenerateInteractions(target != null ? new[] { target } : new IInteractionTarget[0]); InteractionEvent @event = interactionEvent; return(availableInteractions.Where(i => i.CanInteract(@event)).ToList()); }
/// <summary> /// Gets all valid interaction targets from a game object /// </summary> /// <param name="source">The source of the interaction</param> /// <param name="gameObject">The game objects the interaction targets are on</param> /// <returns>A list of all valid interaction targets</returns> private List <IInteractionTarget> GetTargetsFromGameObject(IInteractionSource source, GameObject gameObject) { List <IInteractionTarget> targets = new List <IInteractionTarget>(); // Get all target components which are not disabled and the source can interact with targets.AddRange(gameObject.GetComponents <IInteractionTarget>().Where(x => (x as MonoBehaviour)?.enabled != false && source.CanInteractWithTarget(x))); if (targets.Count < 1) { targets.Add(new InteractionTargetGameObject(gameObject)); } return(targets); }