Esempio n. 1
0
        private static Object2Side DetermineOverlappingCollisionSide(Rectangle collisionIntersection, Rectangle obj1Dest, Rectangle obj2Dest)
        {
            Object2Side side = Object2Side.NoCollision;
            float       percentOfObject1Height = (float)collisionIntersection.Height / ((float)obj1Dest.Height);
            float       percentOfObject1Width  = (float)collisionIntersection.Width / ((float)obj1Dest.Width);
            float       percentOfObject2Height = (float)collisionIntersection.Height / ((float)obj2Dest.Height);
            float       percentOfObject2Width  = (float)collisionIntersection.Width / ((float)obj2Dest.Width);

            if (percentOfObject2Height > percentOfObject2Width || percentOfObject1Height > percentOfObject1Width)
            {
                if (collisionIntersection.X == obj2Dest.X)
                {
                    side = Object2Side.Left;
                }
                else
                {
                    side = Object2Side.Right;
                }
            }
            else
            {
                if (collisionIntersection.Y == obj2Dest.Y)
                {
                    side = Object2Side.Top;
                }
                else
                {
                    side = Object2Side.Bottom;
                }
            }
            return(side);
        }
Esempio n. 2
0
 public void HandleMarioBlockCollisions()
 {
     for (int i = 0; i < blockCollisions.Count; i++)
     {
         IGameObject mario      = blockCollisions[i].Item1;
         IGameObject block1     = blockCollisions[i].Item2;
         Rectangle   marioDest  = mario.Destination;
         Rectangle   block1Dest = block1.Destination;
         if (i + 1 < blockCollisions.Count)
         {
             IGameObject block2 = blockCollisions[i + 1].Item2;
             if (block2.Destination.X - block1Dest.X <= GameUtilities.BlockSize && block1Dest.Y == block2.Destination.Y &&
                 !DetermineToHandleFirstBlockCollision(marioDest, block1Dest, block2.Destination))
             {
                 continue;
             }
         }
         Object2Side side = DetermineCollisionSide(marioDest, block1Dest);
         Tuple <ObjectType, ObjectType, Object2Side> collision = new Tuple <ObjectType, ObjectType, Object2Side>(mario.Type, block1.Type, side);
         if (collisionDictionary.ContainsKey(collision))
         {
             collisionDictionary[collision].Execute(mario, block1);
         }
     }
     blockCollisions.Clear();
 }
Esempio n. 3
0
        private bool TestCollisionSide(string outputDetails, Object2Side actualCollision, Object2Side expectedCollision)
        {
            bool isCorrectSide = actualCollision == expectedCollision;

            Console.WriteLine(outputDetails + isCorrectSide);
            return(isCorrectSide);
        }
Esempio n. 4
0
        public void DetectMarioBlockCollisions(IGameObject object1, IGameObject object2)
        {
            Object2Side side = DetermineCollisionSide(object1.Destination, object2.Destination);

            if (side != Object2Side.NoCollision)
            {
                blockCollisions.Add(new Tuple <IGameObject, IGameObject>(object1, object2));
            }
        }
Esempio n. 5
0
        private static void ParseXMLObject(XmlNode xmlObject, ICollisionHandler collisionHandler)
        {
            ObjectType        object1Type = (ObjectType)Enum.Parse(typeof(ObjectType), xmlObject["object1"].FirstChild.Value);
            ObjectType        object2Type = (ObjectType)Enum.Parse(typeof(ObjectType), xmlObject["object2"].FirstChild.Value);
            Object2Side       side        = (Object2Side)Enum.Parse(typeof(Object2Side), xmlObject["side"].FirstChild.Value);
            ICollisionCommand command     = commandDictionary[xmlObject["command"].FirstChild.Value];

            collisionHandler.RegisterCommand(new Tuple <ObjectType, ObjectType, Object2Side>(object1Type, object2Type, side), command);
        }
Esempio n. 6
0
        public bool HandleCollision(IGameObject object1, IGameObject object2)
        {
            Object2Side side = DetermineCollisionSide(object1.Destination, object2.Destination);

            if (side != Object2Side.NoCollision)
            {
                Tuple <ObjectType, ObjectType, Object2Side> collision = new Tuple <ObjectType, ObjectType, Object2Side>(object1.Type, object2.Type, side);
                if (collisionDictionary.ContainsKey(collision))
                {
                    collisionDictionary[collision].Execute(object1, object2);
                }
            }
            return(side != Object2Side.NoCollision);
        }
Esempio n. 7
0
        public static Object2Side DetermineCollisionSide(Rectangle obj1Dest, Rectangle obj2Dest)
        {
            Object2Side side = Object2Side.NoCollision;
            Rectangle   collisionIntersection = Rectangle.Intersect(obj1Dest, obj2Dest);

            if (collisionIntersection.Width != 0 || collisionIntersection.Height != 0)
            {
                side = DetermineOverlappingCollisionSide(collisionIntersection, obj1Dest, obj2Dest);
            }
            else
            {
                side = DetermineBorderOrNoCollisionSide(obj1Dest, obj2Dest);
            }
            return(side);
        }
Esempio n. 8
0
        private static Object2Side DetermineBorderOrNoCollisionSide(Rectangle obj1Dest, Rectangle obj2Dest)
        {
            Object2Side side = Object2Side.NoCollision;

            if (obj1Dest.Y + obj1Dest.Height == obj2Dest.Y && HasHorizontalIntersection(obj1Dest, obj2Dest))
            {
                side = Object2Side.Top;
            }
            else if (obj1Dest.Y == obj2Dest.Y + obj2Dest.Height && HasHorizontalIntersection(obj1Dest, obj2Dest))
            {
                side = Object2Side.Bottom;
            }
            else if (obj1Dest.X + obj1Dest.Width == obj2Dest.X && HasVerticalIntersection(obj1Dest, obj2Dest))
            {
                side = Object2Side.Left;
            }
            else if (obj1Dest.X == obj2Dest.X + obj2Dest.Width && HasVerticalIntersection(obj1Dest, obj2Dest))
            {
                side = Object2Side.Right;
            }
            return(side);
        }