-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial.pde
More file actions
371 lines (291 loc) · 10.1 KB
/
Copy pathTutorial.pde
File metadata and controls
371 lines (291 loc) · 10.1 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
class Tutorial extends GameSession
{
IntDict flags;
ArrayList<TutorialStep> steps;
PVector previous_player_pos;
Tutorial() { flags = new IntDict(); steps = new ArrayList<TutorialStep>(); }
int get_flag(String k) { if (!flags.hasKey(k)) return 0; return flags.get(k); }
void increment_flag(String k) { flags.set(k, get_flag(k) + 1); }
PVector get_toast_window_pos() { return grid.get_pos().add(grid.get_dim().x, 0f); }
PVector get_toast_window_dim() { return new PVector(width,height).sub(get_toast_window_pos()); }
void start_level()
{
if (steps.isEmpty())
{
grid.set(0,0,globals.gFactory.create_griddle("RandomResourcePool",this));
save_path = dataPath(save_filename());
save();
GameSession newgame = new GameSession();
newgame.save_path = save_path;
newgame.rules = rules;
newgame.load();
globals.game.pop();
globals.game.push(newgame);
}
else
super.start_level();
}
void update_flags()
{
//check on the steps
TutorialStep step = steps.get(0);
if (step.variable.startsWith("Key"))
{
String[] chunks = step.variable.split(":");
switch (chunks[0])
{
case "KeyPressed": flags.set(step.variable, globals.keyboard.is_key_pressed (chunks[1].charAt(0)) ? 1 : 0); break;
case "KeyHeld": flags.set(step.variable, globals.keyboard.is_key_held (chunks[1].charAt(0)) ? 1 : 0); break;
case "KeyReleased": flags.set(step.variable, globals.keyboard.is_key_released(chunks[1].charAt(0)) ? 1 : 0); break;
}
}
if (player.pos.dist(previous_player_pos) >= 1f)
flags.set("PlayerSteps", get_flag("PlayerSteps"));
if (step.variable.startsWith("Held"))
{
String[] chunks = step.variable.split(":");
if (chunks.length > 2)
{
if (chunks[1].equals("Player") || chunks[1].equals("player"))
{
if (player.ng != null)
{
flags.set("Held:player:none",0);
flags.set("Held:player:any", 1);
flags.set("Held:player:" + player.ng.name, 1);
//if (chunks[2].equals("any") || chunks[2].equals(player.ng.name))
// flags.set(step.variable, 1);
}
else
{
flags.set("Held:player:any", 0);
flags.set("Held:player:none", 1);
if (!chunks[2].equals("any") && !chunks[2].equals("none"))
flags.set("Held:player:" + chunks[2], 0);
}
}
else
{
String[] coords = chunks[1].split(",");
if (coords.length > 1)
{
int x = parseInt(coords[0]);
int y = parseInt(coords[1]);
Griddle griddy = grid.get(x,y);
if (griddy.ngs.isEmpty())
{
flags.set(step.variable, 0);
flags.set(chunks[0] + ":" + chunks[1] + ":any", 0);
flags.set(chunks[0] + ":" + chunks[1] + ":none", 1);
}
else
{
flags.set("Held:" + x + "," + y + ":none",0);
flags.set("Held:" + x + "," + y + ":any",1);
int num_match = 0;
for (NonGriddle ng : griddy.ngs)
{
if (ng.name.equals(chunks[2]))
++num_match;
}
flags.set(step.variable, num_match);
}
}
}
}
}
}
void check_current_step()
{
update_flags();
TutorialStep step = steps.get(0);
//check if the step is complete
int current_val = get_flag(step.variable);
boolean done = false;
switch (step.operator)
{
case "<" : done = current_val < step.amount; break;
case "<=" : done = current_val <= step.amount; break;
case ">" : done = current_val > step.amount; break;
case ">=" : done = current_val >= step.amount; break;
case "=" : case "==": done = current_val == step.amount; break;
case "!=": case "<>": done = current_val != step.amount; break;
}
if (done)
{
steps.remove(0);
toasts.clear();
if (steps.isEmpty())
{
start_level();
return;
}
toasts.add(new Toast(steps.get(0).description, 100));
}
}
void update()
{
super.update();
RewardGriddle rg = rewards.get(4);
if (rg.running && rg.time_used > rg.time_needed * 0.5f)
rg.time_used = rg.time_needed * 0.5f;
//keep the toasts alive
for (Toast t : toasts)
t.time_used = 0f;
check_current_step();
}
void draw()
{
super.draw();
if (!steps.isEmpty())
{
TutorialStep step = steps.get(0);
if (!step.spots_to_highlight.isEmpty())
{
noFill();
strokeWeight(4f);
for (TutorialStep.TutorialStepHighlight th : step.spots_to_highlight)
{
stroke(th.border_color);
PVector square_dim = grid.get_square_dim();
PVector top_left = grid.absolute_pos_from_grid_pos(th.pos).sub(square_dim.copy().mult(0.5f));
PVector highlight_dim = (th.dim.toPVec().mult(square_dim.x)).add(10f, 10f);
rect(top_left.x - 5f, top_left.y -5f, highlight_dim.x, highlight_dim.y, 5f);
}
}
}
}
void handle_message(Message message) { super.handle_message(message); }
void onFocus(Message message)
{
super.onFocus(message);
}
void load()
{
deserialize(loadJSONObject(save_path));
//state = GameState.PLAYLEVEL;
start_level();
previous_player_pos = player.pos;
toasts.add(new Toast(steps.get(0).description, 100));
}
void deserialize(JSONObject root)
{
super.deserialize(root);
JSONArray jsteps = root.getJSONArray("steps");
for (int i = 0; i < jsteps.size(); ++i)
{
JSONObject step = jsteps.getJSONObject(i);
TutorialStep ts = new TutorialStep(step);
int order = step.getInt("order",i);
if (order == i)
steps.add(ts);
else
{
for (int ii = steps.size(); ii <= order; ++ii)
steps.add(new TutorialStep());
steps.set(order, ts);
}
}
}
void register_ng(NonGriddle ng) { super.register_ng(ng); increment_flag("Resource:"+ng.name); }
void lose_level() { }
void win_level()
{
super.win_level();
flags.set("Won",1);
}
class TutorialStep
{
String description;
String variable;
ArrayList<TutorialStepHighlight> spots_to_highlight = new ArrayList<TutorialStepHighlight>();
String operator; //this should be deserialized into an enum, similar to the Rule Modifiers, but legibility is far more important here than performance since it's just the tutorial.
int amount;
TutorialStep(String description, String variable, String operator, int amount, ArrayList<TutorialStepHighlight> spots_to_highlight) { this.description = description; this.variable = variable; this.operator = operator; this.amount = amount; this.spots_to_highlight = spots_to_highlight; }
TutorialStep() { this("","","",0, new ArrayList<TutorialStepHighlight>()); }
TutorialStep(String description, String expression) { this(description,expression, new ArrayList<TutorialStepHighlight>()); }
TutorialStep(String description, String expression, ArrayList<TutorialStepHighlight> spots_to_highlight)
{
String[] chunks = split_respecting_quoted_whitespace(expression);
if (chunks.length == 1)
{
this.variable = chunks[0];
this.operator = ">";
this.amount = 0;
}
else if (chunks.length == 3)
{
this.variable = chunks[0];
this.operator = chunks[1];
this.amount = parseInt(chunks[2]);
}
else
println("Error: Tutorial Step expression cannot be parsed. Value '" + expression + "' with description '" + description);
this.description = description;
this.spots_to_highlight = spots_to_highlight;
}
TutorialStep(JSONObject o)
{
this(o.getString("description",""), o.getString("expression"));
if (o.hasKey("highlight"))
{
Object mo = o.get("highlight");
if (mo instanceof JSONArray)
{
JSONArray moa = (JSONArray)mo;
for (int i = 0; i < moa.size(); ++i)
spots_to_highlight.add(new TutorialStepHighlight(moa.getJSONObject(i)));
}
else if (mo instanceof JSONObject)
spots_to_highlight.add(new TutorialStepHighlight((JSONObject)mo));
}
}
class TutorialStepHighlight
{
IntVec pos;
IntVec dim;
color border_color;
TutorialStepHighlight(IntVec pos, IntVec dim, color border_color)
{
this.pos = pos.copy();
this.dim = dim.copy();
this.border_color = border_color;
}
TutorialStepHighlight(JSONObject jo)
{
int x = 0;
int y = 0;
int w = 1;
int h = 1;
border_color = color(255,0,0,180);
if (jo.hasKey("pos"))
{
JSONObject jjo = jo.getJSONObject("pos");
x = jjo.getInt("x");
y = jjo.getInt("y");
}
if (jo.hasKey("x"))
x = jo.getInt("x");
if (jo.hasKey("y"))
y = jo.getInt("y");
if (jo.hasKey("dim"))
{
JSONObject jjo = jo.getJSONObject("dim");
w = jjo.getInt("w", jjo.getInt("x"));
h = jjo.getInt("h", jjo.getInt("y"));
}
if (jo.hasKey("w"))
w = jo.getInt("w");
if (jo.hasKey("h"))
h = jo.getInt("h");
if (jo.hasKey("color") || jo.hasKey("border_color"))
{
int icol = unhex(jo.getString("color", jo.getString("border_color")));
border_color = color(red(icol), green(icol), blue(icol), 180);
}
pos = new IntVec(x,y);
dim = new IntVec(w,h);
}
}
}
}