public void PathfindTo(MapPoint finalDestination) { Console.Clear(); bool moveDone = false; while (_movementRemaining > 0 && !moveDone) { IMappable obstruction = Location.Map.Objects.FirstOrDefault(o => MapPoint.IsPointOnLineSegment(this.Location, finalDestination, o.Location) && o != this); var destination = obstruction != null ? obstruction.Location : finalDestination; string direction = Location.DirectionTo(destination); int distance = Location.DistanceTo(destination); // if distance is greater than movement remaining, only move up to remaining movement: Move($"{direction} {(distance <= _movementRemaining ? distance : _movementRemaining)}"); // attempt to move around obstruction if present: if (obstruction != null && _movementRemaining >= 5) { var openSpace = Location.Map.GetOpenSpaces(Location.GetAdjacentCoordinates()) .RandomElement().ToMapPoint(Location.Map); var tempLocation = Location.ShallowCopy; Move($"{Location.DirectionTo(openSpace)} {5}"); } else { moveDone = true; } } Location.Map.PrintMap(); }
public void PathfindTo(IMappable target) { bool moveDone = false; while (_movementRemaining > 0 && !moveDone) { // Console.WriteLine("pathfinding"); IMappable obstruction = Location.Map.Objects.FirstOrDefault(o => MapPoint.IsPointOnLineSegment(this.Location, target.Location, o.Location) && o != this && o != target); MapPoint destination = (obstruction == null ? target : obstruction).Location; string direction = Location.DirectionTo(destination); // move up to 5 feet if item, up to attack range if entity: int distance = (Location.DistanceTo(destination) * 5) - (target is Entity ? _attackRangeFeet : 5); // if distance is greater than movement remaining, only move up to remaining movement: Move($"{direction} {(distance < _movementRemaining ? distance : _movementRemaining)}"); Location.Map.PrintMap(); PressAnyKeyToContinue(); Console.Clear(); // attempt to move around obstruction if present: if (obstruction != null && _movementRemaining >= 5) { Console.WriteLine("obstruction encounted"); var openSpace = Location.Map.GetOpenSpaces(Location.GetAdjacentCoordinates()) .RandomElement().ToMapPoint(Location.Map); var tempLocation = Location.ShallowCopy; Move($"{Location.DirectionTo(openSpace)} {5}"); Location.Map.PrintMap(); PressAnyKeyToContinue(); Console.Clear(); } else { moveDone = true; } } }
// Move actions: public virtual bool Move(string moveInput) { try { string[] moveInputArr = moveInput.ToLower().Split(' '); string direction = moveInputArr[0]; int distanceFeet = int.Parse(moveInputArr[1]); int distancePoints = distanceFeet / 5; if (distanceFeet > 0) { if (_movementRemaining - distanceFeet >= 0) { var tempLocation = Location.ShallowCopy; tempLocation.Translate(direction, distancePoints); if (Location.Map.OnMap(tempLocation)) { var obstruction = Location.Map.Objects.FirstOrDefault(o => MapPoint.IsPointOnLineSegment(Location, tempLocation, o.Location) && o != this); if (obstruction == null) { Location = tempLocation; Console.WriteLine($"{Name} moved {direction.ToUpper()} {distanceFeet} feet."); _movementRemaining -= distanceFeet; } else { string output = $"{Name} cannot move {direction.ToUpper()} {distanceFeet} feet because the way is obstructed"; if (obstruction is INamed) { var nObstruction = (INamed)obstruction; output += $" by {nObstruction.Name}"; } Console.WriteLine(output + "."); } } else { Console.WriteLine($"{Name} cannot move outside the map."); } } else { Console.WriteLine($"{Name} cannot move {distanceFeet} feet because it only has {_movementRemaining} feet of movement left."); } } else { Console.WriteLine("Distance must be greater than 0 feet."); } } catch (Exception) { Console.WriteLine($"Target '{moveInput}' is not valid."); } return(_movementRemaining <= 0); }
protected bool hasLineOfSightTo(IMappable target) { return(!Location.Map.Objects.Any(o => MapPoint.IsPointOnLineSegment(Location, target.Location, o.Location) && o != this && o != target)); }