-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.java
117 lines (98 loc) · 2.77 KB
/
Item.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
/**
* Item entity for the world map
* @author Johnson Zhou 1302442 <zhoujj@student.unimelb.edu.au>
*
*/
public class Item extends Entity {
public static enum Types {
HEAL('+'), ATTACKUP('^'), WARPSTONE('@');
char symbol;
Types(char symbol) {
this.symbol = symbol;
}
/**
* @return type symbol as char
*/
public char getSymbol() {
return this.symbol;
}
/**
* @param other another string to compare against
* @return <code>True</code> if other string matches one of enum type symbols
*/
public boolean equals(String other) {
return this.toString().equals(other);
}
/**
* @return type symbol as String
*/
@Override
public String toString() {
return Character.toString(this.symbol);
}
};
private Types type;
public Item(Types type) {
this.type = type;
}
/**
* Short cut constructor at also invokes the <code>load</code> method.
* @param loadString matching format <code>item x y symbol</code>
* @param map the world map
* @throws FileIOException if loadString does not contain valid data
*/
public Item(String loadString, Map map) throws FileIOException {
this.load(loadString, map);
}
/**
* Loads Item map information from a string of text
* @param loadString matching format <code>item x y symbol</code>
* @param map the world map
* @throws FileIOException if loadString does not contain valid data
*/
public void load(String loadString, Map map) throws FileIOException {
String[] loaded = loadString.split(" ");
// validate string elements
if (loaded.length != 4) {
throw new FileIOException("Unexpected length of item load string");
}
// validate string identifier
if (!loaded[0].equals("item")) {
throw new FileIOException("'item' identifier expected in loadString");
}
// parse item coordinates
int x = -1;
int y = -1;
try {
x = Integer.parseInt(loaded[1]);
y = Integer.parseInt(loaded[2]);
} catch (NumberFormatException e) {
throw new FileIOException("Could not parse item coordinates");
}
// set item coordinates
if (!map.traversable(x, y) || x < 0 || y < 0) {
throw new FileIOException("Non-traversable item coordinates");
}
this.setX(x);
this.setY(y);
// parse and set item type
for (Types type : Types.values()) {
if (type.equals(loaded[3])) {
this.type = type;
return;
}
}
throw new FileIOException("Item symbol is not of known type");
}
/**
* @return this item type
*/
public Types getType() {
return this.type;
}
/** Entity */
@Override
public char getMapMarker() {
return this.type.getSymbol();
}
}