From e9c61548bd9a141587f1ff89a790b9dccdd0fa61 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 16 Apr 2014 00:48:30 +0200 Subject: Implement LockedDoor --- src/esieequest/controller/Performer.java | 13 ++++++-- src/esieequest/model/Game.java | 46 ++++++++++++++++++++++++----- src/esieequest/model/items/Beamer.java | 4 +-- src/esieequest/model/items/Inventory.java | 16 ++++++++-- src/esieequest/model/items/Item.java | 8 ++++- src/esieequest/model/items/KeyCard.java | 14 +++++++++ src/esieequest/model/map/LockedDoor.java | 49 +++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 src/esieequest/model/items/KeyCard.java create mode 100644 src/esieequest/model/map/LockedDoor.java (limited to 'src') diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index add3fe5..de8d814 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -9,6 +9,7 @@ import esieequest.model.items.Beamer; import esieequest.model.items.Inventory; import esieequest.model.items.Item; import esieequest.model.map.Door; +import esieequest.model.map.LockedDoor; import esieequest.model.map.Room; import esieequest.model.map.TrapDoor; import esieequest.view.View; @@ -115,6 +116,14 @@ class Performer { this.echo(this.game.getNoExitMessage()); return; } + if (door.getClass() == LockedDoor.class) { + final LockedDoor lockedDoor = (LockedDoor) door; + final boolean authorised = this.game.getPlayer().getInventory().containsItem(lockedDoor.getKey()); + if (!authorised) { + this.echo("This door is locked."); + return; + } + } final Room nextRoom = door.getDestination(); this.game.getPlayer().goToRoom(nextRoom); this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); @@ -155,7 +164,7 @@ class Performer { * the item's name */ public void takeItem(final String itemName) { - if (!this.game.getPlayer().getCurrentRoom().getItems().hasItem(itemName)) { + if (!this.game.getPlayer().getCurrentRoom().getItems().containsItem(itemName)) { this.echo("No such item in this room."); return; } @@ -193,7 +202,7 @@ class Performer { * the item's name */ private void moveItem(final Inventory source, final Inventory dest, final String itemName) { - if (!source.hasItem(itemName)) { + if (!source.containsItem(itemName)) { this.echo("No such item."); return; } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 6e6b191..0ff4b92 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -5,7 +5,9 @@ import java.util.HashMap; import esieequest.model.entities.Player; import esieequest.model.items.Beamer; import esieequest.model.items.Item; +import esieequest.model.items.KeyCard; import esieequest.model.map.Door; +import esieequest.model.map.LockedDoor; import esieequest.model.map.Room; import esieequest.model.map.TrapDoor; @@ -88,12 +90,22 @@ public class Game { this.createRoom("WingCorridorTwoOffice", "in front of the office #3254"); this.createRoom("WingOffice", "in the office #3254"); + // off-script rooms this.createRoom("OffscriptItems", "somewhere implementing weight"); this.createRoom("OffscriptItemsStorageroom", "in a storage room"); + this.createRoom("OffscriptTeleportation", "somewhere implementing teleportation"); this.createRoom("OffscriptTeleportationBeamerroom", "on a checkpoint"); + + this.createRoom("OffscriptTrap", "somewhere implementing a trap"); + this.createRoom("OffscriptTrapDeadend", "in a dead end"); + + this.createRoom("OffscriptLock", "somewhere implementing a lock"); + this.createRoom("OffscriptLockLockedroom", "in a locked room"); + 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"); this.createRoom("OffscriptMovingcharacterSumobotroom", "in the Chirac-101's room"); } @@ -167,15 +179,28 @@ public class Game { this.setRoomExit("WingCorridorTwoOffice", "east", "WingOffice"); this.setRoomExit("WingOffice", "west", "WingCorridorTwoOffice"); - this.rooms.get("WingCorridorOne").addExit("east", new TrapDoor(this.rooms.get("OffscriptItems"))); + this.rooms.get("WingCorridorOne").addExit("east", new Door(this.rooms.get("OffscriptItems"))); + // this.rooms.get("WingCorridorOne").addExit("east", new + // TrapDoor(this.rooms.get("OffscriptItems"))); this.setRoomExit("OffscriptItems", "north", "OffscriptItemsStorageroom"); this.setRoomExit("OffscriptItems", "east", "OffscriptTeleportation"); this.setRoomExit("OffscriptItemsStorageroom", "south", "OffscriptItems"); this.setRoomExit("OffscriptTeleportation", "north", "OffscriptTeleportationBeamerroom"); this.setRoomExit("OffscriptTeleportation", "west", "OffscriptItems"); - this.setRoomExit("OffscriptTeleportation", "east", "OffscriptAlea"); + this.setRoomExit("OffscriptTeleportation", "east", "OffscriptTrap"); this.setRoomExit("OffscriptTeleportationBeamerroom", "south", "OffscriptTeleportation"); + // this.setRoomExit("OffscriptTrap", "north", "OffscriptTrapDeadend"); + this.rooms.get("OffscriptTrap").addExit("north", new TrapDoor(this.rooms.get("OffscriptTrapDeadend"))); + this.setRoomExit("OffscriptTrap", "west", "OffscriptTeleportation"); + this.setRoomExit("OffscriptTrap", "east", "OffscriptLock"); + // this.setRoomExit("OffscriptTrapDeadend", "south", "OffscriptTrap"); + // this.setRoomExit("OffscriptLock", "north", "OffscriptLockedroom"); + this.rooms.get("OffscriptLock").addExit("north", new LockedDoor(this.rooms.get("OffscriptLockLockedroom"))); + this.setRoomExit("OffscriptLock", "west", "OffscriptTrap"); + this.setRoomExit("OffscriptLock", "east", "OffscriptAlea"); + // this.setRoomExit("OffscriptLockedroom", "south", "OffscriptLock"); + this.rooms.get("OffscriptLockLockedroom").addExit("south", new LockedDoor(this.rooms.get("OffscriptLock"))); this.setRoomExit("OffscriptAlea", "north", "OffscriptAleaRandomizingroom"); this.setRoomExit("OffscriptAlea", "west", "OffscriptTeleportation"); this.setRoomExit("OffscriptAlea", "east", "OffscriptMovingcharacter"); @@ -189,11 +214,18 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - 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)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Weighted Storage Cube", new Item("A Weighted Storage Cube.", 5, true)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Edgeless Safety Cube", new Item("An Edgeless Safety Cube.", 5, true)); + this.rooms.get("OffscriptItemsStorageroom").getItems().putItem("Portable Black-hole", new Item("A portable black-hole that has a negative mass.", -10, true)); + + this.rooms.get("OffscriptTeleportationBeamerroom").getItems().putItem("Portable Quantum Tunelling Device", new Beamer("Basically a teleporter.")); + + final KeyCard keyCard = new KeyCard("A KeyCard that opens a locked door."); + final LockedDoor lockedDoorLock = (LockedDoor) this.rooms.get("OffscriptLock").getExit("north"); + lockedDoorLock.setKey(keyCard); + final LockedDoor lockedDoorLockedRoom = (LockedDoor) this.rooms.get("OffscriptLockLockedroom").getExit("south"); + lockedDoorLockedRoom.setKey(keyCard); + this.rooms.get("OffscriptLock").getItems().putItem("KeyCard", keyCard); } /** diff --git a/src/esieequest/model/items/Beamer.java b/src/esieequest/model/items/Beamer.java index ba8f361..5618ea8 100644 --- a/src/esieequest/model/items/Beamer.java +++ b/src/esieequest/model/items/Beamer.java @@ -19,8 +19,8 @@ public class Beamer extends Item { * @param description * @param weight */ - public Beamer(final String description, final int weight) { - super(description, weight); + public Beamer(final String description) { + super(description, true); } /** diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index 6de8f1d..94691ad 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -45,17 +45,29 @@ public class Inventory { } /** - * Tells is an inventory contains a given item by its name. + * Tells if the inventory contains a given item by its name. * * @param itemName * the item's name * * @return the existence of the item */ - public boolean hasItem(final String itemName) { + public boolean containsItem(final String itemName) { return this.items.get(itemName) != null; } + /** + * Tells if the inventory contains a given item. + * + * @param item + * the item + * + * @return the existence of the item + */ + public boolean containsItem(final Item item) { + return this.items.containsValue(item); + } + /** * Puts an item in the inventory. * diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java index 01bf3f3..b9d6deb 100644 --- a/src/esieequest/model/items/Item.java +++ b/src/esieequest/model/items/Item.java @@ -10,6 +10,7 @@ public class Item { private final String description; private final int weight; + private final boolean droppable; /** * Creates an item with the given characteristics. @@ -19,9 +20,14 @@ public class Item { * @param weight * the weight of the item */ - public Item(final String description, final int weight) { + public Item(final String description, final int weight, final boolean droppable) { this.description = description; this.weight = weight; + this.droppable = droppable; + } + + public Item(final String description, final boolean droppable) { + this(description, 0, droppable); } /** diff --git a/src/esieequest/model/items/KeyCard.java b/src/esieequest/model/items/KeyCard.java new file mode 100644 index 0000000..1579ee1 --- /dev/null +++ b/src/esieequest/model/items/KeyCard.java @@ -0,0 +1,14 @@ +package esieequest.model.items; + +/** + * A key-card that unlocks a Door. + * + * @author Pacien TRAN-GIRARD + */ +public class KeyCard extends Item { + + public KeyCard(final String description) { + super(description, false); + } + +} diff --git a/src/esieequest/model/map/LockedDoor.java b/src/esieequest/model/map/LockedDoor.java new file mode 100644 index 0000000..08a6513 --- /dev/null +++ b/src/esieequest/model/map/LockedDoor.java @@ -0,0 +1,49 @@ +package esieequest.model.map; + +import esieequest.model.items.KeyCard; + +/** + * A door that links two rooms and requires a keycard to pass through. + * + * @author Pacien TRAN-GIRARD + */ +public class LockedDoor extends Door { + + private KeyCard keyCard; + + public LockedDoor(final Room destination) { + super(destination); + } + + /** + * Returns the KeyCard that opens this door. + * + * @return the KeyCard + */ + public KeyCard getKey() { + return this.keyCard; + } + + /** + * Sets the KeyCard authorised to open this door. + * + * @param keyCard + * the KeyCard to set + */ + public void setKey(final KeyCard keyCard) { + this.keyCard = keyCard; + } + + /** + * Tells if a given KeyCard can open this door. + * + * @param keyCard + * the KeyCard + * + * @return + */ + public boolean isAuthorised(final KeyCard keyCard) { + return keyCard == this.keyCard; + } + +} -- cgit v1.2.3