package ch.epfl.maze.physical; import ch.epfl.maze.util.Direction; import ch.epfl.maze.util.Vector2D; import java.util.Arrays; import java.util.EnumSet; import java.util.Set; /** * Animal inside a {@code World} that can move depending on the available * choices it has at its position. * * @author EPFL * @author Pacien TRAN-GIRARD */ abstract public class Animal { private Vector2D position; private Direction direction; /** * Constructs an animal with a specified position and direction. * * @param position Position of the animal in the labyrinth * @param direction Direction the animal is facing */ public Animal(Vector2D position, Direction direction) { this.position = position; this.direction = direction; } /** * Constructs an animal with a specified position. * * @param position Position of the animal in the labyrinth */ public Animal(Vector2D position) { this(position, Direction.NONE); } /** * Retrieves the next direction of the animal, by selecting one choice among * the ones available from its position. * * @param choices The choices left to the animal at its current position (see * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) * World.getChoices(Vector2D)}) * @return The next direction of the animal, chosen in {@code choices} * @implNote Not abstract for compatibility purpose (in order not to break tests) */ public Direction move(Set choices) { return null; } /** * Retrieves the next direction of the animal, by selecting one choice among * the ones available from its position. * * @param choices The choices left to the animal at its current position (see * {@link ch.epfl.maze.physical.World#getChoices(Vector2D) * World.getChoices(Vector2D)}) * @return The next direction of the animal, chosen in {@code choices} * @apiNote Not final for compatibility purpose (in order not to break tests) * @deprecated Use @code{Direction move(Set choices)} instead */ public Direction move(Direction[] choices) { return this.move(EnumSet.copyOf(Arrays.asList(choices))); } /** * Updates the animal position with a direction. *

* Note : Do not call this function in {@code move(Direction[] * choices)} ! * * @param dir Direction that the animal has taken */ public final void update(Direction dir) { this.position = this.position.addDirectionTo(dir); this.direction = dir; } /** * Sets new position for Animal. *

* Note : Do not call this function in {@code move(Direction[] * choices)} ! * * @param position */ public final void setPosition(Vector2D position) { this.position = position; } /** * Returns position vector of animal. * * @return Current position of animal. */ public final Vector2D getPosition() { return this.position; } /** * Returns the direction of the Animal. * * @return The current direction of the Animal */ public final Direction getDirection() { return this.direction; } abstract public Animal copy(); }