Ejemplo n.º 1
0
        public virtual void FixedUpdate()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif

            if (Collisions.Count == 0 && SurfaceCollisions.Count == 0 &&
                _notifiedCollisions.Count == 0 && _notifiedSurfaceCollisions.Count == 0)
            {
                enabled = false;
                return;
            }

            // Remove collisions that were recorded last update but not this one and invoke their "exit" events
            Collisions.RemoveAll(CollisionsRemover);

            // Move this update's collision list to the last update's
            Collisions = _notifiedCollisions;

            // Invoke their "stay" events if they still fulfill IsSolid
            for (var i = Collisions.Count - 1; i >= 0; i = --i >= Collisions.Count ? Collisions.Count - 1 : i)
            {
                var collision = Collisions[i];
                if (IsSolid(collision))
                {
                    OnPlatformStay.Invoke(collision);
                }
            }

            // Make room in the collision list for the next update
            _notifiedCollisions = new List <TerrainCastHit>();

            // Remove surface collisions that were recorded last update but not this one. Invoke their "exit" events
            SurfaceCollisions.RemoveAll(SurfaceCollisionsRemover);

            // Move this update's surface collision list to the last update's
            SurfaceCollisions = _notifiedSurfaceCollisions;

            // Invoke their "stay" events if they still fulfill IsOnSurface
            for (var i = SurfaceCollisions.Count - 1; i >= 0;
                 i = --i >= SurfaceCollisions.Count ? SurfaceCollisions.Count - 1 : i)
            {
                var collision = SurfaceCollisions[i];
                if (IsOnSurface(collision))
                {
                    OnSurfaceStay.Invoke(collision);
                }
            }

            // Make room in the surface collision list for the next update
            _notifiedSurfaceCollisions = new List <TerrainCastHit>();

            if (Collisions.Count == 0 && SurfaceCollisions.Count == 0)
            {
                enabled = false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Lets the trigger know about a controller standing on its surface.
        /// </summary>
        /// <param name="hit">The collision data.</param>
        public void NotifySurfaceCollision(TerrainCastHit hit, bool bubble)
        {
            if (!IsOnSurface(hit))
            {
                return;
            }

            for (var i = 0; i < SurfaceCollisions.Count; ++i)
            {
                var collision = SurfaceCollisions[i];
                if (collision.Controller != hit.Controller)
                {
                    continue;
                }

                for (i = 0; i < _notifiedSurfaceCollisions.Count; ++i)
                {
                    var notifiedCollision = _notifiedSurfaceCollisions[i];
                    if (notifiedCollision.Controller == hit.Controller)
                    {
                        goto bubble;
                    }
                }

                _notifiedSurfaceCollisions.Add(hit);
                goto bubble;
            }

            if (SurfaceEnterSound != null)
            {
                SoundManager.Instance.PlayClipAtPoint(SurfaceEnterSound, transform.position);
            }
            SurfaceCollisions.Add(hit);
            _notifiedSurfaceCollisions.Add(hit);
            OnSurfaceEnter.Invoke(hit);

            if (SurfaceCollisions.Count == 1 && !enabled)
            {
                enabled = true;
            }

bubble:
            if (!bubble)
            {
                return;
            }
            for (var i = 0; i < Parents.Count; ++i)
            {
                var parent = Parents[i];
                if (!ReceivesEvents(parent, transform))
                {
                    return;
                }
                parent.NotifySurfaceCollision(hit, false);
            }
        }
Ejemplo n.º 3
0
        private bool SurfaceCollisionsRemover(TerrainCastHit hit)
        {
            for (var i = 0; i < _notifiedSurfaceCollisions.Count; ++i)
            {
                if (_notifiedSurfaceCollisions[i].Controller == hit.Controller)
                {
                    return(false);
                }
            }

            if (SurfaceExitSound != null)
            {
                SoundManager.Instance.PlayClipAtPoint(SurfaceExitSound, transform.position);
            }
            OnSurfaceExit.Invoke(hit);
            return(true);
        }