From 6a154d2feb6722cb0395bb1e69af2be5a76cdacc Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Apr 2014 20:50:06 +0200 Subject: Implement the Beamer --- src/esieequest/controller/Interpreter.java | 24 +++++++++++------ src/esieequest/controller/Parser.java | 2 +- src/esieequest/controller/Performer.java | 27 ++++++++++++++++++-- src/esieequest/model/Game.java | 17 ++++++++----- src/esieequest/model/items/Beamer.java | 41 ++++++++++++++++++++++++++++++ src/esieequest/model/items/Inventory.java | 12 +++++++++ 6 files changed, 105 insertions(+), 18 deletions(-) create mode 100644 src/esieequest/model/items/Beamer.java (limited to 'src') diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 32adeda..e09ad91 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -48,6 +48,8 @@ class Interpreter { public void dispatch(final Command command) { if (command.getAction() != null) { switch (command.getAction()) { + + // game related: case NEW: this.performer.newGame(); return; @@ -57,9 +59,17 @@ class Interpreter { case SAVE: this.performer.saveGame(); return; + case QUIT: + this.performer.quitGame(); + return; case SOUND: this.performer.toggleSound(); return; + case HELP: + this.performer.showHelp(); + return; + + // map related: case GO: this.performer.goTo(command.getOption()); return; @@ -69,25 +79,23 @@ class Interpreter { case LOOK: this.performer.look(); return; + + // items related: case INVENTORY: this.performer.listItems(); return; case TAKE: - this.performer.take(command.getOption()); + this.performer.takeItem(command.getOption()); return; case DROP: - this.performer.drop(command.getOption()); + this.performer.dropItem(command.getOption()); return; - case HELP: - this.performer.showHelp(); - return; - case QUIT: - this.performer.quitGame(); + case USE: + this.performer.useItem(command.getOption()); return; } } this.performer.echo("Unknown command."); return; } - } diff --git a/src/esieequest/controller/Parser.java b/src/esieequest/controller/Parser.java index 7b07efb..d13f940 100644 --- a/src/esieequest/controller/Parser.java +++ b/src/esieequest/controller/Parser.java @@ -25,7 +25,7 @@ class Parser { * @return the command */ public Command getCommand(final String commandString) { - final String[] elements = commandString.split(" "); + final String[] elements = commandString.split(" ", 2); CommandWord action = null; String option = null; diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index b5c32ba..add3fe5 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -5,7 +5,9 @@ import java.util.Set; import esieequest.model.Game; import esieequest.model.commands.CommandWord; +import esieequest.model.items.Beamer; import esieequest.model.items.Inventory; +import esieequest.model.items.Item; import esieequest.model.map.Door; import esieequest.model.map.Room; import esieequest.model.map.TrapDoor; @@ -152,7 +154,12 @@ class Performer { * @param itemName * the item's name */ - public void take(final String itemName) { + public void takeItem(final String itemName) { + if (!this.game.getPlayer().getCurrentRoom().getItems().hasItem(itemName)) { + this.echo("No such item in this room."); + return; + } + int weight = this.game.getPlayer().getInventory().getTotalWeight(); weight += this.game.getPlayer().getCurrentRoom().getItems().getItemWeight(itemName); final int maxWeight = this.game.getPlayer().getMaxCarryWeight(); @@ -171,7 +178,7 @@ class Performer { * @param itemName * the item's name */ - public void drop(final String itemName) { + public void dropItem(final String itemName) { this.moveItem(this.game.getPlayer().getInventory(), this.game.getPlayer().getCurrentRoom().getItems(), itemName); } @@ -200,4 +207,20 @@ class Performer { this.echo(Utils.list(this.game.getPlayer().getInventory().getItemList(), "Items:", "No item in your inventory.")); } + public void useItem(final String itemName) { + final Item item = this.game.getPlayer().getInventory().getItem(itemName); + if (item == null) { + this.echo("You don't have such item."); + return; + } + if (item.getClass() == Beamer.class) { + final Beamer beamer = (Beamer) item; + if (beamer.getRoom() == null) { + beamer.setRoom(this.game.getPlayer().getCurrentRoom()); + } else { + this.game.getPlayer().goToRoom(beamer.getRoom()); + } + } + } + } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 83fa143..6e6b191 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -3,6 +3,7 @@ package esieequest.model; import java.util.HashMap; import esieequest.model.entities.Player; +import esieequest.model.items.Beamer; import esieequest.model.items.Item; import esieequest.model.map.Door; import esieequest.model.map.Room; @@ -90,7 +91,7 @@ public class Game { this.createRoom("OffscriptItems", "somewhere implementing weight"); this.createRoom("OffscriptItemsStorageroom", "in a storage room"); this.createRoom("OffscriptTeleportation", "somewhere implementing teleportation"); - this.createRoom("OffscriptTeleportationAnchorroom", "on a checkpoint"); + this.createRoom("OffscriptTeleportationBeamerroom", "on a checkpoint"); this.createRoom("OffscriptAlea", "somewhere implementing alea"); this.createRoom("OffscriptAleaRandomizingroom", "in a weird room that will transport you somewhere else"); this.createRoom("OffscriptMovingcharacter", "somewhere implementing a moving character"); @@ -171,10 +172,10 @@ public class Game { this.setRoomExit("OffscriptItems", "north", "OffscriptItemsStorageroom"); this.setRoomExit("OffscriptItems", "east", "OffscriptTeleportation"); this.setRoomExit("OffscriptItemsStorageroom", "south", "OffscriptItems"); - this.setRoomExit("OffscriptTeleportation", "north", "OffscriptTeleportationAnchorroom"); - this.setRoomExit("OffscriptTeleportation", "west", "OffscriptTime"); + this.setRoomExit("OffscriptTeleportation", "north", "OffscriptTeleportationBeamerroom"); + this.setRoomExit("OffscriptTeleportation", "west", "OffscriptItems"); this.setRoomExit("OffscriptTeleportation", "east", "OffscriptAlea"); - this.setRoomExit("OffscriptTeleportationAnchorroom", "south", "OffscriptTeleportation"); + this.setRoomExit("OffscriptTeleportationBeamerroom", "south", "OffscriptTeleportation"); this.setRoomExit("OffscriptAlea", "north", "OffscriptAleaRandomizingroom"); this.setRoomExit("OffscriptAlea", "west", "OffscriptTeleportation"); this.setRoomExit("OffscriptAlea", "east", "OffscriptMovingcharacter"); @@ -188,9 +189,11 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - 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)); - this.rooms.get("Cafeteria").getItems().putItem("anti-matter", new Item("A block of anti-matter with a negative mass", -10)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Weighted Storage Cube", new Item("A Weighted Storage Cube.", 5)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Edgeless Safety Cube", new Item("An Edgeless Safety Cube.", 5)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Portable Black-hole", new Item("A portable black-hole that has a negative mass.", -10)); + + this.rooms.get("OffscriptTeleportationBeamerroom").getItems().putItem("Portable Quantum Tunelling Device", new Beamer("Basically a teleporter.", 2)); } /** diff --git a/src/esieequest/model/items/Beamer.java b/src/esieequest/model/items/Beamer.java new file mode 100644 index 0000000..ba8f361 --- /dev/null +++ b/src/esieequest/model/items/Beamer.java @@ -0,0 +1,41 @@ +package esieequest.model.items; + +import esieequest.model.map.Room; + +/** + * A beamer is a device that can be charged, and fired. When charged, it + * memorises the current room. When fired, it transports the Player back to the + * room where it was charged. + * + * @author Pacien TRAN-GIRARD + */ +public class Beamer extends Item { + + private Room room; + + /** + * Creates a Beamer. + * + * @param description + * @param weight + */ + public Beamer(final String description, final int weight) { + super(description, weight); + } + + /** + * @return the room + */ + public Room getRoom() { + return this.room; + } + + /** + * @param room + * the room to set + */ + public void setRoom(final Room room) { + this.room = room; + } + +} diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index dddff8f..6de8f1d 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -18,6 +18,18 @@ public class Inventory { this.items = new HashMap(); } + /** + * Returns an item from the inventory, without removing it. + * + * @param itemName + * the item's name + * + * @return the item + */ + public Item getItem(final String itemName) { + return this.items.get(itemName); + } + /** * Takes (returns and removes) an item from the inventory. * -- cgit v1.2.3