-
Notifications
You must be signed in to change notification settings - Fork 3
/
Functions.java
169 lines (150 loc) · 4.9 KB
/
Functions.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
/*
Roee Landesman Version 1.0 (Commented for syncing validation)
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Scanner;
import processing.core.PImage;
import processing.core.PApplet;
final class Functions
{
public static final String BLOB_KEY = "blob";
public static final String QUAKE_KEY = "quake";
public static final String DEFAULT_IMAGE_NAME = "background_default";
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 int PROPERTY_KEY = 0;
private static final String BGND_KEY = "background";
private static final String MINER_KEY = "miner";
private static final String OBSTACLE_KEY = "obstacle";
public static final String ORE_KEY = "ore";
private static final String SMITH_KEY = "blacksmith";
private static final String VEIN_KEY = "vein";
public 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();
}
public static void load(Scanner in, ImageStore imageStore, WorldModel world)
{
int lineNumber = 0;
while (in.hasNextLine())
{
try
{
if (!processLine(in.nextLine(), imageStore, 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++;
}
}
private static boolean processLine(String line, ImageStore imageStore, WorldModel world)
{
String[] properties = line.split("\\s");
if (properties.length > 0)
{
switch (properties[PROPERTY_KEY])
{
case BGND_KEY:
return WorldLoader.parseBackground(properties, imageStore, world);
case MINER_KEY:
return WorldLoader.parseMiner(properties, imageStore, world);
case OBSTACLE_KEY:
return WorldLoader.parseObstacle(properties, imageStore, world);
case ORE_KEY:
return WorldLoader.parseOre(properties, imageStore, world);
case SMITH_KEY:
return WorldLoader.parseSmith(properties, imageStore, world);
case VEIN_KEY:
return WorldLoader.parseVein(properties, imageStore, world);
}
}
return false;
}
public static void loadImages(String filename, ImageStore imageStore,
PApplet screen)
{
try
{
Scanner in = new Scanner(new File(filename));
imageStore.loadImages(in, screen);
}
catch (FileNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public static Background createDefaultBackground(ImageStore imageStore)
{
return new Background(DEFAULT_IMAGE_NAME,
imageStore.getImageList(DEFAULT_IMAGE_NAME));
}
}