Beispiel #1
0
        public virtual void OnCollision(Collision collision)
        {
            if (!collision.SecondObject.Collidable || !Parent.Collidable || !Parent.CanCollideWith(collision.SecondObject))
                return;

            VectorRectangle bounds = Parent.Bounds;
            VectorRectangle lastFrame = Parent.LastFrameBounds;
            Vector2 size = bounds.Size;

            float vX, vY, aX, aY;
            vX = vY = aX = aY = 1;

            if (
                (collision.IsBottom() || collision.IsTop())
                && !collision.IsLeft()
                && !collision.IsRight()
                )
            {
                vY = -collision.SecondObject.Elasticity;

                float newY;

                if (collision.IsBottom())
                {
                    newY = collision.SecondObject.Position.Y - size.Y;
                }
                else
                {
                    newY = collision.SecondObject.Position.Y + collision.SecondObject.Size.Y;
                }
                Parent.Position = new Vector2(Parent.Position.X, newY);
            }

            if (
                (collision.IsLeft() || collision.IsRight())
                && !collision.IsTop()
                && !collision.IsBottom()
                )
            {
                vX = 0;

                float newX;

                if (collision.IsLeft())
                {
                    newX = collision.SecondObject.Position.X + collision.SecondObject.Size.X;
                }
                else
                {
                    newX = collision.SecondObject.Position.X - size.X;
                }
                Parent.Position = new Vector2(newX, Parent.Position.Y);
            }

            if (collision.TouchingSidesCount == 2)
            {
                float width = collision.Intersect.Width, height = collision.Intersect.Height;

                if (lastFrame.IntersectsVertical(collision.SecondObject.Bounds) && !lastFrame.IntersectsHorizontal(collision.SecondObject.Bounds))    // TODO also previous frame
                {
                    height = 0;
                    vX = 0;
                    collision.OnStopColliding(Collision.Side.Top);
                    collision.OnStopColliding(Collision.Side.Bottom);
                    Console.WriteLine("Stopping horizontal");
                }
                else if (lastFrame.IntersectsHorizontal(collision.SecondObject.Bounds))
                {
                    width = 0;
                    vY = 0;
                    collision.OnStopColliding(Collision.Side.Left);
                    collision.OnStopColliding(Collision.Side.Right);
                    Console.WriteLine("Stopping vertical");
                }
                else
                {
                    Parent.OnDecollision(collision.SecondObject);
                }

                if (collision.IsRight())
                    width *= -1;
                if (collision.IsBottom())
                    height *= -1;

                Parent.Position += new Vector2(width, height);
            }

            Parent.Velocity *= new Vector2(vX, vY);
            Parent.Acceleration *= new Vector2(aX, aY);

            //TODO: Make _parent.Velocity.Y equal to Zero when value drops below sertain value to decrease hovering
            //above the platform he is standing on.
            if (Parent.Velocity.Y < 0 && Parent.Velocity.Y > -6)
                Parent.Velocity = new Vector2(Parent.Velocity.X, 0);
            //END OF TODO

            //Console.WriteLine("Collision");
        }