-
Notifications
You must be signed in to change notification settings - Fork 0
/
Room.java
171 lines (150 loc) · 4.49 KB
/
Room.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
import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
/**
* A "Room" represents one location in the scenery of the game. It is
* connected to other rooms via exits. For each existing exit, the room
* stores a reference to the neighboring room.
* The room may also contain, and additionally store Items or Creatures (both Companions and Bosses)
*
* @author 162224
* @version 13/11
*
*/
public abstract class Room
{
public int RoomID;
protected String description;
protected String longdescription;
protected String altdescription;
protected HashMap<String, Room> exits; // stores exits of this room.
protected HashMap<String, Item> contents; //stores contents of the room.
public HashMap<String, Creature> creatures; //stores contents of the room.
public String imageFile;
/**
* Create a room which is given an ID as a parameter. The descriptions generated are dependent on that ID.
* @param description The room's description.
* @param longdescription A detailed description of the room.
* @param altdescription If the room is altered some way, a different description is provided.
*/
public Room(int roomId)
{
RoomID = roomId;
exits = new HashMap<String, Room>();
contents = new HashMap<String, Item>();
creatures = new HashMap<String, Creature>();
setItems();
setCreatures();
}
protected abstract void setItems();
protected abstract void setCreatures();
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* Removes a stored item from the room.
*/
public void removeItem(Item item)
{
contents.remove(item);
}
/**
* @return The short description of the room
*/
public String getShortDescription()
{
return "You " + description + ".\n" + getExitString();
}
/**
* @returns a longer description of the room. If the room has been emptied of creatures, an alternate description is provided.
*
*/
public String getLongDescription()
{
if (creatures.isEmpty()&&altdescription!=null){
return altdescription;
}
else{
return longdescription;
}
}
/**
* Return a string describing the room's exits.
* @return Details of the room's exits.
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction)
{
return exits.get(direction);
}
/**
*Retrieves a stored item from the room.
*@return Item
*/
public Item getItem(String itemName)
{
return contents.get(itemName);
}
/**
*Retrieves a stored creature from the room.
*@return Creature
*/
public Creature getCreature(String creature)
{
return creatures.get(creature);
}
/**
*Removes a stored creature from the room.
*/
public void removeCreature(String creature)
{
creatures.remove(creature);
}
public JPanel getUI()
{
JPanel result = new JPanel();
//result.setLayout(null);
//result.setBounds(0, 0, 650, 500);
JLabel bg = getBackground();
result.add(bg);
for(Item item : contents.values()){
bg.add(item.getUi());
}
for(Creature creature : creatures.values()){
bg.add(creature.getUi());
}
result.setVisible(true);
return result;
}
private JLabel getBackground(){
JLabel background = new JLabel();
//background.setBounds(0, 0, 650, 500);
background.setIcon(new ImageIcon(imageFile));
background.setVisible(true);
return background;
}
}