コード例 #1
0
        private static void handleItemMovement(IItemObjects item, IBlock block, ICollision side)
        {
            Rectangle blockRectangle        = block.returnCollisionRectangle();
            Rectangle itemRectangle         = item.returnCollisionRectangle();
            Rectangle intersectionRectangle = Rectangle.Intersect(itemRectangle, blockRectangle);
            Vector2   itemLocation          = item.returnLocation();
            int       locationDiffToChange  = UtilityClass.zero;

            if (side.returnCollisionSide().Equals(CollisionSide.Left))
            {
                locationDiffToChange = intersectionRectangle.Width;
                int newItemX = (int)itemLocation.X - locationDiffToChange;
                item.updateLocation(new Vector2((newItemX), itemLocation.Y));
                item.RigidBody().RightCollision();
            }
            else if (side.returnCollisionSide().Equals(CollisionSide.Right))
            {
                locationDiffToChange = intersectionRectangle.Width;
                int newItemX = (int)itemLocation.X + locationDiffToChange;
                item.updateLocation(new Vector2((newItemX), itemLocation.Y));
                item.RigidBody().LeftCollision();
            }
            else if (side.returnCollisionSide().Equals(CollisionSide.Top))
            {
                locationDiffToChange = intersectionRectangle.Height;
                int newItemY = (int)itemLocation.Y - locationDiffToChange;
                item.updateLocation(new Vector2(itemLocation.X, newItemY));
                item.RigidBody().BottomCollision();
            }
            else if (side.returnCollisionSide().Equals(CollisionSide.Bottom))
            {
                locationDiffToChange = intersectionRectangle.Height;
                int newItemY = (int)itemLocation.Y + locationDiffToChange;
                item.updateLocation(new Vector2(itemLocation.X, newItemY));
                item.RigidBody().TopCollision();
            }
        }