From 7bd608905ed1be586004621af2a252eafd22c384 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 25 Apr 2014 21:02:43 +0200 Subject: Store Item in enum, remove KeyCard --- src/esieequest/model/Game.java | 26 +++++---- src/esieequest/model/doors/LockedDoor.java | 16 +++--- src/esieequest/model/items/Beamer.java | 2 +- src/esieequest/model/items/Item.java | 51 +++++++---------- src/esieequest/model/items/KeyCard.java | 20 ------- src/esieequest/model/items/SimpleItem.java | 86 +++++++++++++++++++++++++++++ src/esieequest/view/Viewable.java | 4 +- src/esieequest/view/app/UserInterface.java | 4 +- src/esieequest/view/text/TextInterface.java | 4 +- src/esieequest/view/web/WebInterface.java | 4 +- 10 files changed, 138 insertions(+), 79 deletions(-) delete mode 100644 src/esieequest/model/items/KeyCard.java create mode 100644 src/esieequest/model/items/SimpleItem.java (limited to 'src') diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 830b48c..6267d1a 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -7,9 +7,7 @@ import esieequest.model.doors.HiddenDoor; import esieequest.model.doors.LockedDoor; import esieequest.model.doors.TransporterDoor; import esieequest.model.doors.TrapDoor; -import esieequest.model.items.Beamer; import esieequest.model.items.Item; -import esieequest.model.items.KeyCard; import esieequest.model.map.Direction; import esieequest.model.map.Room; @@ -86,8 +84,15 @@ public class Game { this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.SOUTH, Room.SECRET_LAB); this.d(Room.SECRET_CORRIDOR_ENTRANCE, Direction.EAST, Room.SECRET_CORRIDOR_END); this.d(Room.SECRET_CORRIDOR_END, Direction.NORTH, new TrapDoor(Room.DEAD_END)); - this.d(Room.SECRET_CORRIDOR_END, Direction.SOUTH, new LockedDoor(Room.LOCKED_ROOM)); - this.d(Room.LOCKED_ROOM, Direction.NORTH, new LockedDoor(Room.SECRET_CORRIDOR_END)); + + final LockedDoor lockedDoorEnter = new LockedDoor(Room.LOCKED_ROOM); + lockedDoorEnter.setKey(Item.KEYCARD); + this.d(Room.SECRET_CORRIDOR_END, Direction.SOUTH, lockedDoorEnter); + + final LockedDoor lockedDoorExit = new LockedDoor(Room.SECRET_CORRIDOR_END); + lockedDoorExit.setKey(Item.KEYCARD); + this.d(Room.LOCKED_ROOM, Direction.NORTH, lockedDoorExit); + this.d(Room.SECRET_CORRIDOR_END, Direction.EAST, new TransporterDoor(Room.values())); } @@ -124,16 +129,13 @@ public class Game { * Adds Item-s in the map. */ private void addItems() { - this.i(Room.STORAGE_ROOM, Direction.WEST, new Item("Weighted Storage Cube", 5, true)); - this.i(Room.STORAGE_ROOM, Direction.EAST, new Item("Edgeless Safety Cube", 5, true)); - this.i(Room.STORAGE_ROOM, Direction.NORTH, new Item("Portable black-hole", -10, false)); + this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE); + this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE); + this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE); - this.i(Room.SECRET_LAB, Direction.SOUTH, new Beamer("Beamer")); + this.i(Room.SECRET_LAB, Direction.SOUTH, Item.BEAMER); - KeyCard keyCard = new KeyCard("KeyCard"); - this.i(Room.DEAD_END, Direction.NORTH, keyCard); - ((LockedDoor) Room.SECRET_CORRIDOR_END.getSide(Direction.SOUTH).getDoor()).setKey(keyCard); - ((LockedDoor) Room.LOCKED_ROOM.getSide(Direction.NORTH).getDoor()).setKey(keyCard); + this.i(Room.DEAD_END, Direction.NORTH, Item.KEYCARD); } /** diff --git a/src/esieequest/model/doors/LockedDoor.java b/src/esieequest/model/doors/LockedDoor.java index 28c535e..49f61be 100644 --- a/src/esieequest/model/doors/LockedDoor.java +++ b/src/esieequest/model/doors/LockedDoor.java @@ -3,7 +3,7 @@ package esieequest.model.doors; import esieequest.model.Game; import esieequest.model.Player; import esieequest.model.Text; -import esieequest.model.items.KeyCard; +import esieequest.model.items.Item; import esieequest.model.map.Room; import esieequest.view.Viewable; @@ -15,7 +15,7 @@ import esieequest.view.Viewable; */ public class LockedDoor extends Door { - private KeyCard keyCard; + private Item key; /** * Creates a new locked door. @@ -32,18 +32,18 @@ public class LockedDoor extends Door { * * @return the KeyCard */ - public KeyCard getKey() { - return this.keyCard; + public Item getKey() { + return this.key; } /** * Sets the KeyCard authorised to open this door. * - * @param keyCard + * @param key * the KeyCard to set */ - public void setKey(final KeyCard keyCard) { - this.keyCard = keyCard; + public void setKey(final Item key) { + this.key = key; } @Override @@ -65,7 +65,7 @@ public class LockedDoor extends Door { * @return if the Player is authorised to pass */ private boolean isAuthorised(final Player player) { - return player.hasItem(this.keyCard); + return player.hasItem(this.key); } } diff --git a/src/esieequest/model/items/Beamer.java b/src/esieequest/model/items/Beamer.java index 61bef94..3a4ade9 100644 --- a/src/esieequest/model/items/Beamer.java +++ b/src/esieequest/model/items/Beamer.java @@ -12,7 +12,7 @@ import esieequest.view.Viewable; * * @author Pacien TRAN-GIRARD */ -public class Beamer extends Item { +public class Beamer extends SimpleItem { private Room room; diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java index c6f872f..2a0aa2c 100644 --- a/src/esieequest/model/items/Item.java +++ b/src/esieequest/model/items/Item.java @@ -1,37 +1,28 @@ package esieequest.model.items; import esieequest.model.Game; -import esieequest.model.Text; import esieequest.view.Viewable; -/** - * Represents an item with a description and a weight, stored in a room. Can be - * picked, used and dropped. - * - * @author Pacien TRAN-GIRARD - */ -public class Item { +public enum Item { - private final String name; - private final int weight; - private final boolean droppable; + // @formatter:off + + STORAGE_CUBE(new SimpleItem("Weighted Storage Cube", 5, true)), + SAFETY_CUBE(new SimpleItem("Edgeless Safety Cube", 5, true)), + BLACK_HOLE(new SimpleItem("Portable black-hole", -10, false)), + + KEYCARD(new SimpleItem("KeyCard", 0, false)), - /** - * Creates an item with the given characteristics. - * - * @param name - * the description of the item - * @param weight - * the weight of the item - */ - public Item(final String name, final int weight, final boolean droppable) { - this.name = name; - this.weight = weight; - this.droppable = droppable; - } + BEAMER(new Beamer("Beamer")), + + ; + + // @formatter:on + + private final SimpleItem item; - public Item(final String description, final boolean droppable) { - this(description, 0, droppable); + Item(final SimpleItem item) { + this.item = item; } /** @@ -40,7 +31,7 @@ public class Item { * @return the description */ public String getName() { - return this.name; + return this.item.getName(); } /** @@ -49,7 +40,7 @@ public class Item { * @return the weight */ public int getWeight() { - return this.weight; + return this.item.getWeight(); } /** @@ -58,7 +49,7 @@ public class Item { * @return the droppability of the item. */ public boolean isDroppable() { - return this.droppable; + return this.item.isDroppable(); } /** @@ -70,7 +61,7 @@ public class Item { * the View */ public void use(final Game game, final Viewable view) { - view.echo(Text.NO_USE.getText()); + this.item.use(game, view); } } diff --git a/src/esieequest/model/items/KeyCard.java b/src/esieequest/model/items/KeyCard.java deleted file mode 100644 index dee3a08..0000000 --- a/src/esieequest/model/items/KeyCard.java +++ /dev/null @@ -1,20 +0,0 @@ -package esieequest.model.items; - -/** - * A key-card that unlocks a Door. - * - * @author Pacien TRAN-GIRARD - */ -public class KeyCard extends Item { - - /** - * Creates a new KeyCard - * - * @param description - * the description of the KeyCard - */ - public KeyCard(final String description) { - super(description, false); - } - -} diff --git a/src/esieequest/model/items/SimpleItem.java b/src/esieequest/model/items/SimpleItem.java new file mode 100644 index 0000000..59de4d3 --- /dev/null +++ b/src/esieequest/model/items/SimpleItem.java @@ -0,0 +1,86 @@ +package esieequest.model.items; + +import esieequest.model.Game; +import esieequest.model.Text; +import esieequest.view.Viewable; + +/** + * Represents an item with a description and a weight, stored in a room. Can be + * picked, used and dropped. + * + * @author Pacien TRAN-GIRARD + */ +public class SimpleItem { + + private final String name; + private final int weight; + private final boolean droppable; + + /** + * Creates an item with the given characteristics. + * + * @param name + * the name of the item + * @param weight + * the weight of the item + * @param droppable + * if the item is droppable + */ + public SimpleItem(final String name, final int weight, final boolean droppable) { + this.name = name; + this.weight = weight; + this.droppable = droppable; + } + + /** + * Creates an item with no weight. + * + * @param name + * the name of the item + * @param droppable + * if the item is droppable + */ + public SimpleItem(final String name, final boolean droppable) { + this(name, 0, droppable); + } + + /** + * Returns the description of the item. + * + * @return the description + */ + public String getName() { + return this.name; + } + + /** + * Returns the weight of the item. + * + * @return the weight + */ + public int getWeight() { + return this.weight; + } + + /** + * Tells whether the item is droppable. + * + * @return the droppability of the item. + */ + public boolean isDroppable() { + return this.droppable; + } + + /** + * Performs actions when the player uses the Item. + * + * @param game + * the Game model + * @param view + * the View + */ + public void use(final Game game, final Viewable view) { + view.echo(Text.NO_USE.getText()); + } + +} diff --git a/src/esieequest/view/Viewable.java b/src/esieequest/view/Viewable.java index 0d1c9e9..6fc8592 100644 --- a/src/esieequest/view/Viewable.java +++ b/src/esieequest/view/Viewable.java @@ -4,7 +4,7 @@ import java.util.HashMap; import esieequest.controller.GameEngine; import esieequest.model.events.Quest; -import esieequest.model.items.Item; +import esieequest.model.items.SimpleItem; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -76,6 +76,6 @@ public interface Viewable { * @param items * the items */ - public void updateInventory(HashMap items); + public void updateInventory(HashMap items); } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index cee4353..ba6d786 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -20,7 +20,7 @@ import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.events.Quest; -import esieequest.model.items.Item; +import esieequest.model.items.SimpleItem; import esieequest.model.map.Orientation; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -330,7 +330,7 @@ abstract class UserInterface implements Viewable, ActionListener { } @Override - public void updateInventory(final HashMap items) { + public void updateInventory(final HashMap items) { // TODO Auto-generated method stub } diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 98e9a06..c94eb1a 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -5,7 +5,7 @@ import java.util.HashMap; import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.events.Quest; -import esieequest.model.items.Item; +import esieequest.model.items.SimpleItem; import esieequest.model.map.Room; import esieequest.model.map.Side; import esieequest.view.Viewable; @@ -97,7 +97,7 @@ abstract class TextInterface implements Viewable { } @Override - public void updateInventory(final HashMap items) { + public void updateInventory(final HashMap items) { // TODO Auto-generated method stub } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 2e2815d..baa01e9 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -25,7 +25,7 @@ import com.google.gwt.user.client.ui.Widget; import esieequest.controller.GameEngine; import esieequest.controller.commands.Command; import esieequest.model.events.Quest; -import esieequest.model.items.Item; +import esieequest.model.items.SimpleItem; import esieequest.model.map.Orientation; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -306,7 +306,7 @@ class WebInterface extends Composite implements Viewable { } @Override - public void updateInventory(final HashMap items) { + public void updateInventory(final HashMap items) { // TODO Auto-generated method stub } -- cgit v1.2.3