aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/controller/commands/GoCommand.java
blob: 4bcd0e1c1ced8b9ee540c78e0decf35d91aadc01 (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
package esieequest.controller.commands;

import esieequest.model.Game;
import esieequest.model.Text;
import esieequest.model.doors.Door;
import esieequest.model.map.Direction;
import esieequest.view.Viewable;

/**
 * Allows the user to move from connected Room-s to Room-s by giving the
 * Direction.
 * 
 * @author Pacien TRAN-GIRARD
 */
public class GoCommand implements Executable {

	@Override
	public void execute(final String argument, final Game game, final Viewable view) {

		Direction direction;

		if (!argument.isEmpty()) {

			try {
				direction = Direction.valueOf(argument.toUpperCase());
			} catch (final IllegalArgumentException e) {
				view.echo(Text.NO_SUCH_DIRECTION.toString());
				return;
			}

		} else {

			direction = game.getPlayer().getCurrentDirection();

		}

		if (!game.getPlayer().getCurrentRoom().getSide(direction).hasDoor()) {
			view.echo(Text.NO_DOOR.toString());
			return;
		}

		final Door door = game.getPlayer().getCurrentRoom().getSide(direction).getDoor();

		final boolean crossed = door.cross(game, view);

		if (!crossed) {
			return;
		}

		view.updateLocation(game.getPlayer().getCurrentRoom(), direction, game.getPlayer()
				.getCurrentSide(), game.getPlayer().canGoBack());

		// handle challenge mode
		if (game.getPlayer().walk()) {
			view.echo(Text.CHALLENGE_FAILED.toString());
			view.disableInput();
		}

	}

}