From 3e7f684254b88c55ffe46c6d70b7db4265e9852b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 17 Apr 2014 00:51:39 +0200 Subject: Implement Character and MovingCharacter --- src/esieequest/controller/Interpreter.java | 6 +++ src/esieequest/controller/Performer.java | 50 ++++++++++++++++++++++ src/esieequest/model/Game.java | 6 +++ src/esieequest/model/entities/Character.java | 25 +++++++++++ src/esieequest/model/entities/MovingCharacter.java | 33 ++++++++++++++ src/esieequest/model/entities/Sumobot.java | 32 ++++++++++++++ src/esieequest/model/map/Room.java | 27 +++++++++++- src/esieequest/model/map/TransporterDoor.java | 5 ++- 8 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/esieequest/model/entities/Character.java create mode 100644 src/esieequest/model/entities/MovingCharacter.java create mode 100644 src/esieequest/model/entities/Sumobot.java diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 5f9dafa..1895099 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -96,6 +96,12 @@ class Interpreter { case USE: this.performer.useItem(command.getOption()); return; + + // characters related + case TALK: + this.performer.talk(command.getOption()); + return; + } } this.performer.echo("Unknown command."); diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 17d2cd8..6526ef7 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -1,10 +1,15 @@ package esieequest.controller; +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; +import java.util.Random; import java.util.Set; import esieequest.model.Game; import esieequest.model.commands.CommandWord; +import esieequest.model.entities.Character; +import esieequest.model.entities.MovingCharacter; import esieequest.model.items.Beamer; import esieequest.model.items.Inventory; import esieequest.model.items.Item; @@ -156,6 +161,46 @@ class Performer { this.echo("You died of exhaustion..."); this.quitGame(); } + + this.moveMovingCharacters(); + } + + /** + * Moves all MovingCharacters that can to adjacent rooms. + */ + private void moveMovingCharacters() { + final HashMap movingCharactersPositions = new HashMap(); + final Collection rooms = this.game.getRooms().values(); + for (final Room room : rooms) { + if (room.getCharacter() == null) { + continue; + } + if (room.getCharacter() instanceof MovingCharacter) { + if (!((MovingCharacter) room.getCharacter()).canMove()) { + continue; + } + final int randomIndex = new Random().nextInt(room.getExits().values().size()); + final Room destinationRoom = ((Door) room.getExits().values().toArray()[randomIndex]).getDestination(); + movingCharactersPositions.put(room, destinationRoom); + } + } + + for (final Room source : movingCharactersPositions.keySet()) { + this.swapCharacters(source, movingCharactersPositions.get(source)); + } + } + + /** + * Swaps the characters in the given rooms. + * + * @param source + * @param destination + */ + private void swapCharacters(final Room source, final Room destination) { + final Character characterS = source.getCharacter(); + final Character characterD = destination.getCharacter(); + destination.setCharacter(characterS); + source.setCharacter(characterD); } /** @@ -267,4 +312,9 @@ class Performer { } } } + + public void talk(final String message) { + this.echo(this.game.getPlayer().getCurrentRoom().getCharacter().talk()); + } + } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 17fad02..87d3d51 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.HashSet; import esieequest.model.entities.Player; +import esieequest.model.entities.Sumobot; import esieequest.model.items.Beamer; import esieequest.model.items.Item; import esieequest.model.items.KeyCard; @@ -37,6 +38,7 @@ public class Game { this.createRooms(); this.linkRooms(); this.createItems(); + this.createCharacters(); this.goToRoom("AmphitheaterSeat"); } @@ -204,6 +206,10 @@ public class Game { this.rooms.get("Dead-end").getItems().putItem("KeyCard", keyCard); } + private void createCharacters() { + this.rooms.get("Locked room").setCharacter(new Sumobot()); + } + /** * Sets the current room designated by its name. * diff --git a/src/esieequest/model/entities/Character.java b/src/esieequest/model/entities/Character.java new file mode 100644 index 0000000..03b1cfa --- /dev/null +++ b/src/esieequest/model/entities/Character.java @@ -0,0 +1,25 @@ +package esieequest.model.entities; + +/** + * A character that can talk with the player. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class Character { + + private final String name; + + public Character(final String name) { + this.name = name; + } + + public abstract String talk(); + + /** + * @return the name + */ + public String getName() { + return this.name; + } + +} diff --git a/src/esieequest/model/entities/MovingCharacter.java b/src/esieequest/model/entities/MovingCharacter.java new file mode 100644 index 0000000..5265a28 --- /dev/null +++ b/src/esieequest/model/entities/MovingCharacter.java @@ -0,0 +1,33 @@ +package esieequest.model.entities; + +/** + * A character that can move from rooms to rooms. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class MovingCharacter extends Character { + + private boolean canMove; + + public MovingCharacter(final String name) { + super(name); + + this.canMove = false; + } + + /** + * @return the canMove + */ + public boolean canMove() { + return this.canMove; + } + + /** + * @param canMove + * the canMove to set + */ + public void setCanMove(final boolean canMove) { + this.canMove = canMove; + } + +} diff --git a/src/esieequest/model/entities/Sumobot.java b/src/esieequest/model/entities/Sumobot.java new file mode 100644 index 0000000..a700a71 --- /dev/null +++ b/src/esieequest/model/entities/Sumobot.java @@ -0,0 +1,32 @@ +package esieequest.model.entities; + +import java.util.HashSet; +import java.util.Random; + +public class Sumobot extends MovingCharacter { + + private final Random randomGenerator; + private final HashSet messages; + + public Sumobot() { + super("Sumobot DK-02"); + + this.randomGenerator = new Random(); + + this.messages = new HashSet(); + // exterminate ! + this.messages.add("絶やす !"); + // you will be deleted + this.messages.add("あなたが削除されます。"); + // your death will now be implemented + this.messages.add("あなたの死が実装されます。"); + } + + @Override + public String talk() { + this.setCanMove(true); + final int randomIndex = this.randomGenerator.nextInt(this.messages.size()); + return (String) this.messages.toArray()[randomIndex]; + } + +} diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index 3e606d7..294c63b 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.HashSet; import esieequest.controller.Utils; +import esieequest.model.entities.Character; import esieequest.model.items.Inventory; /** @@ -21,6 +22,7 @@ public class Room { private final String imageName; private final HashMap exits; private final Inventory items; + private Character character; /** * Creates a room with a description and an associated image. @@ -35,6 +37,7 @@ public class Room { this.imageName = imageName; this.exits = new HashMap(); this.items = new Inventory(); + this.character = null; } public Room(final String description) { @@ -57,7 +60,7 @@ public class Room { * @return the informations about the room, including exits and items */ public String getInformations() { - return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.items.listItems(); + return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.items.listItems() + "\n" + this.listCharacters(); } /** @@ -122,4 +125,26 @@ public class Room { return this.exits; } + private String listCharacters() { + if (this.character == null) { + return "There is no one in this room."; + } + return this.character.getName() + " is here."; + } + + /** + * @return the character + */ + public Character getCharacter() { + return this.character; + } + + /** + * @param character + * the character to set + */ + public void setCharacter(final Character character) { + this.character = character; + } + } diff --git a/src/esieequest/model/map/TransporterDoor.java b/src/esieequest/model/map/TransporterDoor.java index 47fb6c2..9dd5acc 100644 --- a/src/esieequest/model/map/TransporterDoor.java +++ b/src/esieequest/model/map/TransporterDoor.java @@ -10,10 +10,13 @@ import java.util.Set; */ public class TransporterDoor extends Door { + private final Random randomGenerator; private final Set possibleDestinations; public TransporterDoor(final Set possibleDestinations) { super(null); + + this.randomGenerator = new Random(); this.possibleDestinations = possibleDestinations; } @@ -26,7 +29,7 @@ public class TransporterDoor extends Door { } private Room findRandomRoom() { - final int randomIndex = new Random().nextInt(this.possibleDestinations.size()); + final int randomIndex = this.randomGenerator.nextInt(this.possibleDestinations.size()); return (Room) this.possibleDestinations.toArray()[randomIndex]; } -- cgit v1.2.3