From 639b8b989036241ad510546a8e433f366559ed54 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 27 May 2014 17:48:57 +0200 Subject: Add two fatal outcomes --- src/esieequest/controller/GameEngine.java | 34 ++++++++++++++++++++-- .../model/characters/MovingCharacter.java | 14 +++++---- src/esieequest/model/characters/Sumobot.java | 33 +++++++++++++-------- src/esieequest/model/events/Scene.java | 7 +++-- src/esieequest/model/items/Beamer.java | 2 ++ src/esieequest/model/items/Item.java | 2 ++ 6 files changed, 69 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java index 8e16ea1..8326607 100644 --- a/src/esieequest/controller/GameEngine.java +++ b/src/esieequest/controller/GameEngine.java @@ -4,6 +4,10 @@ import esieequest.controller.commands.Command; import esieequest.model.Game; import esieequest.model.Text; import esieequest.model.characters.MovingCharacter; +import esieequest.model.events.Scene; +import esieequest.model.items.Beamer; +import esieequest.model.items.Item; +import esieequest.model.map.Room; import esieequest.view.Viewable; /** @@ -73,15 +77,41 @@ public class GameEngine { final String argument = input.getArgument(); - this.executeRoutines(); + this.executePreRoutines(); command.execute(argument, this.game, this.view); + this.executeAfterRoutines(); } /** * Performs routine actions executed every time a Command is entered. */ - private void executeRoutines() { + private void executePreRoutines() { MovingCharacter.moveAll(); } + private void executeAfterRoutines() { + this.checkStalemate(); + } + + private void checkStalemate() { + + final boolean inDeadEnd = this.game.getPlayer().getCurrentRoom() == Room.DEAD_END; + final boolean canGoBack = this.game.getPlayer().canGoBack(); + final boolean hasBeamer = this.game.getPlayer().getInventory().hasItem(Item.BEAMER); + boolean canTeleport = hasBeamer; + + if (hasBeamer) { + final Beamer beamer = (Beamer) Item.BEAMER.getItem(); + canTeleport = (beamer.getRoom() != null) && (beamer.getRoom() != Room.DEAD_END); + } + + final boolean trapped = inDeadEnd && !canGoBack && !canTeleport; + + if (trapped) { + this.view.playScene(Scene.GAME_LOST); + this.view.echo("You are trapped. You losed."); + } + + } + } diff --git a/src/esieequest/model/characters/MovingCharacter.java b/src/esieequest/model/characters/MovingCharacter.java index b12c4ce..94aeaf1 100644 --- a/src/esieequest/model/characters/MovingCharacter.java +++ b/src/esieequest/model/characters/MovingCharacter.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import lombok.Getter; import lombok.Setter; import net.pacien.util.CleanJSONObject; @@ -40,9 +41,10 @@ public abstract class MovingCharacter extends SimpleCharacter { private static final String CURRENT_DIRECTION_LABEL = "D"; private Direction currentDirection; - private static final String CAN_MOVE_LABEL = "M"; + private static final String MOBILE_LABEL = "M"; + @Getter @Setter - private boolean canMove; + private boolean mobile; /** * Creates a MovingCharacter with a given name and location. @@ -60,7 +62,7 @@ public abstract class MovingCharacter extends SimpleCharacter { this.currentRoom = room; this.currentDirection = direction; - this.canMove = false; + this.mobile = false; } /** @@ -69,7 +71,7 @@ public abstract class MovingCharacter extends SimpleCharacter { * Character. */ public void move() { - if (!this.canMove) { + if (!this.mobile) { return; } @@ -106,7 +108,7 @@ public abstract class MovingCharacter extends SimpleCharacter { if (this.currentDirection != null) { o.put(MovingCharacter.CURRENT_DIRECTION_LABEL, this.currentDirection.name()); } - o.put(MovingCharacter.CAN_MOVE_LABEL, this.canMove); + o.put(MovingCharacter.MOBILE_LABEL, this.mobile); return o; } @@ -116,7 +118,7 @@ public abstract class MovingCharacter extends SimpleCharacter { this.currentRoom = Room.valueOf((String) o.get(MovingCharacter.CURRENT_ROOM_LABEL)); this.currentDirection = Direction.valueOf((String) o .get(MovingCharacter.CURRENT_DIRECTION_LABEL)); - this.canMove = (Boolean) o.get(MovingCharacter.CAN_MOVE_LABEL); + this.mobile = (Boolean) o.get(MovingCharacter.MOBILE_LABEL); } } diff --git a/src/esieequest/model/characters/Sumobot.java b/src/esieequest/model/characters/Sumobot.java index fb4e7b3..baeabad 100644 --- a/src/esieequest/model/characters/Sumobot.java +++ b/src/esieequest/model/characters/Sumobot.java @@ -1,9 +1,11 @@ package esieequest.model.characters; -import java.util.HashSet; import java.util.Random; +import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Text; + import esieequest.model.Game; +import esieequest.model.events.Scene; import esieequest.model.map.Direction; import esieequest.model.map.Room; import esieequest.view.Viewable; @@ -15,8 +17,11 @@ import esieequest.view.Viewable; */ public class Sumobot extends MovingCharacter { + private static final String INIT_MESSAGE = "Waking up..."; + private static final String[] MESSAGES = { "絶やす !", "あなたが削除されます。", "あなたの死が実装されます。" }; + private static final String KILLED_MESSAGE = "The Sumobot fatally pushed you. You died."; + private final Random randomGenerator; - private final HashSet messages; /** * Creates a Sumobot with its primary location. @@ -30,21 +35,25 @@ public class Sumobot extends MovingCharacter { super("Sumobot DK-02", room, direction); 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("あなたの死が実装されます。"); + private String getRandomMessage() { + final int randomIndex = this.randomGenerator.nextInt(Sumobot.MESSAGES.length); + return Sumobot.MESSAGES[randomIndex]; } @Override public void talk(final Game game, final Viewable view) { - this.setCanMove(true); - final int randomIndex = this.randomGenerator.nextInt(this.messages.size()); - view.echo((String) this.messages.toArray()[randomIndex]); + + if (!this.isMobile()) { + this.setMobile(true); + view.echo(Sumobot.INIT_MESSAGE); + return; + } + + view.playScene(Scene.GAME_LOST); + view.echo(this.getRandomMessage() + Text.NEW_LINE + Sumobot.KILLED_MESSAGE); + } } diff --git a/src/esieequest/model/events/Scene.java b/src/esieequest/model/events/Scene.java index d67cc0b..6948178 100644 --- a/src/esieequest/model/events/Scene.java +++ b/src/esieequest/model/events/Scene.java @@ -13,9 +13,10 @@ public enum Scene { // @formatter:off - INTRO("THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY!", "...", 90000), - RUN_CONSOLE("Stalemate Resolved.", "...", 13000), - END("Good work getting this far, future-starter!", "...", 95000), + INTRO("THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY!", "Wow! *.*", 90000), + RUN_CONSOLE("Stalemate Resolved.", "Stalemate Resolved.", 13000), + END("Good work getting this far, future-starter!", "Good work getting this far, future-starter!", 95000), + GAME_LOST("Try again...", "Sorry, you lose.", 210000), ; diff --git a/src/esieequest/model/items/Beamer.java b/src/esieequest/model/items/Beamer.java index 87d94da..616ccd5 100644 --- a/src/esieequest/model/items/Beamer.java +++ b/src/esieequest/model/items/Beamer.java @@ -1,5 +1,6 @@ package esieequest.model.items; +import lombok.Getter; import net.pacien.util.CleanJSONObject; import org.json.simple.JSONObject; @@ -19,6 +20,7 @@ import esieequest.view.Viewable; public class Beamer extends SimpleItem { private static final String ROOM_LABEL = "R"; + @Getter private Room room; /** diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java index a592249..ae23c16 100644 --- a/src/esieequest/model/items/Item.java +++ b/src/esieequest/model/items/Item.java @@ -1,5 +1,6 @@ package esieequest.model.items; +import lombok.Getter; import net.pacien.util.Mappable; import org.json.simple.JSONArray; @@ -34,6 +35,7 @@ public enum Item implements Mappable, SerialisableObject { // @formatter:on + @Getter private final SimpleItem item; Item(final SimpleItem item) { -- cgit v1.2.3