aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-04-06 15:05:21 +0200
committerPacien TRAN-GIRARD2014-04-06 15:05:21 +0200
commitb8771ccb25d5d12f39dffd761e6534fe09642bcb (patch)
tree8bfa9a510e0e07b98dcf45311bf39e339b08aeb9 /src
parent4ef10e49ecf6a786c60eda0461871aa9d0012e0b (diff)
downloadesieequest-b8771ccb25d5d12f39dffd761e6534fe09642bcb.tar.gz
Implement maximum inventory weight
Diffstat (limited to 'src')
-rw-r--r--src/esieequest/controller/Performer.java9
-rw-r--r--src/esieequest/model/Game.java6
-rw-r--r--src/esieequest/model/entities/Player.java11
-rw-r--r--src/esieequest/model/items/Inventory.java12
4 files changed, 34 insertions, 4 deletions
diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java
index d1f1bb6..466f9da 100644
--- a/src/esieequest/controller/Performer.java
+++ b/src/esieequest/controller/Performer.java
@@ -138,6 +138,15 @@ class Performer {
138 * the item's name 138 * the item's name
139 */ 139 */
140 public void take(final String itemName) { 140 public void take(final String itemName) {
141 int weight = this.game.getPlayer().getInventory().getTotalWeight();
142 weight += this.game.getPlayer().getCurrentRoom().getItems().getItemWeight(itemName);
143 final int maxWeight = this.game.getPlayer().getMaxCarryWeight();
144
145 if (weight >= maxWeight) {
146 this.echo("Maximum inventory weight reached. Cannot pick up item.");
147 return;
148 }
149
141 this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName); 150 this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName);
142 } 151 }
143 152
diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java
index 63c1eee..f08f264 100644
--- a/src/esieequest/model/Game.java
+++ b/src/esieequest/model/Game.java
@@ -24,7 +24,7 @@ public class Game {
24 public Game() { 24 public Game() {
25 this.rooms = new HashMap<String, Room>(); 25 this.rooms = new HashMap<String, Room>();
26 26
27 this.player = new Player(); 27 this.player = new Player(10);
28 28
29 this.createRooms(); 29 this.createRooms();
30 this.linkRooms(); 30 this.linkRooms();
@@ -191,8 +191,8 @@ public class Game {
191 * Creates and adds items into rooms. 191 * Creates and adds items into rooms.
192 */ 192 */
193 private void createItems() { 193 private void createItems() {
194 this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12)); 194 this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 5));
195 this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15)); 195 this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 6));
196 } 196 }
197 197
198 /** 198 /**
diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java
index 5a97726..91888eb 100644
--- a/src/esieequest/model/entities/Player.java
+++ b/src/esieequest/model/entities/Player.java
@@ -11,12 +11,14 @@ public class Player {
11 private final Stack<Room> previousRooms; 11 private final Stack<Room> previousRooms;
12 12
13 private final Inventory inventory; 13 private final Inventory inventory;
14 private final int maxCarryWeight;
14 15
15 public Player() { 16 public Player(final int maxCarryWeight) {
16 this.currentRoom = null; 17 this.currentRoom = null;
17 this.previousRooms = new Stack<Room>(); 18 this.previousRooms = new Stack<Room>();
18 19
19 this.inventory = new Inventory(); 20 this.inventory = new Inventory();
21 this.maxCarryWeight = maxCarryWeight;
20 } 22 }
21 23
22 /** 24 /**
@@ -57,4 +59,11 @@ public class Player {
57 return this.inventory; 59 return this.inventory;
58 } 60 }
59 61
62 /**
63 * @return the maxCarryWeight
64 */
65 public int getMaxCarryWeight() {
66 return this.maxCarryWeight;
67 }
68
60} 69}
diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java
index 99b48a9..86e86b5 100644
--- a/src/esieequest/model/items/Inventory.java
+++ b/src/esieequest/model/items/Inventory.java
@@ -74,4 +74,16 @@ public class Inventory {
74 return Utils.list(this.items.keySet(), "Items:", "No items."); 74 return Utils.list(this.items.keySet(), "Items:", "No items.");
75 } 75 }
76 76
77 public int getItemWeight(final String itemName) {
78 return this.items.get(itemName).getWeight();
79 }
80
81 public int getTotalWeight() {
82 int totalWeight = 0;
83 for (final Item item : this.items.values()) {
84 totalWeight += item.getWeight();
85 }
86 return totalWeight;
87 }
88
77} 89}