-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
345 lines (284 loc) · 10.6 KB
/
Main.java
File metadata and controls
345 lines (284 loc) · 10.6 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import processing.core.*;
import javax.swing.text.html.parser.Entity;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.File;
public class Main extends PApplet
{
private static final int WORLD_WIDTH_SCALE = 2;
private static final int WORLD_HEIGHT_SCALE = 2;
private static final int SCREEN_WIDTH = 640;
private static final int SCREEN_HEIGHT = 480;
private static final int TILE_WIDTH = 32;
private static final int TILE_HEIGHT = 32;
private static final int TIMER_ACTION_DELAY = 100;
private static final String IMAGE_LIST_FILE_NAME = "imagelist";
private static final String DEFAULT_IMAGE_NAME = "background_default";
private static final int DEFAULT_IMAGE_COLOR = 0x808080;
private static final String SAVE_FILE_NAME = "gaia.sav";
private ImageStore imageStore;
private long next_time;
private WorldModel world;
private WorldView view;
private long time;
private int flag = 0;
public void setup()
{
size(SCREEN_WIDTH, SCREEN_HEIGHT);
imageStore = new ImageStore(
createImageColored(TILE_WIDTH, TILE_HEIGHT, DEFAULT_IMAGE_COLOR));
loadImages(IMAGE_LIST_FILE_NAME, imageStore, this);
int num_cols = SCREEN_WIDTH / TILE_WIDTH * WORLD_WIDTH_SCALE;
int num_rows = SCREEN_HEIGHT / TILE_HEIGHT * WORLD_HEIGHT_SCALE;
// create default background
Background background = createDefaultBackground(imageStore);
// create world model
world = new WorldModel(num_rows, num_cols, background);
// load world
Map<String, PropertyParser> parsers = buildPropertyParsers(world,
imageStore, System.currentTimeMillis());
loadWorld(world, SAVE_FILE_NAME, imageStore, parsers);
// create world view
view = new WorldView(SCREEN_WIDTH / TILE_WIDTH,
SCREEN_HEIGHT / TILE_HEIGHT, this, world, TILE_WIDTH, TILE_HEIGHT);
// update view?
next_time = System.currentTimeMillis() + TIMER_ACTION_DELAY;
}
public void draw()
{
time = System.currentTimeMillis();
if (time >= next_time)
{
world.updateOnTime(time);
next_time = time + TIMER_ACTION_DELAY;
}
view.drawViewport();
}
public void keyPressed()
{
if (key == CODED)
{
int dx = 0;
int dy = 0;
switch (keyCode)
{
case UP:
dy = -1;
break;
case DOWN:
dy = 1;
break;
case LEFT:
dx = -1;
break;
case RIGHT:
dx = 1;
break;
}
view.updateView(dx, dy);
}
}
public void mouseClicked()
{
Point pt = view.create_ship(mouseX, mouseY, imageStore, time);
if(pt != null)
{
List<Blacksmith> b_list = new ArrayList<>();
for (WorldEntity ent : world.getEntities())
{
if (ent instanceof Blacksmith)
{
if ( (ent.getPosition().x - pt.x) * (ent.getPosition().x - pt.x) +
(ent.getPosition().y - pt.y) * (ent.getPosition().y - pt.y) < 625)
{
Blacksmith smith = (Blacksmith) ent;
b_list.add(smith);
}
}
}
make_moving_smiths(b_list, world, imageStore, time);
}
}
private static void make_moving_smiths(List<Blacksmith> list, WorldModel world, ImageStore imageStore, long time)
{
for(Blacksmith smith : list)
{
Point pt2 = smith.getPosition();
String name2 = smith.getName();
world.removeEntityAt(smith.getPosition());
Worker new_worker = new Worker(String.format("worker_%s", name2),
pt2, randInt(600, 1000), 100, imageStore.get("worker"));
world.addEntity(new_worker);
new_worker.schedule(world, time + new_worker.getRate(), imageStore);
}
}
private static Background createDefaultBackground(ImageStore imageStore)
{
List<PImage> bgndImgs = imageStore.get(DEFAULT_IMAGE_NAME);
return new Background(DEFAULT_IMAGE_NAME, bgndImgs);
}
private static PImage createImageColored(int width, int height, int color)
{
PImage img = new PImage(TILE_WIDTH, TILE_HEIGHT, RGB);
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++)
{
img.pixels[i] = color;
}
img.updatePixels();
return img;
}
private static void loadImages(String filename, ImageStore imageStore,
PApplet screen)
{
try
{
Scanner in = new Scanner(new File(filename));
ImageStore.loadImages(in, imageStore, TILE_WIDTH,
TILE_HEIGHT, screen);
}
catch (FileNotFoundException e)
{
System.err.println(e.getMessage());
}
}
private static void loadWorld(WorldModel world, String filename,
ImageStore imageStore, Map<String, PropertyParser> parsers)
{
try
{
Scanner in = new Scanner(new File(filename));
WorldLoad.load(in, world, imageStore, parsers);
}
catch (FileNotFoundException e)
{
System.err.println(e.getMessage());
}
}
private static final String BGND_KEY = "background";
private static final int BGND_NUM_PROPERTIES = 4;
private static final int BGND_NAME = 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_NAME = 1;
private static final int MINER_LIMIT = 4;
private static final int MINER_COL = 2;
private static final int MINER_ROW = 3;
private static final int MINER_RATE = 5;
private static final int MINER_ANIMATION_RATE = 6;
private static final String OBSTACLE_KEY = "obstacle";
private static final int OBSTACLE_NUM_PROPERTIES = 4;
private static final int OBSTACLE_NAME = 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_NAME = 1;
private static final int ORE_COL = 2;
private static final int ORE_ROW = 3;
private static final int ORE_RATE = 4;
private static final String SMITH_KEY = "blacksmith";
private static final int SMITH_NUM_PROPERTIES = 4;
private static final int SMITH_NAME = 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 = 6;
private static final int VEIN_NAME = 1;
private static final int VEIN_RATE = 4;
private static final int VEIN_COL = 2;
private static final int VEIN_ROW = 3;
private static final int VEIN_REACH = 5;
private static Map<String, PropertyParser> buildPropertyParsers(
WorldModel world, ImageStore imageStore, long time)
{
Map<String, PropertyParser> parsers = new HashMap<>();
parsers.put(BGND_KEY, properties -> {
if (properties.length >= BGND_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[BGND_COL]),
Integer.parseInt(properties[BGND_ROW]));
String name = properties[BGND_NAME];
world.setBackground(pt, new Background(name,
imageStore.get(name)));
}
});
parsers.put(MINER_KEY, properties -> {
if (properties.length == MINER_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[MINER_COL]),
Integer.parseInt(properties[MINER_ROW]));
Actor entity = new MinerNotFull(properties[MINER_NAME],
pt,
Integer.parseInt(properties[MINER_RATE]),
Integer.parseInt(properties[MINER_ANIMATION_RATE]),
Integer.parseInt(properties[MINER_LIMIT]),
imageStore.get(MINER_KEY));
world.addEntity(entity);
entity.schedule(world, time + entity.getRate(), imageStore);
}
});
parsers.put(OBSTACLE_KEY, properties -> {
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_NAME],
pt,
imageStore.get(OBSTACLE_KEY));
world.addEntity(entity);
}
});
parsers.put(ORE_KEY, properties -> {
if (properties.length == ORE_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[ORE_COL]),
Integer.parseInt(properties[ORE_ROW]));
Actor entity = new Ore(properties[ORE_NAME],
pt,
Integer.parseInt(properties[ORE_RATE]),
imageStore.get(ORE_KEY));
world.addEntity(entity);
entity.schedule(world, time + entity.getRate(), imageStore);
}
});
parsers.put(SMITH_KEY, properties -> {
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_NAME],
pt,
imageStore.get(SMITH_KEY));
world.addEntity(entity);
}
});
parsers.put(VEIN_KEY, properties -> {
if (properties.length == VEIN_NUM_PROPERTIES)
{
Point pt = new Point(Integer.parseInt(properties[VEIN_COL]),
Integer.parseInt(properties[VEIN_ROW]));
Actor entity = new Vein(properties[VEIN_NAME],
pt,
Integer.parseInt(properties[VEIN_RATE]),
Integer.parseInt(properties[VEIN_REACH]),
imageStore.get(VEIN_KEY));
world.addEntity(entity);
entity.schedule(world, time + entity.getRate(), imageStore);
}
});
return parsers;
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args)
{
PApplet.main("Main");
}
}