getClosestEdge method

EpitaphEdge getClosestEdge([
  1. Vector? position,
  2. List<EpitaphEdge>? edges
])

Returns the closest edge (in edges) from position

Implementation

EpitaphEdge getClosestEdge([Vector? position, List<EpitaphEdge>? edges]) {
  position ??= finalPosition.toVector();
  edges ??= _graph.getAllEdges();
  Room? currentRoom = _building.getCurrentRoom(finalPosition);

  EpitaphEdge output = edges.first;
  double tmp = 0;

  for (EpitaphEdge edge in edges) {
    double shortestDistance = edge.shortestDistance(position);

    if (currentRoom?.area.pointInArea(edge.source.point) ?? false) {
      shortestDistance = 0;
    } else if (currentRoom?.area.pointInArea(edge.target.point) ?? false) {
      shortestDistance = 0;
    }

    if (shortestDistance == 0) {
      return edge;
    }

    if (tmp > shortestDistance) {
      tmp = shortestDistance;
      output = edge;
    }
  }

  return output;
}