-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLoader.java
More file actions
361 lines (309 loc) · 8.2 KB
/
FileLoader.java
File metadata and controls
361 lines (309 loc) · 8.2 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* The FileLoader parses all textfiles and loads them into the game
* Edited. Will make more edits once updated txt files are uploaded.
*/
import java.io.*;
import java.util.*;
public class FileLoader
{
private static Map<String, Room> gameMap = new HashMap<>();
private static ArrayList<Item> itemAL = new ArrayList<>();
private static ArrayList<Puzzle> puzzleAL = new ArrayList<>();
private static ArrayList<Character> charAL = new ArrayList<>();
public static Map<String, Room> loadRooms(String fileName)
{
try(Scanner scan = new Scanner(new File(fileName)))
{
while(scan.hasNextLine())
{
String line = scan.nextLine().trim();
try
{
String[] parts = line.split("%");
String roomID = parts[0];
String roomFloorID = parts[1];
String roomName = parts[2];
String roomDescr = parts[3];
String roomExits = parts[4];
boolean isCheckpoint = parts.length > 5 && parts[5].equals("1");
Room room = new Room(roomID, roomFloorID, roomName, roomDescr);
room.setCheckpoint(isCheckpoint);
//add exits for each room
String[] exitIDs = roomExits.split(",");
String[] directions = {"north", "south", "west", "east"};
for (int i = 0; i < 4; i++)
{
String exitRoomID = exitIDs[i].trim();
if (!exitRoomID.equals("-1"))
{ // Only add valid exits
room.addExit(directions[i], exitRoomID);
}
}
//add rooms to game map
gameMap.put(roomID, room);
}
catch(Exception e)
{
System.out.println("Error parsing line: " + line);
e.printStackTrace();
}
}
}
catch(FileNotFoundException fnfe)
{
System.out.println("Rooms file not found.");
fnfe.printStackTrace();
}
return gameMap;
}
public static ArrayList<Item> loadItems(String fileName)
{
try(Scanner scan = new Scanner(new File(fileName)))
{
while(scan.hasNextLine())
{
String line = scan.nextLine().trim();
if (line.isEmpty()) continue;
try
{
String[] parts = line.split("%");
if (parts.length < 6)
{
System.out.println("Invalid item line: " + line);
continue;
}
String itemID = parts[0].trim();
String itemName = parts[1].trim();
String itemDesc = parts[2].trim();
String itemType = parts[3].trim();
int itemValue = Integer.parseInt(parts[4].trim());
int maxStack = (parts.length > 5 && !parts[5].trim().isEmpty()) ?
Integer.parseInt(parts[5].trim()) : 1;
String itemRoomID = (parts.length > 6) ? parts[6].trim() : "";
Item item;
switch(itemType)
{
case "weapon" ->
item = new Weapon(itemID, itemName, itemDesc, itemValue);
case "consumable" ->
item = new Consumable(itemID, itemName, itemDesc, itemValue, maxStack);
case "useable" ->
item = new Useable(itemID, itemName, itemDesc, itemValue, maxStack);
default ->
{
System.out.println("Unknown item type.");
continue;
}
}
if(itemRoomID.contains(","))
{
String [] itemRoomIDs = itemRoomID.split(",");
for(String entry : itemRoomIDs)
{
entry = entry.trim();
String[] parts2 = entry.split(":");
String roomID = parts2[0].trim();
int quantity = (parts.length > 1) ? Integer.parseInt(parts2[1]) : 1;
Room room = gameMap.get(roomID);
if(room != null)
{
Item itemCopy = item.clone();
itemCopy.currentStack = quantity;
room.addItem(itemCopy);
itemAL.add(itemCopy);
}
else
{
//System.out.println("Room " + roomID +
//"not found for this item" + itemName);
}
}
}
else
{
Room room = gameMap.get(itemRoomID);
if (room != null)
{
room.addItem(item);
itemAL.add(item);
}
else
{
itemAL.add(item);
}
}
}
catch(Exception e)
{
System.out.println("Error parsing line: " + line);
e.printStackTrace();
}
}
}
catch(FileNotFoundException fnfe)
{
System.out.println("Items file not found.");
fnfe.printStackTrace();
}
return itemAL;
}
public static ArrayList<Puzzle> loadPuzzles(String fileName)
{
try (Scanner scan = new Scanner(new File(fileName)))
{
while (scan.hasNextLine())
{
String line = scan.nextLine().trim();
if (line.isEmpty()) continue;
try
{
String[] parts = line.split("%");
if (parts.length < 6) {
System.out.println("Invalid puzzle line: " + line);
continue;
}
String puzzleID = parts[0].trim();
String name = parts[1].trim();
String location = parts[2].trim();
String description = parts[3].trim();
String answer = parts[4].trim();
String passMessage = parts[5].trim();
String failMessage = parts[6].trim();
int maxAttempts = Integer.parseInt(parts[7].trim());
Puzzle puzzle = new Puzzle(
puzzleID,
name,
location,
description,
answer,
maxAttempts,
passMessage,
failMessage
);
puzzleAL.add(puzzle);
Room room = gameMap.get(location);
// room exists
if (room != null)
{
room.setPuzzle(puzzle);
}
// room does not exist
else
{
System.out.println("Room " + location +
" not found for puzzle " + puzzleID);
}
}
catch (Exception e)
{
System.out.println("Error parsing line: " + line);
e.printStackTrace();
}
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Puzzles file not found.");
fnfe.printStackTrace();
}
return puzzleAL;
}
public static ArrayList<Character> loadChars(String fileName)
{
try(Scanner scan = new Scanner(new File(fileName)))
{
while(scan.hasNextLine())
{
String line = scan.nextLine().trim();
if (line.isEmpty()) continue;
try
{
String[] parts = line.split("%");
if (parts.length < 6)
{
System.out.println("Invalid character line: " + line);
continue;
}
String iD = parts[0].trim();
String name = parts[1].trim();
String charType = parts[2].trim();
String preExisting = parts[3].trim();
String spawn = parts[4].trim();
String monsterDies = parts[5].trim();
String playerDies = parts[6].trim();
int damage = Integer.parseInt(parts[7].trim());
int health = Integer.parseInt(parts[8].trim());
String location = parts[9].trim();
Character character;
switch(charType)
{
case "NPC" ->
character = new NPC(iD, name, preExisting, spawn, monsterDies, playerDies);
case "Monster" ->
character = new Monster(iD, name, preExisting, spawn, monsterDies,
playerDies, damage, health);
default ->
{
System.out.println("Unknown character type.");
continue;
}
}
if(location.contains(","))
{
String [] locationRoomID = location.split(",");
for(String entry : locationRoomID)
{
entry = entry.trim();
Room room = gameMap.get(entry);
if(room != null)
{
room.setCharacter(character);
charAL.add(character);
}
else
{
//System.out.println("Room " + location +
//"not found for this character" + name);
}
}
}
else
{
Room room = gameMap.get(location);
if (room != null)
{
room.setCharacter(character);
charAL.add(character);
}
}
}
catch(Exception e)
{
System.out.println("Error parsing line: " + line);
e.printStackTrace();
}
}
}
catch(FileNotFoundException fnfe)
{
System.out.println("Characters file not found.");
fnfe.printStackTrace();
}
return charAL;
}
public static Map<String, Room> getRoomMap()
{
return gameMap;
}
public static ArrayList<Item> getItemList()
{
return itemAL;
}
public static ArrayList<Puzzle> getPuzzleList()
{
return puzzleAL;
}
public static ArrayList<Character> getCharList()
{
return charAL;
}
}