diff options
Diffstat (limited to 'src/esieequest/model/characters/WanderingCharacter.java')
-rw-r--r-- | src/esieequest/model/characters/WanderingCharacter.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/esieequest/model/characters/WanderingCharacter.java b/src/esieequest/model/characters/WanderingCharacter.java new file mode 100644 index 0000000..1b891e8 --- /dev/null +++ b/src/esieequest/model/characters/WanderingCharacter.java | |||
@@ -0,0 +1,52 @@ | |||
1 | package esieequest.model.characters; | ||
2 | |||
3 | import java.util.List; | ||
4 | import java.util.Random; | ||
5 | |||
6 | import esieequest.model.Game; | ||
7 | import esieequest.model.map.Direction; | ||
8 | import esieequest.model.map.Room; | ||
9 | |||
10 | /** | ||
11 | * A MovingCharacter that randomly moves from one Room to another adjacent Room. | ||
12 | * | ||
13 | * @author Pacien TRAN-GIRARD | ||
14 | */ | ||
15 | public abstract class WanderingCharacter extends MovingCharacter { | ||
16 | |||
17 | /** | ||
18 | * Instantiates a WanderingCharacter. | ||
19 | * | ||
20 | * @param name | ||
21 | * the name | ||
22 | * @param room | ||
23 | * the initial Room | ||
24 | * @param direction | ||
25 | * the initial Direction | ||
26 | */ | ||
27 | public WanderingCharacter(final String name, final Room room, final Direction direction) { | ||
28 | super(name, room, direction); | ||
29 | } | ||
30 | |||
31 | @Override | ||
32 | public void move(final Game game) { | ||
33 | if (!this.isMobile()) { | ||
34 | return; | ||
35 | } | ||
36 | |||
37 | final List<Direction> possibleDirections = this.getCurrentRoom().listExitsDirections(); | ||
38 | |||
39 | if (possibleDirections.size() < 1) { | ||
40 | return; | ||
41 | } | ||
42 | |||
43 | final int randomIndex = (new Random()).nextInt(possibleDirections.size()); | ||
44 | final Direction direction = possibleDirections.get(randomIndex); | ||
45 | final Room destination = this.getCurrentRoom().getSide(direction).getDoor() | ||
46 | .getDestination(); | ||
47 | final Direction newDirection = direction.getOpposite(); | ||
48 | |||
49 | this.changePosition(destination, newDirection); | ||
50 | } | ||
51 | |||
52 | } | ||