-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoom.java
More file actions
112 lines (98 loc) · 2.6 KB
/
Room.java
File metadata and controls
112 lines (98 loc) · 2.6 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
public class Room
{
private Exit[] theExits;
private String name;
private Player thePlayer;
private int id;
public Room(JSONObject obj)
{
this.id = ((JSONNumberVariable)obj.getVariableForName("id")).getValue();
this.name = ((JSONStringVariable)obj.getVariableForName("name")).getValue();
//get the exits
JSONArrayVariable av = (JSONArrayVariable)obj.getVariableForName("exits");
this.theExits = new Exit[av.getCurrSize()];
JSONObject[] theObjects = av.getValue();
for(int i = 0; i < av.getCurrSize(); i++)
{
this.theExits[i] = new Exit(theObjects[i]);
}
}
public Room(String name, int id)
{
this.name = name;
this.id = id;
this.thePlayer = null;
this.theExits = new Exit[0];
}
public JSONObject getJSONObject()
{
JSONObject theObj = new JSONObject();
theObj.addVariable(new JSONNumberVariable("id", this.id));
theObj.addVariable(new JSONStringVariable("name", this.name));
JSONArrayVariable theExits = new JSONArrayVariable("exits");
for(Exit e : this.theExits)
{
theExits.addJSONObject(e.getJSONObject());
}
theObj.addVariable(theExits);
return theObj;
}
public int getId()
{
return id;
}
public Player removeThePlayer()
{
Player playerToReturn = this.thePlayer;
this.thePlayer = null;
return playerToReturn;
}
public boolean takeExit(String exitName)
{
for(int i = 0; i < this.theExits.length; i++)
{
if(this.theExits[i].getName().equalsIgnoreCase(exitName))
{
for(Room r : CaveCore.theRooms)
{
if(r.getId() == this.theExits[i].getDestinationID())
{
r.addThePlayer(this.thePlayer);
this.thePlayer = null;
return true;
}
}
}
}
this.thePlayer.displayToUser("Destination Room not found!!!");
return false;
}
public void displayDetailsToUser()
{
this.thePlayer.displayToUser("You have entered: " + this.name);
this.thePlayer.displayToUser("Possible Exits: ");
for(int i = 0; i < this.theExits.length; i++)
{
this.thePlayer.displayToUser(this.theExits[i].getName());
}
this.thePlayer.showPrompt();
}
public void addThePlayer(Player thePlayer)
{
//let the player know about the room they are entering
thePlayer.setCurrentRoom(this);
//set this room's player to thePlayer and display details to thePlayer
this.thePlayer = thePlayer;
this.displayDetailsToUser();
}
public void addExit(String name, int destinationID)
{
Exit[] tempExits = new Exit[this.theExits.length+1];
for(int i = 0; i < this.theExits.length; i++)
{
tempExits[i] = this.theExits[i];
}
tempExits[tempExits.length-1] = new Exit(name, destinationID);
this.theExits = tempExits;
}
}