package ch.epfl.maze.physical.pacman; import ch.epfl.maze.physical.Animal; import ch.epfl.maze.physical.Daedalus; import ch.epfl.maze.util.Vector2D; import java.util.NoSuchElementException; /** * Blue ghost from the Pac-Man game, targets the result of two times the vector * from Blinky to its target. * * @author EPFL * @author Pacien TRAN-GIRARD */ public class Inky extends Ghost { private Ghost companion; /** * Finds Inky's best friend (Blinky) in the Daedalus. * * @param daedalus The Daedalus * @return The companion if found, null otherwise */ private static Ghost findCompanion(Daedalus daedalus) { try { return (Blinky) daedalus .getPredatorSet() .stream() .filter(pred -> pred instanceof Blinky) .findFirst() .get(); } catch (NoSuchElementException e) { return null; } } /** * Constructs a Inky with a starting position. * * @param position Starting position of Inky in the labyrinth * @param companion Inky's accomplice */ public Inky(Vector2D position, Ghost companion) { super(position); this.companion = companion; } /** * Constructs a Inky with a starting position. * * @param position Starting position of Inky in the labyrinth */ public Inky(Vector2D position) { this(position, null); } /** * Returns Inky's companion. * * @param daedalus The Daedalus * @return Inky's companion if present, null otherwise */ private Ghost getCompanion(Daedalus daedalus) { if (this.companion == null) this.companion = Inky.findCompanion(daedalus); return this.companion; } /** * Returns Inky's companion's position. * * @param daedalus The Daedalus * @return Inky's companion's position if present, null otherwise */ private Vector2D getCompanionPosition(Daedalus daedalus) { Ghost companion = this.getCompanion(daedalus); if (companion == null) return new Vector2D(); return companion.getPosition(); } /** * Targets beyond his friend's scope. * * @param daedalus The Daedalus * @return The targeted position */ @Override protected Vector2D getPreyTargetPosition(Daedalus daedalus) { return this .getPreyPosition(daedalus) .mul(2) .sub(this.getCompanionPosition(daedalus)); } @Override public Animal copy() { return new Inky(this.getPosition()); } }