diff options
Diffstat (limited to 'src/esieequest/model/Game.java')
-rw-r--r-- | src/esieequest/model/Game.java | 81 |
1 files changed, 65 insertions, 16 deletions
diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 549fdc8..7f124e7 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java | |||
@@ -1,7 +1,12 @@ | |||
1 | package esieequest.model; | 1 | package esieequest.model; |
2 | 2 | ||
3 | import net.pacien.util.CleanJSONObject; | ||
4 | |||
5 | import org.json.simple.JSONArray; | ||
6 | import org.json.simple.JSONObject; | ||
7 | |||
8 | import esieequest.controller.utils.SerialisableObject; | ||
3 | import esieequest.model.characters.Character; | 9 | import esieequest.model.characters.Character; |
4 | import esieequest.model.characters.Sumobot; | ||
5 | import esieequest.model.doors.Door; | 10 | import esieequest.model.doors.Door; |
6 | import esieequest.model.doors.HiddenDoor; | 11 | import esieequest.model.doors.HiddenDoor; |
7 | import esieequest.model.doors.LockedDoor; | 12 | import esieequest.model.doors.LockedDoor; |
@@ -16,17 +21,24 @@ import esieequest.model.map.Room; | |||
16 | * | 21 | * |
17 | * @author Pacien TRAN-GIRARD | 22 | * @author Pacien TRAN-GIRARD |
18 | */ | 23 | */ |
19 | public class Game { | 24 | public class Game implements SerialisableObject { |
20 | 25 | ||
21 | private static final int DEFAULT_INVENTORY_LIMIT = 10; | 26 | public static final int DEFAULT_INVENTORY_LIMIT = 10; |
22 | private static final int DEFAULT_STEPS_LIMIT = 0; | 27 | public static final int DEFAULT_STEPS_LIMIT = 0; |
23 | private static final int CHALLENGE_STEPS_LIMIT = 50; | 28 | public static final int CHALLENGE_STEPS_LIMIT = 50; |
24 | private static final Direction DEFAULT_DIRECTION = Direction.NORTH; | 29 | public static final Direction DEFAULT_DIRECTION = Direction.NORTH; |
25 | private static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT; | 30 | public static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT; |
26 | 31 | ||
32 | private static final String CHALLENGE_LABEL = "L"; | ||
27 | private final boolean challenge; | 33 | private final boolean challenge; |
34 | |||
35 | private static final String PLAYER_LABEL = "P"; | ||
28 | private Player player; | 36 | private Player player; |
29 | 37 | ||
38 | private static final String ROOMS_LABEL = "R"; | ||
39 | private static final String ITEMS_LABEL = "I"; | ||
40 | private static final String CHARACTERS_LABEL = "C"; | ||
41 | |||
30 | /** | 42 | /** |
31 | * Creates a Game with a step limit for the Player. | 43 | * Creates a Game with a step limit for the Player. |
32 | * | 44 | * |
@@ -35,7 +47,6 @@ public class Game { | |||
35 | */ | 47 | */ |
36 | public Game(final boolean challenge) { | 48 | public Game(final boolean challenge) { |
37 | this.challenge = challenge; | 49 | this.challenge = challenge; |
38 | this.connectRooms(); | ||
39 | } | 50 | } |
40 | 51 | ||
41 | /** | 52 | /** |
@@ -48,10 +59,16 @@ public class Game { | |||
48 | /** | 59 | /** |
49 | * Initialises a new Game. | 60 | * Initialises a new Game. |
50 | */ | 61 | */ |
51 | public void newGame() { | 62 | public void newGame(final boolean populate, final boolean challengeMode) { |
52 | this.player = new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, this.challenge ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT); | 63 | Room.createAllSides(); |
53 | this.addItems(); | 64 | this.connectRooms(); |
54 | this.addCharacters(); | 65 | |
66 | this.setPlayer(new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, challengeMode ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT)); | ||
67 | |||
68 | if (populate) { | ||
69 | this.addItems(); | ||
70 | this.addCharacters(); | ||
71 | } | ||
55 | } | 72 | } |
56 | 73 | ||
57 | /** | 74 | /** |
@@ -62,9 +79,19 @@ public class Game { | |||
62 | } | 79 | } |
63 | 80 | ||
64 | /** | 81 | /** |
82 | * Sets the player | ||
83 | * | ||
84 | * @param player | ||
85 | * the Player to set | ||
86 | */ | ||
87 | public void setPlayer(final Player player) { | ||
88 | this.player = player; | ||
89 | } | ||
90 | |||
91 | /** | ||
65 | * Connects Room-s together using Door-s. | 92 | * Connects Room-s together using Door-s. |
66 | */ | 93 | */ |
67 | private void connectRooms() { | 94 | public void connectRooms() { |
68 | this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE); | 95 | this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE); |
69 | this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA); | 96 | this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA); |
70 | this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET); | 97 | this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET); |
@@ -143,7 +170,7 @@ public class Game { | |||
143 | /** | 170 | /** |
144 | * Adds Item-s in the map. | 171 | * Adds Item-s in the map. |
145 | */ | 172 | */ |
146 | private void addItems() { | 173 | public void addItems() { |
147 | this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE); | 174 | this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE); |
148 | this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE); | 175 | this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE); |
149 | this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE); | 176 | this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE); |
@@ -170,8 +197,8 @@ public class Game { | |||
170 | /** | 197 | /** |
171 | * Adds Character-s to the map. | 198 | * Adds Character-s to the map. |
172 | */ | 199 | */ |
173 | private void addCharacters() { | 200 | public void addCharacters() { |
174 | this.c(Room.LOCKED_ROOM, Direction.SOUTH, new Sumobot(Room.LOCKED_ROOM, Direction.SOUTH)); | 201 | this.c(Room.LOCKED_ROOM, Direction.SOUTH, Character.SUMOBOT); |
175 | } | 202 | } |
176 | 203 | ||
177 | /** | 204 | /** |
@@ -188,4 +215,26 @@ public class Game { | |||
188 | room.getSide(direction).setCharacter(character); | 215 | room.getSide(direction).setCharacter(character); |
189 | } | 216 | } |
190 | 217 | ||
218 | @Override | ||
219 | public JSONObject serialise() { | ||
220 | final CleanJSONObject o = new CleanJSONObject(); | ||
221 | |||
222 | o.put(Game.CHALLENGE_LABEL, this.challenge); | ||
223 | |||
224 | o.put(Game.PLAYER_LABEL, this.player.serialise()); | ||
225 | o.put(Game.ROOMS_LABEL, Room.serialiseAll()); | ||
226 | o.put(Game.ITEMS_LABEL, Item.serialiseAll()); | ||
227 | o.put(Game.CHARACTERS_LABEL, Character.serialiseAll()); | ||
228 | |||
229 | return o; | ||
230 | } | ||
231 | |||
232 | @Override | ||
233 | public void deserialise(final JSONObject o) { | ||
234 | this.player.deserialise((JSONObject) o.get(Game.PLAYER_LABEL)); | ||
235 | Room.deserialiseAll((JSONArray) o.get(Game.ROOMS_LABEL)); | ||
236 | Item.deserialiseAll((JSONArray) o.get(Game.ITEMS_LABEL)); | ||
237 | Character.deserialiseAll((JSONArray) o.get(Game.CHARACTERS_LABEL)); | ||
238 | } | ||
239 | |||
191 | } | 240 | } |