From 6e388ec789ba5897cc65a32cfac070c79c042eae Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 15 Apr 2014 20:08:49 +0200 Subject: Implement TrapDoor --- src/esieequest/Main.java | 4 +-- src/esieequest/controller/Performer.java | 18 ++++++++----- src/esieequest/model/Game.java | 43 ++++++++++++++----------------- src/esieequest/model/entities/Player.java | 15 ++++++++--- src/esieequest/model/map/Door.java | 20 ++++++++++++++ src/esieequest/model/map/Room.java | 8 +++--- src/esieequest/model/map/TrapDoor.java | 15 +++++++++++ 7 files changed, 84 insertions(+), 39 deletions(-) create mode 100644 src/esieequest/model/map/Door.java create mode 100644 src/esieequest/model/map/TrapDoor.java diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java index e8cff08..362af9c 100755 --- a/src/esieequest/Main.java +++ b/src/esieequest/Main.java @@ -62,10 +62,10 @@ public class Main extends JApplet { view = new Window(); } - if(arguments.contains("--challenge")) { + if (arguments.contains("--challenge")) { game.getPlayer().setMaxNbSteps(50); } - + new GameEngine(game, view); } diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 63cf75b..b5c32ba 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -6,7 +6,9 @@ import java.util.Set; import esieequest.model.Game; import esieequest.model.commands.CommandWord; import esieequest.model.items.Inventory; +import esieequest.model.map.Door; import esieequest.model.map.Room; +import esieequest.model.map.TrapDoor; import esieequest.view.View; /** @@ -106,13 +108,17 @@ class Performer { * the direction of the new room */ public void goTo(final String direction) { - final Room nextRoom = this.game.getPlayer().getCurrentRoom().getExit(direction); - if (nextRoom != null) { - this.game.getPlayer().goToRoom(nextRoom); - this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); - this.walk(); - } else { + final Door door = this.game.getPlayer().getCurrentRoom().getExit(direction); + if (door == null) { this.echo(this.game.getNoExitMessage()); + return; + } + final Room nextRoom = door.getDestination(); + this.game.getPlayer().goToRoom(nextRoom); + this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); + this.walk(); + if (door.getClass() == TrapDoor.class) { + this.game.getPlayer().clearRoomHistory(); } } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index cc6157d..83fa143 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -4,7 +4,9 @@ import java.util.HashMap; import esieequest.model.entities.Player; import esieequest.model.items.Item; +import esieequest.model.map.Door; import esieequest.model.map.Room; +import esieequest.model.map.TrapDoor; /** * Represents the game. @@ -53,20 +55,6 @@ public class Game { this.rooms.put(name, new Room(description)); } - /** - * Sets the exit room of a given room designated by its name. - * - * @param roomName - * the name of the room - * @param direction - * the direction of the exit - * @param exitRoomName - * the name of the exit room - */ - private void setRoomExit(final String roomName, final String direction, final String exitRoomName) { - this.rooms.get(roomName).addExit(direction, this.rooms.get(exitRoomName)); - } - /** * Creates all the rooms. */ @@ -101,15 +89,28 @@ public class Game { this.createRoom("OffscriptItems", "somewhere implementing weight"); this.createRoom("OffscriptItemsStorageroom", "in a storage room"); - this.createRoom("OffscriptTime", "somewhere implementing time"); - this.createRoom("OffscriptTimeCountdownroom", "in a dangerous room"); this.createRoom("OffscriptTeleportation", "somewhere implementing teleportation"); this.createRoom("OffscriptTeleportationAnchorroom", "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"); this.createRoom("OffscriptMovingcharacterSumobotroom", "in the Chirac-101's room"); + } + /** + * Sets the exit room of a given room designated by its name. + * + * @param roomName + * the name of the room + * @param direction + * the direction of the exit + * @param exitRoomName + * the name of the exit room + */ + private void setRoomExit(final String roomName, final String direction, final String exitRoomName) { + final Room room = this.rooms.get(exitRoomName); + final Door door = new Door(room); + this.rooms.get(roomName).addExit(direction, door); } /** @@ -153,7 +154,6 @@ public class Game { this.setRoomExit("WingStreet", "east", "ClubnixStreet"); this.setRoomExit("WingCorridorOne", "west", "WingStairsOne"); this.setRoomExit("WingCorridorOne", "south", "WingStreet"); - this.setRoomExit("WingCorridorOne", "east", "OffscriptItems"); this.setRoomExit("WingStairsOne", "south", "WingStairsTwo"); this.setRoomExit("WingStairsOne", "up", "WingStairsTwo"); this.setRoomExit("WingStairsOne", "east", "WingCorridorOne"); @@ -166,14 +166,11 @@ 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.setRoomExit("OffscriptItems", "north", "OffscriptItemsStorageroom"); - this.setRoomExit("OffscriptItems", "west", "WingCorridorOne"); - this.setRoomExit("OffscriptItems", "east", "OffscriptTime"); + this.setRoomExit("OffscriptItems", "east", "OffscriptTeleportation"); this.setRoomExit("OffscriptItemsStorageroom", "south", "OffscriptItems"); - this.setRoomExit("OffscriptTime", "north", "OffscriptTimeCountdownroom"); - this.setRoomExit("OffscriptTime", "west", "OffscriptTakeStorageroom"); - this.setRoomExit("OffscriptTime", "east", "OffscriptTeleportation"); - this.setRoomExit("OffscriptTimeCountdownroom", "south", "OffscriptTime"); this.setRoomExit("OffscriptTeleportation", "north", "OffscriptTeleportationAnchorroom"); this.setRoomExit("OffscriptTeleportation", "west", "OffscriptTime"); this.setRoomExit("OffscriptTeleportation", "east", "OffscriptAlea"); diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java index aaa6651..11a893f 100644 --- a/src/esieequest/model/entities/Player.java +++ b/src/esieequest/model/entities/Player.java @@ -61,6 +61,13 @@ public class Player { } } + /** + * Clears the previous rooms history. + */ + public void clearRoomHistory() { + this.previousRooms.clear(); + } + /** * Gets the player's inventory. * @@ -81,14 +88,14 @@ public class Player { * @return the nbSteps */ public int getNbSteps() { - return nbSteps; + return this.nbSteps; } /** * @param nbSteps * the nbSteps to set */ - public void setNbSteps(int nbSteps) { + public void setNbSteps(final int nbSteps) { this.nbSteps = nbSteps; } @@ -96,14 +103,14 @@ public class Player { * @return the maxNbSteps */ public int getMaxNbSteps() { - return maxNbSteps; + return this.maxNbSteps; } /** * @param maxNbSteps * the maxNbSteps to set */ - public void setMaxNbSteps(int maxNbSteps) { + public void setMaxNbSteps(final int maxNbSteps) { this.maxNbSteps = maxNbSteps; } diff --git a/src/esieequest/model/map/Door.java b/src/esieequest/model/map/Door.java new file mode 100644 index 0000000..303ea45 --- /dev/null +++ b/src/esieequest/model/map/Door.java @@ -0,0 +1,20 @@ +package esieequest.model.map; + +/** + * A door that links two rooms. + * + * @author Pacien TRAN-GIRARD + */ +public class Door { + + private final Room destination; + + public Door(final Room destination) { + this.destination = destination; + } + + public Room getDestination() { + return this.destination; + } + +} diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index fc20cc5..b39413d 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -18,7 +18,7 @@ public class Room { private final String description; private final String imageName; - private final HashMap exits; + private final HashMap exits; private final Inventory items; /** @@ -32,7 +32,7 @@ public class Room { public Room(final String description, final String imageName) { this.description = description; this.imageName = imageName; - this.exits = new HashMap(); + this.exits = new HashMap(); this.items = new Inventory(); } @@ -74,7 +74,7 @@ public class Room { * * @return the exit room */ - public Room getExit(final String direction) { + public Door getExit(final String direction) { return this.exits.get(direction); } @@ -95,7 +95,7 @@ public class Room { * @param neighbor * the room in the given direction */ - public void addExit(final String direction, final Room neighbor) { + public void addExit(final String direction, final Door neighbor) { this.exits.put(direction, neighbor); } diff --git a/src/esieequest/model/map/TrapDoor.java b/src/esieequest/model/map/TrapDoor.java new file mode 100644 index 0000000..b86e8b5 --- /dev/null +++ b/src/esieequest/model/map/TrapDoor.java @@ -0,0 +1,15 @@ +package esieequest.model.map; + +/** + * A one-way door that links two rooms and prevent the usage of the 'back' + * command. + * + * @author Pacien TRAN-GIRARD + */ +public class TrapDoor extends Door { + + public TrapDoor(final Room destination) { + super(destination); + } + +} -- cgit v1.2.3