-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjitterbug.pde
348 lines (281 loc) · 7.36 KB
/
jitterbug.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
int maximum_cycle = 1000;
int minimum_cycle = 250;
class jitterbug
{
boolean player_must_pump = false;
jitterbug()
{
pulse_milli = millis() + map(overall_state, 0,1.0,minimum_cycle,maximum_cycle);
}
int jitter_steps = 15;
float jitter_range = 2;
int jitter_current = 0;
float jitter_x_last;
float jitter_y_last;
float jitter_x;
float jitter_y;
// We pump the marker into position by getting the pulse right:
float marker_position = 50;
float displayed_marker = 50;
// How out of place it is adds to the panic bar behind it.
float overall_state = 1.0;
float state_pregradient = 1.0;
void get_rekt(float amount)
{
state_pregradient = constrain(state_pregradient - amount, 0, 1.0);
}
void calc_jitter()
{
if (jitter_current == jitter_steps)
{
jitter_x_last = jitter_x;
jitter_y_last = jitter_y;
jitter_current = 0;
recalc_calm();
}
else
{
jitter_current++;
}
}
// Recalculate Calm is called whenever we reach the end of our jitter cycle. It is called internally
private void recalc_calm()
{
// Tell the UI to recalculate the amount of jitter to apply to the cursor.
theUI.calc_jitter();
float next_calm = overall_state;
pulse_nextmax = int(map(next_calm, 0,1.0,minimum_cycle,maximum_cycle));
jitter_range = map(next_calm, 0.0,0.9, 12,0);
if (jitter_range < 0)
jitter_range = 0;
jitter_steps = int(map(next_calm, 0.0,1.0, 1,15));
jitter_x = random(jitter_range) - jitter_range / 2;
jitter_y = random(jitter_range) - jitter_range / 2;
}
float apply_jitter_x(float x)
{
return x + lerp(jitter_x_last,jitter_x,jitter_current / float(jitter_steps));
}
float apply_jitter_y(float y)
{
return y + lerp(jitter_y_last,jitter_y,jitter_current / float(jitter_steps));
}
boolean pulse = false;
int pulse_progress = 0;
int pulse_nextmax = maximum_cycle;
int pulse_max = maximum_cycle;
float pulse_milli = 0;
float sound_milli = 0;
float calm_start = -1;
float release_start = -1;
float hold_timer = 0.0;
float hold_total;
float release_total;
float ideal_beat_total = 2000;
void reset_pulse()
{
ideal_beat_total = map(overall_state, 0,1.0,minimum_cycle,maximum_cycle);
pulse_milli = millis() + ideal_beat_total;
sound_milli = pulse_milli + breath_ms_delay;
theUI.SetTrackerPulseTime(pulse_milli,ideal_beat_total);
}
float get_current_beat_cycle()
{
return map(overall_state, 0,1.0,minimum_cycle,maximum_cycle);
}
int things_done;
void assess_input_score()
{
things_done += 2;
float total_delta = hold_total + release_total;
//println("delta: " + total_delta);
//println("% held: " + (hold_total/total_delta));
// we need to work out how even the breathe was
// and we need to work out how close it is to the correct cycle of time
float wrong_delta = (0.5 - (hold_total / total_delta)) / 0.5;
// wrong delta now stores how wrong we were for the delta from -1 to 1
float score = 1 - map(total_delta,0,2 * ideal_beat_total,0.0,1.0);
float score2 = wrong_delta * wrong_delta;
//println(score2);
// delta needs to be more forgiving
if (abs(score2) > abs(score))
{
score = score2;
}
// TODO to integrage % held here somehow
// numbers below 1 will indicate that the beat was too fast, and numbers above 1 will indicate that the beat was too slow
// we want hyper ventilating to go up, so, we do this:
float new_marker_position = 50 * (constrain(1 + score,0.0,2.0));
float difference = constrain((new_marker_position - marker_position),-25,25);
marker_position = marker_position + difference;
pump_bar();
}
void pump_bar()
{
// this is called every time we pump c or fail to pump c
make_sound.breath_loudness(overall_state);
}
float get_state()
{
return overall_state;
}
void apply_bar_math()
{
if (system_state)
{
// this is called every frame
float difference = abs(marker_position - 50) / 50;
// difference will be a value from 0 to 1 based on how wrong we are
// we want to get better if we're within 0.1
float tick = 1.0/60;
//println(difference);
float modifier = 0;
if (difference < 0.2)
{
modifier = 0.1 * tick * constrain(difference,0.3,1.0);
}
else if (difference < 0.4)
{
// we want to get worse but with a cap
if (overall_state > 0.8)
{
modifier = - 0.1 * tick;
}
else if (overall_state < 0.4)
{
modifier = 0.05 * tick;
}
}
else if (difference < 0.6)
{
if (overall_state > 0.4)
{
modifier = -0.06 * tick;
}
else
{
modifier = 0.05 * tick;
}
}
else if (difference < 0.8)
{
modifier = -0.1 * tick;
}
else
{
modifier = -0.2 * tick;
}
// gradient effect where we move along a curve
state_pregradient += modifier;
}
state_pregradient = constrain(state_pregradient,0.0,1.0);
overall_state = sin(state_pregradient * 0.5 * PI);
overall_state = constrain(overall_state,0,1.0);
pump_bar();
}
void check_did_anything()
{
if (things_done > 2 && punish_inaction)
{
// we're ok this turn
things_done = 2;
}
else if (things_done == 0)
{
// we should be punished
marker_position = 0;
pump_bar();
}
else if (things_done >= 0 && punish_inaction)
{
// weve skipped a beat, but, we could easily get stiffed on this so we have this one beat window each time.
if (things_done == 1)
{
marker_position = 25;
// prevent perfect action making things worse, but not provide too much of an advantage:
release_start += 3 * ideal_beat_total;
}
if (things_done > 0)
things_done--;
}
}
boolean punish_inaction = false;
void inaction_on()
{
punish_inaction = true;
things_done = 3;
}
void inaction_off()
{
punish_inaction = false;
}
boolean system_state = false;
void anxiety(boolean state)
{
system_state = state;
}
void MoveMarker(int value)
{
marker_position = value;
}
void TakeInput(inputblob i)
{
// This is also called every frame so we should make use of it
// Take input first so we give them the benefit of an extra frame to see it coming I guess
if (system_state)
{
if (i.c_down)
{
player_must_pump = true;
release_total = millis() - release_start;
if (release_start > 0)
{
//println("Released time: " + hold_total);
assess_input_score();
}
calm_start = millis();
//println("Calm start: " + calm_start);
}
if (i.c_state > 0)
{
hold_timer = millis();
}
else if (calm_start > 0)
{
//println("Held end: " + hold_timer);
hold_total = hold_timer - calm_start;
//println("Held time: " + hold_total);
calm_start = -1;
release_start = millis();
}
}
apply_bar_math();
theUI.set_buildup(overall_state);
marker_position = constrain(marker_position,0,100);
displayed_marker += (marker_position - displayed_marker) * 0.1;
theUI.SetMarker(map(displayed_marker,0,100,0.0,2.0));
boolean text_box_forgive = player_must_pump;
//println("player must pump? " + str(player_must_pump));
if (texter != null)
{
text_box_forgive = false;
//println("but forgiven!");
things_done = 5;
}
if (millis() > sound_milli)
{
sound_milli += 99999;
theUI.pulse_sound(pulse);
}
if (millis() > pulse_milli)
{
reset_pulse();
pulse = !pulse;
theUI.pulse(pulse);
if (pulse == false && text_box_forgive)
{
check_did_anything();
}
}
}
}