Ejemplo n.º 1
0
        public void ResolveCollision(ICollider other)
        {
            if (other == this || !other.CollideRectangle.IntersectsWith(CollideRectangle))
            {
                return;
            }

            OnTrigger?.Invoke(other);
            other.InvokeTrigger(this);
            if (Static || !Collision || !other.Collision)
            {
                return;
            }
            OnCollision?.Invoke(other);
            other.InvokeCollision(this);

            var intersectionType = GetIntersectionType(CollideRectangle, other.CollideRectangle);

            if ((intersectionType == IntersectionType.Right ||
                 intersectionType == IntersectionType.Left) && other.Static)
            {
                Velocity.X = 0;
            }
            switch (intersectionType)
            {
            case IntersectionType.Left:
                SetX(other.CollideRectangle.Left - CollideRectangle.Width);
                break;

            case IntersectionType.Right:
                SetX(other.CollideRectangle.Right);
                break;

            case IntersectionType.Top:
                SetY(other.CollideRectangle.Top - CollideRectangle.Height + other.Velocity.Y);
                //Position.Y = other.CollideRectangle.Top - CollideRectangle.Height;
                if (Velocity.Y > 0)
                {
                    Velocity.Y = 0;
                }
                break;

            case IntersectionType.Bottom:
                SetY(other.CollideRectangle.Bottom);
                if (Velocity.Y < 0)
                {
                    Velocity.Y = 0;
                }
                break;
            }
        }