-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.java
151 lines (137 loc) · 4.42 KB
/
Player.java
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
import java.util.HashMap;
/**
* This class represents players in the game.
* Each player has a current location.
* Externalisation of the current room
*
* @author Alexandre Boursier & Nolan Potier
* @version 2011.10.24
*/
public class Player
{
private Room currentRoom;
private HashMap<String, Item> items;
/**
* Constructor for objects of class Player
*/
public Player()
{
currentRoom = null;
items = new HashMap<String, Item>();
}
/**
* Return the current room for this player.
*/
public Room getCurrentRoom()
{
return currentRoom;
}
/**
* Set the current room for this player.
*/
public void setCurrentRoom(Room room)
{
currentRoom = room;
}
/**
* Try to go to one direction. If there is an exit, enter the new room,
* otherwise print an error message.
*
* New form of time limit : Each time a player uses the command "GO"
* with a good given direction (ex : go east), it is counted as a move
*
* All cases are checked in order to prevent the player from going outside without
* the key, or without having opened the door, etc.
*
* If the number of moves equals to the limit number of moves,
* the player loses the game.
*
*/
public boolean goRoom(String direction)
{
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no door in that direction.");
boolean decision = Game.countMove();
return decision;
}
if(currentRoom.getDoor(direction).isLocked())
{
if(items.get("Key")==null) {
System.out.println("Sorry but the door is locked ! You will need to use a key.");
boolean decision = Game.countMove();
return decision;
}
else
{
System.out.println("You have to open the door to get out of there!");
return false;
}
}
else
{
setCurrentRoom(nextRoom);
//Check if there are Character in the room
//
Character person = currentRoom.getCharacter();
if(person != null && !person.hasSpoken()) {
System.out.println("\nThere is a person in this Room...\n" + person.getName() + ": " + person.getDialogue());
person.setHasSpoken(true);
}
//Check that is randomRoom
//
if(currentRoom.equals(Game.getRandomRoom())) {
Game.goRandomRoom();
}
// Check if the player is in the trap room and if he has already been there
// Yes : the game continues normally
// No : he falls into the trap
if (!Trap.Is_catched_by_trap() && currentRoom.getType().equals(Trap.getChosen_trap().getType()))
{
System.out.println();
System.out.println("----You travel through the portal and are now in a new room -----------");
System.out.println();
for(Room r : Game.getRooms()){
if(r.getType().equals(Type.TRAP_ROOM)){
setCurrentRoom(r);
}
}
Trap.setIs_catched_by_trap(true);
}
// Check if the nextroom is the final exit
if (currentRoom.getType().equals(Type.OUTSIDE)) {
System.out.println("The outside checkpoint has been reached ! ");
System.out.println("You have won the game, well done Indiana ;)");
System.out.println();
System.out.println();
return true;
}
System.out.println(currentRoom.getLongDescription());
}
boolean decision = Game.countMove();
return decision;
}
/**
* @return the items
*/
public HashMap<String, Item> getItems()
{
return items;
}
/**
* @param items the items to set
*/
public void setItems(HashMap<String, Item> items)
{
this.items = items;
}
/**
* Add a item to inventory of player.
* @param item
*/
public void addItem(Item item)
{
items.put(item.getName().toLowerCase(), item);
}
}