aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/controller/Performer.java
blob: eb1cf9a2e333a197a1f722dc58b8636c40e80bfb (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package esieequest.controller;

import java.util.HashSet;
import java.util.Set;

import esieequest.model.Game;
import esieequest.model.commands.CommandWord;
import esieequest.model.items.Beamer;
import esieequest.model.items.Inventory;
import esieequest.model.items.Item;
import esieequest.model.map.Door;
import esieequest.model.map.LockedDoor;
import esieequest.model.map.Room;
import esieequest.model.map.TrapDoor;
import esieequest.view.View;

/**
 * Performs actions and modifies the Game model.
 * 
 * @author Pacien TRAN-GIRARD
 * @author Benoît LUBRANO DI SBARAGLIONE
 */
class Performer {

	private final Game game;
	private final View view;

	/**
	 * Instantiates a Performer.
	 * 
	 * @param game
	 *            the Game model
	 * @param view
	 *            the view
	 */
	public Performer(final Game game, final View view) {
		this.game = game;
		this.view = view;
	}

	/**
	 * Displays the message "Not implemented." in the view.
	 */
	private void notImplemented() {
		this.echo("Not implemented.");
	}

	/**
	 * Displays a message in the view.
	 * 
	 * @param message
	 *            the message
	 */
	public void echo(final String message) {
		this.view.echo(message);
	}

	/**
	 * Loads a new game.
	 */
	public void newGame() {
		// this.loadGame(default game);
		// TODO
		this.view.enable();
		this.echo(this.game.getWelcomeMessage());
	}

	/**
	 * Loads a saved game.
	 */
	public void loadGame() {
		// TODO
		this.notImplemented();
	}

	/**
	 * Saves the current game.
	 */
	public void saveGame() {
		// TODO
		this.notImplemented();
	}

	/**
	 * Toggles the sound.
	 */
	public void toggleSound() {
		// TODO
		this.notImplemented();
	}

	/**
	 * Ends the current game.
	 */
	public void quitGame() {
		this.echo(this.game.getQuitMessage());
		this.view.disable();
	}

	/**
	 * Displays the help message and the available commands.
	 */
	public void showHelp() {
		final Set<String> commands = new HashSet<String>();
		for (final CommandWord command : CommandWord.values()) {
			commands.add(command.name().toLowerCase());
		}
		this.echo(Utils.list(commands, "Commands:", "No commands."));
	}

	/**
	 * Changes the current room to the one placed in the given direction.
	 * 
	 * @param direction
	 *            the direction of the new room
	 */
	public void goTo(final String direction) {
		final Door door = this.game.getPlayer().getCurrentRoom().getExit(direction);
		if (door == null) {
			this.echo(this.game.getNoExitMessage());
			return;
		}
		if (door.getClass() == LockedDoor.class) {
			final LockedDoor lockedDoor = (LockedDoor) door;
			final boolean authorised = this.game.getPlayer().getInventory().containsItem(lockedDoor.getKey());
			if (!authorised) {
				this.echo("This door is locked.");
				return;
			}
		}
		final Room nextRoom = door.getDestination();
		this.game.getPlayer().goToRoom(nextRoom);
		this.view.updateRoom(this.game.getPlayer().getCurrentRoom());
		this.walk();
		if (door.getClass() == TrapDoor.class) {
			this.game.getPlayer().clearRoomHistory();
		}
	}

	/**
	 * Changes the current room to the previous one.
	 */
	public void goBack() {
		this.game.getPlayer().goToPreviousRoom();
		this.view.updateRoom(this.game.getPlayer().getCurrentRoom());
		this.walk();
	}

	/**
	 * Increments and checks the number of steps made by the player.
	 */
	private void walk() {
		this.game.getPlayer().setNbSteps(this.game.getPlayer().getNbSteps() + 1);
		if (this.game.getPlayer().getNbSteps() == this.game.getPlayer().getMaxNbSteps()) {
			this.echo("You died of exhaustion...");
			this.quitGame();
		}
	}

	/**
	 * Displays informations about the current place.
	 */
	public void look() {
		this.echo(this.game.getPlayer().getCurrentRoom().getInformations());
	}

	/**
	 * Moves an item from the current Room to the Player's inventory.
	 * 
	 * @param itemName
	 *            the item's name
	 */
	public void takeItem(final String itemName) {
		if (!this.game.getPlayer().getCurrentRoom().getItems().containsItem(itemName)) {
			this.echo("No such item in this room.");
			return;
		}

		int weight = this.game.getPlayer().getInventory().getTotalWeight();
		weight += this.game.getPlayer().getCurrentRoom().getItems().getItemWeight(itemName);
		final int maxWeight = this.game.getPlayer().getMaxCarryWeight();

		if (weight >= maxWeight) {
			this.echo("Maximum inventory weight reached. Cannot pick up item.");
			return;
		}

		this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName);
	}

	/**
	 * Moves an item from the Player's inventory to the current Room.
	 * 
	 * @param itemName
	 *            the item's name
	 */
	public void dropItem(final String itemName) {
		if (!this.game.getPlayer().getInventory().getItem(itemName).isDroppable()) {
			this.echo("This item cannot be dropped.");
			return;
		}
		this.moveItem(this.game.getPlayer().getInventory(), this.game.getPlayer().getCurrentRoom().getItems(), itemName);
	}

	/**
	 * Moves a given item referred by its name from an inventory to another.
	 * 
	 * @param source
	 *            the source inventory
	 * @param dest
	 *            the destination inventory
	 * @param itemName
	 *            the item's name
	 */
	private void moveItem(final Inventory source, final Inventory dest, final String itemName) {
		if (!source.containsItem(itemName)) {
			this.echo("No such item.");
			return;
		}
		dest.putItem(itemName, source.takeItem(itemName));
	}

	/**
	 * Lists the items contained in the player's inventory.
	 */
	public void listItems() {
		this.echo(Utils.list(this.game.getPlayer().getInventory().getItemList(), "Items:", "No item in your inventory."));
	}

	/**
	 * Performs an action according to the use of an item.
	 * 
	 * @param itemName
	 *            the item's name
	 */
	public void useItem(final String itemName) {
		final Item item = this.game.getPlayer().getInventory().getItem(itemName);
		if (item == null) {
			this.echo("You don't have such item.");
			return;
		}
		if (item.getClass() == Beamer.class) {
			final Beamer beamer = (Beamer) item;
			if (beamer.getRoom() == null) {
				beamer.setRoom(this.game.getPlayer().getCurrentRoom());
			} else {
				this.game.getPlayer().goToRoom(beamer.getRoom());
			}
		}
	}

}