From b8771ccb25d5d12f39dffd761e6534fe09642bcb Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:05:21 +0200 Subject: Implement maximum inventory weight --- src/esieequest/controller/Performer.java | 9 +++++++++ src/esieequest/model/Game.java | 6 +++--- src/esieequest/model/entities/Player.java | 11 ++++++++++- src/esieequest/model/items/Inventory.java | 12 ++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src') 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 { * the item's name */ public void take(final String itemName) { + int weight = this.game.getPlayer().getInventory().getTotalWeight(); + weight += this.game.getPlayer().getCurrentRoom().getItems().getItemWeight(itemName); + final int maxWeight = this.game.getPlayer().getMaxCarryWeight(); + + if (weight >= maxWeight) { + this.echo("Maximum inventory weight reached. Cannot pick up item."); + return; + } + this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName); } 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 { public Game() { this.rooms = new HashMap(); - this.player = new Player(); + this.player = new Player(10); this.createRooms(); this.linkRooms(); @@ -191,8 +191,8 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12)); - this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15)); + this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 5)); + this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 6)); } /** 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 { private final Stack previousRooms; private final Inventory inventory; + private final int maxCarryWeight; - public Player() { + public Player(final int maxCarryWeight) { this.currentRoom = null; this.previousRooms = new Stack(); this.inventory = new Inventory(); + this.maxCarryWeight = maxCarryWeight; } /** @@ -57,4 +59,11 @@ public class Player { return this.inventory; } + /** + * @return the maxCarryWeight + */ + public int getMaxCarryWeight() { + return this.maxCarryWeight; + } + } 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 { return Utils.list(this.items.keySet(), "Items:", "No items."); } + public int getItemWeight(final String itemName) { + return this.items.get(itemName).getWeight(); + } + + public int getTotalWeight() { + int totalWeight = 0; + for (final Item item : this.items.values()) { + totalWeight += item.getWeight(); + } + return totalWeight; + } + } -- cgit v1.2.3