-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageStore.java
296 lines (268 loc) · 9.89 KB
/
ImageStore.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.util.*;
import processing.core.PApplet;
import processing.core.PImage;
final class ImageStore
{
private static final int PROPERTY_KEY = 0;
private static final String QUAKE_KEY = "quake";
private static final String QUAKE_ID = "quake";
private static final int COLOR_MASK = 0xffffff;
private static final int KEYED_IMAGE_MIN = 5;
private static final int KEYED_RED_IDX = 2;
private static final int KEYED_GREEN_IDX = 3;
private static final int KEYED_BLUE_IDX = 4;
private static final String BGND_KEY = "background";
private static final int BGND_NUM_PROPERTIES = 4;
private static final int BGND_ID = 1;
private static final int BGND_COL = 2;
private static final int BGND_ROW = 3;
private static final String MINER_KEY = "miner";
private static final int MINER_NUM_PROPERTIES = 7;
private static final int MINER_ID = 1;
private static final int MINER_COL = 2;
private static final int MINER_ROW = 3;
private static final int MINER_LIMIT = 4;
private static final int MINER_ACTION_PERIOD = 5;
private static final int MINER_ANIMATION_PERIOD = 6;
private static final String OBSTACLE_KEY = "obstacle";
private static final int OBSTACLE_NUM_PROPERTIES = 4;
private static final int OBSTACLE_ID = 1;
private static final int OBSTACLE_COL = 2;
private static final int OBSTACLE_ROW = 3;
private static final String ORE_KEY = "ore";
private static final int ORE_NUM_PROPERTIES = 5;
private static final int ORE_ID = 1;
private static final int ORE_COL = 2;
private static final int ORE_ROW = 3;
private static final int ORE_ACTION_PERIOD = 4;
private static final String SMITH_KEY = "blacksmith";
private static final int SMITH_NUM_PROPERTIES = 4;
private static final int SMITH_ID = 1;
private static final int SMITH_COL = 2;
private static final int SMITH_ROW = 3;
private static final String VEIN_KEY = "vein";
private static final int VEIN_NUM_PROPERTIES = 5;
private static final int VEIN_ID = 1;
private static final int VEIN_COL = 2;
private static final int VEIN_ROW = 3;
private static final int VEIN_ACTION_PERIOD = 4;
private Map<String, List<PImage>> images;
private List<PImage> defaultImages;
public ImageStore(PImage defaultImage)
{
this.images = new HashMap<>();
defaultImages = new LinkedList<>();
defaultImages.add(defaultImage);
}
public static String getQuakeKey() {
return QUAKE_KEY;
}
public static String getQuakeId() {
return QUAKE_ID;
}
public static String getBgndKey() {
return BGND_KEY;
}
public static String getOreKey() {
return ORE_KEY;
}
private static void processImageLine(Map<String, List<PImage>> images,
String line, PApplet screen)
{
String[] attrs = line.split("\\s");
if (attrs.length >= 2)
{
String key = attrs[0];
PImage img = screen.loadImage(attrs[1]);
if (img != null && img.width != -1)
{
List<PImage> imgs = getImages(images, key);
imgs.add(img);
if (attrs.length >= KEYED_IMAGE_MIN)
{
int r = Integer.parseInt(attrs[KEYED_RED_IDX]);
int g = Integer.parseInt(attrs[KEYED_GREEN_IDX]);
int b = Integer.parseInt(attrs[KEYED_BLUE_IDX]);
setAlpha(img, screen.color(r, g, b), 0);
}
}
}
}
private static List<PImage> getImages(Map<String, List<PImage>> images,
String key)
{
List<PImage> imgs = images.get(key);
if (imgs == null)
{
imgs = new LinkedList<>();
images.put(key, imgs);
}
return imgs;
}
/*
Called with color for which alpha should be set and alpha value.
setAlpha(img, color(255, 255, 255), 0));
*/
private static void setAlpha(PImage img, int maskColor, int alpha)
{
int alphaValue = alpha << 24;
int nonAlpha = maskColor & COLOR_MASK;
img.format = PApplet.ARGB;
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++)
{
if ((img.pixels[i] & COLOR_MASK) == nonAlpha)
{
img.pixels[i] = alphaValue | nonAlpha;
}
}
img.updatePixels();
}
private boolean processLine(String line, WorldModel world)
{
String[] properties = line.split("\\s");
if (properties.length > 0)
{
switch (properties[PROPERTY_KEY])
{
case BGND_KEY:
return parseBackground(properties, world);
case MINER_KEY:
return parseMiner(properties, world);
case OBSTACLE_KEY:
return parseObstacle(properties, world);
case ORE_KEY:
return parseOre(properties, world);
case SMITH_KEY:
return parseSmith(properties, world);
case VEIN_KEY:
return parseVein(properties, world);
}
}
return false;
}
public void load(Scanner in, WorldModel world)
{
int lineNumber = 0;
while (in.hasNextLine())
{
try
{
if (!this.processLine(in.nextLine(), world))
{
System.err.println(String.format("invalid entry on line %d",
lineNumber));
}
}
catch (NumberFormatException e)
{
System.err.println(String.format("invalid entry on line %d",
lineNumber));
}
catch (IllegalArgumentException e)
{
System.err.println(String.format("issue on line %d: %s",
lineNumber, e.getMessage()));
}
lineNumber++;
}
}
public void loadImages(Scanner in, PApplet screen)
{
int lineNumber = 0;
while (in.hasNextLine())
{
try
{
processImageLine(images, in.nextLine(), screen);
}
catch (NumberFormatException e)
{
System.out.println(String.format("Image format error on line %d",
lineNumber));
}
lineNumber++;
}
}
private boolean parseBackground(String[] properties, WorldModel world)
{
if (properties.length == BGND_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[BGND_COL]),
Integer.parseInt(properties[BGND_ROW]));
String id = properties[BGND_ID];
world.setBackground(pt, new Background(id, getImageList(id)));
}
return properties.length == BGND_NUM_PROPERTIES;
}
private boolean parseMiner(String[] properties, WorldModel world)
{
if (properties.length == MINER_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[MINER_COL]),
Integer.parseInt(properties[MINER_ROW]));
MinerEntity entity = new MinerNotFull(properties[MINER_ID],
Integer.parseInt(properties[MINER_LIMIT]), pt,
Integer.parseInt(properties[MINER_ACTION_PERIOD]),
Integer.parseInt(properties[MINER_ANIMATION_PERIOD]),
getImageList(MINER_KEY));
world.tryAddEntity(entity);
}
return properties.length == MINER_NUM_PROPERTIES;
}
private boolean parseObstacle(String[] properties, WorldModel world)
{
if (properties.length == OBSTACLE_NUM_PROPERTIES)
{
Point pt = new Point(
Integer.parseInt(properties[OBSTACLE_COL]),
Integer.parseInt(properties[OBSTACLE_ROW]));
WorldEntity entity = new Obstacle(properties[OBSTACLE_ID],
pt, getImageList(OBSTACLE_KEY));
world.tryAddEntity(entity);
}
return properties.length == OBSTACLE_NUM_PROPERTIES;
}
private boolean parseOre(String[] properties, WorldModel world)
{
if (properties.length == ORE_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[ORE_COL]),
Integer.parseInt(properties[ORE_ROW]));
ActiveEntity entity = new Ore(properties[ORE_ID],
pt, Integer.parseInt(properties[ORE_ACTION_PERIOD]),
getImageList(getOreKey()));
world.tryAddEntity(entity);
}
return properties.length == ORE_NUM_PROPERTIES;
}
private boolean parseSmith(String[] properties, WorldModel world)
{
if (properties.length == SMITH_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[SMITH_COL]),
Integer.parseInt(properties[SMITH_ROW]));
WorldEntity entity = new Blacksmith(properties[SMITH_ID], pt,
getImageList(SMITH_KEY));
world.tryAddEntity(entity);
}
return properties.length == SMITH_NUM_PROPERTIES;
}
private boolean parseVein(String[] properties, WorldModel world)
{
if (properties.length == VEIN_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[VEIN_COL]),
Integer.parseInt(properties[VEIN_ROW]));
ActiveEntity entity = new Vein(properties[VEIN_ID], pt,
Integer.parseInt(properties[VEIN_ACTION_PERIOD]),
getImageList(VEIN_KEY));
world.tryAddEntity(entity);
}
return properties.length == VEIN_NUM_PROPERTIES;
}
public List<PImage> getImageList(String key)
{
return images.getOrDefault(key, defaultImages);
}
}