From da9bbc02e3d707a947297750df6b7ea7f6b47a21 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 29 May 2014 00:51:39 +0200 Subject: Separate Following and Wandering MovingCharacter-s --- src/esieequest/controller/GameEngine.java | 10 +--- src/esieequest/model/Player.java | 1 + .../model/characters/FollowingCharacter.java | 52 +++++++++++++++++ .../model/characters/MovingCharacter.java | 67 +++++++++++++--------- src/esieequest/model/characters/Sumobot.java | 4 +- .../model/characters/WanderingCharacter.java | 52 +++++++++++++++++ 6 files changed, 150 insertions(+), 36 deletions(-) create mode 100644 src/esieequest/model/characters/FollowingCharacter.java create mode 100644 src/esieequest/model/characters/WanderingCharacter.java diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java index 8326607..ae76059 100644 --- a/src/esieequest/controller/GameEngine.java +++ b/src/esieequest/controller/GameEngine.java @@ -77,20 +77,16 @@ public class GameEngine { final String argument = input.getArgument(); - this.executePreRoutines(); command.execute(argument, this.game, this.view); - this.executeAfterRoutines(); + this.executeRoutines(); } /** * Performs routine actions executed every time a Command is entered. */ - private void executePreRoutines() { - MovingCharacter.moveAll(); - } - - private void executeAfterRoutines() { + private void executeRoutines() { this.checkStalemate(); + MovingCharacter.moveAll(this.game); } private void checkStalemate() { diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index e9c3c7f..c06f5b2 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java @@ -36,6 +36,7 @@ public class Player implements SerialisableObject { private Room currentRoom; private static final String PREVIOUS_ROOMS_LABEL = "H"; + @Getter private final Stack previousRooms; private static final String CURRENT_DIRECTION_LABEL = "D"; diff --git a/src/esieequest/model/characters/FollowingCharacter.java b/src/esieequest/model/characters/FollowingCharacter.java new file mode 100644 index 0000000..ab1a62e --- /dev/null +++ b/src/esieequest/model/characters/FollowingCharacter.java @@ -0,0 +1,52 @@ +package esieequest.model.characters; + +import java.util.Stack; + +import esieequest.model.Game; +import esieequest.model.map.Direction; +import esieequest.model.map.Room; + +/** + * A MovingCharacter that follows the Player. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class FollowingCharacter extends MovingCharacter { + + /** + * Instantiates a FollowingCharacter. + * + * @param name + * the name + * @param room + * the initial Room + * @param direction + * the initial Direction + */ + public FollowingCharacter(final String name, final Room room, final Direction direction) { + super(name, room, direction); + } + + @Override + public void move(final Game game) { + if (!this.isMobile()) { + return; + } + + final Stack playerHistory = game.getPlayer().getPreviousRooms(); + + if (playerHistory.size() < 2) { + return; + } + + final Room previousRoom = playerHistory.elementAt(playerHistory.size() - 1); + final Room previousPreviousRoom = playerHistory.elementAt(playerHistory.size() - 2); + + if (previousPreviousRoom == this.getCurrentRoom()) { + return; + } + + this.changePosition(previousRoom, game.getPlayer().getCurrentDirection().getOpposite()); + } + +} diff --git a/src/esieequest/model/characters/MovingCharacter.java b/src/esieequest/model/characters/MovingCharacter.java index 94aeaf1..2304f94 100644 --- a/src/esieequest/model/characters/MovingCharacter.java +++ b/src/esieequest/model/characters/MovingCharacter.java @@ -2,7 +2,6 @@ package esieequest.model.characters; import java.util.ArrayList; import java.util.List; -import java.util.Random; import lombok.Getter; import lombok.Setter; @@ -10,6 +9,7 @@ import net.pacien.util.CleanJSONObject; import org.json.simple.JSONObject; +import esieequest.model.Game; import esieequest.model.map.Direction; import esieequest.model.map.Room; @@ -29,16 +29,18 @@ public abstract class MovingCharacter extends SimpleCharacter { /** * Moves all the MovingCharacter in the map. */ - public static void moveAll() { + public static void moveAll(final Game game) { for (final MovingCharacter movingCharacter : MovingCharacter.instances) { - movingCharacter.move(); + movingCharacter.move(game); } } private static final String CURRENT_ROOM_LABEL = "C"; + @Getter private Room currentRoom; private static final String CURRENT_DIRECTION_LABEL = "D"; + @Getter private Direction currentDirection; private static final String MOBILE_LABEL = "M"; @@ -66,38 +68,49 @@ public abstract class MovingCharacter extends SimpleCharacter { } /** - * Moves the MovingCharacter to a randomly chosen adjacent Room, if he can - * move and if the destination Room is not currently occupied by another - * Character. + * Changes the position of the MovingCharacter. + * + * @param room + * the new Room + * @param direction + * the new Direction */ - public void move() { - if (!this.mobile) { - return; - } - - final List possibleDirections = this.currentRoom.listExitsDirections(); - - if (possibleDirections.size() < 1) { - return; - } - - final int randomIndex = (new Random()).nextInt(possibleDirections.size()); - final Direction direction = possibleDirections.get(randomIndex); - final Room destination = this.currentRoom.getSide(direction).getDoor().getDestination(); - - if (destination.getSide(direction).hasCharacter()) { + public void changePosition(final Room room, final Direction direction) { + if (room.getSide(direction).hasCharacter()) { return; } this.currentRoom.getSide(this.currentDirection).setCharacter(null); + this.currentRoom = room; + this.currentDirection = direction; + this.currentRoom.getSide(this.currentDirection).setCharacter(Character.getCharacter(this)); + } + + /** + * Changes the Room of the MovingCharacter. + * + * @param room + * the Room + */ + public void changeRoom(final Room room) { + this.changePosition(room, this.currentDirection); + } - final Direction newDirection = direction.getOpposite(); - final Character character = Character.getCharacter(this); - destination.getSide(newDirection).setCharacter(character); - this.currentRoom = destination; - this.currentDirection = newDirection; + /** + * Changes the Direction of the MovingCharacter. + * + * @param direction + * the Direction + */ + public void changeDirection(final Direction direction) { + this.changePosition(this.currentRoom, direction); } + /** + * Makes the character move. + */ + public abstract void move(final Game game); + @Override public JSONObject serialise() { final CleanJSONObject o = new CleanJSONObject(); diff --git a/src/esieequest/model/characters/Sumobot.java b/src/esieequest/model/characters/Sumobot.java index a546e1a..5264ed6 100644 --- a/src/esieequest/model/characters/Sumobot.java +++ b/src/esieequest/model/characters/Sumobot.java @@ -14,7 +14,7 @@ import esieequest.view.Viewable; * * @author Pacien TRAN-GIRARD */ -public class Sumobot extends MovingCharacter { +public class Sumobot extends FollowingCharacter { private static final String INIT_MESSAGE = "Waking up..."; private static final String[] MESSAGES = { "絶やす !", "あなたが削除されます。", "あなたの死が実装されます。" }; @@ -31,7 +31,7 @@ public class Sumobot extends MovingCharacter { * the Direction at which the Sumobot is located */ public Sumobot(final Room room, final Direction direction) { - super("Sumobot DK-02", room, direction); + super("Sumobot", room, direction); this.randomGenerator = new Random(); } diff --git a/src/esieequest/model/characters/WanderingCharacter.java b/src/esieequest/model/characters/WanderingCharacter.java new file mode 100644 index 0000000..1b891e8 --- /dev/null +++ b/src/esieequest/model/characters/WanderingCharacter.java @@ -0,0 +1,52 @@ +package esieequest.model.characters; + +import java.util.List; +import java.util.Random; + +import esieequest.model.Game; +import esieequest.model.map.Direction; +import esieequest.model.map.Room; + +/** + * A MovingCharacter that randomly moves from one Room to another adjacent Room. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class WanderingCharacter extends MovingCharacter { + + /** + * Instantiates a WanderingCharacter. + * + * @param name + * the name + * @param room + * the initial Room + * @param direction + * the initial Direction + */ + public WanderingCharacter(final String name, final Room room, final Direction direction) { + super(name, room, direction); + } + + @Override + public void move(final Game game) { + if (!this.isMobile()) { + return; + } + + final List possibleDirections = this.getCurrentRoom().listExitsDirections(); + + if (possibleDirections.size() < 1) { + return; + } + + final int randomIndex = (new Random()).nextInt(possibleDirections.size()); + final Direction direction = possibleDirections.get(randomIndex); + final Room destination = this.getCurrentRoom().getSide(direction).getDoor() + .getDestination(); + final Direction newDirection = direction.getOpposite(); + + this.changePosition(destination, newDirection); + } + +} -- cgit v1.2.3