/** * Handle the even when a new pirate becomes available. * @param pirateEvent new pirate emerged */ @Subscribe public void handleNewPirate(NewPirateEvent pirateEvent) { IShip ship = (IShip) pirateEvent.getPirate().getShip(); Point2D adjustedSource = aStarHeuristicProvider.findClosest(ship.getLocation()); ship.setLocation(adjustedSource); selectDestination(pirateEvent.getPirate()); }
} /** * Select a new destination for the pirate. * @param pirate free sea pirate */ private void selectDestination(ISeaPirate pirate) { Point2D destination; if (pirate instanceof INonFreeSeaPirate && ((INonFreeSeaPirate) pirate).roamingNearCity().isPresent()) { ICity city = ((INonFreeSeaPirate) pirate).roamingNearCity().get(); destination = mapService.getRandomPointAtSeaNear(city.getCoordinates()); } else { destination = mapService.getLocationAtOtherEndOfMap(pirate.getShip().getLocation()); } // make sure that the source of the pirate location is in the graph Point2D adjustedDestination = aStarHeuristicProvider.findClosest(destination); aStarHeuristicProvider.addTargetNodeToHeuristic(adjustedDestination); logger.debug("Source point of pirate is {}", pirate.getShip().getLocation()); seafaringService.travelNotBetweenCities(pirate.getShip(), adjustedDestination); }