示例#1
0
        void sensorDetectionEvents()
        {
            var detectedEnumerator = DetectedObjects.GetEnumerator();

            while (detectedEnumerator.MoveNext())
            {
                var go = detectedEnumerator.Current;
                if (previousDetectedObjects.Contains(go))
                {
                    previousDetectedObjects.Remove(go);
                }
                else
                {
                    // This is a newly detected object
                    OnDetected.Invoke(go);
                }
            }

            // Any object still in previousDetectedObjects is no longer detected
            var previousDetectedEnumerator = previousDetectedObjects.GetEnumerator();

            while (previousDetectedEnumerator.MoveNext())
            {
                var go = previousDetectedEnumerator.Current;
                OnLostDetection.Invoke(go);
            }

            previousDetectedObjects.Clear();
            detectedEnumerator = DetectedObjects.GetEnumerator();
            while (detectedEnumerator.MoveNext())
            {
                previousDetectedObjects.Add(detectedEnumerator.Current);
            }
        }
示例#2
0
        void testSensor()
        {
            prepareCollidersBuffer();

            var numberDetected = Physics.OverlapSphereNonAlloc(transform.position, SensorRange, collidersBuffer, DetectsOnLayers);

            if (numberDetected == CurrentBufferSize)
            {
                if (DynamicallyIncreaseBufferSize)
                {
                    CurrentBufferSize *= 2;
                    testSensor();
                    return;
                }
                else
                {
                    logInsufficientBufferSize();
                }
            }

            clearColliders();

            for (int i = 0; i < numberDetected; i++)
            {
                var newDetection = addCollider(collidersBuffer[i]);
                if (newDetection != null)
                {
                    if (previousDetectedObjects.Contains(newDetection))
                    {
                        previousDetectedObjects.Remove(newDetection);
                    }
                    else
                    {
                        OnDetected.Invoke(newDetection, this);
                    }
                }
            }

            // Any entries still in previousDetectedObjects are no longer detected
            var previousDetectedEnumerator = previousDetectedObjects.GetEnumerator();

            while (previousDetectedEnumerator.MoveNext())
            {
                var lostDetection = previousDetectedEnumerator.Current;
                OnLostDetection.Invoke(lostDetection, this);
            }

            previousDetectedObjects.Clear();
            var detectedEnumerator = DetectedObjects.GetEnumerator();

            while (detectedEnumerator.MoveNext())
            {
                previousDetectedObjects.Add(detectedEnumerator.Current);
            }

            if (OnSensorUpdate != null)
            {
                OnSensorUpdate();
            }
        }
示例#3
0
        // Returns true when the passed GameObject is currently detected by the sensor, false otherwise.
        public virtual bool IsDetected(GameObject go)
        {
            var detectedEnumerator = DetectedObjects.GetEnumerator();

            while (detectedEnumerator.MoveNext())
            {
                if (detectedEnumerator.Current == go)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#4
0
        void testSensor()
        {
            clearColliders();
            var sensedColliders = Physics2D.OverlapCircleAll(transform.position, SensorRange, DetectsOnLayers);

            for (int i = 0; i < sensedColliders.Length; i++)
            {
                var newDetection = addCollider(sensedColliders[i]);
                if (newDetection != null)
                {
                    if (previousDetectedObjects.Contains(newDetection))
                    {
                        previousDetectedObjects.Remove(newDetection);
                    }
                    else
                    {
                        OnDetected.Invoke(newDetection);
                    }
                }
            }

            // Any entries still in previousDetectedObjects are no longer detected
            var previousDetectedEnumerator = previousDetectedObjects.GetEnumerator();

            while (previousDetectedEnumerator.MoveNext())
            {
                var lostDetection = previousDetectedEnumerator.Current;
                OnLostDetection.Invoke(lostDetection);
            }

            previousDetectedObjects.Clear();
            var detectedEnumerator = DetectedObjects.GetEnumerator();

            while (detectedEnumerator.MoveNext())
            {
                previousDetectedObjects.Add(detectedEnumerator.Current);
            }

            if (OnSensorUpdate != null)
            {
                OnSensorUpdate();
            }
        }
示例#5
0
        private void SensorDetectionEvents()
        {
            var detectedEnumerator = DetectedObjects.GetEnumerator();

            try {
                while (detectedEnumerator.MoveNext())
                {
                    var go = detectedEnumerator.Current;
                    if (_previousDetectedObjects.Contains(go))
                    {
                        _previousDetectedObjects.Remove(go);
                    }
                    //else {
                    //    // This is a newly detected object
                    //    //OnDetected.Invoke(go);
                    //}
                }
            }
            finally {
                detectedEnumerator.Dispose();
            }
            // Any object still in previousDetectedObjects is no longer detected
            //var previousDetectedEnumerator = _previousDetectedObjects.GetEnumerator();
            //while (previousDetectedEnumerator.MoveNext()) {
            //    //var go = previousDetectedEnumerator.Current;
            //    //OnLostDetection.Invoke(go);
            //}
            _previousDetectedObjects.Clear();
            detectedEnumerator = DetectedObjects.GetEnumerator();
            try {
                while (detectedEnumerator.MoveNext())
                {
                    _previousDetectedObjects.Add(detectedEnumerator.Current);
                }
            }
            finally {
                detectedEnumerator.Dispose();
            }
        }
示例#6
0
        void sensorDetectionEvents()
        {
            // User may call 'DetectedObjects' while enumerating, so we want to copy list of detected objects
            // out first. or we'll get an exception that enumeration is modified.
            tempGOList.Clear();
            tempGOList.AddRange(DetectedObjects);
            var detectedEnumerator = tempGOList.GetEnumerator();

            while (detectedEnumerator.MoveNext())
            {
                var go = detectedEnumerator.Current;
                if (previousDetectedObjects.Contains(go))
                {
                    previousDetectedObjects.Remove(go);
                }
                else
                {
                    // This is a newly detected object
                    OnDetected.Invoke(go, this);
                }
            }

            // Any object still in previousDetectedObjects is no longer detected
            var previousDetectedEnumerator = previousDetectedObjects.GetEnumerator();

            while (previousDetectedEnumerator.MoveNext())
            {
                var go = previousDetectedEnumerator.Current;
                OnLostDetection.Invoke(go, this);
            }

            previousDetectedObjects.Clear();
            detectedEnumerator = DetectedObjects.GetEnumerator();
            while (detectedEnumerator.MoveNext())
            {
                previousDetectedObjects.Add(detectedEnumerator.Current);
            }
        }