summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/pacman/Inky.java
blob: de0466d9370d7dd98c08c431bd05b96a1012e18e (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
package ch.epfl.maze.physical.pacman;

import ch.epfl.maze.physical.Animal;
import ch.epfl.maze.physical.Daedalus;
import ch.epfl.maze.physical.GhostPredator;
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 Pacien TRAN-GIRARD
 */
public class Inky extends GhostPredator {

    private GhostPredator companion;

    /**
     * Finds Inky's best friend (Blinky) in the Daedalus.
     *
     * @param daedalus The Daedalus
     * @return The companion if found, null otherwise
     */
    private static GhostPredator findCompanion(Daedalus daedalus) {
        try {
            return (Blinky) daedalus
                    .getPredators()
                    .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, GhostPredator 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 GhostPredator 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) {
        GhostPredator 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());
    }

}