aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/model/Game.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/esieequest/model/Game.java')
-rw-r--r--src/esieequest/model/Game.java111
1 files changed, 82 insertions, 29 deletions
diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java
index 830b48c..05fdcd2 100644
--- a/src/esieequest/model/Game.java
+++ b/src/esieequest/model/Game.java
@@ -1,15 +1,20 @@
1package esieequest.model; 1package esieequest.model;
2 2
3import lombok.Getter;
4import lombok.Setter;
5import net.pacien.util.CleanJSONObject;
6
7import org.json.simple.JSONArray;
8import org.json.simple.JSONObject;
9
10import esieequest.controller.utils.SerialisableObject;
3import esieequest.model.characters.Character; 11import esieequest.model.characters.Character;
4import esieequest.model.characters.Sumobot;
5import esieequest.model.doors.Door; 12import esieequest.model.doors.Door;
6import esieequest.model.doors.HiddenDoor; 13import esieequest.model.doors.HiddenDoor;
7import esieequest.model.doors.LockedDoor; 14import esieequest.model.doors.LockedDoor;
8import esieequest.model.doors.TransporterDoor; 15import esieequest.model.doors.TransporterDoor;
9import esieequest.model.doors.TrapDoor; 16import esieequest.model.doors.TrapDoor;
10import esieequest.model.items.Beamer;
11import esieequest.model.items.Item; 17import esieequest.model.items.Item;
12import esieequest.model.items.KeyCard;
13import esieequest.model.map.Direction; 18import esieequest.model.map.Direction;
14import esieequest.model.map.Room; 19import esieequest.model.map.Room;
15 20
@@ -18,9 +23,25 @@ import esieequest.model.map.Room;
18 * 23 *
19 * @author Pacien TRAN-GIRARD 24 * @author Pacien TRAN-GIRARD
20 */ 25 */
21public class Game { 26public class Game implements SerialisableObject {
27
28 public static final int DEFAULT_INVENTORY_LIMIT = 10;
29 public static final int DEFAULT_STEPS_LIMIT = 0;
30 public static final int CHALLENGE_STEPS_LIMIT = 50;
31 public static final Direction DEFAULT_DIRECTION = Direction.NORTH;
32 public static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT;
33
34 private static final String CHALLENGE_LABEL = "L";
35 private final boolean challenge;
36
37 private static final String PLAYER_LABEL = "P";
38 @Getter
39 @Setter
40 private Player player;
22 41
23 private final Player player; 42 private static final String ROOMS_LABEL = "R";
43 private static final String ITEMS_LABEL = "I";
44 private static final String CHARACTERS_LABEL = "C";
24 45
25 /** 46 /**
26 * Creates a Game with a step limit for the Player. 47 * Creates a Game with a step limit for the Player.
@@ -28,31 +49,36 @@ public class Game {
28 * @param maxNbSteps 49 * @param maxNbSteps
29 * the step limit for the Player 50 * the step limit for the Player
30 */ 51 */
31 public Game(final int maxNbSteps) { 52 public Game(final boolean challenge) {
32 this.player = new Player(10, maxNbSteps); 53 this.challenge = challenge;
33 this.connectRooms();
34 this.addItems();
35 this.addCharacters();
36 } 54 }
37 55
38 /** 56 /**
39 * Creates a Game. 57 * Creates a Game.
40 */ 58 */
41 public Game() { 59 public Game() {
42 this(0); 60 this(false);
43 } 61 }
44 62
45 /** 63 /**
46 * @return the player 64 * Initialises a new Game.
47 */ 65 */
48 public Player getPlayer() { 66 public void newGame(final boolean populate, final boolean challengeMode) {
49 return this.player; 67 Room.createAllSides();
68 this.connectRooms();
69
70 this.setPlayer(new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, challengeMode ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT));
71
72 if (populate) {
73 this.addItems();
74 this.addCharacters();
75 }
50 } 76 }
51 77
52 /** 78 /**
53 * Connects Room-s together using Door-s. 79 * Connects Room-s together using Door-s.
54 */ 80 */
55 private void connectRooms() { 81 public void connectRooms() {
56 this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE); 82 this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE);
57 this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA); 83 this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA);
58 this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET); 84 this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET);
@@ -70,7 +96,8 @@ public class Game {
70 this.d(Room.WING_STREET, Direction.NORTH, Room.WING_FLOOR_ONE); 96 this.d(Room.WING_STREET, Direction.NORTH, Room.WING_FLOOR_ONE);
71 this.d(Room.WING_FLOOR_ONE, Direction.WEST, Room.WING_STAIRS_ONE); 97 this.d(Room.WING_FLOOR_ONE, Direction.WEST, Room.WING_STAIRS_ONE);
72 this.d(Room.WING_STAIRS_ONE, Direction.SOUTH, Room.WING_STAIRS_TWO); 98 this.d(Room.WING_STAIRS_ONE, Direction.SOUTH, Room.WING_STAIRS_TWO);
73 this.d(Room.WING_STAIRS_ONE, Direction.UP, Room.WING_STAIRS_TWO); 99 this.d(Room.WING_STAIRS_ONE, Direction.UP, new HiddenDoor(Room.WING_STAIRS_TWO));
100 this.d(Room.WING_STAIRS_TWO, Direction.DOWN, new HiddenDoor(Room.WING_STAIRS_ONE));
74 this.d(Room.WING_STAIRS_TWO, Direction.EAST, Room.WING_FLOOR_TWO); 101 this.d(Room.WING_STAIRS_TWO, Direction.EAST, Room.WING_FLOOR_TWO);
75 this.d(Room.WING_FLOOR_TWO, Direction.NORTH, Room.WING_OFFICE_CORRIDOR); 102 this.d(Room.WING_FLOOR_TWO, Direction.NORTH, Room.WING_OFFICE_CORRIDOR);
76 this.d(Room.WING_OFFICE_CORRIDOR, Direction.EAST, Room.WING_OFFICE); 103 this.d(Room.WING_OFFICE_CORRIDOR, Direction.EAST, Room.WING_OFFICE);
@@ -86,8 +113,15 @@ public class Game {
86 this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.SOUTH, Room.SECRET_LAB); 113 this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.SOUTH, Room.SECRET_LAB);
87 this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.EAST, Room.SECRET_CORRIDOR_END); 114 this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.EAST, Room.SECRET_CORRIDOR_END);
88 this.d(Room.SECRET_CORRIDOR_END, Direction.NORTH, new TrapDoor(Room.DEAD_END)); 115 this.d(Room.SECRET_CORRIDOR_END, Direction.NORTH, new TrapDoor(Room.DEAD_END));
89 this.d(Room.SECRET_CORRIDOR_END, Direction.SOUTH, new LockedDoor(Room.LOCKED_ROOM)); 116
90 this.d(Room.LOCKED_ROOM, Direction.NORTH, new LockedDoor(Room.SECRET_CORRIDOR_END)); 117 final LockedDoor lockedDoorEnter = new LockedDoor(Room.LOCKED_ROOM);
118 lockedDoorEnter.setKey(Item.KEYCARD);
119 this.d(Room.SECRET_CORRIDOR_END, Direction.SOUTH, lockedDoorEnter);
120
121 final LockedDoor lockedDoorExit = new LockedDoor(Room.SECRET_CORRIDOR_END);
122 lockedDoorExit.setKey(Item.KEYCARD);
123 this.d(Room.LOCKED_ROOM, Direction.NORTH, lockedDoorExit);
124
91 this.d(Room.SECRET_CORRIDOR_END, Direction.EAST, new TransporterDoor(Room.values())); 125 this.d(Room.SECRET_CORRIDOR_END, Direction.EAST, new TransporterDoor(Room.values()));
92 } 126 }
93 127
@@ -123,17 +157,14 @@ public class Game {
123 /** 157 /**
124 * Adds Item-s in the map. 158 * Adds Item-s in the map.
125 */ 159 */
126 private void addItems() { 160 public void addItems() {
127 this.i(Room.STORAGE_ROOM, Direction.WEST, new Item("Weighted Storage Cube", 5, true)); 161 this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE);
128 this.i(Room.STORAGE_ROOM, Direction.EAST, new Item("Edgeless Safety Cube", 5, true)); 162 this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE);
129 this.i(Room.STORAGE_ROOM, Direction.NORTH, new Item("Portable black-hole", -10, false)); 163 this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE);
130 164
131 this.i(Room.SECRET_LAB, Direction.SOUTH, new Beamer("Beamer")); 165 this.i(Room.SECRET_LAB, Direction.SOUTH, Item.BEAMER);
132 166
133 KeyCard keyCard = new KeyCard("KeyCard"); 167 this.i(Room.DEAD_END, Direction.NORTH, Item.KEYCARD);
134 this.i(Room.DEAD_END, Direction.NORTH, keyCard);
135 ((LockedDoor) Room.SECRET_CORRIDOR_END.getSide(Direction.SOUTH).getDoor()).setKey(keyCard);
136 ((LockedDoor) Room.LOCKED_ROOM.getSide(Direction.NORTH).getDoor()).setKey(keyCard);
137 } 168 }
138 169
139 /** 170 /**
@@ -153,8 +184,8 @@ public class Game {
153 /** 184 /**
154 * Adds Character-s to the map. 185 * Adds Character-s to the map.
155 */ 186 */
156 private void addCharacters() { 187 public void addCharacters() {
157 this.c(Room.LOCKED_ROOM, Direction.SOUTH, new Sumobot(Room.LOCKED_ROOM, Direction.SOUTH)); 188 this.c(Room.LOCKED_ROOM, Direction.SOUTH, Character.SUMOBOT);
158 } 189 }
159 190
160 /** 191 /**
@@ -171,4 +202,26 @@ public class Game {
171 room.getSide(direction).setCharacter(character); 202 room.getSide(direction).setCharacter(character);
172 } 203 }
173 204
205 @Override
206 public JSONObject serialise() {
207 final CleanJSONObject o = new CleanJSONObject();
208
209 o.put(Game.CHALLENGE_LABEL, this.challenge);
210
211 o.put(Game.PLAYER_LABEL, this.player.serialise());
212 o.put(Game.ROOMS_LABEL, Room.serialiseAll());
213 o.put(Game.ITEMS_LABEL, Item.serialiseAll());
214 o.put(Game.CHARACTERS_LABEL, Character.serialiseAll());
215
216 return o;
217 }
218
219 @Override
220 public void deserialise(final JSONObject o) {
221 this.player.deserialise((JSONObject) o.get(Game.PLAYER_LABEL));
222 Room.deserialiseAll((JSONArray) o.get(Game.ROOMS_LABEL));
223 Item.deserialiseAll((JSONArray) o.get(Game.ITEMS_LABEL));
224 Character.deserialiseAll((JSONArray) o.get(Game.CHARACTERS_LABEL));
225 }
226
174} 227}