package esieequest.engine; import esieequest.engine.commands.Command; import esieequest.engine.scheduler.Callback; import esieequest.game.Game; import esieequest.game.Text; import esieequest.game.characters.MovingCharacter; import esieequest.game.items.Beamer; import esieequest.game.items.Item; import esieequest.game.map.Room; import esieequest.game.states.Scene; import esieequest.ui.View; /** * The game main controller class. * * @author Pacien TRAN-GIRARD * @author BenoƮt LUBRANO DI SBARAGLIONE */ public class GameEngine { private final Game game; private final View view; /** * Instantiates a game engine with the given model and view. * * @param game * the game model * @param view * the view */ public GameEngine(final View view, final Game game) { this.game = game; this.view = view; this.view.setGameEngine(this); this.view.show(); } /** * Instantiates a game with the given view and mode. * * @param view * the view * @param challengeMode * the mode */ public GameEngine(final View view, final boolean challengeMode) { this(view, new Game(challengeMode)); } /** * Instantiates a game with the given view. * * @param view * the view */ public GameEngine(final View view) { this(view, false); } /** * Interprets a command. * * @param inputString * the input String */ public void interpret(final String inputString) { final Input input = new Input(inputString/* .toLowerCase() */); final Command command = input.getCommand(); if (command == null) { this.view.echo(Text.UNKNOWN_COMMAND.toString()); return; } final String argument = input.getArgument(); command.execute(argument, this.game, this.view); this.executeRoutines(); } /** * Performs routine actions executed every time a Command is entered. */ private void executeRoutines() { this.checkStalemate(); MovingCharacter.moveAll(this.game); } 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, new Callback() { @Override public void call() { return; } }); this.view.echo("You are trapped. Game Over."); } } }