-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_game.cc
1080 lines (982 loc) · 32 KB
/
scene_game.cc
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "main.hh"
#include "utils.hh"
#include <cstdio>
#include <utility>
#include <vector>
static inline bool seg_intxn(
const vec2 a, const vec2 b,
const vec2 c, const vec2 d
) {
return (c - a).det(b - a) * (d - a).det(b - a) <= 0 &&
(a - c).det(d - c) * (b - c).det(d - c) <= 0;
}
class scene_game : public scene {
public:
// ==== Display-related constants ====
static constexpr float BOARD_W = 20;
static constexpr float BOARD_H = 12;
static constexpr float SCALE = 35;
static rl::Vector2 scr(vec2 p) {
vec2 q = p * SCALE + vec2(W, H) / 2;
return (rl::Vector2){q.x, q.y};
}
static vec2 board(float x, float y) {
return (vec2(x, y) - vec2(W, H) / 2) / SCALE;
}
static const int STEPS = 480;
// ==== Tracks ====
struct track {
vec2 o;
float len; // Total length in units
enum {
ATTRACT = (1 << 0),
RETURN = (1 << 1),
COLLI = ATTRACT | RETURN,
FIXED = (1 << 4),
};
unsigned flags;
bool sel; // Is selected
track() { }
track(vec2 o, float len, unsigned flags)
: o(o), len(len),
flags(flags),
sel(false)
{ }
virtual ~track() { }
// Local position at given phase
virtual vec2 local_at(float t) const = 0;
vec2 at(float t) const { return local_at(t) + o; }
// Phase of nearest point on path, given a local point
// Returns <phase, distance>
virtual std::pair<float, float> local_nearest(vec2 p) const = 0;
std::pair<float, float> nearest(vec2 p) const { return local_nearest(p - o); }
virtual void draw(int T) const = 0;
inline rl::Color tint() const {
rl::Color t = (rl::Color){128, 128, 128, 255};
if (flags & ATTRACT) t = (rl::Color){136, 136, 64, 255};
if (flags & RETURN) t = (rl::Color){160, 96, 216, 255};
if (sel) {
t.r = 255 - (255 - t.r) / 2;
t.g = 255 - (255 - t.g) / 2;
t.b = 255 - (255 - t.b) / 2;
}
/*if (flags & FIXED) {
t.r = t.r * 2/3;
t.g = t.g * 2/3;
t.b = t.b * 2/3;
}*/
return t;
}
inline void ripples(int T, float &dist, float &alpha) const {
#ifndef SHOWCASE
if (flags & RETURN) {
float phase = (float)((T + 450) % 900) / 600;
if (phase < 1) {
dist = (1 - powf(1 - phase, 4)) * 0.26;
alpha = (13 * expf(-4 * phase) * sin(phase) * (1 - phase)) * 0.6;
}
}
if (flags & ATTRACT) {
float phase = (float)(T % 900) / 600;
if (phase < 1) {
dist = powf(1 - phase, 4) * 0.26;
alpha = (19 * expf(-5.9 * phase) * sin(phase) * (1 - phase)) * 0.6;
}
}
#endif
}
};
static inline rl::Color premul_alpha(rl::Color tint, float alpha) {
return (rl::Color){
(unsigned char)(tint.r * alpha),
(unsigned char)(tint.g * alpha),
(unsigned char)(tint.b * alpha),
(unsigned char)(tint.a * alpha),
};
}
struct track_cir : public track {
float r; // Radius
float fix_angle; // Angle at which the fix mark is displayed
int fix_count; // Number of fix marks
track_cir(vec2 o, float r, unsigned flags = 0,
float fix_angle = 0, int fix_count = 2)
: r(r), fix_angle(fix_angle), fix_count(fix_count),
track(o, 2 * M_PI * r, flags)
{ }
vec2 local_at(float t) const { return vec2(r, 0).rot(t / r); }
std::pair<float, float> local_nearest(vec2 p) const {
float a = atan2f(p.y, p.x);
if (a < 0) a += 2 * M_PI;
return {a * r, (p - vec2(r, 0).rot(a)).norm()};
}
void draw(int T) const {
using namespace rl;
float w = (flags & FIXED) ? 2 : 2;
DrawRing(scr(o),
r * SCALE - w / 2, r * SCALE + w / 2,
0, 360, 24 * (r < 1 ? 1 : r), tint());
if (flags & FIXED) {
float angle = fix_angle;
vec2 p = vec2(r, 0).rot(angle);
vec2 move = vec2(0.13, 0).rot(angle - 1.0);
DrawLineEx(scr(o + p - move), scr(o + p + move), 2, tint());
if (fix_count != 1)
DrawLineEx(scr(o - p - move), scr(o - p + move), 2, tint());
}
float dist = 0, alpha = 0;
ripples(T, dist, alpha);
if (alpha > 0) {
DrawRing(scr(o),
(r + dist) * SCALE - w / 2,
(r + dist) * SCALE + w / 2,
0, 360, 48, premul_alpha(tint(), alpha));
if (r > dist) DrawRing(scr(o),
(r - dist) * SCALE - w / 2,
(r - dist) * SCALE + w / 2,
0, 360, 48, premul_alpha(tint(), alpha));
}
}
};
struct track_seg : public track {
vec2 ext; // Extension on both sides
track_seg(vec2 o, vec2 ext, unsigned flags = 0)
: ext(ext / ext.norm()),
track(o, ext.norm() * 2, flags)
{ }
vec2 local_at(float t) const { return ext * (t - len / 2); }
std::pair<float, float> local_nearest(vec2 p) const {
float t = p.dot(ext);
t = (t < -len / 2 ? -len / 2 : (t > len / 2 ? len / 2 : t));
return {t + len / 2, (p - (ext * t)).norm()};
}
void draw(int T) const {
using namespace rl;
DrawLineEx(
scr(o - ext * len / 2), scr(o + ext * len / 2),
2, tint());
vec2 n = (ext / ext.norm()).rot(M_PI / 2);
if (flags & FIXED) {
for (vec2 endpt : {(o - ext * len / 2), (o + ext * len / 2)}) {
DrawLineEx(
scr(endpt - n * 0.1), scr(endpt + n * 0.1),
2, tint());
}
}
float dist = 0, alpha = 0;
ripples(T, dist, alpha);
if (alpha > 0) {
vec2 move = n * dist;
DrawLineEx(
scr(o + move - ext * len / 2), scr(o + move + ext * len / 2),
2, premul_alpha(tint(), alpha));
DrawLineEx(
scr(o - move - ext * len / 2), scr(o - move + ext * len / 2),
2, premul_alpha(tint(), alpha));
}
}
};
// ==== Fireflies ===
struct firefly {
// Position (track + phase)
const track *tr;
float t;
inline vec2 pos() const { return tr->at(t); }
float v; // Velocity
bool sel; // Selected?
static const int TRAIL_N = 20;
static const int TRAIL_I = 8;
vec2 trail[TRAIL_N];
firefly(const track *tr, float t, float v)
: tr(tr), t(t), v(v),
sel(false)
{ }
inline void update(const std::vector<track *> &tracks) {
float t_prev = t;
vec2 p1 = pos();
t += v / STEPS;
if (t >= tr->len) t -= tr->len;
if (t < 0) t += tr->len;
vec2 p2 = pos();
// Attracting tracks
for (const auto tr : tracks) if (tr != this->tr && (tr->flags & track::COLLI)) {
auto near = tr->nearest(p1);
if (near.second >= 0.01) continue;
float t1 = near.first;
float t2 = tr->nearest(p2).first;
if (fabs(t1 - t2) < 1e-6) {
float d = (t1 < 1 ? 1e-6 : (t1 * 1e-6));
t1 -= d;
t2 += d;
}
// Lemma: (p1, p2) crosses the curve C iff
// (p1, p2) crosses (C(t1), C(t2))
if (seg_intxn(p1, p2, tr->at(t1), tr->at(t2))) {
// Point of intersection
if (tr->flags & track::ATTRACT) {
// Move to the new track
this->tr = tr;
// Take the later parameter to avoid recursion
this->t = t2;
// Reverse if making acute turns
if (this->v * (t2 - t1) < 0) this->v = -this->v;
}
if (tr->flags & track::RETURN) {
/*
printf("p1 = (%f, %f)\n", p1.x, p1.y);
printf("p2 = (%f, %f)\n", p2.x, p2.y);
printf("tr->at(t1) = (%f, %f)\n", tr->at(t1).x, tr->at(t1).y);
printf("tr->at(t2) = (%f, %f)\n", tr->at(t2).x, tr->at(t2).y);
*/
this->t = t_prev;
this->v = -this->v;
}
break;
}
}
}
inline void draw(int offs) const {
using namespace rl;
Color tint = (sel ?
(Color){255, 64, 64, 255} :
(Color){255, 255, 16, 255});
float alpha = 1.0/8 * (v < 1 ? 1 : v);
Color fade = (Color){
(unsigned char)(tint.r * alpha),
(unsigned char)(tint.g * alpha),
(unsigned char)(tint.b * alpha),
(unsigned char)(tint.a * alpha)
};
DrawCircleV(scr(pos()), 4, tint);
for (int i = 0; i < TRAIL_N; i++) {
vec2 p = trail[(i + offs) % TRAIL_N];
DrawCircleV(scr(p), 4 - (float)i / TRAIL_N * 2, fade);
}
}
struct trail_manager {
std::vector<firefly> &fireflies;
int counter = 0;
int pointer = 0;
trail_manager(std::vector<firefly> &fireflies)
: fireflies(fireflies),
counter(0), pointer(0)
{ }
void reset() {
counter = pointer = 0;
}
void recalc_init() {
for (auto &f : fireflies) {
for (int i = 0; i < TRAIL_N; i++)
f.trail[i] = f.tr->at(f.t - f.v * (float(TRAIL_I) / STEPS) * i);
}
}
void step() {
if (++counter == TRAIL_I) {
counter = 0;
pointer = (pointer + (TRAIL_N - 1)) % TRAIL_N;
for (firefly &f : fireflies)
f.trail[pointer] = f.pos();
}
}
};
};
// ==== Bellflowers ====
struct bellflower {
vec2 o;
float r;
int c0, c; // Initial count and current count
bellflower(vec2 o, float r, int c0)
: o(o), r(r), c0(c0)
{
reset();
}
virtual ~bellflower() { }
bool last_on;
int since_on, since_off;
virtual void reset() {
last_on = false;
since_on = since_off = 9999;
c = c0;
}
bool update(bool on) { // Returns whether triggered
bool triggered = false;
since_on++;
since_off++;
if (!last_on && on) { c--; since_on = 0; triggered = true; }
if (last_on && !on) since_off = 0;
last_on = on;
return triggered;
}
void update_anim_only() {
since_on++;
since_off++;
}
virtual bool update(const std::vector<firefly> &fireflies) = 0;
virtual void draw1(int finish_anim) const { }
virtual void draw2(int finish_anim) const { }
inline bool fireflies_within(const std::vector<firefly> &fireflies) {
for (const auto f : fireflies)
if ((f.pos() - o).norm() <= r) return true;
return false;
}
inline float tint() const {
float t_on = since_on / 480.0f;
float t_off = since_off / 480.0f;
if (last_on) {
if (t_on >= 0.2) return 1;
return t_on * 5;
} else {
if (t_off >= 0.2) return 0;
return (0.2 - t_off) * 5;
}
}
};
struct bellflower_ord : public bellflower {
bellflower_ord(vec2 o, float r, int c0)
: bellflower(o, r, c0)
{ }
bool update(const std::vector<firefly> &fireflies) {
bool on = fireflies_within(fireflies);
return bellflower::update(on);
}
void draw1(int finish_anim) const {
using namespace rl;
Vector2 cen = scr(o);
float t = tint();
if (finish_anim >= 0) {
float tf = finish_anim / 480.0f;
t *= (tf > 0.5 ? 0 : 1 - sqrtf(tf * 2));
}
Color off = (Color){32, 60, 96, 80};
Color on = (Color){96, 96, 64, 80};
DrawCircleV(cen, r * SCALE, (Color){
(unsigned char)(off.r + (float)(on.r - off.r) * t),
(unsigned char)(off.g + (float)(on.g - off.g) * t),
(unsigned char)(off.b + (float)(on.b - off.b) * t),
80
});
}
void draw2(int finish_anim) const {
using namespace rl;
Vector2 cen = scr(o);
float t = since_on / 960.0f;
float scale = 1;
if (t < 2) scale = 1 + 0.15 * expf(-t) * sinf(t * 8) * (2 - t);
float alpha = 0.85 + (0.15 * tint());
if (c == 0) alpha = 1 - (1 - alpha) * 0.3;
if (finish_anim >= 0) {
float tf = finish_anim / 480.0f;
alpha = alpha + (1 - alpha) * (tf > 0.5 ? 1 : sqrtf(tf * 2));
const float A = 0.3;
const float B = A + 0.3;
const float C = B + 2;
if (tf >= A && tf < B) {
scale *= 0.7 + 0.3 * expf(-(tf - A) * 25) * ((B - tf) / (B - A));
} else if (tf >= B && tf < C) {
float t = tf - B;
scale *= 1.0 - 0.3 * (expf(-20 * t) - expf(-5 * t) * sinf(24 * t)) * ((C - tf) / (C - B));
}
}
painter::image(
c == 0 ? "bellflower_call" : "bellflower_ord",
vec2(cen.x - 42, cen.y - 66 * scale),
vec2(80, 80 * scale),
tint4(alpha, alpha, alpha, alpha));
char s[8];
snprintf(s, sizeof s, "%d", c);
painter::text(s, 32, vec2(cen.x, cen.y + 20), vec2(0.5, 0.5),
tint4(0.8, 0.8, 0.8, 0.6));
}
};
struct bellflower_delay : public bellflower {
int d, d0;
bellflower_delay(vec2 o, float r, int c0, float d0)
: bellflower(o, r, c0), d0(d0 * STEPS)
{ reset(); }
void reset() {
bellflower::reset();
d = d0;
}
bool update(const std::vector<firefly> &fireflies) {
bool on = fireflies_within(fireflies);
if (on) {
if (d > 0) d--;
} else {
d = d0;
}
return bellflower::update(d == 0);
}
void draw1(int finish_anim) const {
using namespace rl;
DrawRing(scr(o), r * SCALE - 1, r * SCALE + 1, 0, 360, 48, (Color){64, 64, 64, 128});
DrawCircleV(scr(o), 0.5 * SCALE, GRAY);
DrawCircleV(scr(o), 0.5 * SCALE * (d0 - d) / d0, GREEN);
char s[8];
snprintf(s, sizeof s, "%d", c);
DrawText(s, scr(o).x - 4, scr(o).y - 8, 16, BLACK);
}
};
// ==== Scene ====
int T; // Update counter. Overflows after 51 days but whatever
struct tutorial {
vec2 pos;
const char *text;
vec2 cir0;
float cir0_radius;
vec2 cir1;
float cir1_radius;
bool allows_interaction;
};
int puzzle_id;
const char *title;
std::vector<track *> tracks;
std::vector<firefly> fireflies, fireflies_init;
std::vector<bellflower *> bellflowers;
std::vector<std::vector<std::pair<firefly *, float>>> ff_links;
std::vector<tutorial> tutorials;
int to_text;
float bellflowers_x_cen; // Used for sounds
firefly::trail_manager trail_m;
int tut_show_start, tut_show_end;
int tut_show_time, tut_hide_time;
firefly *sel_ff;
track *sel_track;
vec2 sel_offs;
int run_state = (8 << 1); // Initial speed 8 steps/update
int finish_timer = -1;
// Scaling factor for render targets
const float RT_SCALE_BASE = 2;
const float RT_SCALE_BLOOM =
#ifdef SHOWCASE
2
#else
1
#endif
;
rl::RenderTexture2D texBloomBase, texBloomStage1, texBloomStage2;
rl::Shader shaderBloom;
int shaderBloomPassLoc;
rl::Shader shaderSpotlight;
int shaderSpotlightCenLoc, shaderSpotlightRadLoc;
static const int BG_TREES_N = 25;
struct {
vec2 pos;
float rot_cen, rot_amp, rot_period;
float tint;
} trees[BG_TREES_N];
button_group buttons;
scene_game(int puzzle_id)
: T(0),
puzzle_id(puzzle_id),
sel_ff(nullptr), sel_track(nullptr),
trail_m(fireflies)
{
using button = button_group::button;
buttons.buttons = {(button){
vec2(10, 10), vec2(60, 60),
nullptr, // Will be filled later
[this]() { this->btn_play(); }
}, (button){
vec2(10, 80), vec2(60, 60),
nullptr,
[this]() { this->btn_speed(); }
}};
update_buttons_images();
to_text = -1;
std::vector<std::vector<int>> links;
switch (puzzle_id) {
#define T_cir new track_cir
#define T_seg new track_seg
#define B_ord new bellflower_ord
#define B_delay new bellflower_delay
#define F(_i, _t, ...) \
firefly(tracks[_i], tracks[_i]->len * (_t), __VA_ARGS__)
#include "puzzles.hh"
}
build_links(links);
float x_sum = 0;
for (auto b : bellflowers) x_sum += b->o.x;
bellflowers_x_cen = x_sum / bellflowers.size();
trail_m.recalc_init();
#ifdef SHOWCASE
tutorials = {};
#endif
tut_show_start = 0;
update_tut_show_range(true);
tut_hide_time = -1;
texBloomBase = rl::LoadRenderTexture(W * RT_SCALE_BASE, H * RT_SCALE_BASE);
rl::SetTextureFilter(texBloomBase.texture, rl::TEXTURE_FILTER_BILINEAR);
rl::SetTextureWrap(texBloomBase.texture, rl::TEXTURE_WRAP_CLAMP);
texBloomStage1 = rl::LoadRenderTexture(W * RT_SCALE_BLOOM, H * RT_SCALE_BLOOM);
rl::SetTextureFilter(texBloomStage1.texture, rl::TEXTURE_FILTER_BILINEAR);
rl::SetTextureWrap(texBloomStage1.texture, rl::TEXTURE_WRAP_CLAMP);
texBloomStage2 = rl::LoadRenderTexture(W * RT_SCALE_BLOOM, H * RT_SCALE_BLOOM);
rl::SetTextureFilter(texBloomStage2.texture, rl::TEXTURE_FILTER_BILINEAR);
rl::SetTextureWrap(texBloomStage2.texture, rl::TEXTURE_WRAP_CLAMP);
#ifdef PLATFORM_WEB
shaderBloom = rl::LoadShader("res/bloom_web.vert", "res/bloom_web.frag");
shaderSpotlight = rl::LoadShader("res/spotlight_web.vert", "res/spotlight_web.frag");
#else
shaderBloom = rl::LoadShader("res/bloom.vert", "res/bloom.frag");
shaderSpotlight = rl::LoadShader("res/spotlight.vert", "res/spotlight.frag");
#endif
shaderBloomPassLoc = rl::GetShaderLocation(shaderBloom, "pass");
shaderSpotlightCenLoc = rl::GetShaderLocation(shaderSpotlight, "spotCen");
shaderSpotlightRadLoc = rl::GetShaderLocation(shaderSpotlight, "spotRadius");
unsigned seed = 20220128;
for (const char *s = title; *s != '\0'; s++)
seed = (seed * 997 + *s);
for (int i = 0; i < BG_TREES_N; i++) {
unsigned rands[5];
for (int j = 0; j < 5; j++) {
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
rands[j] = seed;
}
trees[i] = {
.pos = vec2(rands[0] % W, rands[1] % H),
.rot_cen = (float)rands[2] / (float)0x7fffffff * (float)M_PI * 2,
.rot_amp = 0.05f + (float)rands[3] / (float)0x7fffffff * 0.05f,
.rot_period = 1200 + 1200 * (float)((rands[4] >> 8) % 256) / 256,
.tint = (192 + ((rands[4] >> 16) % 32)) / 255.0f,
};
}
for (int it = 0; it < 1000; it++) {
for (int i = 0; i < BG_TREES_N; i++) {
vec2 move = vec2(0, 0);
for (int j = 0; j < BG_TREES_N; j++) if (j != i) {
vec2 d = (trees[i].pos - trees[j].pos);
if (d.norm() < 240)
move = move + d / d.norm() * (240 - d.norm());
}
trees[i].pos = trees[i].pos + move * 0.01;
if (trees[i].pos.x < 0) trees[i].pos.x /= 2;
if (trees[i].pos.x > W) trees[i].pos.x -= (trees[i].pos.x - W) / 2;
if (trees[i].pos.y < 0) trees[i].pos.y /= 2;
if (trees[i].pos.y > H) trees[i].pos.y -= (trees[i].pos.y - H) / 2;
}
}
}
~scene_game() {
rl::UnloadRenderTexture(texBloomBase);
rl::UnloadRenderTexture(texBloomStage1);
rl::UnloadRenderTexture(texBloomStage2);
rl::UnloadShader(shaderBloom);
rl::UnloadShader(shaderSpotlight);
for (auto t : tracks) delete t;
for (auto b : bellflowers) delete b;
}
inline void build_links(std::vector<std::vector<int>> links) {
ff_links.clear();
ff_links.resize(fireflies.size());
for (const auto group : links) {
for (const auto indep : group) {
auto &list = ff_links[indep];
float t = fireflies[indep].t;
list.reserve(group.size() - 1);
for (const auto dep : group) if (dep != indep) {
bool is_reverse = (fireflies[indep].v * fireflies[dep].v < 0);
float diff = (is_reverse ? fireflies[dep].t + t : fireflies[dep].t - t);
list.push_back({&fireflies[dep], diff});
}
}
}
}
inline bool tut_has_next() const {
return (tut_show_start < tutorials.size() &&
tutorials[tut_show_end - 1].cir0_radius != 0);
}
inline bool tut_allows_interaction() const {
return tutorials[tut_show_end - 1].allows_interaction;
}
inline void update_tut_show_range(bool always = false) {
if (!always && !tut_has_next()) return;
for (tut_show_end = tut_show_start;
tut_show_end < tutorials.size(); tut_show_end++)
if (tutorials[tut_show_end].cir0_radius != 0) {
tut_show_end++;
break;
}
tut_show_time = T;
}
inline std::pair<firefly *, track *> find(const vec2 p) {
// Find the nearest firefly
firefly *best_ff = nullptr;
float best_ff_dist = 2;
for (auto &f : fireflies) {
float dist = (p - f.pos()).norm();
if (dist < best_ff_dist) {
best_ff_dist = dist;
best_ff = &f;
}
}
// Find the nearest firefly track
track *best_track = nullptr;
float best_track_dist = 1.5;
for (const auto t : tracks) if (!(t->flags & track::FIXED)) {
auto result = t->nearest(p);
if (result.second < best_track_dist) {
best_track_dist = result.second;
best_track = t;
}
}
if (best_ff != nullptr && best_track != nullptr) {
if (best_ff_dist > best_track_dist &&
best_ff->tr != best_track)
best_ff = nullptr;
else best_track = nullptr;
}
return {best_ff, best_track};
}
void pton(float x, float y) {
if (tut_has_next() && !tut_allows_interaction()) return;
if (buttons.pton(x, y)) return;
if (run_state & 1) return;
vec2 p = board(x, y);
auto near = find(p);
if (near.first != nullptr) {
sel_ff = near.first;
sel_ff->sel = true;
sel_offs = sel_ff->pos() - p;
}
if (near.second != nullptr) {
sel_track = near.second;
sel_track->sel = true;
sel_offs = sel_track->o - p;
}
}
void ptmove(float x, float y) {
if (tut_has_next() && !tut_allows_interaction()) return;
if (buttons.ptmove(x, y)) return;
vec2 p = board(x, y);
if (sel_ff != nullptr) {
sel_ff->t = sel_ff->tr->nearest(p + sel_offs).first;
// Move linked fireflies
int index = sel_ff - &fireflies[0];
for (const auto link : ff_links[index]) {
link.first->t =
(link.first->v * sel_ff->v < 0) ?
link.second - sel_ff->t :
link.second + sel_ff->t;
}
trail_m.recalc_init();
}
if (sel_track != nullptr) {
sel_track->o = p + sel_offs;
trail_m.recalc_init();
}
}
void ptoff(float x, float y) {
if (tut_has_next() && tut_hide_time == -1)
tut_hide_time = T;
if (buttons.ptoff(x, y)) return;
if (sel_ff != nullptr) {
sel_ff->sel = false;
sel_ff = nullptr;
}
if (sel_track != nullptr) {
sel_track->sel = false;
sel_track = nullptr;
}
}
// Button callbacks
void btn_play() {
if (finish_timer >= 0) return;
run_state ^= 1;
if (run_state & 1) start_run(); else stop_run();
}
void btn_speed() {
if (finish_timer >= 0) return;
if ((run_state >> 1) == 8)
run_state = (32 << 1) | (run_state & 1);
else
run_state = (8 << 1) | (run_state & 1);
update_buttons_images();
}
inline void update_buttons_images() {
buttons.buttons[0].content = ((run_state & 1) ? "#btn_stop" : "#btn_play");
buttons.buttons[1].content = ((run_state >> 1) == 8 ? "#btn_1x" : "#btn_2x");
}
inline void start_run() {
// Save
fireflies_init = fireflies;
update_buttons_images();
}
inline void stop_run() {
fireflies = fireflies_init;
for (auto b : bellflowers) b->reset();
trail_m.reset();
update_buttons_images();
}
bool last_space_down = false;
bool last_tab_down = false;
#ifndef SHOWCASE
#define show_title true
#define show_grid true
#else
bool show_title = false;
bool show_grid = true;
bool last_1_down = false, last_2_down = false;
bool last_left_down = false, last_right_down = false;
#endif
void update() {
T++;
if (finish_timer >= 0) finish_timer++;
if (tut_hide_time >= 0 && T == tut_hide_time + 60) {
tut_hide_time = -1;
tut_show_start = tut_show_end;
update_tut_show_range();
if (tut_show_start < tutorials.size())
sound::play("hint");
}
bool space_down = rl::IsKeyDown(rl::KEY_SPACE);
if (sel_track == nullptr &&
finish_timer == -1 &&
!last_space_down && space_down) {
if (tut_has_next() && tut_hide_time == -1) {
tut_hide_time = T;
} else {
run_state ^= 1;
if (run_state & 1) start_run(); else stop_run();
}
}
last_space_down = space_down;
bool tab_down = rl::IsKeyDown(rl::KEY_TAB);
if (finish_timer == -1 &&
!last_tab_down && tab_down) {
btn_speed();
}
last_tab_down = tab_down;
#ifdef SHOWCASE
bool _1_down = rl::IsKeyDown(rl::KEY_ONE);
if (!last_1_down && _1_down) show_title = !show_title;
last_1_down = _1_down;
bool _2_down = rl::IsKeyDown(rl::KEY_TWO);
if (!last_2_down && _2_down) show_grid = !show_grid;
last_2_down = _2_down;
bool left_down = rl::IsKeyDown(rl::KEY_LEFT);
if (T >= 240 && !last_left_down && left_down) replace_scene(new scene_game(puzzle_id - 1));
last_left_down = left_down;
bool right_down = rl::IsKeyDown(rl::KEY_RIGHT);
if (T >= 240 && !last_right_down && right_down) replace_scene(new scene_game(puzzle_id + 1));
last_right_down = right_down;
#endif
if (run_state & 1) for (int i = 0; i < (run_state >> 1); i++) {
for (auto &f : fireflies) f.update(tracks);
trail_m.step();
if (finish_timer == -1) {
std::vector<float> trigger_ord, trigger_zero;
for (auto b : bellflowers)
if (b->update(fireflies)) {
if (b->c == 0) trigger_zero.push_back(b->o.x);
else trigger_ord.push_back(b->o.x);
}
// Play sounds
if (!trigger_zero.empty()) {
int total_zeros = 0;
bool has_minus = false;
for (auto b : bellflowers)
if (b->c == 0) total_zeros++;
else if (b->c < 0) has_minus = true;
if (!has_minus) {
for (int i = 0; i < trigger_zero.size(); i++) {
sound::play(
sound::bellflower_pop_zero(
total_zeros - trigger_zero.size() + i + 1,
bellflowers.size()),
sound::bellflowers_pan(
trigger_zero[i], bellflowers_x_cen)
);
}
} else {
for (float x : trigger_zero)
sound::play(
"bellflower_pop_zero_0",
sound::bellflowers_pan(x, bellflowers_x_cen)
);
}
}
for (float x : trigger_ord) {
sound::play(
"bellflower_pop_ord",
sound::bellflowers_pan(x, bellflowers_x_cen)
);
}
} else {
// No bellflowers are changed after finish
for (auto b : bellflowers) b->update_anim_only();
}
// Check for finish
if (finish_timer == -1) {
bool finish = true;
for (auto b : bellflowers)
if (b->c != 0) { finish = false; break; }
if (finish) {
finish_timer = 0;
run_state = (8 << 1) | 1; // Back to normal speed
}
}
}
if (finish_timer == 360 + 1.2 * 240 + 20)
sound::play("puzzle_solved");
if (finish_timer == 960) {
if (to_text != -1)
replace_scene(scene_text(to_text));
else
replace_scene(new scene_game(puzzle_id + 1));
}
}
void draw() {
using namespace rl;
ClearBackground((Color){5, 8, 1, 255});
// Background
for (int i = 0; i < BG_TREES_N; i++) {
int id = i % 4;
#ifdef SHOWCASE
int T = 220810;
#endif
float rot = trees[i].rot_cen + trees[i].rot_amp *
sinf((float)T / trees[i].rot_period * M_PI * 2);
float tint = trees[i].tint;
if (finish_timer >= 360 + 1.2 * 240) {
float x = (finish_timer - (360 + 1.2 * 240)) / (0.5 * 240.0f);
if (x > 1) x = 1; else x = 1 - (1 - x) * (1 - x);
tint = tint + (1 - tint) * x * 0.95;
}
painter::image("board_bg",
trees[i].pos,
vec2(240, 240),
vec2(i % 4 * 240, 0),
vec2(240, 240),
vec2(120, 120),
rot,
tint4(tint, tint, tint));
}
// Rule grid
if (show_grid) {
int x_range = (W / 2 / SCALE) + 1;
for (int i = -x_range; i <= x_range; i++) {
float x = scr(vec2(i, 0)).x;
DrawLineV((Vector2){x, 0}, (Vector2){x, H}, (Color){30, 30, 30, 255});
}
int y_range = (H / 2 / SCALE) + 1;
for (int i = -y_range; i <= y_range; i++) {
float y = scr(vec2(0, i)).y;
DrawLineV((Vector2){0, y}, (Vector2){W, y}, (Color){30, 30, 30, 255});
}
}
// Render scaled to texture
BeginBlendMode(BLEND_ADD_COLORS);
Color bg = (Color){0, 0, 0, 0};
BeginTextureMode(texBloomBase);
BeginMode2D((Camera2D){(Vector2){0, 0}, (Vector2){0, 0}, 0, RT_SCALE_BASE});
ClearBackground(bg);
for (const auto t : tracks) t->draw(T);
for (const auto &f : fireflies) f.draw(trail_m.pointer);
EndMode2D();
EndTextureMode();
int pass;
BeginTextureMode(texBloomStage1);
BeginMode2D((Camera2D){(Vector2){0, 0}, (Vector2){0, 0}, 0, RT_SCALE_BLOOM});
pass = 1;
SetShaderValue(shaderBloom, shaderBloomPassLoc, &pass, SHADER_UNIFORM_INT);
BeginShaderMode(shaderBloom);
ClearBackground(bg);
DrawTexturePro(texBloomBase.texture,
(Rectangle){0, 0, W * RT_SCALE_BASE, -H * RT_SCALE_BASE},
(Rectangle){0, 0, W, H},
(Vector2){0, 0}, 0, WHITE);
EndShaderMode();
EndMode2D();
EndTextureMode();
BeginTextureMode(texBloomStage2);
BeginMode2D((Camera2D){(Vector2){0, 0}, (Vector2){0, 0}, 0, RT_SCALE_BLOOM});
pass = 2;
SetShaderValue(shaderBloom, shaderBloomPassLoc, &pass, SHADER_UNIFORM_INT);
BeginShaderMode(shaderBloom);
ClearBackground(bg);
DrawTexturePro(texBloomStage1.texture,
(Rectangle){0, 0, W * RT_SCALE_BLOOM, -H * RT_SCALE_BLOOM},
(Rectangle){0, 0, W, H},
(Vector2){0, 0}, 0, WHITE);
EndShaderMode();
EndMode2D();
EndTextureMode();
EndBlendMode();
int finish_anim = -1;
if (finish_timer >= 360)
finish_anim = finish_timer - 360;
for (const auto b : bellflowers) b->draw1(finish_anim);
DrawTexturePro(texBloomBase.texture,
(Rectangle){0, 0, W * RT_SCALE_BASE, -H * RT_SCALE_BASE},
(Rectangle){0, 0, W, H},