aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/engine/GameEngine.java
blob: 9211eb0e6690c75a184a17e1b0cad1c892bc32c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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. You losed.");
		}

	}

}