forked from BenVanCitters/fallingLeaves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridTiler.pde
352 lines (315 loc) · 10.5 KB
/
GridTiler.pde
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
346
347
348
349
import java.util.Hashtable;
boolean populate_tiles = true;
//***************************************************************
// grid responsible for managing, drawingm and updating all of the
// animated an non animated tiles
//***************************************************************
class GridTiler implements XMLLoadable
{
protected float[] xAxis = {20,5};
protected float[] yAxis = {5,20};
protected float[] origin = {0,0};
ArrayList<BaseGridTile> xmlTiles = new ArrayList<BaseGridTile>();
ArrayList<BaseGridTile> genTiles = new ArrayList<BaseGridTile>();
// table of tiles that come from xml
// Hashtable<pos,BaseGridTile> xmlTiles = new Hashtable<pos,BaseGridTile>();
//table of tile that we generate ourselves
// Hashtable<pos,BaseGridTile> genTiles = new Hashtable<pos,BaseGridTile>();
//***************************************************************
// xml - xml object containing serialized object
//***************************************************************
public GridTiler(XML xml)
{
loadWithXML(xml);
if (populate_tiles) {
populateNearbyTiles();
}
}
//***************************************************************
// graphscale: scale of unit vectors
// xAxisTheta: radian measurement of the xaxis
// yAxisTheta: radian measurement of the yaxis
//***************************************************************
public GridTiler(float graphScale, float xAxisTheta, float yAxisTheta)
{
this(new float[]{width/2.f,height/2.f}, graphScale, xAxisTheta, yAxisTheta);
}
//***************************************************************
// origin: 2d vector screen space offset
// graphscale: scale of unit vectors
// xAxisTheta: radian measurement of the xaxis
// yAxisTheta: radian measurement of the yaxis
//***************************************************************
public GridTiler(float[] origin, float graphScale, float xAxisTheta, float yAxisTheta)
{
this.origin = origin;
xAxis = new float[]{graphScale*cos(xAxisTheta),
graphScale*sin(xAxisTheta)};
yAxis = new float[]{graphScale*cos(yAxisTheta),
graphScale*sin(yAxisTheta)};
}
//***************************************************************
// populate nearby tiles (with a terrible O(n^2) alg because I
// can't figure out how to get java's hash maps or hash tables
// to do what I want them to here...
//***************************************************************
private void populateNearbyTiles()
{
int[] dims = {-12,12};
for(int i = dims[0]; i < dims[1]; i++)
{
for(int j = dims[0]; j < dims[1]; j++)
{
boolean occupied = isTileOccupied(i,j,3,3);
if(!occupied)
{
addRandomTile(i,j,3,3);
}
occupied = isTileOccupied(i,j,2,2);
if(!occupied)
{
addRandomTile(i,j,2,2);
}
// occupied = isTileOccupied(i,j,2,1);
// if(!occupied)
// {
// addRandomTile(i,j,2,1);
// }
occupied = isTileOccupied(i,j,1,1);
if(!occupied)
{
addRandomTile(i,j,1,1);
}
}
}
}
void addRandomTile(int x, int y, int w, int h)
{
BaseGridTile tile = null;
if(w==3 && h ==3)
{
if(random(1) > .5)
{
String[] names = {"terrain_block1-3x3.png","terrain_wave1-3x3.png","terrain_star1-3x3.png","leaves3x3-0x75.png","bldg3x3-0x75.png","leafs3x3-0x75.png","sand3x3-0x75.png"};
int rndIndex = (int)(random(names.length));
tile = new PNGGridTile(new int[]{x,y}, new int[]{3,3}, new float[]{0,75},names[rndIndex] );
}
else
{
tile = new StaticProceduralTile(x,y,3,3);
}
}
else if(w==2 && h ==2)
{
if(random(1) > .5)
{
tile = new StaticProceduralTile(x,y,2,2);
}
else
{
String[] names = {"metal2x2-0x50 copy.png","grass2x2-0x50.png","hair2x2-0x50.png"};
int rndIndex = (int)(random(names.length));
tile = new PNGGridTile(new int[]{x,y}, new int[]{2,2}, new float[]{0,50},names[rndIndex] );
}
}
else if(w==2 && h == 1)
{
String[] names = {"text2x1-0x25.png"};
int rndIndex = (int)(random(names.length));
tile = new PNGGridTile(new int[]{x,y}, new int[]{2,1}, new float[]{0,75},names[rndIndex] );
}
else if(w==1 && h ==1)
{
if(random(1) > .5)
{
tile = new StaticProceduralTile(x,y,1,1);
}
else
{
String[] names = {"pants1x1-0x25.png","corn1x1-0x25.png"};
int rndIndex = (int)(random(names.length));
tile = new PNGGridTile(new int[]{x,y}, new int[]{1,1}, new float[]{0,25},names[rndIndex] );
}
}
addTile(genTiles,tile);
}
boolean isTileOccupied(int x, int y, int w, int h)
{
boolean occupied = false;
for(BaseGridTile tile : xmlTiles)
{
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
occupied = occupied || ((tile.position[0] == x+i) && (tile.position[1] == y+j));
}
}
}
for(BaseGridTile tile : genTiles)
{
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
occupied = occupied || ((tile.position[0] == x+i) && (tile.position[1] == y+j));
}
}
}
return occupied;
}
ArrayList<BaseGridTile> getGenTiles()
{
return new ArrayList<BaseGridTile>(genTiles);
}
//***************************************************************
// updates all tiles and the grid itself
//***************************************************************
void update(float dt)
{
for(BaseGridTile tile : xmlTiles)
{
tile.update(dt);
}
for(BaseGridTile tile : genTiles)
{
tile.update(dt);
}
}
//***************************************************************
// draws all tiles and the grid itself
//***************************************************************
void draw()
{
pushMatrix();
translate(origin[0],origin[1]);
// background(255,0,0);
for(BaseGridTile tile : xmlTiles)
{
pushMatrix();
translate(tile.position[0]*xAxis[0] + tile.position[1]*yAxis[0],
tile.position[0]*xAxis[1] + tile.position[1]*yAxis[1]);
tile.draw();
//println("tile.position: " + tile.position[0] + ", " + tile.position[1]);
popMatrix();
}
for(BaseGridTile tile : genTiles)
{
pushMatrix();
translate(tile.position[0]*xAxis[0] + tile.position[1]*yAxis[0],
tile.position[0]*xAxis[1] + tile.position[1]*yAxis[1]);
tile.draw();
//println("tile.position: " + tile.position[0] + ", " + tile.position[1]);
popMatrix();
}
popMatrix();
}
//***************************************************************
// grabs the basis vectors from us
//***************************************************************
float[][] getBasisVectors()
{
return new float[][]{{xAxis[0],xAxis[1]},{yAxis[0],yAxis[1]}};
}
//***************************************************************
// load fresh from disk
//***************************************************************
void loadWithXML(XML xml)
{
//init properties
println("XML: Initializing " + this.getClass().getName());
float graphScale = xml.getFloat("scale");
println("graphScale: " + graphScale);
XML xAxisElem = xml.getChild("xAxis");
xAxis[0] = xAxisElem.getFloat("x");
xAxis[1] = xAxisElem.getFloat("y");
println("xaxis: " + xAxis[0] + ", " + xAxis[1]);
XML yAxisElem = xml.getChild("yAxis");
yAxis[0] = yAxisElem.getFloat("x");
yAxis[1] = yAxisElem.getFloat("y");
println("yaxis: " + yAxis[0] + ", " + yAxis[1]);
// apply scaling
xAxis[0] *= graphScale; xAxis[1] *= graphScale;
yAxis[0] *= graphScale; yAxis[1] *= graphScale;
XML originElem = xml.getChild("origin");
origin[0] = originElem.getFloat("x");
origin[1] = originElem.getFloat("y");
println("origin: " + origin[0] + ", " + origin[1]);
//create babies!!!
XML tileXML = xml.getChild("Tiles");
XML[] tileElems = tileXML.getChildren();
for(int i = 0; i < tileElems.length; i++)
{
XML currentTileXML = tileElems[i];
String className = currentTileXML.getName();
BaseGridTile tile = null;
if(className == "BaseGridTile")
{ tile = new BaseGridTile(currentTileXML); }
else if(className == "AnimatedGridTile")
{ tile = new AnimatedGridTile(currentTileXML); }
else if(className == "PNGGridTile")
{ tile = new PNGGridTile(currentTileXML); }
else if(className == "PNGSequenceGridTile")
{ tile = new PNGSequenceGridTile(currentTileXML); }
else if(className == "ProceduralAnimatedGridTile")
{ tile = new ProceduralAnimatedGridTile(currentTileXML); }
else if(className == "StaticProceduralTile")
{ tile = new StaticProceduralTile(currentTileXML); }
else if(className == "AnimatedGridTile")
{ tile = new AnimatedGridTile(currentTileXML); }
else if(className == "RiverTile")
{ tile = new RiverTile(currentTileXML,xAxis,yAxis); }
else if (className == "#text")
{ /*do nothing empty whitespace nodes.*/}
else
{
println("XML: Error! Encountered unknown tile with class: " + className);
println("XML: CONTENTS:\n " + currentTileXML.format(0) + ":ENDCONTENTS");
}
if(tile != null)
{
// tiles.add(new pos(tile.position[0],tile.position[0]),tile);
// xmlTiles.add(tile);
addTile(xmlTiles,tile);
}
}
}
void addTile(ArrayList<BaseGridTile> list, BaseGridTile tile)
{
for(BaseGridTile t : genTiles)
{
if(t.position[0] == tile.position[0] && t.position[1] == tile.position[1])
return;
}
list.add(tile);
for(BaseGridTile t : tile.subTiles)
{
list.add(t);
}
}
void removeTileAt( int x, int y)
{
for(BaseGridTile tile : genTiles)
{
if(tile.position[0] == x && tile.position[1] == y )
{
removeTile(genTiles, tile);
return;
}
}
}
//removes a tile from the grid and its children and its parent
void removeTile(ArrayList<BaseGridTile> list, BaseGridTile tile)
{
if(list.remove(tile))
{
removeTile(list,tile.parentTile);
tile.parentTile = null;
for(BaseGridTile t : tile.subTiles)
{
removeTile(list,t);
}
tile.subTiles.clear();
}
}
}