コード例 #1
0
        //------------------------------------------------------------------------/
        // Routines
        //------------------------------------------------------------------------/
        /// <summary>
        /// The main procedure for the sensor
        /// </summary>
        /// <returns></returns>
        private bool ScanForInteractables()
        {
            Collider[] castResults = Physics.OverlapSphere(this.scanPosition, this.range);
            List <InteractionQuery> interactions = new List <InteractionQuery>();
            bool foundInteractions = false;

            // The scan event that will be sent
            InteractableDetectedEvent detectEvent = new InteractableDetectedEvent(this);

            if (castResults != null)
            {
                foreach (var hit in castResults)
                {
                    // Check whether it can be detected, depending on the scanmode
                    bool detected = Detect(hit.transform);
                    if (!detected)
                    {
                        continue;
                    }

                    // Check whether it is an interactable
                    StratusInteractable interactable = hit.transform.gameObject.GetComponent <StratusInteractable>();
                    if (interactable == null)
                    {
                        continue;
                    }

                    // Scan it
                    interactable.gameObject.Dispatch <InteractableDetectedEvent>(detectEvent);

                    // Create a query object, filling with scanned data
                    InteractionQuery query = new InteractionQuery();
                    query.data         = detectEvent.scanData;
                    query.distance     = Vector3.Distance(root.position, hit.transform.position);
                    query.interactable = interactable;

                    // Now store the interaction
                    interactions.Add(query);

                    // Note that we have scanned something
                    foundInteractions = true;
                }
            }

            // Sort the interactions by the closest one
            interactions.Sort((a, b) => a.distance.CompareTo(b.distance));
            // Save the current interactions
            interactivesInRange = interactions.ToArray();

            // Now inform the agent of the current results
            onScan?.Invoke(foundInteractions);
            return(foundInteractions);
        }
コード例 #2
0
        /// <summary>
        /// Interacts with the given interactable if within interaction radius
        /// </summary>
        /// <param name="interactable"></param>
        public void Interact(StratusInteractable interactable)
        {
            if (!CanInteract(interactable.transform))
            {
                if (showDebug)
                {
                    StratusDebug.Log($"Out of interaction range from {interactable.name}", this);
                }
                return;
            }

            interactable.gameObject.Dispatch <InteractEvent>(this.interactEvent);

            if (showDebug)
            {
                StratusDebug.Log($"Interacted with {interactable.name}", this);
            }
        }