Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether the monster can move in the specified direction.
        /// If it can't, it will try other directions in turn until it finds a direction that is available.
        /// </summary>
        /// <param name="m">The monster that is moving</param>
        /// <param name="intendedDirection">The direction the monster hopes to move in</param>
        /// <param name="feasibleDirection">The direction the monster can move in, or Direction.None if it's unable to move</param>
        /// <returns>True if the monster can move in the intendedDirection, or False otherwise</returns>
        public static bool ConfirmDirectionToMoveIn(this Monster m, IDirectionChosen intendedDirection, out ConfirmedDirection feasibleDirection)
        {
            if (intendedDirection.Direction == Direction.None)
            {
                feasibleDirection = ConfirmedDirection.None;
                return(true);
            }

            if (intendedDirection is ConfirmedDirection confirmedDirection)
            {
                feasibleDirection = confirmedDirection;
                return(true);
            }

            if (m.CanMoveInDirection(intendedDirection.Direction))
            {
                feasibleDirection = intendedDirection.Confirm();
                return(true);
            }

            var directionToTry = new PossibleDirection(intendedDirection.Direction);

            for (int i = 0; i < 3; i++)
            {
                directionToTry = GetNextDirection(directionToTry);
                if (m.CanMoveInDirection(directionToTry.Direction))
                {
                    feasibleDirection = directionToTry.Confirm();
                    return(false);
                }
            }

            feasibleDirection = ConfirmedDirection.None;
            return(false);
        }