-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.java
More file actions
253 lines (218 loc) · 12.8 KB
/
Functions.java
File metadata and controls
253 lines (218 loc) · 12.8 KB
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
import java.util.*;
import processing.core.PImage;
import processing.core.PApplet;
/**
* This class contains many functions written in a procedural style.
* You will reduce the size of this class over the next several weeks
* by refactoring this codebase to follow an OOP style.
*/
public final class Functions {
public static final Random rand = new Random();
public 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 List<String> PATH_KEYS = new ArrayList<>(Arrays.asList("bridge", "dirt", "dirt_horiz", "dirt_vert_left", "dirt_vert_right", "dirt_bot_left_corner", "dirt_bot_right_up", "dirt_vert_left_bot"));
public static final double SAPLING_ACTION_ANIMATION_PERIOD = 1.000; // have to be in sync since grows and gains health at same time
public static final int SAPLING_HEALTH_LIMIT = 5;
public static final int PROPERTY_KEY = 0;
public static final int PROPERTY_ID = 1;
public static final int PROPERTY_COL = 2;
public static final int PROPERTY_ROW = 3;
public static final int ENTITY_NUM_PROPERTIES = 4;
public static final String STUMP_KEY = "stump";
public static final int STUMP_NUM_PROPERTIES = 0;
public static final String SAPLING_KEY = "sapling";
public static final int SAPLING_HEALTH = 0;
public static final int SAPLING_NUM_PROPERTIES = 1;
public static final String OBSTACLE_KEY = "obstacle";
public static final int OBSTACLE_ANIMATION_PERIOD = 0;
public static final int OBSTACLE_NUM_PROPERTIES = 1;
public static final String DUDE_KEY = "dude";
public static final int DUDE_ACTION_PERIOD = 0;
public static final int DUDE_ANIMATION_PERIOD = 1;
public static final int DUDE_LIMIT = 2;
public static final int DUDE_NUM_PROPERTIES = 3;
public static final String HOUSE_KEY = "house";
public static final int HOUSE_NUM_PROPERTIES = 0;
public static final String FAIRY_KEY = "fairy";
public static final int FAIRY_ANIMATION_PERIOD = 0;
public static final int FAIRY_ACTION_PERIOD = 1;
public static final int FAIRY_NUM_PROPERTIES = 2;
public static final String TREE_KEY = "tree";
public static final int TREE_ANIMATION_PERIOD = 0;
public static final int TREE_ACTION_PERIOD = 1;
public static final int TREE_HEALTH = 2;
public static final int TREE_NUM_PROPERTIES = 3;
public static final double TREE_ANIMATION_MAX = 0.600;
public static final double TREE_ANIMATION_MIN = 0.050;
public static final double TREE_ACTION_MAX = 1.400;
public static final double TREE_ACTION_MIN = 1.000;
public static final int TREE_HEALTH_MAX = 3;
public static final int TREE_HEALTH_MIN = 1;
public static int getIntFromRange(int max, int min) {
Random rand = new Random();
return min + rand.nextInt(max-min);
}//does not use data from any other class so it belongs here
public static double getNumFromRange(double max, double min) {
Random rand = new Random();
return min + rand.nextDouble() * (max - min);
}//does not use data from any other class so it belongs here
public static void parseSapling(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == SAPLING_NUM_PROPERTIES) {
int health = Integer.parseInt(properties[SAPLING_HEALTH]);
Entities entity = createSapling(id, pt, getImageList(imageStore, SAPLING_KEY), health);
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", SAPLING_KEY, SAPLING_NUM_PROPERTIES));
}
}
public static void parseDude(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == DUDE_NUM_PROPERTIES) {
Entities entity = createDudeNotFull(id, pt, Double.parseDouble(properties[DUDE_ACTION_PERIOD]), Double.parseDouble(properties[DUDE_ANIMATION_PERIOD]), Integer.parseInt(properties[DUDE_LIMIT]), Functions.getImageList(imageStore, DUDE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", DUDE_KEY, DUDE_NUM_PROPERTIES));
}
}
public static void parseFairy(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == FAIRY_NUM_PROPERTIES) {
Entities entity = createFairy(id, pt, Double.parseDouble(properties[FAIRY_ACTION_PERIOD]), Double.parseDouble(properties[FAIRY_ANIMATION_PERIOD]), Functions.getImageList(imageStore, FAIRY_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", FAIRY_KEY, FAIRY_NUM_PROPERTIES));
}
}
public static void parseTree(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == TREE_NUM_PROPERTIES) {
Entities entity = createTree(id, pt, Double.parseDouble(properties[TREE_ACTION_PERIOD]), Double.parseDouble(properties[TREE_ANIMATION_PERIOD]), Integer.parseInt(properties[TREE_HEALTH]), Functions.getImageList(imageStore, TREE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", TREE_KEY, TREE_NUM_PROPERTIES));
}
}
public static void parseObstacle(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == OBSTACLE_NUM_PROPERTIES) {
Entities entity = createObstacle(id, pt, Double.parseDouble(properties[OBSTACLE_ANIMATION_PERIOD]), Functions.getImageList(imageStore, OBSTACLE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", OBSTACLE_KEY, OBSTACLE_NUM_PROPERTIES));
}
}
public static void parseHouse(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == HOUSE_NUM_PROPERTIES) {
Entities entity = createHouse(id, pt, Functions.getImageList(imageStore, HOUSE_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", HOUSE_KEY, HOUSE_NUM_PROPERTIES));
}
}
public static void parseStump(WorldModel world, String[] properties, Point pt, String id, ImageStore imageStore) {
if (properties.length == STUMP_NUM_PROPERTIES) {
Entities entity = createStump(id, pt, Functions.getImageList(imageStore, STUMP_KEY));
world.tryAddEntity(entity);
}else{
throw new IllegalArgumentException(String.format("%s requires %d properties when parsing", STUMP_KEY, STUMP_NUM_PROPERTIES));
}
}
public static Optional<Entities> getOccupant(WorldModel world, Point pos) {
if (pos.isOccupied(world)) {
return Optional.of(pos.getOccupancyCell(world));
} else {
return Optional.empty();
}
}
public static Entities createSapling(String id, Point position, List<PImage> images, int health) {
return new Sapling(EntityKind.SAPLING, id, position, images, 0, 0, SAPLING_ACTION_ANIMATION_PERIOD, SAPLING_ACTION_ANIMATION_PERIOD, 0, SAPLING_HEALTH_LIMIT);
}
public static Entities createFairy(String id, Point position, double actionPeriod, double animationPeriod, List<PImage> images) {
return new Fairy(EntityKind.FAIRY, id, position, images, 0, 0, actionPeriod, animationPeriod, 0, 0);
}
public static Entities createDudeNotFull(String id, Point position, double actionPeriod, double animationPeriod, int resourceLimit, List<PImage> images) {
return new DudeNotFull(EntityKind.DUDE_NOT_FULL, id, position, images, resourceLimit, 0, actionPeriod, animationPeriod, 0, 0);
}
public static Entities createHouse(String id, Point position, List<PImage> images) {
return new House(EntityKind.HOUSE, id, position, images, 0, 0, 0, 0, 0, 0);
}
public static Entities createDudeFull(String id, Point position, double actionPeriod, double animationPeriod, int resourceLimit, List<PImage> images) {
return new DudeFull(EntityKind.DUDE_FULL, id, position, images, resourceLimit, 0, actionPeriod, animationPeriod, 0, 0);
}
public static Entities createObstacle(String id, Point position, double animationPeriod, List<PImage> images) {
return new Obstacle(EntityKind.OBSTACLE, id, position, images, 0, 0, 0, animationPeriod, 0, 0);
}
public static Entities createTree(String id, Point position, double actionPeriod, double animationPeriod, int health, List<PImage> images) {
return new Tree(EntityKind.TREE, id, position, images, 0, 0, actionPeriod, animationPeriod, health, 0);
}
public static Entities createStump(String id, Point position, List<PImage> images) {
return new Stump(EntityKind.STUMP, id, position, images, 0, 0, 0, 0, 0, 0);
}
public static void parseEntity(WorldModel world, String line, ImageStore imageStore) {
String[] properties = line.split(" ", Functions.ENTITY_NUM_PROPERTIES + 1);
if (properties.length >= Functions.ENTITY_NUM_PROPERTIES) {
String key = properties[PROPERTY_KEY];
String id = properties[Functions.PROPERTY_ID];
Point pt = new Point(Integer.parseInt(properties[Functions.PROPERTY_COL]), Integer.parseInt(properties[Functions.PROPERTY_ROW]));
properties = properties.length == Functions.ENTITY_NUM_PROPERTIES ?
new String[0] : properties[Functions.ENTITY_NUM_PROPERTIES].split(" ");
switch (key) {
case Functions.OBSTACLE_KEY -> Functions.parseObstacle(world, properties, pt, id, imageStore);
case Functions.DUDE_KEY -> Functions.parseDude(world, properties, pt, id, imageStore);
case Functions.FAIRY_KEY -> Functions.parseFairy(world, properties, pt, id, imageStore);
case Functions.HOUSE_KEY -> Functions.parseHouse(world, properties, pt, id, imageStore);
case Functions.TREE_KEY -> Functions.parseTree(world, properties, pt, id, imageStore);
case Functions.SAPLING_KEY -> Functions.parseSapling(world, properties, pt, id, imageStore);
case Functions.STUMP_KEY -> Functions.parseStump(world, properties, pt, id, imageStore);
default -> throw new IllegalArgumentException("Entity key is unknown");
}
}else{
throw new IllegalArgumentException("Entity must be formatted as [key] [id] [x] [y] ...");
}
}
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);
}
}
}
}
public static List<PImage> getImages(Map<String, List<PImage>> images, String key) {
return images.computeIfAbsent(key, k -> new LinkedList<>());
}
public 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 List<PImage> getImageList(ImageStore imageStore, String key) {
return imageStore.images.getOrDefault(key, imageStore.defaultImages);
}
public static void loadImages(Scanner in, ImageStore imageStore, PApplet screen) {
int lineNumber = 0;
while (in.hasNextLine()) {
try {
processImageLine(imageStore.images, in.nextLine(), screen);
} catch (NumberFormatException e) {
System.out.printf("Image format error on line %d\n", lineNumber);
}
lineNumber++;
}
}
}