-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameModel.java
More file actions
241 lines (214 loc) · 9.01 KB
/
Copy pathGameModel.java
File metadata and controls
241 lines (214 loc) · 9.01 KB
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
import java.util.*;
public class GameModel {
private final HashMap<String, Location> gameLocations;
private final ArrayList<Action> gameActions;
private final ArrayList<Player> playerList;
private String startLocation;
private final HashMap<String, Entity>entityList;
private String deathMessage;
public GameModel(){
gameLocations = new HashMap<>();
gameActions = new ArrayList<>();
playerList = new ArrayList<>();
entityList = new HashMap<>();
}
public void setStartLocation(String location){
startLocation = location;
}
public String getStartLocation(){
return startLocation;
}
public void setActionObject(Action newAction){
gameActions.add(newAction);
}
public ArrayList<Action> getActions(){
return gameActions;
}
// Creates a new location in the GameModel
public void createLocation(Location location, String locationName){
// If the list is empty, it's the first location --> starting location.
if (gameLocations.isEmpty()){
setStartLocation(locationName);
}
gameLocations.put(locationName, location);
}
// Returns a Location object from the GameModel
public Location getOneLocation(String locationName){
if (gameLocations.containsKey(locationName)){
return gameLocations.get(locationName);
}
return null;
}
// Checks if the locations exists
public boolean locationExist(String locationName){
return gameLocations.containsKey(locationName);
}
// Checks if the player exists
public boolean playerExist(String name){
for (Player player : playerList){
if (player.getPlayerName().equals(name)){
return true;
}
}
return false;
}
// For LOOK command : Returns the entities to the screen to display what's in location.
public ArrayList<String> getEntitiesAtLocation(String locationName, Player currentPlayer) {
ArrayList<String> descriptionList = new ArrayList<>();
for (Entity entity : gameLocations.get(locationName).getEntityList()){
if (!currentPlayer.getPlayerName().equals(entity.getName())) { // Don't return the current player as an entity.
if (!descriptionList.contains(entity.getDescription())) { // Stops entities from being added twice - such as players.
descriptionList.add(entity.getDescription());
}
}
}
return descriptionList;
}
// Checks if the object is in the inventory of a specific player.
public boolean isObjectInInventory(String entity, String currentPlayer){
for (Player player : playerList){
if (player.getPlayerName().equals(currentPlayer)){
if (player.getInventory().contains(entity)) {
return true;
}
}
}
return false;
}
// Removes the entity from the player's inventory (If it's been consumed).
public void updatePlayerInventory(String entityName, String currentPlayer, GameModel model){
for (Player player : playerList){
if (player.getPlayerName().equals(currentPlayer)){
if (player.isInInventory(entityName)){
player.removeFromInventory(entityName, model, "consumed");
}
}
}
}
// Checks if the entity is in the location
public boolean isEntityAtLocation(String entity, String location){
return gameLocations.get(location).containsEntity(entity);
}
// Removes the object from the location specified. If it's not there, checks the unplaced.
public void updateLocationObjects(String locationName, String entityName, GameModel model, String currentPlayer){
if (isEntityAtLocation(entityName, locationName)){
gameLocations.get(locationName).removeEntity(entityName);
}
if (gameLocations.get("unplaced").containsEntity(entityName)){
if (isObjectInInventory(entityName, currentPlayer)) {
Entity newEntity = gameLocations.get("unplaced").getEntity(entityName);
gameLocations.get(locationName).addEntity(newEntity.getName(), model);
gameLocations.get("unplaced").removeEntity(entityName);
}
}
}
public void removeLocationFromPath(String consumed, String currentLocation){
for(Map.Entry<String, Entity> entry : entityList.entrySet()) {
Entity entity = entry.getValue();
if (entity.getName().equals(consumed) && entity.getType().equals("location")) {
gameLocations.get(currentLocation).removePaths(consumed);
}
}
}
// Moves entities from one location to another --> removes from current, adds to next.
// if it's a location, adds to the path.
public void moveEntityToLocation(String produced, String locationName, GameModel model){
for(Map.Entry<String, Entity> entry : entityList.entrySet()) {
Entity entity = entry.getValue();
if (entity.getName().equals(produced)){
if (entity.getType().equals("location")) {
if (!gameLocations.get(locationName).isItAPath(produced)) {
gameLocations.get(locationName).setPaths(produced); // Adds the location to the path
}
}
else {
removeEntityFromLocation(entity.getName());
gameLocations.get(locationName).addEntity(produced, model); // Adds the entity into the game
}
}
}
}
// Removes an entity from the location.
public void removeEntityFromLocation(String entity){
for(Map.Entry<String, Location> entry : gameLocations.entrySet()) {
Location location = entry.getValue();
if (location.containsEntity(entity)) {
gameLocations.get(location.getLocationName()).removeEntity(entity);
}
}
}
// Checks and updates the player's health as according to the healthLevel --> 'add' or 'remove'.
public boolean checkPlayerHealth(String playerName, String healthLevel, GameModel model){
for (Player player : playerList){
if (player.getPlayerName().equals(playerName)){
int index = playerList.indexOf(player);
if (healthLevel.equals("add")){
player.addToHealth();
}
if (healthLevel.equals("remove")){
player.subtractHealth();
}
if (player.isPlayerDead()){
resetPlayer(player, model);
setPlayerDeadMessage(getStartLocation());
return false;
}
playerList.set(index, player);
}
}
return true;
}
public void setPlayerDeadMessage(String locationName){
deathMessage = "Your health level reached 0 and you were killed. You have returned to " + locationName + "\n";
}
public String getPlayerDeadMessage(){
return deathMessage;
}
public void addPlayer(Player newPlayer){
playerList.add(newPlayer);
}
public ArrayList<Player> getPlayers(){
return playerList;
}
// Returns an entity object from the GameModel.
public Entity getEntity(String name){
for(Map.Entry<String, Entity> entry : entityList.entrySet()) {
Entity entity = entry.getValue();
if (entity.getName().equals(name)) {
return entity;
}
}
return null;
}
public boolean containsEntity(String name){
for(Map.Entry<String, Entity> entry : entityList.entrySet()) {
Entity entity = entry.getValue();
if (entity.getName().equals(name)) {
return true;
}
}
return false;
}
// Sets the name, description and types of entities in the game
public void setNewEntity(String entity, String description, String type){
Entity newEntity = new Entity();
newEntity.setName(entity);
newEntity.setDescription(description);
newEntity.setType(type);
if (!containsEntity(entity)) {
entityList.put(entity, newEntity);
}
}
// Resets a player back to its original state eg after dying, or when a new player is created
public void resetPlayer(Player playerName, GameModel model){
if (!playerName.getInventory().isEmpty()){
Iterator<String> itr = playerName.getInventory().iterator();
while (itr.hasNext()) { // Removes all items from player's inventory
model.getOneLocation(playerName.getCurrentLocation()).addEntity(itr.next(), model);
itr.remove();
}
}
playerName.setStartLocation(getStartLocation()); // Gets the first location from the location list
playerName.setHealthLevel(); // Sets health level back to original level
}
}