diff options
author | Pacien TRAN-GIRARD | 2014-05-29 00:51:39 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-05-29 00:51:39 +0200 |
commit | da9bbc02e3d707a947297750df6b7ea7f6b47a21 (patch) | |
tree | f89a51542466987793cdfdc88d77a6d19606f796 /src | |
parent | f8a8af9f47cd91eabf8f0f367b0f0a9b1d053b65 (diff) | |
download | esieequest-da9bbc02e3d707a947297750df6b7ea7f6b47a21.tar.gz |
Separate Following and Wandering MovingCharacter-s
Diffstat (limited to 'src')
-rw-r--r-- | src/esieequest/controller/GameEngine.java | 10 | ||||
-rw-r--r-- | src/esieequest/model/Player.java | 1 | ||||
-rw-r--r-- | src/esieequest/model/characters/FollowingCharacter.java | 52 | ||||
-rw-r--r-- | src/esieequest/model/characters/MovingCharacter.java | 67 | ||||
-rw-r--r-- | src/esieequest/model/characters/Sumobot.java | 4 | ||||
-rw-r--r-- | src/esieequest/model/characters/WanderingCharacter.java | 52 |
6 files changed, 150 insertions, 36 deletions
diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java index 8326607..ae76059 100644 --- a/src/esieequest/controller/GameEngine.java +++ b/src/esieequest/controller/GameEngine.java | |||
@@ -77,20 +77,16 @@ public class GameEngine { | |||
77 | 77 | ||
78 | final String argument = input.getArgument(); | 78 | final String argument = input.getArgument(); |
79 | 79 | ||
80 | this.executePreRoutines(); | ||
81 | command.execute(argument, this.game, this.view); | 80 | command.execute(argument, this.game, this.view); |
82 | this.executeAfterRoutines(); | 81 | this.executeRoutines(); |
83 | } | 82 | } |
84 | 83 | ||
85 | /** | 84 | /** |
86 | * Performs routine actions executed every time a Command is entered. | 85 | * Performs routine actions executed every time a Command is entered. |
87 | */ | 86 | */ |
88 | private void executePreRoutines() { | 87 | private void executeRoutines() { |
89 | MovingCharacter.moveAll(); | ||
90 | } | ||
91 | |||
92 | private void executeAfterRoutines() { | ||
93 | this.checkStalemate(); | 88 | this.checkStalemate(); |
89 | MovingCharacter.moveAll(this.game); | ||
94 | } | 90 | } |
95 | 91 | ||
96 | private void checkStalemate() { | 92 | private void checkStalemate() { |
diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index e9c3c7f..c06f5b2 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java | |||
@@ -36,6 +36,7 @@ public class Player implements SerialisableObject { | |||
36 | private Room currentRoom; | 36 | private Room currentRoom; |
37 | 37 | ||
38 | private static final String PREVIOUS_ROOMS_LABEL = "H"; | 38 | private static final String PREVIOUS_ROOMS_LABEL = "H"; |
39 | @Getter | ||
39 | private final Stack<Room> previousRooms; | 40 | private final Stack<Room> previousRooms; |
40 | 41 | ||
41 | private static final String CURRENT_DIRECTION_LABEL = "D"; | 42 | private static final String CURRENT_DIRECTION_LABEL = "D"; |
diff --git a/src/esieequest/model/characters/FollowingCharacter.java b/src/esieequest/model/characters/FollowingCharacter.java new file mode 100644 index 0000000..ab1a62e --- /dev/null +++ b/src/esieequest/model/characters/FollowingCharacter.java | |||
@@ -0,0 +1,52 @@ | |||
1 | package esieequest.model.characters; | ||
2 | |||
3 | import java.util.Stack; | ||
4 | |||
5 | import esieequest.model.Game; | ||
6 | import esieequest.model.map.Direction; | ||
7 | import esieequest.model.map.Room; | ||
8 | |||
9 | /** | ||
10 | * A MovingCharacter that follows the Player. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD | ||
13 | */ | ||
14 | public abstract class FollowingCharacter extends MovingCharacter { | ||
15 | |||
16 | /** | ||
17 | * Instantiates a FollowingCharacter. | ||
18 | * | ||
19 | * @param name | ||
20 | * the name | ||
21 | * @param room | ||
22 | * the initial Room | ||
23 | * @param direction | ||
24 | * the initial Direction | ||
25 | */ | ||
26 | public FollowingCharacter(final String name, final Room room, final Direction direction) { | ||
27 | super(name, room, direction); | ||
28 | } | ||
29 | |||
30 | @Override | ||
31 | public void move(final Game game) { | ||
32 | if (!this.isMobile()) { | ||
33 | return; | ||
34 | } | ||
35 | |||
36 | final Stack<Room> playerHistory = game.getPlayer().getPreviousRooms(); | ||
37 | |||
38 | if (playerHistory.size() < 2) { | ||
39 | return; | ||
40 | } | ||
41 | |||
42 | final Room previousRoom = playerHistory.elementAt(playerHistory.size() - 1); | ||
43 | final Room previousPreviousRoom = playerHistory.elementAt(playerHistory.size() - 2); | ||
44 | |||
45 | if (previousPreviousRoom == this.getCurrentRoom()) { | ||
46 | return; | ||
47 | } | ||
48 | |||
49 | this.changePosition(previousRoom, game.getPlayer().getCurrentDirection().getOpposite()); | ||
50 | } | ||
51 | |||
52 | } | ||
diff --git a/src/esieequest/model/characters/MovingCharacter.java b/src/esieequest/model/characters/MovingCharacter.java index 94aeaf1..2304f94 100644 --- a/src/esieequest/model/characters/MovingCharacter.java +++ b/src/esieequest/model/characters/MovingCharacter.java | |||
@@ -2,7 +2,6 @@ package esieequest.model.characters; | |||
2 | 2 | ||
3 | import java.util.ArrayList; | 3 | import java.util.ArrayList; |
4 | import java.util.List; | 4 | import java.util.List; |
5 | import java.util.Random; | ||
6 | 5 | ||
7 | import lombok.Getter; | 6 | import lombok.Getter; |
8 | import lombok.Setter; | 7 | import lombok.Setter; |
@@ -10,6 +9,7 @@ import net.pacien.util.CleanJSONObject; | |||
10 | 9 | ||
11 | import org.json.simple.JSONObject; | 10 | import org.json.simple.JSONObject; |
12 | 11 | ||
12 | import esieequest.model.Game; | ||
13 | import esieequest.model.map.Direction; | 13 | import esieequest.model.map.Direction; |
14 | import esieequest.model.map.Room; | 14 | import esieequest.model.map.Room; |
15 | 15 | ||
@@ -29,16 +29,18 @@ public abstract class MovingCharacter extends SimpleCharacter { | |||
29 | /** | 29 | /** |
30 | * Moves all the MovingCharacter in the map. | 30 | * Moves all the MovingCharacter in the map. |
31 | */ | 31 | */ |
32 | public static void moveAll() { | 32 | public static void moveAll(final Game game) { |
33 | for (final MovingCharacter movingCharacter : MovingCharacter.instances) { | 33 | for (final MovingCharacter movingCharacter : MovingCharacter.instances) { |
34 | movingCharacter.move(); | 34 | movingCharacter.move(game); |
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | private static final String CURRENT_ROOM_LABEL = "C"; | 38 | private static final String CURRENT_ROOM_LABEL = "C"; |
39 | @Getter | ||
39 | private Room currentRoom; | 40 | private Room currentRoom; |
40 | 41 | ||
41 | private static final String CURRENT_DIRECTION_LABEL = "D"; | 42 | private static final String CURRENT_DIRECTION_LABEL = "D"; |
43 | @Getter | ||
42 | private Direction currentDirection; | 44 | private Direction currentDirection; |
43 | 45 | ||
44 | private static final String MOBILE_LABEL = "M"; | 46 | private static final String MOBILE_LABEL = "M"; |
@@ -66,38 +68,49 @@ public abstract class MovingCharacter extends SimpleCharacter { | |||
66 | } | 68 | } |
67 | 69 | ||
68 | /** | 70 | /** |
69 | * Moves the MovingCharacter to a randomly chosen adjacent Room, if he can | 71 | * Changes the position of the MovingCharacter. |
70 | * move and if the destination Room is not currently occupied by another | 72 | * |
71 | * Character. | 73 | * @param room |
74 | * the new Room | ||
75 | * @param direction | ||
76 | * the new Direction | ||
72 | */ | 77 | */ |
73 | public void move() { | 78 | public void changePosition(final Room room, final Direction direction) { |
74 | if (!this.mobile) { | 79 | if (room.getSide(direction).hasCharacter()) { |
75 | return; | ||
76 | } | ||
77 | |||
78 | final List<Direction> possibleDirections = this.currentRoom.listExitsDirections(); | ||
79 | |||
80 | if (possibleDirections.size() < 1) { | ||
81 | return; | ||
82 | } | ||
83 | |||
84 | final int randomIndex = (new Random()).nextInt(possibleDirections.size()); | ||
85 | final Direction direction = possibleDirections.get(randomIndex); | ||
86 | final Room destination = this.currentRoom.getSide(direction).getDoor().getDestination(); | ||
87 | |||
88 | if (destination.getSide(direction).hasCharacter()) { | ||
89 | return; | 80 | return; |
90 | } | 81 | } |
91 | 82 | ||
92 | this.currentRoom.getSide(this.currentDirection).setCharacter(null); | 83 | this.currentRoom.getSide(this.currentDirection).setCharacter(null); |
84 | this.currentRoom = room; | ||
85 | this.currentDirection = direction; | ||
86 | this.currentRoom.getSide(this.currentDirection).setCharacter(Character.getCharacter(this)); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Changes the Room of the MovingCharacter. | ||
91 | * | ||
92 | * @param room | ||
93 | * the Room | ||
94 | */ | ||
95 | public void changeRoom(final Room room) { | ||
96 | this.changePosition(room, this.currentDirection); | ||
97 | } | ||
93 | 98 | ||
94 | final Direction newDirection = direction.getOpposite(); | 99 | /** |
95 | final Character character = Character.getCharacter(this); | 100 | * Changes the Direction of the MovingCharacter. |
96 | destination.getSide(newDirection).setCharacter(character); | 101 | * |
97 | this.currentRoom = destination; | 102 | * @param direction |
98 |