-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCave.java
More file actions
61 lines (53 loc) · 1.52 KB
/
Cave.java
File metadata and controls
61 lines (53 loc) · 1.52 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
public class Cave
{
private String name;
private int startRoomID;
public Cave(String name, String filename)
{
this.name = name;
CaveParser cp = new CaveParser(filename);
JSONObject theObject = cp.parse();
//this.theObject.display();
//get the startRoomID
this.startRoomID = ((JSONNumberVariable)theObject.getVariableForName("startRoomID")).getValue();
//get the Rooms array
JSONArrayVariable av = (JSONArrayVariable)theObject.getVariableForName("rooms");
//av.display();
//build all of the rooms
CaveCore.theRooms = new Room[av.getCurrSize()];
JSONObject[] theObjects = av.getValue();
for(int i = 0; i < av.getCurrSize(); i++)
{
CaveCore.theRooms[i] = new Room(theObjects[i]);
}
//Let CaveCore know about this Cave
CaveCore.theCave = this;
}
public JSONObject toJSON()
{
JSONObject theObj = new JSONObject();
theObj.addVariable(new JSONNumberVariable("startRoomID", this.startRoomID));
JSONArrayVariable theRooms = new JSONArrayVariable("rooms");
//fill our theRooms variable
for(Room r : CaveCore.theRooms)
{
theRooms.addJSONObject(r.getJSONObject());
}
theObj.addVariable(theRooms);
return theObj;
}
public void addPlayer(Player p)
{
//find the Room object in our array of Rooms that has an
//id that matches the startRoomID of this cave. Then
//add the Player to that Room
for(int i = 0; i < CaveCore.theRooms.length; i++)
{
if(CaveCore.theRooms[i].getId() == this.startRoomID)
{
CaveCore.theRooms[i].addThePlayer(p);
return;
}
}
}
}