Esempio 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;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Lets the trigger know about a collision with a controller.
        /// </summary>
        /// <param name="hit">The collision data.</param>
        /// <param name="bubble"></param>
        public void NotifyCollision(TerrainCastHit hit, bool bubble)
        {
            if (!IsSolid(hit))
            {
                return;
            }

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

                    _notifiedCollisions.Add(hit);
                    goto bubble;
                }
            }

            if (PlatformEnterSound != null)
            {
                SoundManager.Instance.PlayClipAtPoint(PlatformEnterSound, transform.position);
            }

            Collisions.Add(hit);
            _notifiedCollisions.Add(hit);
            OnPlatformEnter.Invoke(hit);

            if (Collisions.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))
                {
                    continue;
                }
                parent.NotifyCollision(hit, false);
            }
        }
Esempio n. 3
0
        private bool CollisionsRemover(TerrainCastHit hit)
        {
            for (var i = 0; i < _notifiedCollisions.Count; ++i)
            {
                if (_notifiedCollisions[i].Controller == hit.Controller)
                {
                    return(false);
                }
            }

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