-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
302 lines (269 loc) · 8.84 KB
/
Player.java
File metadata and controls
302 lines (269 loc) · 8.84 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
import java.util.Stack;
import java.util.HashMap;
import java.util.Set;
/**
* The Player class represents a player in the game.
* A player has a name, a current room, a collection of carried items,
* and a stack of visited rooms to facilitate navigation.
* The player can carry items up to a specified maximum weight.
* The player can interact with the environment by picking up and dropping items,
* and eating food.
*
* @author Florian NELCHA
*/
public class Player
{
// ### Attributes ###
private String aName;
private Room aCurrentRoom;
private double aInvWeight;
private double aMaxWeight;
public Stack<Room> aVisitedRooms;
private Item aItemInHand;
private ItemList aInventory;
private UserInterface aGui;
// ### Constructor ###
/**
* Constructs a new player with a name and starting room.
* The player's inventory is initialized with zero weight and a maximum weight of 10.0.
*
* @param pName The name of the player.
* @param pStartRoom The starting room for the player.
*/
public Player(final String pName, final Room pStartRoom)
{
this.aName = pName;
this.aCurrentRoom = pStartRoom;
this.aInvWeight = 0.0;
this.aMaxWeight = 10.0;
this.aVisitedRooms = new Stack<>();
this.aInventory = new ItemList();
} // Player()
// ### Setters ###
/**
* Sets the game graphical user interface.
*
* @param pUserInterface instance of the userInterface class.
*/
public void setGUI( final UserInterface pUserInterface )
{
this.aGui = pUserInterface;
} // setGUI()
// ### Getters ###
/**
* Gets the player's name.
*
* @return The name of the player.
*/
public String getPlayerName()
{
return this.aName;
} // getPlayerName()
/**
* Gets the current room the player is in.
*
* @return The current room.
*/
public Room getCurrentRoom()
{
return this.aCurrentRoom;
} // getCurrentRoom()
/**
* Gets the total weight of the items in the player's inventory.
*
* @return The total weight of the inventory.
*/
public double getInvWeight()
{
return this.aInventory.getTotalWeight();
} // getInvWeight()
/**
* Returns a string representation of the player's inventory.
*
* @return A string displaying the items in the inventory and their total weight.
*/
public String getInvString()
{
return "Inventory : " + this.aInventory.getItemString() + "\nTotal weight: " + this.getInvWeight() + " (max " + this.aMaxWeight + ")";
} // getInvString()
// ### Other methods ###
/**
* Moves the player to a new room and adds the
* current room to the "visited rooms" history.
*
* @param pR The room the player is moving to.
*/
public void goRoom(final Room pR)
{
this.aVisitedRooms.push(this.aCurrentRoom);
this.aCurrentRoom = pR;
} // goRoom()
/**
* Moves the player back to the previous room (if it's accessible)
* using the "visited rooms" history.
*/
public void goBack()
{
Room vPreviousRoom = this.aVisitedRooms.pop();
if (!this.aCurrentRoom.isExit(vPreviousRoom))
{
this.aGui.println("You cannot go back this way.");
return;
}
this.aCurrentRoom = vPreviousRoom;
} // goBack()
/**
* Allows the player to pick up an item from the current room and add it to their inventory.
* The player cannot carry the item if it exceeds their maximum weight capacity.
*
* @param pItemName The name of the item to be picked up.
*/
public void take(final String pItemName)
{
Item vItem = this.aCurrentRoom.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("There is no item with that name here.");
return;
}
double vNewWeight = this.getInvWeight() + vItem.getWeight();
if (vNewWeight > this.aMaxWeight)
{
this.aGui.println("Maximum weight exceeded. You cannot carry that item.");
return;
}
this.aInventory.addItem(vItem);
this.aCurrentRoom.removeItem(pItemName);
this.aGui.println(pItemName + " picked up.");
if(pItemName.equals("watch"))
{
this.aGui.aTimerLabel.setVisible(true);
}
} // take()
/**
* Drops an item from the player's inventory into the current room.
*
* @param pItemName The name of the item to be dropped.
*/
public void drop(final String pItemName)
{
Item vItem = this.aInventory.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("You are not carrying that item.");
return;
}
this.aInventory.removeItem(pItemName);
this.aCurrentRoom.addItem(vItem);
this.aGui.println(pItemName + " dropped.");
if(pItemName.equals("watch"))
{
this.aGui.aTimerLabel.setVisible(false);
}
} // drop()
/**
* When called, the player uses the specified item.
* Only works if the player wants to use a "usable" item, in the right room.
*
* @param pItemName The name of the item to be used.
* @return True if the item has correctly been used, false otherwise.
*/
public boolean use(final String pItemName)
{
Item vItem = this.aInventory.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("You are not carrying that item.");
return false;
}
if (pItemName.equals("sensor") && this.aInventory.getItem("toolbox") == null)
{
this.aGui.println("You need a toolbox to place the sensor.");
return false;
}
if (pItemName.equals("sensor") || pItemName.equals("fuse") || pItemName.equals("fuel"))
{
this.aInventory.removeItem(pItemName);
return true;
}
else
{
this.aGui.println("You cannot use this.");
return false;
}
} // use()
/**
* Allows the player to eat an item, modifying the player's attributes
* based on the item.
*
* @param pItemName The name of the item to be eaten.
*/
public void eat(final String pItemName)
{
Item vItem = this.aInventory.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("You are not carrying that item.");
return;
}
if (pItemName.equals("boost_pill"))
{
this.aMaxWeight *= 2;
this.aInventory.removeItem(pItemName);
this.aGui.println("You ate the Boost Pill! Your carrying capacity has doubled to " + this.aMaxWeight + ".");
}
else if (pItemName.equals("chips"))
{
this.aInventory.removeItem(pItemName);
this.aGui.println("You ate a pack of Orbitos ! You are not hungry anymore.");
}
else
{
this.aGui.println("You cannot eat this.");
}
} // eat()
/**
* Charges the beamer in the player's inventory with the current
* room, if the player is carrying it. Otherwise, a message is displayed.
*
* @param pItemName The room to be charged in the beamer.
*/
public void chargeBeamer(final String pItemName)
{
Item vItem = this.aInventory.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("You are not carrying the beamer.");
return;
}
Beamer vB = (Beamer)vItem;
vB.charge(this.aCurrentRoom);
this.aGui.println("Beamer charged.");
}
/**
* Fires the beamer in the player's inventory to teleport them to the charged
* room, if the player is carrying it.
* If its not charged or not in the player's inventory, a message is displayed.
*
* @param pItemName The room to be teleported in.
*/
public void fireBeamer(final String pItemName)
{
Item vItem = this.aInventory.getItem(pItemName);
if (vItem == null)
{
this.aGui.println("You are not carrying the beamer.");
return;
}
Beamer vB = (Beamer)vItem;
Room vFiredRoom = vB.fire();
if(vFiredRoom == null)
{
this.aGui.println("The beamer is not charged.");
return;
}
this.aCurrentRoom = vFiredRoom;
this.aGui.println("Teleported " + this.aCurrentRoom.getLongDescription().substring(8));
this.aGui.showImage(this.aCurrentRoom.getImageName());
}
}