-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.java
395 lines (334 loc) · 10.1 KB
/
Level.java
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//-------------------------------------------------------------
//File: Level.java
//Desc: Holds a collection of blocks, enemies, collectables, player, and size of level
// Methodes to save/load a level
//-------------------------------------------------------------
package model;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
/**
* Class that holds an instance of a level.
*/
public class Level {
// arrayList to hold the enemies in the level
private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
// arrayList to hold the goals
private ArrayList<Goal> goals = new ArrayList<Goal>();
// arraylist to hold the blocks that make up the map
private ArrayList<Block> blocks = new ArrayList<Block>();
// arraylist to hold collectables
private ArrayList<Collectable> collectables = new ArrayList<Collectable>();
// width of the level
private int width = 0;
// height of the level
private int height = 0;
// Name of the level
private String levelName;
// Point object that holds the spawn point
private Point spawnPoint = new Point();
public Level() {
}
/**
* get the goal objects of the level
*
* @return Goal
*/
public ArrayList<Goal> getGoals() {
return goals;
}
/**
* get the Point objects of the level
*
* @return Point
*/
public Point getSpawnPoint() {
return spawnPoint;
}
/**
* set the spawn point for the level
*/
public void setSpawnPoint(Point spawnPoint) {
this.spawnPoint.copyFrom(spawnPoint);
}
/**
* get the name of the level
*
* @return - levelName
*/
public String getLevelName() {
return levelName;
}
/**
* set the name of the level
*
* @param levelName
*/
public void setLevelName(String levelName) {
this.levelName = levelName;
}
/**
* find the Entity with the given id
*
* @param id
* @return Entity
*/
public Enemy findEntity(int id) {
for (Enemy entity : enemies) {
if (entity.getId() == id) {
return entity;
}
}
return null;
}
/**
* remove the given enemy from entities
*
* @param Enemy entity
*/
public void removeEntity(Enemy entity) {
enemies.remove(entity);
}
/**
* find the surface with the given id
*
* @param id
* @return Block
*/
public Block findBlock(int id) {
for (Block block : blocks) {
if (block.getId() == id) {
return block;
}
}
return null;
}
/**
* remove the given block from blocks
*
* @param block
*/
public void removeBlock(Block block) {
blocks.remove(block);
}
/**
* remove the given item from collectables
*
* @param id
*/
public void removeCollectable(Collectable item) {
collectables.remove(item);
}
/**
* remove the given goal from the level
*
* @param flag
*/
public void removeGoal(Goal flag) {
goals.remove(flag);
}
/**
* adds enemy to entities
*
* @param entity
*/
public void addEntity(Enemy entity) {
enemies.add(entity);
}
/**
* adds surface to surfaces
*
* @param block
*/
public void addBlock(Block block) {
blocks.add(block);
}
/**
* adds item to collectables
*
* @param item
*/
public void addCollectable(Collectable item) {
collectables.add(item);
}
/**
* Gets the surfaces in the level
*
* @return blocks in the level
*/
public ArrayList<Block> getBlocks() {
return blocks;
}
/**
* Gets the Entities in the level
*
* @return surfaces
*/
public ArrayList<Enemy> getEntites() {
return enemies;
}
/**
* Gets the items in the level
*
* @return surfaces
*/
public ArrayList<Collectable> getCollectables() {
return collectables;
}
/**
* get the width of the world
*
* @return width
*/
public int getWidth() {
return width;
}
/**
* set teh width of the world
*
* @param width
*/
public void setWidth(int width) {
this.width = width;
}
/**
* get the hight of the world
*
* @return height
*/
public int getHeight() {
return height;
}
/**
* Set the hight of the world
*
* @param height
*/
public void setHeight(int height) {
this.height = height;
}
/**
* Save the file
*/
public void save(String filename) throws IOException {
try (DataOutputStream writer = new DataOutputStream(new FileOutputStream(filename, false))) {
// write the size of the level
writer.writeInt(width);
writer.writeInt(height);
// write the spawn point
writer.writeInt(spawnPoint.getIntX());
writer.writeInt(spawnPoint.getIntY());
// write the number of goals
writer.writeInt(goals.size());
// write the coardinates for each Goal
for (int i = 0; i < goals.size(); ++i) {
writer.writeInt(goals.get(i).centerPoint().getIntX());
writer.writeInt(goals.get(i).centerPoint().getIntY());
writer.writeInt(goals.get(i).getHeight());
writer.writeInt(goals.get(i).getWidth());
}
// write how many entities their are
writer.writeInt(enemies.size());
// Iterate through the entities saving each's data
for (int i = 0; i < enemies.size(); ++i) {
writer.writeInt(enemies.get(i).getId());
writer.writeUTF(enemies.get(i).getType().toString());
writer.writeInt(enemies.get(i).centerPoint().getIntX());
writer.writeInt(enemies.get(i).centerPoint().getIntY());
}
// Write how many blocks there are
writer.writeInt(blocks.size());
// Iterate through the blocks saving each's data
for (int i = 0; i < blocks.size(); ++i) {
writer.writeInt(blocks.get(i).getId());
writer.writeUTF(blocks.get(i).getTexture());
writer.writeInt(blocks.get(i).centerPoint().getIntX());
writer.writeInt(blocks.get(i).centerPoint().getIntY());
writer.writeInt(blocks.get(i).getHeight());
writer.writeInt(blocks.get(i).getWidth());
}
// write how many collectables there are
writer.writeInt(collectables.size());
// Iterate through the collectables saving each's data
for (int i = 0; i < collectables.size(); ++i) {
// save data for each collacetable
writer.writeUTF(collectables.get(i).getStringType());
writer.writeInt(collectables.get(i).centerPoint().getIntX());
writer.writeInt(collectables.get(i).centerPoint().getIntY());
}
}
}
/**
*
* Load the level
*/
public void load(String fileName) throws IOException {
// Set the name of the level
setLevelName(fileName);
// clear the level of old items/enemies/blocks
enemies.clear();
blocks.clear();
collectables.clear();
goals.clear();
// Create reader to load the level
var reader = new DataInputStream(new FileInputStream(fileName));
// read the size of the level
int width = reader.readInt();
int height = reader.readInt();
// read and update the spawn point
int spawnX = reader.readInt();
int spawnY = reader.readInt();
// read the bumber of goals
int sizeOfGoals = reader.readInt();
for (int i = 0; i < sizeOfGoals; ++i) {
Goal flag = new Goal(reader.readInt(), reader.readInt());
flag.setHeight(reader.readInt());
flag.setWidth(reader.readInt());
goals.add(flag);
}
// read the number of entities
int sizeOfEntities = reader.readInt();
// iterate over each playing gathering their values
for (int i = 0; i < sizeOfEntities; ++i) {
Enemy entity = new Enemy();
entity.setId(reader.readInt());
entity.setType(reader.readUTF());
entity.centerPoint().setXY(reader.readInt(), reader.readInt());
entity.setWidth(59);
entity.setHeight(50);
enemies.add(entity);
}
// get blocks
int sizeOfBlocks = reader.readInt();
// iterate over block in the save file and create a matching block
for (int i = 0; i < sizeOfBlocks; ++i) {
Block box = new Block();
box.setId(reader.readInt());
box.setTexture(reader.readUTF());
box.centerPoint().setXY(reader.readInt(), reader.readInt());
box.setWidth(reader.readInt());
box.setHeight(reader.readInt());
blocks.add(box);
}
// Load number of collectables
int sizeOfCollectables = reader.readInt();
// Iterate through the collectables loading each's data
for (int i = 0; i < sizeOfCollectables; ++i) {
String type = reader.readUTF();
int x = reader.readInt();
int y = reader.readInt();
Collectable col = new Collectable(x, y, CollectableType.valueOf(type));
collectables.add(col);
}
// set the spawn point
setSpawnPoint(new Point(spawnX, spawnY));
// set the size of the level
setWidth(width);
setHeight(height);
//close the reader
reader.close();
}
}