Beispiel #1
0
        /// <summary>
        /// sdfs
        /// </summary>
        protected void HandleCollisions()
        {
            Rectangle bounds = EnemyRect;

            isStanding = false;

            //Finding neighboring blocks
            int leftBlock   = (int)Math.Floor((float)bounds.Left / Block.Width);
            int rightBlock  = (int)Math.Ceiling(((float)bounds.Right / Block.Width)) - 1;
            int topBlock    = (int)Math.Floor((float)bounds.Top / Block.Height);
            int bottomBlock = (int)Math.Ceiling(((float)bounds.Bottom / Block.Height)) - 1;

            //Loop through each of the possible block collisions
            for (int y = topBlock; y <= bottomBlock; y++)
            {
                for (int x = leftBlock; x <= rightBlock; x++)
                {
                    //Checking collision type of block at the given coordinates
                    BlockCollision blockCollision = Level.GetCollision(x, y);
                    if (blockCollision != BlockCollision.Passable)
                    {
                        Rectangle blockRect = new Rectangle(x * Block.Width, y * Block.Height,
                                                            Block.Width, Block.Height);

                        //Check for collision
                        Vector2 depth = GameMath.CollisionDepth(bounds, blockRect);
                        if (depth != Vector2.Zero)
                        {
                            if (Math.Abs(depth.Y) <= Math.Abs(depth.X) || blockCollision == BlockCollision.Platform)
                            {
                                //Check if player is on the ground
                                if (previousBottom <= blockRect.Top)
                                {
                                    isStanding = true;
                                }

                                // Ignore platforms, unless we are on the ground
                                if (blockCollision == BlockCollision.Impassable || IsStanding)
                                {
                                    //resolve the collision along the Y axis
                                    Position   = new Vector2(Position.X, Position.Y + depth.Y);
                                    velocity.Y = 0;

                                    //Update bounds
                                    bounds = EnemyRect;
                                }
                            }

                            else if (blockCollision == BlockCollision.Impassable) // Ignore platforms
                            {
                                // Resolve the collision along the X axis.
                                Position = new Vector2(Position.X + depth.X, Position.Y);
                                hitWall  = true;

                                //Update bounds
                                bounds = EnemyRect;
                            }
                        }
                    }
                }
            }

            // Save the new bounds bottom.
            previousBottom = bounds.Bottom;
        }