Example #1
0
 /// <summary>
 /// Checks whether current direction is possible
 /// </summary>
 /// <param name="direction">Direction</param>
 /// <param name="possibleDirections">Possible directions</param>
 /// <returns>
 ///    TRUE = Direction possible.
 ///    FALSE = Direction is not possible.
 /// </returns>
 protected bool IsCurrentDirectionPossible(int direction, CharacterPossibleDirections possibleDirections)
 {
     if (direction == (int)possibleDirections.Left)
     {
         return(true);
     }
     else if (direction == (int)possibleDirections.Right)
     {
         return(true);
     }
     else if (direction == (int)possibleDirections.Down)
     {
         return(true);
     }
     else if (direction == (int)possibleDirections.Up)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #2
0
        /// <summary>
        /// Get the amount of possible directions.
        /// </summary>
        /// <param name="directions">Direction table</param>
        /// <returns>Possible directions</returns>
        protected int AmoutOfPossibleDirections(CharacterPossibleDirections directions)
        {
            int count = 0;

            if (directions.Down != 0)
            {
                count++;
            }
            if (directions.Up != 0)
            {
                count++;
            }
            if (directions.Left != 0)
            {
                count++;
            }
            if (directions.Right != 0)
            {
                count++;
            }

            return(count);
        }
Example #3
0
        /// <summary>
        /// Handles Hero movement (own handler for the Hero).
        /// </summary>
        public override void Move()
        {
            CharacterPossibleDirections directions = GetPossibleDirections();

            if (creatureDirectionPref != (int)Directions.NA &&
                IsCurrentDirectionPossible(creatureDirectionPref, directions) &&
                allowDirectionChange == true)
            {
                creatureDirection     = creatureDirectionPref;
                creatureDirectionPref = (int)Directions.NA;
            }

            if (IsCurrentDirectionPossible(creatureDirection, directions))
            {
                switch (creatureDirection)
                {
                case (int)Directions.LEFT:
                    coordinates.X -= 2;
                    break;

                case (int)Directions.RIGHT:
                    coordinates.X += 2;
                    break;

                case (int)Directions.DOWN:
                    coordinates.Y += 2;
                    break;

                case (int)Directions.UP:
                    coordinates.Y -= 2;
                    break;

                default:
                    break;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Gets the possible directions, based on the game board layout.
        /// </summary>
        /// <returns>Possible directions.</returns>
        protected CharacterPossibleDirections GetPossibleDirections()
        {
            CharacterPossibleDirections direction    = new CharacterPossibleDirections();
            CharacterGameBoardLocation  charLocation = GetCharacterGameBoardLocation();

            if (!gameBoardLayout[charLocation.Row, charLocation.Col + 1].Equals('W'))
            {
                direction.Right = (int)Directions.RIGHT;
            }
            if (!gameBoardLayout[charLocation.Row, charLocation.Col - 1].Equals('W'))
            {
                direction.Left = (int)Directions.LEFT;
            }
            if (!gameBoardLayout[charLocation.Row + 1, charLocation.Col].Equals('W'))
            {
                direction.Down = (int)Directions.DOWN;
            }
            if (!gameBoardLayout[charLocation.Row - 1, charLocation.Col].Equals('W'))
            {
                direction.Up = (int)Directions.UP;
            }

            return(direction);
        }
Example #5
0
        /// <summary>
        /// Handles creature movement.
        /// </summary>
        public virtual void Move()
        {
            CharacterPossibleDirections directions = GetPossibleDirections();

            // Remove illegal moves, i.e. if the creature is going right, do not allow
            // move to left, if there is still possibility to continue. This is to avoid
            // "ping-pong" effect.
            if (1 < AmoutOfPossibleDirections(directions) && allowDirectionChange == true)
            {
                if (creatureDirection == (int)Directions.RIGHT && directions.Left == (int)Directions.LEFT)
                {
                    directions.Left = (int)Directions.NA;
                }
                else if (creatureDirection == (int)Directions.LEFT && directions.Right == (int)Directions.RIGHT)
                {
                    directions.Right = (int)Directions.NA;
                }
                else if (creatureDirection == (int)Directions.UP && directions.Down == (int)Directions.DOWN)
                {
                    directions.Down = (int)Directions.NA;
                }
                else if (creatureDirection == (int)Directions.DOWN && directions.Up == (int)Directions.UP)
                {
                    directions.Up = (int)Directions.NA;
                }
            }

            // If we can move, move the creature and if direction change is
            // allowed, randomize new direction.
            if (IsCurrentDirectionPossible(creatureDirection, directions))
            {
                if (allowDirectionChange == true)
                {
                    int direction = rand.Next(1, 5);

                    while (!IsCurrentDirectionPossible(direction, directions))
                    {
                        direction = rand.Next(1, 5);
                    }

                    creatureDirection = direction;
                }

                switch (creatureDirection)
                {
                case (int)Directions.LEFT:
                    coordinates.X -= 2;
                    break;

                case (int)Directions.RIGHT:
                    coordinates.X += 2;
                    break;

                case (int)Directions.DOWN:
                    coordinates.Y += 2;
                    break;

                case (int)Directions.UP:
                    coordinates.Y -= 2;
                    break;

                default:
                    break;
                }
            }
            else
            {
                if (allowDirectionChange == true)
                {
                    int direction = rand.Next(1, 5);

                    while (!IsCurrentDirectionPossible(direction, directions))
                    {
                        direction = rand.Next(1, 5);
                    }

                    creatureDirection = direction;
                }

                switch (creatureDirection)
                {
                case (int)Directions.LEFT:
                    coordinates.X -= 2;
                    break;

                case (int)Directions.RIGHT:
                    coordinates.X += 2;
                    break;

                case (int)Directions.DOWN:
                    coordinates.Y += 2;
                    break;

                case (int)Directions.UP:
                    coordinates.Y -= 2;
                    break;

                default:
                    break;
                }
            }
        }