Пример #1
0
        /// <summary>
        /// This method will find all possible directions of the enemy
        /// </summary>
        private void FindDirection()
        {
            List <Map.Direction> possibleDirections = new List <Map.Direction>();

            // Iterate all existing directions (up, down, left, right)
            foreach (KeyValuePair <Map.Direction, Point> item in Map.DirectionRelation)
            {
                // Find the relative position of the direciton and the character
                Point pos = new Point(item.Value.X + Position.X, item.Value.Y + Position.Y);

                // Check if the direciton is not the oppisite
                if (item.Key != Map.GetOppositeDirection(Direction) &&
                    // Check if the direciton is walkable
                    Map.IsWalkable(pos) &&
                    // Check if another enemy is not standing at the position
                    !Game.IsEnemyAtPosition(pos))
                {
                    // If all above succeeded, add the direction to the possible directions list
                    possibleDirections.Add(item.Key);
                }
            }

            // Make sure that there was atleast one possible directions
            if (possibleDirections.Count > 0)
            {
                // Select a random direction of the possible ones
                Direction = possibleDirections[new Random().Next(0, possibleDirections.Count)];
            }
            else
            {
                // If no possible direction was found, turn around to the opposite direction
                Direction = Map.GetOppositeDirection(Direction);
            }
        }