-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGriddle.pde
150 lines (111 loc) · 4.33 KB
/
Griddle.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
class Griddle
{
PVector pos;
PVector dim;
int quarter_turns;
boolean traversable = false;
ArrayList<NonGriddle> ngs;
PShape sprite;
String template = "";
String type = "Griddle";
GridGameFlowBase game;
Griddle(GridGameFlowBase game) { this.game = game; pos = new PVector(); dim = new PVector(); quarter_turns = 0; traversable = false; ngs = new ArrayList<NonGriddle>(); sprite = null; template = ""; type = "Griddle"; }
void draw()
{
draw(sprite);
}
void draw(PShape s)
{
if (s != null)
{
pushMatrix();
translate(center_center());
rotate(-quarter_turns * HALF_PI);
translate(dim.copy().mult(-0.5f));
scale(dim.x, dim.y);
shape(s);
popMatrix();
}
}
void level_editor_draw()
{
draw();
}
void update()
{
center_ngs();
}
JSONObject serialize()
{
JSONObject o = new JSONObject();
o.setString("type", type);
o.setJSONArray("sprites", new JSONArray(get_spritenames()));
o.setBoolean("traversable", traversable);
o.setInt("quarter_turns", quarter_turns);
o.setString("_template", template);
return o;
}
void deserialize(JSONObject o) { StringList spritenames = getStringList("sprites",o); sprite = globals.sprites.get(spritenames); quarter_turns = o.getInt("quarter_turns", 0); template = o.getString("_template",o.getString("type",type)); }
NonGriddle ng() { if (ngs.isEmpty()) return null; return ngs.get(ngs.size()-1); }
boolean can_accept_ng(NonGriddle n) { return ngs.size() <= 1; }
boolean can_give_ng() { return true; }
boolean receive_ng(NonGriddle ng) { if (!can_accept_ng(ng)) return false; ngs.add(ng); return true; }
void remove_ng(NonGriddle ng) { ngs.remove(ng); }
void remove_ng() { if (!ngs.isEmpty()) ngs.remove(ngs.size() - 1); }
void center_ngs()
{
if (ngs.size() == 1)
ngs.get(0).pos = center_center();
if (ngs.size() <= 1)
return;
float xtw = dim.x * 0.4f;
float xstep = xtw / ngs.size();
float xstart = xtw * -0.5f;
for (int i = 0 ; i < ngs.size(); ++i)
ngs.get(i).pos = center_center().add(xstart + xstep * i,0f);
}
StringList get_spritenames() { StringList retval = new StringList(); if (sprite != null) { if (sprite.getName().equals("_GROUP")) { for (PShape psc : sprite.getChildren()) retval.append(psc.getName()); } else retval.append(sprite.getName()); } return retval; }
void player_interact(Player player) { }
void player_interact_end(Player player) { }
IntVec get_grid_pos() { return game.grid.get_grid_pos_from_object(this); }
PVector top_left() { return pos.copy(); }
PVector top_center() { return pos.copy().add(dim.x * 0.5f, 0f); }
PVector top_right() { return pos.copy().add(dim.x, 0f); }
PVector center_left() { return pos.copy().add(0f, dim.y * 0.5f); }
PVector center_center() { return pos.copy().add(dim.copy().mult(0.5f)); }
PVector center_right() { return pos.copy().add(dim.x, dim.y * 0.5f); }
PVector bottom_left() { return pos.copy().add(0f, dim.y); }
PVector bottom_center() { return pos.copy().add(dim.x * 0.5f, dim.y); }
PVector bottom_right() { return pos.copy().add(dim); }
}
class NullGriddle extends Griddle
{
NullGriddle(GridGameFlowBase game) { super(game); type = "NullGriddle"; }
NullGriddle() { this(null); }
boolean can_accept_ng(NonGriddle n) { return false; }
void draw() { }
void update() { }
}
class EmptyGriddle extends Griddle
{
EmptyGriddle(GridGameFlowBase game) { super(game); traversable = true; type = "EmptyGriddle"; }
boolean can_accept_ng(NonGriddle n) { return ngs.isEmpty(); }
void draw()
{
noFill();
stroke(#000000);
strokeWeight(1f);
rect(pos.x, pos.y, dim.x, dim.y);
}
void update() { if (ng() != null) ng().pos = center_center(); }
}
class WallGriddle extends Griddle
{
WallGriddle() { this(null); }
WallGriddle(GridGameFlowBase game) { super(game); traversable = false; type = "WallGriddle"; }
boolean can_accept_ng(NonGriddle n) { return false; }
}
class PlayerGriddle extends Griddle
{
PlayerGriddle(GridGameFlowBase game) { super(game); type = "PlayerGriddle"; }
}