Beispiel #1
0
        /// <summary>
        /// See if a move is allowed by a figure.
        /// </summary>
        /// <param name="figure">The figure to move.</param>
        /// <param name="move">The desired move amount.</param>
        /// <returns>Whether the move is valid.</returns>
        public static bool IsMoveAllowed(Figure figure, Vector2 move, List<Block> blocks)
        {
            //Set some startup variables.
            bool valid = false;

            //Project the current figure to the new position and see whether the move was valid.
            figure.Move(move);
            valid = !blocks.Exists(block => figure.Intersects(block));
            figure.Move(-move);

            //Return the result.
            return valid;
        }
Beispiel #2
0
        /// <summary>
        /// See if a rotation is allowed by a figure.
        /// </summary>
        /// <returns>Whether the rotation is valid.</returns>
        public static bool IsRotationAllowed(Figure figure, List<Block> blocks)
        {
            //Project the current figure to the new position.
            var proj = new Figure(figure) { Left = figure.Left, Bottom = figure.Bottom };
            proj.Rotate();

            //Return whether the movement is valid.
            return !blocks.Exists(block => !figure.Blocks.Contains(block) && proj.Intersects(block));
        }
Beispiel #3
0
        /// <summary>
        /// See if a move is allowed by a figure.
        /// </summary>
        /// <param name="figure">The figure to move.</param>
        /// <param name="move">The desired move amount.</param>
        /// <param name="assist">The move assist.</param>
        /// <param name="allowNegativeY">Whether to allow the figure to find a position above the current one.</param>
        /// <returns>Whether the move is valid.</returns>
        public static bool IsMoveAllowed(Figure figure, Vector2 move, List<Block> blocks, out Vector2 assist, bool allowNegativeY)
        {
            //Set some startup variables.
            bool valid = false;
            assist = move;

            //Try to find an acceptable position by granting the figure some leeway.
            for (int x = 0; x <= WIDTH / 2; x++)
            {
                for (int y = 0; y <= HEIGHT / 2; y++)
                {
                    //Do four tests; (x, y), (x, -y), (-x, y), (-x, -y).
                    for (int n = 0; n < 4; n++)
                    {
                        //Decide upon the x, y configuration.
                        Vector2 config = Vector2.Zero;
                        switch (n)
                        {
                            case 0: { config = new Vector2(x, y); break; }
                            case 1: { config = allowNegativeY ? new Vector2(x, -y) : new Vector2(x, y); break; }
                            case 2: { config = new Vector2(-x, y); break; }
                            case 3: { config = allowNegativeY ? new Vector2(-x, -y) : new Vector2(-x, y); break; }
                        }

                        //Project the current figure to the new position and see whether the move was valid.
                        figure.Move(move + config);
                        valid = !blocks.Exists(block => figure.Intersects(block));
                        figure.Move(-(move + config));

                        //If the move was valid, stop here.
                        if (valid) { assist = move + config; return valid; }
                    }
                }
            }

            //Return the result (Hint: fail).
            return valid;
        }