summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/Animal.java
blob: e15cb74191ec26d0ffffbcde264008567f57297f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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<Direction> 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<Direction> choices)} instead
     */
    public Direction move(Direction[] choices) {
        return this.move(EnumSet.copyOf(Arrays.asList(choices)));
    }

    /**
     * Updates the animal position with a direction.
     * <p>
     * <b>Note</b> : 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.
     * <p>
     * <b>Note</b> : 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();

}