-
Notifications
You must be signed in to change notification settings - Fork 13
/
Player.java
189 lines (174 loc) · 4.64 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
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
import java.util.HashMap;
import java.util.LinkedList;
public class Player {
public Player(Room currentRoom) {
this(currentRoom, new LinkedList<Item>());
this.score = 0;
}
public Player(Room currentRoom, LinkedList<Item> items) {
this.items = items;
this.disguise = null;
//this.currentRoom = currentRoom;
this.currentRoom = currentRoom;
this.currentRoom.player = this;
System.out.println(this.currentRoom.description());
}
public Item drop(Item item) {
if(this.items.remove(item)) {
return item;
}
else {
return null;
}
}
public boolean dropItem(Item item) {
Item dropped = drop(item);
if (dropped == null) {
System.out.println("You don't have this item to drop");
return false;
}
this.currentRoom.putItem(dropped);
return true;
}
public boolean pickup(Item item){
this.pick(item);
return true;
}
public void pick(Item item) {
this.items.add(item);
}
public boolean hasItem(Item item) {
if(item == null) return false;
if(this.disguise == item) return true;
return this.items.contains(item);
}
public boolean hasItemOfType(String str) {
try {
for(Item item : this.items) {
if(Class.forName(str).isInstance(item)) {
return true;
}
}
}
catch(ClassNotFoundException e) {
}
return false;
}
public LinkedList<Item> getItems() {
return this.items;
}
public void putItemInItem(Item direct, Item indirect) {
((Hostable)indirect).install(direct);
if(indirect instanceof ItemMagicBox && direct instanceof Valuable) {
score((Valuable)direct);
}
}
public void wearDisguise(Item disguise) {
if(this.disguise != null) {
this.currentRoom.putItem(this.disguise);
}
this.disguise = disguise;
}
public Item disguise() {
return this.disguise;
}
public void move(Room nextRoom) {
nextRoom.setPlayer(this);
if(this.currentRoom != null && nextRoom.compareTo(this.currentRoom) != 0) {
Action directionOfTravel = this.currentRoom.getDirectionForRoom(nextRoom);
HashMap<Action, String> messages = this.currentRoom.transitionMessages();
String message = messages.get(directionOfTravel);
int delay = this.currentRoom.transitionDelay();
if(message != null) {
if(delay != 0) {
for(int i=0; i < 3; i++) {
System.out.println("...");
try{
Thread.sleep(delay);
}
catch(Exception e1) {
// pass
}
}
}
System.out.println(message);
}
}
if(nextRoom instanceof RoomRequiredItem) {
RoomRequiredItem r = (RoomRequiredItem)nextRoom;
if(r.diesOnEntry()) {
System.out.println(r.deathMessage());
this.die();
}
}
this.currentRoom = nextRoom;
System.out.println(this.currentRoom.description());
// TODO not sure if I want to leave this line in for the final release
//System.out.println(this.currentRoom.visibleItems());
if(nextRoom instanceof RoomSky) {
RoomSky sky = (RoomSky)nextRoom;
sky.freefall();
}
}
public void move(Action a) {
if(this.currentRoom instanceof RoomRequiredItem) {
RoomRequiredItem room = (RoomRequiredItem)this.currentRoom;
if(room.shouldDieForAction(a)) {
System.out.println(room.deathMessage());
this.die();
}
}
else if(this.currentRoom instanceof RoomDark) {
RoomDark room = (RoomDark)this.currentRoom;
if(room.isDark() && room.willDieInDirection(a) && !this.hasItemOfType("Luminous")) {
System.out.println(room.deathMessage());
this.die();
}
}
if(this.currentRoom.canMoveToRoomInDirection(a)) {
Room nextRoom = this.currentRoom.getRoomForDirection(a);
// test if requires key
if(nextRoom instanceof RoomLockable) {
RoomLockable lockedRoom = (RoomLockable)nextRoom;
if(lockedRoom.isLocked()) {
if(lockedRoom.causesDeath()) {
System.out.println(lockedRoom.deathMessage());
this.die();
}
System.out.println("This door is locked.");
return;
}
}
else if(nextRoom instanceof RoomObscured) {
RoomObscured obscuredRoom = (RoomObscured)nextRoom;
if(obscuredRoom.isObscured()) {
System.out.println("You can't move that way.");
return;
}
}
move(nextRoom);
}
else {
System.out.println("You can't move that way.");
}
}
public Room currentRoom() {
return this.currentRoom;
}
public void look() {
System.out.println(this.currentRoom.toString());
}
public void score(Valuable object) {
int score = object.value();
System.out.println("You scored " + score + " points.");
this.score+=score;
}
public void die() {
System.out.println("You scored " + this.score + " out of 90 possible points. You are dead.");
System.exit(0);
}
protected int score;
protected Item disguise;
protected LinkedList<Item> items;
protected Room currentRoom;
}