aboutsummaryrefslogtreecommitdiff
path: root/src/esieequest/model/Player.java
blob: c06f5b289810b48bdc1d80a130754fa0d74034cd (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
package esieequest.model;

import java.util.Stack;

import lombok.Getter;
import lombok.Setter;
import net.pacien.util.CleanJSONObject;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import esieequest.controller.utils.EnumUtils;
import esieequest.controller.utils.SerialisableObject;
import esieequest.model.events.Quest;
import esieequest.model.items.Inventory;
import esieequest.model.map.Direction;
import esieequest.model.map.Room;
import esieequest.model.map.Side;

/**
 * Represents a Player impersonated by the user.
 * 
 * @author Pacien TRAN-GIRARD
 * @author Benoît LUBRANO DI SBARAGLIONE
 */
public class Player implements SerialisableObject {

	private static final String CURRENT_QUEST_LABEL = "Q";
	@Getter
	@Setter
	private Quest currentQuest;

	private static final String CURRENT_ROOM_LABEL = "R";
	@Getter
	@Setter
	private Room currentRoom;

	private static final String PREVIOUS_ROOMS_LABEL = "H";
	@Getter
	private final Stack<Room> previousRooms;

	private static final String CURRENT_DIRECTION_LABEL = "D";
	@Getter
	@Setter
	private Direction currentDirection;

	private static final String INVENTORY_LABEL = "I";
	@Getter
	private final Inventory inventory;

	private static final String INVENTORY_WEIGHT_LIMIT_LABEL = "W";
	@Getter
	private int inventoryWeightLimit;

	private static final String NB_STEPS_LABEL = "S";
	private int nbSteps;

	private static final String NB_STEPS_LIMIT_LABEL = "L";
	private int nbStepsLimit;

	/**
	 * Creates a Player with a current location (Room and Direction) and limits
	 * (inventory weight and number of steps).
	 * 
	 * @param currentRoom
	 *            the Room
	 * @param currentDirection
	 *            the Direction
	 * @param carryWeightLimit
	 *            the inventory weight limit
	 * @param nbStepsLimit
	 *            the maximum number of steps
	 */
	public Player(final Quest currentQuest, final Room currentRoom,
			final Direction currentDirection, final int carryWeightLimit, final int nbStepsLimit) {
		this.currentQuest = currentQuest;
		this.currentRoom = currentRoom;
		this.previousRooms = new Stack<>();
		this.currentDirection = currentDirection;
		this.inventory = new Inventory();
		this.inventoryWeightLimit = carryWeightLimit;
		this.nbSteps = 0;
		this.nbStepsLimit = nbStepsLimit;
	}

	/**
	 * Sets the current room and stacks previous rooms.
	 * 
	 * @param room
	 *            the destination room
	 */
	public void goToRoom(final Room room) {
		this.previousRooms.push(this.currentRoom);
		this.currentRoom = room;
	}

	/**
	 * Sets the current room to the previous one.
	 */
	public void goToPreviousRoom() {
		if (!this.previousRooms.empty()) {
			this.currentRoom = this.previousRooms.pop();
		}
	}

	/**
	 * Tells if the Player can go back.
	 * 
	 * @return false if the Player's Room history is empty.
	 */
	public boolean canGoBack() {
		return !this.previousRooms.isEmpty();
	}

	/**
	 * Clears the previous rooms history.
	 */
	public void clearRoomHistory() {
		this.previousRooms.clear();
	}

	/**
	 * @return the current Side where the Player is
	 */
	public Side getCurrentSide() {
		return this.currentRoom.getSide(this.currentDirection);
	}

	/**
	 * Increments the step counter.
	 * 
	 * @return if the maximum number of steps has been reached
	 */
	public boolean walk() {
		this.nbSteps++;
		return this.nbSteps == this.nbStepsLimit;
	}

	@Override
	public JSONObject serialise() {
		final CleanJSONObject o = new CleanJSONObject();

		o.put(Player.CURRENT_QUEST_LABEL, this.currentQuest.name());
		o.put(Player.CURRENT_ROOM_LABEL, this.currentRoom.name());
		o.put(Player.PREVIOUS_ROOMS_LABEL, EnumUtils.nameAll(this.previousRooms
				.toArray(new Room[0])));

		o.put(Player.CURRENT_DIRECTION_LABEL, this.currentDirection.name());

		o.put(Player.INVENTORY_LABEL, this.inventory.serialise());
		o.put(Player.INVENTORY_WEIGHT_LIMIT_LABEL, this.inventoryWeightLimit);

		o.put(Player.NB_STEPS_LABEL, this.nbSteps);
		o.put(Player.NB_STEPS_LIMIT_LABEL, this.nbStepsLimit);

		return o;
	}

	@Override
	public void deserialise(final JSONObject o) {

		this.currentQuest = Quest.valueOf((String) o.get(Player.CURRENT_QUEST_LABEL));
		this.currentRoom = Room.valueOf((String) o.get(Player.CURRENT_ROOM_LABEL));

		this.previousRooms.clear();
		final Object previousRoomsObject = o.get(Player.PREVIOUS_ROOMS_LABEL);
		if (previousRoomsObject != null) {
			this.previousRooms.addAll(EnumUtils.valuesOf(Room.class,
					((JSONArray) previousRoomsObject).toArray(new String[0])));
		}

		this.currentDirection = Direction.valueOf((String) o.get(Player.CURRENT_DIRECTION_LABEL));

		this.inventory.empty();
		final Object inventoryObject = o.get(Player.INVENTORY_LABEL);
		if (inventoryObject != null) {
			this.inventory.deserialise((JSONArray) inventoryObject);
		}

		this.inventoryWeightLimit = ((Long) o.get(Player.INVENTORY_WEIGHT_LIMIT_LABEL)).intValue();
		this.nbSteps = ((Long) o.get(Player.NB_STEPS_LIMIT_LABEL)).intValue();
		this.nbStepsLimit = ((Long) o.get(Player.NB_STEPS_LIMIT_LABEL)).intValue();

	}

}