-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.pde
165 lines (125 loc) · 4.98 KB
/
Grid.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
class Grid
{
int w;
int h;
float edge_width;
PVector pos;
PVector original_pos;
PVector dim;
ArrayList<Griddle> griddles;
ArrayList<GridAlteration> alterations;
Grid(PVector pos, PVector dim, GridGameFlowBase parent) { this(pos, dim, 0, 0, parent); }
Grid(PVector pos, PVector dim, int w, int h, GridGameFlowBase parent)
{
this.pos = pos.copy();
original_pos = pos.copy();
this.dim = dim.copy();
init(w, h, parent);
}
void init(int w, int h, GridGameFlowBase parent)
{
this.w = w;
this.h = h;
edge_width = floor(Math.min(dim.x / w, dim.y / h));
pos = original_pos.copy().add(dim.copy().sub((new PVector(w,h)).mult(edge_width)).mult(0.5f));
griddles = new ArrayList<Griddle>();
alterations = new ArrayList<GridAlteration>();
for (int i = 0; i < w * h; ++i)
{
EmptyGriddle eg = new EmptyGriddle(parent);
eg.pos = (new PVector(i % w, i / w)).mult(edge_width).add(pos);
eg.dim = new PVector(edge_width, edge_width);
griddles.add(eg);
}
}
void deserialize(JSONObject loaded_json, GridGameFlowBase parent)
{
w = loaded_json.getInt("width");
h = loaded_json.getInt("height");
init(w,h, parent);
JSONArray root = loaded_json.getJSONArray("grid");
for (int i = 0; i < root.size(); ++i)
{
JSONObject o = root.getJSONObject(i);
int x = o.getInt("x",-1);
int y = o.getInt("y",-1);
String type = o.getString("type","NullGriddle");
Griddle g = globals.gFactory.create_griddle(type, o, parent);
set(x,y,g);
}
apply_alterations();
}
JSONObject serialize()
{
JSONObject root = new JSONObject();
root.setInt("width", w);
root.setInt("height", h);
JSONArray retval = new JSONArray();
for (Griddle g : griddles)
{
JSONObject o = g.serialize();
IntVec p = get_grid_pos_from_object(g);
o.setInt("x", p.x);
o.setInt("y", p.y);
retval.append(o);
}
root.setJSONArray("grid", retval);
return root;
}
Griddle get(int x, int y) { if (x < 0 || x >= w || y < 0 || y >= h) return new NullGriddle(); return griddles.get(x + y * w); }
Griddle get(PVector point)
{
IntVec c = grid_pos_from_absolute_pos(point);
if (point.x < pos.x || point.y < pos.y || c.x >= w || c.x < 0 || c.y >= h || c.y < 0)
return new NullGriddle();
return get(c.x,c.y);
}
IntVec grid_pos_from_absolute_pos(PVector p) { return new IntVec(p.copy().sub(pos).div(edge_width)); }
PVector absolute_pos_from_grid_pos(IntVec i) { return new PVector(i.x, i.y).mult(edge_width).add(edge_width * 0.5f, edge_width * 0.5f).add(pos); }
Griddle get(IntVec xy) { return get(xy.x, xy.y); }
void set(int x, int y, Griddle newval) { set(new IntVec(x,y), newval); }
void set(IntVec xy, Griddle newval) { alterations.add(new GridAlteration(xy, newval)); }
void draw()
{
noFill();
stroke(#000000);
strokeWeight(1);
rect(pos.x, pos.y, w * edge_width, h * edge_width);
for (Griddle g : griddles)
g.draw();
}
void update(GridGameFlowBase game)
{
for (int i = 0; i < griddles.size(); ++i)
griddles.get(i).update();
apply_alterations();
}
void apply_alterations()
{
for (GridAlteration ga : alterations)
{
if (ga.xy.x < 0 || ga.xy.y < 0 || ga.xy.x >= w || ga.xy.y >= h)
println("Unable to set object in spot " + ga.xy.print());
else
{
ga.newval.pos = (new PVector(ga.xy.x,ga.xy.y)).mult(edge_width).add(pos);
ga.newval.dim = get_square_dim();
griddles.set(ga.xy.x + ga.xy.y * w, ga.newval);
}
}
alterations.clear();
}
PVector get_square_dim() { return new PVector(edge_width, edge_width); }
PVector get_dim() { return (new PVector(w,h)).mult(edge_width); }
PVector get_pos() { return pos.copy(); }
IntVec get_grid_pos_from_object(Griddle g) { int i = griddles.indexOf(g); if (i < 0) { println("That object doesn't exist!"); return null; } return new IntVec(i % w, i / w); }
<T extends Griddle> ArrayList<T> get_all_of_type(Class<T> clazz) { ArrayList<T> retval = new ArrayList<T>(); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { Griddle gg = get(x,y); if (clazz.isInstance(gg)) retval.add((T)gg); } } return retval; }
<T extends Griddle> BitGrid get_map_of_type(Class<T> clazz) { BitGrid retval = new BitGrid(w,h); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { Griddle gg = get(x,y); if (clazz.isInstance(gg)) retval.set_bit(x,y); } } return retval; }
}
class GridAlteration
{
IntVec xy;
Griddle newval;
GridAlteration() { xy = new IntVec(0,0); newval = new NullGriddle(); }
GridAlteration(IntVec xy, Griddle newval) { this.xy = xy.copy(); this.newval = newval; }
}