-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.v
1389 lines (1273 loc) · 34.5 KB
/
app.v
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
// Copyright(C) 2023 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
import os
import rand
import shy.lib as shy
import shy.embed
import shy.particle
import shy.easy
import shy.mth
enum Mode {
menu
game
options
}
enum GameMode {
relaxed
time
}
fn game_mode_from_string(s string) GameMode {
return match s {
'time' {
.time
}
else {
.relaxed
}
}
}
@[heap]
pub struct App {
embed.ExampleApp
mut:
settings &UserSettings = &UserSettings{}
mode Mode //= .game // nice if you're debugging game play
puzzle &Puzzle = shy.null
image_selector &ImageSelector = shy.null
dim_selector &DimensionSelector = shy.null
start_button &MenuButton = shy.null
back_button &BackButton = shy.null
options_button &OptionsButton = shy.null
is_starting bool
ps &easy.ParticleSystem = shy.null
//
sfx map[string]SFXInfo
music map[string]Music
cur_music string
hovered_pieces []Piece // To avoid re-allocating a new array every event
toasts []Toast
//
save_settings_next bool
is_settings_loaded bool // for stupid wasm32_emscripten async filesystem
//
game_start_time u64
game_end_time u64
current_image_best_solve Solve // to show best solve time for current image + puzzle dimension
last_solve Solve
best_solve Solve
//
version_full string = version_full()
}
@[markused]
pub fn (mut a App) shutdown() ! {
a.save_settings() or { eprintln('Saving settings failed: ${err}') }
a.ExampleApp.shutdown()!
}
fn (mut a App) set_mode(mode Mode) {
from := a.mode
to := mode
if to == .menu {
if from == .options {
a.save_settings() or {
eprintln('Saving settings failed: ${err}')
a.show_toast(Toast{
text: 'Saving settings failed'
duration: 2.5
})
}
}
if from == .game {
a.puzzle.grabbed = 0
}
}
a.mode = to
}
pub fn (mut a App) update_canvas() {
// For playing around with fake "high DPI"
/*
fake := f32(2)
a.set_canvas(shy.Canvas{
...a.window.canvas()
factor: fake
factor_x: fake
factor_y: fake
})
*/
}
@[markused]
pub fn (mut a App) init() ! {
a.ExampleApp.init()!
a.update_canvas()
a.window.set_title('Puzzle Vibes')
$if wasm32_emscripten {
println('Welcome to Puzzle Vibes!\nUse your mouse to interact with the game and to start the music.\nEnjoy!')
}
icon := $embed_file('assets/images/icon_128x128.png')
a.window.set_icon(icon)!
a.ps = a.easy.new_particle_system(
width: a.canvas().width
height: a.canvas().height
pool: 2500
)
design_factor := f32(1440) / a.canvas().width
scale := f32(2.0) * 1 / design_factor
a.ps.add(particle.Emitter{
enabled: false // true
rate: 50
position: shy.vec2[f32](shy.half * a.canvas().width, shy.half * a.canvas().height)
velocity: particle.AngleDirection{
angle: 0
angle_variation: 360
magnitude: 15
magnitude_variation: 1
}
acceleration: particle.AngleDirection{
angle: 0
angle_variation: 360
magnitude: -20
magnitude_variation: 2
}
start_size: shy.vec2[f32](30.0 * scale, 35 * scale)
size_variation: shy.vec2[f32](10.0 * scale, 10 * scale)
life_time: 500 //* (1 / design_factor)
life_time_variation: 500 //* (1 / design_factor)
movement_velocity: 20 * (1 / design_factor)
})
a.ps.replace_default_painter(a.easy.image_particle_painter(
color: shy.rgba(255, 255, 255, 100)
))
a.quick.load(shy.ImageOptions{
resize: a.canvas().factor
source: a.asset(default_image)
min_filter: .nearest
mag_filter: .nearest
})!
mut puzzle_images := []ImageSelectorEntry{}
puzzle_images << ImageSelectorEntry{
name: 'Pixel Art Ruins'
source: a.asset(default_image)
}
for e in image_db {
image := a.asset(os.join_path('images', e.file))
entry := ImageSelectorEntry{
name: e.name
source: image
}
if entry !in puzzle_images {
filter := if e.file.starts_with('pixelated_') {
shy.ImageFilter.nearest
} else {
shy.ImageFilter.linear
}
a.quick.load(shy.ImageOptions{
resize: a.canvas().factor
source: image
min_filter: filter
mag_filter: filter
})!
puzzle_images << entry
}
}
a.quick.load(shy.ImageOptions{
resize: a.canvas().factor
source: a.asset('images/puzzle_vibes_logo.png')
})!
a.quick.load(shy.ImageOptions{
resize: a.canvas().factor
source: a.asset('images/seamless_wooden_texture.jpg')
wrap_u: .repeat
wrap_v: .repeat
})!
// mut asset := a.easy.load(source: a.asset('music/River Meditation.mp3'))!
// a.music['River Meditation'] = asset.to[shy.Sound](shy.SoundOptions{})!
for e in music_db {
music := a.asset(os.join_path('music', e.file))
mut asset := a.easy.load(source: music)!
mut sound := asset.to[shy.Sound](shy.SoundOptions{})!
sound.on_end = fn (sound shy.Sound) {
// println('sound id: ${sound.id} ended')
mut app := sound.asset.shy.app[App]()
// println('Restarting River Meditation')
app.play_random_music()
}
/*
sound.on_start = fn (sound shy.Sound) {
app := sound.asset.shy.app[App]()
// println('Restarting River Meditation')
app.play_random_music()
}*/
a.music[e.name] = Music{
info: e
sound: sound
}
}
a.play_music('River Meditation')
// a.music['River Meditation'].on_start = fn (sound shy.Sound) {
// println('River Meditation started sound id: ${sound.id}')
// }
// a.music['River Meditation'].on_pause = fn (sound shy.Sound, paused bool) {
// println('River Meditation paused:${paused} sound id: ${sound.id}')
// }
// a.music['River Meditation'].on_end = fn (sound shy.Sound) {
// println('River Meditation ended sound id: ${sound.id}')
// app := sound.asset.shy.app[App]()
// println('Restarting River Meditation')
// app.music['River Meditation'].play()
// }
// a.music['River Meditation'].play()
img, _ := a.assets.get[shy.Image](a.asset(default_image))
for e in sfx_db {
sfx := a.asset(os.join_path('sfx', e.file))
a.sfx[e.name] = e
a.quick.load(shy.SoundOptions{
source: sfx
max_repeats: 3
})!
}
viewport := a.canvas().rect()
a.puzzle = new_puzzle(
app: a
viewport: viewport
image: img
)!
a.bind_button_handlers()!
// Menu
a.start_button = &MenuButton{
app: a
label: 'START'
on_clicked: fn (mut a App) bool {
a.start_game() or { panic(err) }
a.shy.once(fn (t ­.Timer) {
mut a := t.shy.app[App]()
// println('${a.mode} -> .game')
a.start_button.scale = 1
a.set_mode(.game)
}, 150)
return true
}
on_pressed: fn (mut a App) bool {
a.start_button.scale = 0.98
a.play_sfx('Squish')
return false
}
on_leave: fn (mut a App) bool {
a.start_button.scale = 1
return false
}
}
// Game
a.back_button = &BackButton{
app: a
label: 'QUIT'
on_clicked: fn (mut a App) bool {
if a.mode == .menu {
$if wasm32_emscripten {
a.window.toggle_fullscreen()
} $else {
mut events := a.shy.events()
events.send(shy.QuitEvent{
timestamp: a.shy.ticks()
window_id: a.window.id
request: true
}) or {}
}
} else {
if a.mode == .options {
a.settings.dimensions = a.dim_selector.dim
a.update_current_image_best_solve()
}
a.shy.once(fn (t ­.Timer) {
mut a := t.shy.app[App]()
// println('${a.mode} -> .game')
a.back_button.scale = 1
a.set_mode(.menu)
}, 150)
}
return true
}
on_pressed: fn (mut a App) bool {
a.play_sfx('Squish')
a.back_button.scale = 0.98
return false
}
on_leave: fn (mut a App) bool {
a.back_button.scale = 1
return false
}
}
$if wasm32_emscripten {
a.back_button.label = if a.window.is_fullscreen() {
'BACK'
} else {
'FULLSCREEN'
}
}
a.options_button = &OptionsButton{
app: a
label: 'OPTIONS'
on_clicked: fn (mut a App) bool {
if a.mode == .menu {
a.shy.once(fn (t ­.Timer) {
mut a := t.shy.app[App]()
// println('${a.mode} -> .options')
a.options_button.scale = 1
a.dim_selector.dim = a.settings.dimensions
a.set_mode(.options)
}, 150)
return true
}
return false
}
on_pressed: fn (mut a App) bool {
a.play_sfx('Squish')
a.options_button.scale = 0.98
return false
}
on_leave: fn (mut a App) bool {
a.options_button.scale = 1
return false
}
}
a.image_selector = &ImageSelector{
app: a
label: 'Select a puzzle'
images: puzzle_images
on_clicked: fn (mut a App) bool {
if a.mode == .menu {
ims_rect := a.image_selector.window_de_origin_rect()
left_side := shy.Rect{
x: ims_rect.x
y: ims_rect.y
width: shy.half * ims_rect.width
height: ims_rect.height
}
// right_side := shy.Rect{
// x: ims_rect.x + shy.half * ims_rect.width
// y: ims_rect.y
// width:shy.half * ims_rect.width
// height: ims_rect.height
// }
// Close button click
if image := a.image_selector.get_selected_image() {
ims_rect_c := a.image_selector.window_rect()
if image.removable {
margin := ims_rect_c.height * 0.1
radius := (mth.min(ims_rect_c.width, ims_rect_c.height) * 0.05) * 2
scale := f32(0.95)
close_center_x := (ims_rect_c.x + (shy.half * ims_rect_c.width * scale) - margin)
close_center_y := (ims_rect_c.y - (shy.half * ims_rect_c.height * scale) +
margin)
remove_area := shy.Rect{
x: close_center_x
y: close_center_y
width: radius
height: radius
}.displaced_from(shy.Anchor.center)
if remove_area.contains(a.mouse.x, a.mouse.y) {
a.image_selector.remove_selected_image()
a.remove_user_image(image.source.str())
a.save_settings_when_time_permits()
a.show_toast(Toast{
text: 'Removed "${image.name}"'
duration: 1.5
})
return true
}
}
}
if left_side.contains(a.mouse.x, a.mouse.y) {
a.select_prev_image()
} else {
a.select_next_image()
}
return true
}
return false
}
}
a.dim_selector = &DimensionSelector{
app: a
dim: shy.size(3, 3)
on_pressed: fn (mut a App) bool {
if a.mode == .options {
if cell := a.dim_selector.to_cell(shy.vec2[f32](a.mouse.x, a.mouse.y)) {
a.dim_selector.dim = shy.Size{
width: cell.x
height: cell.y
}
a.dim_selector.label = '${a.dim_selector.dim.width:.0f}x${a.dim_selector.dim.height:.0f} Puzzle, ${int(a.dim_selector.dim.area())} pieces'
a.play_sfx('Squish')
// println(cell)
}
}
return false
}
}
// We load the image here since we need valid references to image_selector and dim_selector
a.load_settings()!
$if !wasm32_emscripten {
// See variable_update/1 for wasm32_emscripten workaround for slow/async JS filesystem
a.is_settings_loaded = true
}
a.dim_selector.label = '${a.settings.dimensions.width:.0f}x${a.settings.dimensions.height:.0f} Puzzle, ${int(a.settings.dimensions.area())} pieces'
a.on_resize()
a.update_current_image_best_solve()
}
pub fn (mut a App) update_current_image_best_solve() {
if iss := a.image_selector.get_selected_image() {
img, _ := a.assets.get[shy.Image](iss.source) // TODO: this can be avoided by doing what to_image_id does directly with `iss.source`...
a.current_image_best_solve = a.get_solve(a.to_image_id(img), a.settings.dimensions)
}
}
pub fn (mut a App) bind_button_handlers() ! {
a.mouse.on_button_down(fn (s ­.Shy, mbe shy.MouseButtonEvent) bool {
if mbe.button != .left {
return false
}
mut a := s.app[App]()
mouse := a.mouse
mut handled := false
mut bb := a.back_button
mut area := bb.Button.window_rect()
mut mouse_area := area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
bb.click_started = true
if bb.on_pressed != unsafe { nil } {
return bb.on_pressed(mut a)
}
}
if a.mode == .menu {
mut mb := a.start_button
area = mb.Button.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
mb.click_started = true
if mb.on_pressed != unsafe { nil } {
return mb.on_pressed(mut a)
}
}
mut ob := a.options_button
area = ob.Button.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
ob.click_started = true
if ob.on_pressed != unsafe { nil } {
return ob.on_pressed(mut a)
}
}
mut ims := a.image_selector
area = ims.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
// println(imse.clicks)
ims.click_started = true
if ims.on_pressed != unsafe { nil } {
return ims.on_pressed(mut a)
}
}
}
if a.mode == .options {
mut dims := a.dim_selector
area = dims.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
// println(imse.clicks)
dims.click_started = true
if dims.on_pressed != unsafe { nil } {
return dims.on_pressed(mut a)
}
}
}
return handled
})
a.mouse.on_motion(fn (s ­.Shy, mme shy.MouseMotionEvent) bool {
mut a := s.app[App]()
mouse := a.mouse
mut handled := false
mut bb := a.back_button
mut area := bb.Button.window_rect()
mut mouse_area := area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
bb.is_hovered = true
if bb.on_hovered != unsafe { nil } {
handled = bb.on_hovered(mut a)
}
} else {
bb.is_hovered = false
if bb.on_leave != unsafe { nil } {
handled = bb.on_leave(mut a)
}
}
if handled {
return handled
}
if a.mode == .menu {
mut mb := a.start_button
area = mb.Button.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
mb.is_hovered = true
if mb.on_hovered != unsafe { nil } {
handled = mb.on_hovered(mut a)
}
} else {
mb.is_hovered = false
if mb.on_leave != unsafe { nil } {
handled = mb.on_leave(mut a)
}
}
mut ob := a.options_button
area = ob.Button.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
ob.is_hovered = true
if ob.on_hovered != unsafe { nil } {
handled = ob.on_hovered(mut a)
}
} else {
ob.is_hovered = false
if ob.on_leave != unsafe { nil } {
handled = ob.on_leave(mut a)
}
}
mut ims := a.image_selector
area = ims.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
ims.is_hovered = true
if ims.on_hovered != unsafe { nil } {
handled = ims.on_hovered(mut a)
}
} else {
ims.is_hovered = false
if ims.on_leave != unsafe { nil } {
handled = ims.on_leave(mut a)
}
}
}
if a.mode == .options {
mut dims := a.dim_selector
area = dims.window_rect()
mouse_area = area.displaced_from(shy.Anchor.center)
if mouse_area.contains(mouse.x, mouse.y) {
dims.is_hovered = true
if dims.on_hovered != unsafe { nil } {
handled = dims.on_hovered(mut a)
}
} else {
dims.is_hovered = false
if dims.on_leave != unsafe { nil } {
handled = dims.on_leave(mut a)
}
}
}
return handled
})
a.mouse.on_button_click(fn (s ­.Shy, mbe shy.MouseButtonEvent) bool {
if mbe.button != .left {
return false
}
mut a := s.app[App]()
mouse := a.mouse
mut handled := false
mut bb := a.back_button
mut was_started := bb.click_started
bb.click_started = false
mut area := bb.Button.window_rect()
mut mouse_area := area.displaced_from(shy.Anchor.center)
if was_started && mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
if bb.on_clicked != unsafe { nil } {
handled = bb.on_clicked(mut a)
}
} else {
bb.is_hovered = false
if bb.on_leave != unsafe { nil } {
handled = bb.on_leave(mut a)
}
}
if handled {
return handled
}
mut ob_mouse_area := shy.Rect{}
mut mb_mouse_area := shy.Rect{}
mut ims_mouse_area := shy.Rect{}
mut dims_mouse_area := shy.Rect{}
if a.mode == .menu {
mut mb := a.start_button
was_started = mb.click_started
mb.click_started = false
area = mb.Button.window_rect()
mb_mouse_area = area.displaced_from(shy.Anchor.center)
if was_started && mb_mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
if mb.on_clicked != unsafe { nil } {
handled = mb.on_clicked(mut a)
}
}
mut ob := a.options_button
was_started = ob.click_started
ob.click_started = false
area = ob.Button.window_rect()
ob_mouse_area = area.displaced_from(shy.Anchor.center)
if was_started && ob_mouse_area.contains(mouse.x, mouse.y) {
// println(mbe.clicks)
if ob.on_clicked != unsafe { nil } {
handled = ob.on_clicked(mut a)
}
}
mut ims := a.image_selector
was_started = ims.click_started
ims.click_started = false
area = ims.window_rect()
ims_mouse_area = area.displaced_from(shy.Anchor.center)
if was_started && ims_mouse_area.contains(mouse.x, mouse.y) {
// println(imse.clicks)
if ims.on_clicked != unsafe { nil } {
handled = ims.on_clicked(mut a)
}
}
}
if a.mode == .options {
mut dims := a.dim_selector
was_started = dims.click_started
dims.click_started = false
area = dims.window_rect()
dims_mouse_area = area.displaced_from(shy.Anchor.center)
if was_started && dims_mouse_area.contains(mouse.x, mouse.y) {
// println(imse.clicks)
if dims.on_clicked != unsafe { nil } {
handled = dims.on_clicked(mut a)
}
}
}
if !(was_started && mb_mouse_area.contains(mouse.x, mouse.y)) {
mut mb := a.start_button
mb.is_hovered = false
if mb.on_leave != unsafe { nil } {
handled = mb.on_leave(mut a)
}
}
if !(was_started && ob_mouse_area.contains(mouse.x, mouse.y)) {
mut ob := a.options_button
ob.is_hovered = false
if ob.on_leave != unsafe { nil } {
handled = ob.on_leave(mut a)
}
}
if !(was_started && ims_mouse_area.contains(mouse.x, mouse.y)) {
mut ims := a.image_selector
ims.is_hovered = false
if ims.on_leave != unsafe { nil } {
handled = ims.on_leave(mut a)
}
}
if !(was_started && dims_mouse_area.contains(mouse.x, mouse.y)) {
mut dims := a.dim_selector
dims.is_hovered = false
if dims.on_leave != unsafe { nil } {
handled = dims.on_leave(mut a)
}
}
return handled
})
// A click anywhere when the puzzle is solved goes back to menu
a.mouse.on_button_click(fn (s ­.Shy, mbe shy.MouseButtonEvent) bool {
mut a := s.app[App]()
if a.mode != .game {
return false
}
if mbe.button != .left {
return false
}
if a.puzzle.solved {
// println('${a.mode} -> .game')
a.set_mode(.menu)
return true
}
return false
})
}
pub fn (mut a App) on_resize() {
// Placement of Start button
a.update_canvas()
draw_canvas := a.canvas()
a.start_button.Button.Rect = shy.Rect{
x: shy.half * draw_canvas.width
y: 0.87 * draw_canvas.height //+ (draw_canvas.height * 0.15)
width: 0.12 * draw_canvas.width
height: 0.05 * draw_canvas.width
}
a.image_selector.Rect = shy.Rect{
x: shy.half * draw_canvas.width
y: shy.half * draw_canvas.height + (draw_canvas.height * 0.03)
width: 0.4 * draw_canvas.width
height: 0.2 * draw_canvas.width
}
a.dim_selector.Rect = shy.Rect{
x: shy.half * draw_canvas.width
y: shy.half * draw_canvas.height - (draw_canvas.height * 0.05)
width: 0.3 * draw_canvas.width
height: 0.18 * draw_canvas.width
}
if a.image_selector.images.len > 0 {
mut emitters := a.ps.emitters()
design_factor := f32(1440) / a.canvas().width
scale := f32(2.0) * (1 / design_factor)
for mut em in emitters {
// TODO this is stupid
// We're adjusting values so effects look a-like in different screen-widths...
em.start_size = shy.vec2[f32](30.0 * scale, 35 * scale)
em.size_variation = shy.vec2[f32](10.0 * scale, 10 * scale)
// em.life_time = 1000 * (1 / design_factor)
// em.life_time_variation = 500 * (1 / design_factor)
em.movement_velocity = 20 * (1 / design_factor)
em.velocity = particle.AngleDirection{
angle: 0
angle_variation: 360
magnitude: 15 * (1 / design_factor)
magnitude_variation: 1 * (1 / design_factor)
}
em.acceleration = particle.AngleDirection{
angle: 0
angle_variation: 360
magnitude: -20 * (1 / design_factor)
magnitude_variation: 2 * (1 / design_factor)
}
em.position.x = a.image_selector.x
em.position.y = a.image_selector.y
}
}
a.options_button.on_resize()
a.back_button.on_resize()
}
@[markused]
pub fn (mut a App) variable_update(dt f64) {
a.ExampleApp.variable_update(dt)
if a.mode == .menu {
a.back_button.label = 'QUIT'
$if wasm32_emscripten {
a.back_button.label = if a.window.is_fullscreen() {
'BACK'
} else {
'FULLSCREEN'
}
}
} else {
a.back_button.label = 'BACK'
}
if a.save_settings_next {
a.save_settings() or {
eprintln('Saving settings failed: ${err}')
a.show_toast(Toast{
text: 'Saving settings failed'
duration: 2.5
})
}
a.save_settings_next = false
}
$if wasm32_emscripten {
if a.mode != .game && !a.is_settings_loaded {
save_file := a.settings_file() or { '' }
if os.exists(save_file) {
a.load_settings() or {}
a.is_settings_loaded = true
a.dim_selector.label = '${a.settings.dimensions.width:.0f}x${a.settings.dimensions.height:.0f} Puzzle, ${int(a.settings.dimensions.area())} pieces'
a.update_current_image_best_solve()
a.on_resize()
}
}
}
}
@[markused]
pub fn (mut a App) frame(dt f64) {
// a.draw.push_matrix()
// a.draw.scale(0.5,0.5,1)
// a.draw.translate(0,1280,0)
// println('mode: ${a.mode}')
match a.mode {
.game {
a.render_game_frame(dt)
}
.menu {
a.render_menu_frame(dt)
}
.options {
a.render_options_frame(dt)
}
}
// a.draw.pop_matrix()
a.back_button.draw()
a.draw_toasts(dt)
}
// asset returns a `string` with the full path to the asset.
// asset unifies locating project assets.
pub fn (a App) asset(relative_path string) string {
path := relative_path // relative_path.replace('\\','/')
$if wasm32_emscripten || android {
return path
}
$if macos {
exe_dir := os.resource_abs_path('').trim_right(os.path_separator)
if exe_dir.ends_with('MacOS') {
if os.real_path(os.join_path(exe_dir, '..')).ends_with('Contents') {
if os.real_path(os.join_path(exe_dir, '..', '..')).ends_with('.app') {
return os.real_path(os.join_path(exe_dir, '..', 'Resources', 'assets',
path))
}
}
}
}
return os.resource_abs_path(os.join_path('assets', path))
}
pub fn (mut a App) start_game() ! {
a.settings.dimensions = a.dim_selector.dim
imse := a.image_selector.get_selected_image() or {
return error('Failed getting selected image')
}
img, _ := a.assets.get[shy.Image](imse.source)
a.puzzle.on_piece_init = fn (mut piece Piece) {
// mut p := unsafe { piece }
piece.rotation = rand.f32_in_range(-4, 4) or { 0 }
}
a.puzzle.init(
app: a
viewport: a.canvas().rect()
image: img
dimensions: a.settings.dimensions
)!
a.puzzle.scramble()!
a.game_start_time = a.shy.ticks()
a.last_solve = Solve{}
a.best_solve = a.get_solve(a.to_image_id(a.puzzle.image), a.puzzle.dim)
}
pub fn (mut a App) end_game() {
a.game_end_time = a.shy.ticks()
a.last_solve = Solve{
image_id: a.to_image_id(a.puzzle.image)
time: u64(a.game_end_time - a.game_start_time)
dimensions: a.puzzle.dim
}
a.save_if_best_solve(a.last_solve)
a.update_current_image_best_solve()
}
fn (mut a App) select_next_image() {
if a.mode != .menu {
return
}
mut emitters := a.ps.emitters()
for i, mut em in emitters {
if i == 0 {
em.burst(400)
}
}
a.image_selector.next_image()
a.update_current_image_best_solve()
a.play_sfx_with_random_pitch('Whoosh')
}
fn (mut a App) select_prev_image() {
if a.mode != .menu {
return
}
// TODO reference bug
// if mut emitter := a.ps.emitter_at_index(0) {
// emitter.burst(100)
//}
// TODO
mut emitters := a.ps.emitters()
for i, mut em in emitters {
if i == 0 {
em.burst(400)
}
}
a.image_selector.prev_image()
a.update_current_image_best_solve()
a.play_sfx_with_random_pitch('Whoosh')
}
pub fn (mut a App) add_user_image(path string) ! {
if os.is_file(path) {
filename := os.file_name(path)
entry := ImageSelectorEntry{
removable: true
name: filename.all_before_last('.')
source: path
}
a.quick.load(shy.ImageOptions{
resize: a.canvas().factor
source: path
})!
a.image_selector.images << entry
a.image_selector.selected = a.image_selector.images.len - 1
if path !in a.settings.images {
a.settings.images << path
// println(a.settings.images)
}
}
}
pub fn (mut a App) remove_user_image(path string) {
for i, image_path in a.settings.images {
if path == image_path {
a.settings.images.delete(i)
a.remove_solves_based_on_image_id(os.file_name(path))
break
}
}
a.play_sfx_with_random_pitch_in_range('Disagree', 0.8, 1.2)