-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldLoader.java
More file actions
327 lines (294 loc) · 17.9 KB
/
WorldLoader.java
File metadata and controls
327 lines (294 loc) · 17.9 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
import java.util.*;
public class WorldLoader {
public static final String STUMP_KEY = "stump";
public static final String SAPLING_KEY = "sapling";
public static final String TREE_KEY = "tree";
private static final int PROPERTY_KEY = 0;
private static final int PROPERTY_ID = 1;
private static final int PROPERTY_COL = 2;
private static final int PROPERTY_ROW = 3;
private static final int ENTITY_NUM_PROPERTIES = 4;
private static final int STUMP_NUM_PROPERTIES = 0;
private static final int SAPLING_NUM_PROPERTIES = 1;
private static final String OBSTACLE_KEY = "obstacle";
private static final int OBSTACLE_ANIMATION_PERIOD = 0;
private static final int OBSTACLE_NUM_PROPERTIES = 1;
private static final String PERSON_KEY = "person";
private static final int PERSON_ACTION_PERIOD = 0;
private static final int PERSON_ANIMATION_PERIOD = 1;
private static final int PERSON_LIMIT = 2;
private static final int PERSON_NUM_PROPERTIES = 3;
private static final String HOUSE_KEY = "house";
private static final int HOUSE_NUM_PROPERTIES = 0;
private static final String FAIRY_KEY = "fairy";
private static final int FAIRY_ANIMATION_PERIOD = 1;
private static final int FAIRY_ACTION_PERIOD = 0;
private static final int FAIRY_NUM_PROPERTIES = 2;
private static final int TREE_ANIMATION_PERIOD = 1;
private static final int TREE_ACTION_PERIOD = 0;
private static final int TREE_HEALTH = 2;
private static final int TREE_NUM_PROPERTIES = 3;
//new variables for project 4
public static final String BABY_COW_KEY = "babycow";
public static final int BABY_COW_NUM_PROPERTIES = 1;
public static final String BABY_PIG_KEY = "babypig";
private static final int BABY_PIG_NUM_PROPERTIES = 1;
public static final String STEAK_KEY = "steak";
private static final int STEAK_NUM_PROPERTIES = 0;
public static final String GROWN_COW_KEY = "growncow";
private static final int GROWN_COW_ANIMATION_PERIOD = 1;
private static final int GROWN_COW_ACTION_PERIOD = 0;
private static final int GROWN_COW_HEALTH = 2;
private static final int GROWN_COW_NUM_PROPERTIES = 3;
public static final String BACON_KEY = "bacon";
private static final int BACON_NUM_PROPERTIES = 0;
public static final String GROWN_PIG_KEY = "grownpig";
private static final int GROWN_PIG_ANIMATION_PERIOD = 1;
private static final int GROWN_PIG_ACTION_PERIOD = 0;
private static final int GROWN_PIG_HEALTH = 2;
private static final int GROWN_PIG_NUM_PROPERTIES = 3;
private static final String FENCE_KEY = "fence";
private static final int FENCE_NUM_PROPERTIES = 0;
private static final String BARN_KEY ="barn";
private static final int BARN_NUM_PROPERTIES = 0;
private static final String VET_KEY = "vet";
private static final int VET_NUM_PROPERTIES = 2;
private static final int VET_ANIMATION_PERIOD = 1;
private static final int VET_ACTION_PERIOD = 0;
private static final String WOLF_KEY = "wolf";
private static final int WOLF_ACTION_PERIOD = 0;
private static final int WOLF_ANIMATION_PERIOD = 1;
private static final int WOLF_LIMIT = 2;
private static final int WOLF_NUM_PROPERTIES = 3;
public static final String FARMER_KEY = "farmer";
private static final int FARMER_ACTION_PERIOD = 0;
private static final int FARMER_ANIMATION_PERIOD = 1;
private static final int FARMER_LIMIT = 2;
private static final int FARMER_NUM_PROPERTIES = 3;
public static void load(WorldModel world, Scanner saveFile, ImageStore imageStore, Background defaultBackground){
parseSaveFile(world, saveFile, imageStore, defaultBackground);
if(world.getBackground() == null){
world.setBackground(new Background[world.getNumRows()][world.getNumCols()]);
for (Background[] row : world.getBackground())
Arrays.fill(row, defaultBackground);
}
if(world.getOccupancy() == null){
world.setOccupancy(new Entity[world.getNumRows()][world.getNumCols()]);
world.setEntities(new HashSet<>());
}
}
private static void parseSaveFile(WorldModel world, Scanner saveFile, ImageStore imageStore, Background defaultBackground){
String lastHeader = "";
int headerLine = 0;
int lineCounter = 0;
while(saveFile.hasNextLine()){
lineCounter++;
String line = saveFile.nextLine().strip();
if(line.endsWith(":")){
headerLine = lineCounter;
lastHeader = line;
switch (line){
case "Backgrounds:" -> world.setBackground(new Background[world.getNumRows()][world.getNumCols()]);
case "Entities:" -> {
world.setOccupancy(new Entity[world.getNumRows()][world.getNumCols()]);
world.setEntities(new HashSet<>());
}
}
}else{
switch (lastHeader){
case "Rows:" -> world.setNumRows(Integer.parseInt(line));
case "Cols:" -> world.setNumCols(Integer.parseInt(line));
case "Backgrounds:" -> parseBackgroundRow(world, line, lineCounter-headerLine-1, imageStore);
case "Entities:" -> parseEntity(world, line, imageStore);
}
}
}
}
private static void parseBackgroundRow(WorldModel world, String line, int row, ImageStore imageStore) {
String[] cells = line.split(" ");
if(row < world.getNumRows()){
int rows = Math.min(cells.length, world.getNumCols());
for (int col = 0; col < rows; col++){
world.getBackground()[row][col] = new Background(cells[col], imageStore.getImageList(cells[col]));
}
}
}
private static void parseEntity(WorldModel world, String line, ImageStore imageStore) {
String[] properties = line.split(" ", ENTITY_NUM_PROPERTIES + 1);
if (properties.length >= ENTITY_NUM_PROPERTIES) {
String key = properties[PROPERTY_KEY];
String id = properties[PROPERTY_ID];
Point pt = new Point(Integer.parseInt(properties[PROPERTY_COL]), Integer.parseInt(properties[PROPERTY_ROW]));
properties = properties.length == ENTITY_NUM_PROPERTIES ?
new String[0] : properties[ENTITY_NUM_PROPERTIES].split(" ");
switch (key) {
case OBSTACLE_KEY -> parseObstacle(world, properties, pt, id, imageStore);
case PERSON_KEY -> parsePerson(world, properties, pt, id, imageStore);
case FAIRY_KEY -> parseFairy(world, properties, pt, id, imageStore);
case HOUSE_KEY -> parseHouse(world, properties, pt, id, imageStore);
case TREE_KEY -> parseTree(world, properties, pt, id, imageStore);
case SAPLING_KEY -> parseSapling(world, properties, pt, id, imageStore);
case STUMP_KEY -> parseStump(world, properties, pt, id, imageStore);
case FENCE_KEY -> parseFence(world, properties, pt, id, imageStore);
case BARN_KEY -> parseBarn(world, properties, pt, id, imageStore);
case STEAK_KEY -> parseSteak(world, properties, pt, id, imageStore);
case BACON_KEY -> parseBacon(world, properties, pt, id, imageStore);
case VET_KEY -> parseVet(world, properties, pt, id, imageStore);
case BABY_PIG_KEY -> parseBabyPig(world, properties, pt, id, imageStore);
case BABY_COW_KEY -> parseBabyCow(world, properties, pt, id, imageStore);
case GROWN_PIG_KEY -> parseGrownPig(world, properties, pt, id, imageStore);
case GROWN_COW_KEY -> parseGrownCow(world, properties, pt, id, imageStore);
case FARMER_KEY -> parseFarmer(world, properties, pt, id, imageStore);
case WOLF_KEY -> parseWolf(world, properties, pt, id, imageStore);
default -> throw new IllegalArgumentException("Entity key is unknown");
}
}else{
throw new IllegalArgumentException("Entity must be formatted as [key] [id] [x] [y] ...");
}
}
private static void parseSapling(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == SAPLING_NUM_PROPERTIES) {
Entity entity = Factory.createSapling(id, pt, imageStore.getImageList(SAPLING_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", SAPLING_KEY, SAPLING_NUM_PROPERTIES));
}
}
private static void parsePerson(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == PERSON_NUM_PROPERTIES) {
Entity entity = Factory.createPersonSearching(id, pt, Double.parseDouble(properties[PERSON_ACTION_PERIOD]), Double.parseDouble(properties[PERSON_ANIMATION_PERIOD]), Integer.parseInt(properties[PERSON_LIMIT]), imageStore.getImageList(PERSON_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", PERSON_KEY, PERSON_NUM_PROPERTIES));
}
}
private static void parseFairy(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == FAIRY_NUM_PROPERTIES) {
Entity entity = Factory.createFairy(id, pt, Double.parseDouble(properties[FAIRY_ACTION_PERIOD]), Double.parseDouble(properties[FAIRY_ANIMATION_PERIOD]), imageStore.getImageList(FAIRY_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", FAIRY_KEY, FAIRY_NUM_PROPERTIES));
}
}
private static void parseTree(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == TREE_NUM_PROPERTIES) {
Entity entity = Factory.createTree(id, pt, Double.parseDouble(properties[TREE_ACTION_PERIOD]), Double.parseDouble(properties[TREE_ANIMATION_PERIOD]), Integer.parseInt(properties[TREE_HEALTH]), imageStore.getImageList(TREE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", TREE_KEY, TREE_NUM_PROPERTIES));
}
}
private static void parseObstacle(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == OBSTACLE_NUM_PROPERTIES) {
Entity entity = Factory.createObstacle(id, pt, Double.parseDouble(properties[OBSTACLE_ANIMATION_PERIOD]), imageStore.getImageList(OBSTACLE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", OBSTACLE_KEY, OBSTACLE_NUM_PROPERTIES));
}
}
private static void parseHouse(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == HOUSE_NUM_PROPERTIES) {
Entity entity = Factory.createHouse(id, pt, imageStore.getImageList(HOUSE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", HOUSE_KEY, HOUSE_NUM_PROPERTIES));
}
}
private static void parseStump(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == STUMP_NUM_PROPERTIES) {
Entity entity = Factory.createStump(id, pt, imageStore.getImageList(STUMP_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", STUMP_KEY, STUMP_NUM_PROPERTIES));
}
}
// new parse functions for project 4
private static void parseFence(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == FENCE_NUM_PROPERTIES) {
Entity entity = Factory.createFence(id, pt, imageStore.getImageList(FENCE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", FENCE_KEY, FENCE_NUM_PROPERTIES));
}
}
private static void parseBarn(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == BARN_NUM_PROPERTIES) {
Entity entity = Factory.createBarn(id, pt, imageStore.getImageList(BARN_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", BARN_KEY, BARN_NUM_PROPERTIES));
}
}
private static void parseSteak(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == STEAK_NUM_PROPERTIES) {
Entity entity = Factory.createSteak(id, pt, imageStore.getImageList(STEAK_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", STEAK_KEY, STEAK_NUM_PROPERTIES));
}
}
private static void parseBacon(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == BACON_NUM_PROPERTIES) {
Entity entity = Factory.createBacon(id, pt, imageStore.getImageList(BACON_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", BACON_KEY, BACON_NUM_PROPERTIES));
}
}
private static void parseVet(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == VET_NUM_PROPERTIES) {
Entity entity = Factory.createCowVet(id, pt, Double.parseDouble(properties[VET_ACTION_PERIOD]), Double.parseDouble(properties[VET_ANIMATION_PERIOD]), imageStore.getImageList(VET_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", VET_KEY, VET_NUM_PROPERTIES));
}
}
private static void parseBabyPig(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == BABY_PIG_NUM_PROPERTIES) {
Entity entity = Factory.createBabyPig(id, pt, imageStore.getImageList(BABY_PIG_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", BABY_PIG_KEY, BABY_PIG_NUM_PROPERTIES));
}
}
private static void parseBabyCow(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == BABY_COW_NUM_PROPERTIES) {
Entity entity = Factory.createBabyCow(id, pt, imageStore.getImageList(BABY_COW_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", BABY_COW_KEY, BABY_COW_NUM_PROPERTIES));
}
}
private static void parseGrownPig(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == GROWN_PIG_NUM_PROPERTIES) {
Entity entity = Factory.createGrownPig(id, pt, Double.parseDouble(properties[GROWN_PIG_ACTION_PERIOD]), Double.parseDouble(properties[GROWN_PIG_ANIMATION_PERIOD]), Integer.parseInt(properties[GROWN_PIG_HEALTH]), imageStore.getImageList(GROWN_PIG_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", GROWN_PIG_KEY, GROWN_PIG_NUM_PROPERTIES));
}
}
private static void parseGrownCow(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == GROWN_COW_NUM_PROPERTIES) {
Entity entity = Factory.createGrownCow(id, pt, Double.parseDouble(properties[GROWN_COW_ACTION_PERIOD]), Double.parseDouble(properties[GROWN_COW_ANIMATION_PERIOD]), Integer.parseInt(properties[GROWN_COW_HEALTH]), imageStore.getImageList(GROWN_COW_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", GROWN_COW_KEY, GROWN_COW_NUM_PROPERTIES));
}
}
private static void parseWolf(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == WOLF_NUM_PROPERTIES) {
Entity entity = Factory.createWolfSearching(id, pt, Double.parseDouble(properties[WOLF_ACTION_PERIOD]), Double.parseDouble(properties[WOLF_ANIMATION_PERIOD]), Integer.parseInt(properties[WOLF_LIMIT]), imageStore.getImageList(WOLF_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", WOLF_KEY, WOLF_NUM_PROPERTIES));
}
}
private static void parseFarmer(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == FARMER_NUM_PROPERTIES) {
Entity entity = Factory.createFarmerSearching(id, pt, Double.parseDouble(properties[FARMER_ACTION_PERIOD]), Double.parseDouble(properties[FARMER_ANIMATION_PERIOD]), Integer.parseInt(properties[FARMER_LIMIT]), imageStore.getImageList(FARMER_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", FARMER_KEY, FARMER_NUM_PROPERTIES));
}
}
}