/// <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); } }
/// <summary> /// Move in the facing direction /// </summary> /// <returns>If the character could move there</returns> public bool Move() { // Find the related direction Point pos = Map.DirectionRelation[mDirection]; // Add it to the current position pos = new Point(pos.X + Position.X, pos.Y + Position.Y); // Make sure that it's possible to walk there and that the character is not already moving somewhere bool walkable = IsAtWantedPosition() && Map.IsWalkable(pos); if (walkable) { mWantedPosition = pos; } return(walkable); }