pixelsnake 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +21 -0
  4. data/README.md +122 -0
  5. data/bin/pixelsnake +8 -0
  6. data/lib/pixelsnake/abacus.rb +102 -0
  7. data/lib/pixelsnake/app.rb +130 -0
  8. data/lib/pixelsnake/art/big_font.rb +196 -0
  9. data/lib/pixelsnake/art/heads.rb +68 -0
  10. data/lib/pixelsnake/art/small_font.rb +73 -0
  11. data/lib/pixelsnake/autopilot.rb +34 -0
  12. data/lib/pixelsnake/award.rb +67 -0
  13. data/lib/pixelsnake/board.rb +36 -0
  14. data/lib/pixelsnake/canvas.rb +189 -0
  15. data/lib/pixelsnake/choice.rb +92 -0
  16. data/lib/pixelsnake/clockwork.rb +169 -0
  17. data/lib/pixelsnake/controls.rb +26 -0
  18. data/lib/pixelsnake/countdown.rb +80 -0
  19. data/lib/pixelsnake/courtyard.rb +306 -0
  20. data/lib/pixelsnake/day.rb +23 -0
  21. data/lib/pixelsnake/death.rb +115 -0
  22. data/lib/pixelsnake/debug_overlay.rb +53 -0
  23. data/lib/pixelsnake/demo.rb +85 -0
  24. data/lib/pixelsnake/density.rb +47 -0
  25. data/lib/pixelsnake/ease.rb +27 -0
  26. data/lib/pixelsnake/fade.rb +30 -0
  27. data/lib/pixelsnake/feats.rb +86 -0
  28. data/lib/pixelsnake/fine.rb +78 -0
  29. data/lib/pixelsnake/fireflies.rb +63 -0
  30. data/lib/pixelsnake/floating_text.rb +36 -0
  31. data/lib/pixelsnake/font.rb +62 -0
  32. data/lib/pixelsnake/fruit.rb +149 -0
  33. data/lib/pixelsnake/game.rb +294 -0
  34. data/lib/pixelsnake/gear.rb +132 -0
  35. data/lib/pixelsnake/high_scores.rb +91 -0
  36. data/lib/pixelsnake/hud.rb +217 -0
  37. data/lib/pixelsnake/iris.rb +76 -0
  38. data/lib/pixelsnake/jukebox.rb +143 -0
  39. data/lib/pixelsnake/keyboard.rb +96 -0
  40. data/lib/pixelsnake/laurel.rb +76 -0
  41. data/lib/pixelsnake/leaf.rb +73 -0
  42. data/lib/pixelsnake/lettering.rb +111 -0
  43. data/lib/pixelsnake/medal.rb +29 -0
  44. data/lib/pixelsnake/medal_view.rb +428 -0
  45. data/lib/pixelsnake/menu.rb +213 -0
  46. data/lib/pixelsnake/meter.rb +108 -0
  47. data/lib/pixelsnake/metronome.rb +24 -0
  48. data/lib/pixelsnake/name.rb +22 -0
  49. data/lib/pixelsnake/name_field.rb +156 -0
  50. data/lib/pixelsnake/odometer.rb +76 -0
  51. data/lib/pixelsnake/palette.rb +64 -0
  52. data/lib/pixelsnake/panel.rb +45 -0
  53. data/lib/pixelsnake/particles.rb +188 -0
  54. data/lib/pixelsnake/piece.rb +82 -0
  55. data/lib/pixelsnake/playback.rb +22 -0
  56. data/lib/pixelsnake/preferences.rb +96 -0
  57. data/lib/pixelsnake/rare_fruit.rb +268 -0
  58. data/lib/pixelsnake/recording.rb +44 -0
  59. data/lib/pixelsnake/ribbon.rb +110 -0
  60. data/lib/pixelsnake/save_file.rb +51 -0
  61. data/lib/pixelsnake/scenes/game_over.rb +249 -0
  62. data/lib/pixelsnake/scenes/medal_case.rb +268 -0
  63. data/lib/pixelsnake/scenes/pause.rb +171 -0
  64. data/lib/pixelsnake/scenes/play.rb +326 -0
  65. data/lib/pixelsnake/scenes/scores.rb +420 -0
  66. data/lib/pixelsnake/scenes/settings.rb +344 -0
  67. data/lib/pixelsnake/scenes/size_notice.rb +38 -0
  68. data/lib/pixelsnake/scenes/statistics.rb +420 -0
  69. data/lib/pixelsnake/scenes/title.rb +152 -0
  70. data/lib/pixelsnake/screen.rb +163 -0
  71. data/lib/pixelsnake/shed.rb +40 -0
  72. data/lib/pixelsnake/skin.rb +36 -0
  73. data/lib/pixelsnake/snake.rb +53 -0
  74. data/lib/pixelsnake/snake_view.rb +393 -0
  75. data/lib/pixelsnake/sound/band.rb +354 -0
  76. data/lib/pixelsnake/sound/instruments.rb +143 -0
  77. data/lib/pixelsnake/sound/mixer.rb +60 -0
  78. data/lib/pixelsnake/sound/music.rb +15 -0
  79. data/lib/pixelsnake/sound/pulse.rb +149 -0
  80. data/lib/pixelsnake/sound/song.rb +22 -0
  81. data/lib/pixelsnake/sound/sounds.rb +225 -0
  82. data/lib/pixelsnake/sound/speaker.rb +199 -0
  83. data/lib/pixelsnake/sound/synth.rb +277 -0
  84. data/lib/pixelsnake/sound/tune.rb +145 -0
  85. data/lib/pixelsnake/sound/tunes/dusk.rb +154 -0
  86. data/lib/pixelsnake/sound/tunes/fireflies.rb +106 -0
  87. data/lib/pixelsnake/sound/tunes/jade.rb +100 -0
  88. data/lib/pixelsnake/sound/tunes/lanterns.rb +126 -0
  89. data/lib/pixelsnake/sound/tunes.rb +40 -0
  90. data/lib/pixelsnake/sprite.rb +58 -0
  91. data/lib/pixelsnake/stage.rb +95 -0
  92. data/lib/pixelsnake/stats.rb +131 -0
  93. data/lib/pixelsnake/terminal.rb +84 -0
  94. data/lib/pixelsnake/terrarium.rb +206 -0
  95. data/lib/pixelsnake/tile.rb +32 -0
  96. data/lib/pixelsnake/toolbox.rb +251 -0
  97. data/lib/pixelsnake/trophy.rb +99 -0
  98. data/lib/pixelsnake/tuft.rb +55 -0
  99. data/lib/pixelsnake/upscale.rb +60 -0
  100. data/lib/pixelsnake/version.rb +5 -0
  101. data/lib/pixelsnake.rb +143 -0
  102. metadata +146 -0
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ module Tunes
5
+ # A waltz for the fireflies coming out: a celesta over a harp's
6
+ # oom-pah-pah, a kalimba answering, and now and then one lighting up
7
+ # high on glass. In a game the brushes keep the time, and a shaker and
8
+ # a triangle join it.
9
+ FIREFLIES = Tune.new(:fireflies, <<~MUSIC, tempo: 102, steps: 2, meter: 3, again: "a", fade_out: 0.5, echo: 0.28, quickens: 0.3,
10
+ intro
11
+ bass 2 | c..... | G..... | G..... | D..... |
12
+ harp 3 | ..G.G. | ..G.G. | ..G.G. | ..G.G. |
13
+ harp 3 | ..c.c. | ..c.c. | ..A.A. | ..A.A. |
14
+ harp 3 | ..e.e. | ..e.e. | ..d.d. | ..d.d. |
15
+ blink 5 | ....e. | ...... | ....g. | ...... |
16
+ kick | X..... | X..... | X..... | X..... |
17
+ brush | ..x.x. | ..x.x. | ..x.x. | ..x.X. |
18
+
19
+ a
20
+ melody 4 | e--dc- | G----- | A--Gc- | e----- | a--ge- | c----- | d--ed- | G----- |
21
+ | e--dc- | G----- | A--Gc- | e---d- | c--de- | g--ed- | c----- | ...... |
22
+ bass 2 | c..... | G..... | A..... | E..... | F..... | c..... | G..... | D..... |
23
+ | c..... | G..... | A..... | E..... | F..... | G..... | c..... | G..... |
24
+ harp 3 | ..G.G. | ..G.G. | ..A.A. | ..A.A. | ..F.F. | ..F.F. | ..G.G. | ..G.G. |
25
+ | ..G.G. | ..G.G. | ..A.A. | ..A.A. | ..F.F. | ..G.G. | ..G.G. | ..G.G. |
26
+ harp 3 | ..c.c. | ..c.c. | ..c.c. | ..c.c. | ..A.A. | ..A.A. | ..A.A. | ..A.A. |
27
+ | ..c.c. | ..c.c. | ..c.c. | ..c.c. | ..A.A. | ..A.A. | ..c.c. | ..c.c. |
28
+ harp 3 | ..e.e. | ..e.e. | ..e.e. | ..e.e. | ..c.c. | ..c.c. | ..d.d. | ..d.d. |
29
+ | ..e.e. | ..e.e. | ..e.e. | ..e.e. | ..c.c. | ..d.d. | ..e.e. | ..e.e. |
30
+ blink 5 | ...... | ...... | ...... | ...... | ...... | ...e.. | ...... | ...... |
31
+ | ...... | ...... | ...... | ...... | ...... | ...... | ...... | ....e. |
32
+ kick | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
33
+ | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
34
+ brush | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. |
35
+ | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.X. |
36
+ shaker | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
37
+ | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
38
+ triangle | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
39
+ | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
40
+
41
+ b
42
+ melody 4 | c-d-e- | g--ed- | e-d-G- | A----- | c-d-e- | g--ag- | e--dc- | c----- |
43
+ | a-g-e- | d--ed- | g--ed- | e----- | d-c-A- | c--Ad- | d----- | ...... |
44
+ bass 2 | F..... | G..... | E..... | A..... | F..... | G..... | c..... | G..... |
45
+ | F..... | G..... | E..... | A..... | D..... | A..... | G..... | D..... |
46
+ harp 3 | ..F.F. | ..G.G. | ..E.E. | ..A.A. | ..F.F. | ..G.G. | ..G.G. | ..G.G. |
47
+ | ..F.F. | ..G.G. | ..E.E. | ..A.A. | ..F.F. | ..F.F. | ..G.G. | ..G.G. |
48
+ harp 3 | ..A.A. | ..A.A. | ..G.G. | ..c.c. | ..A.A. | ..A.A. | ..c.c. | ..c.c. |
49
+ | ..A.A. | ..A.A. | ..G.G. | ..c.c. | ..A.A. | ..A.A. | ..A.A. | ..A.A. |
50
+ harp 3 | ..c.c. | ..d.d. | ..d.d. | ..e.e. | ..c.c. | ..d.d. | ..e.e. | ..e.e. |
51
+ | ..c.c. | ..d.d. | ..d.d. | ..e.e. | ..d.d. | ..d.d. | ..d.d. | ..d.d. |
52
+ counter 4 | ...... | ...... | ...... | ..c.e. | ...... | ...... | ...... | ..e.g. |
53
+ | ...... | ...... | ...... | ..c.e. | ...... | ...... | ..A.c. | ..d.e. |
54
+ blink 5 | ...... | ...... | ...... | ....g. | ...... | ...... | ...... | ..c... |
55
+ | ...... | ...... | ...... | ...a.. | ...... | ...... | ...... | ...... |
56
+ kick | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
57
+ | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
58
+ brush | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. |
59
+ | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.X. |
60
+ shaker | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
61
+ | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
62
+ triangle | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
63
+ | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
64
+ sparkle 5 | ...... | ....g. | ...... | ...... | ...... | ....a. | ...... | ....e. |
65
+ | ...... | ....d. | ...... | ...... | ...... | ...... | ...... | ....g. |
66
+
67
+ a2
68
+ melody 4 | e--dc- | G----- | A--Gc- | e----- | a--ge- | c----- | d--ed- | G----- |
69
+ | e--dc- | G----- | A--Gc- | e---d- | c--de- | g--ed- | c----- | ...... |
70
+ counter 4 | ...... | ..e.g. | ...... | ..c.A. | ...... | ..e.a. | ...... | ..d.g. |
71
+ | ...... | ..e.g. | ...... | ...... | ...... | ...... | ...... | ..G.c. |
72
+ blink 5 | ...... | ...... | ...... | ....e. | ...... | ...... | ...... | ...... |
73
+ | ...... | ...... | ...... | ...... | ...... | ...g.. | ...... | ...... |
74
+ bass 2 | c..... | G..... | A..... | E..... | F..... | c..... | G..... | D..... |
75
+ | c..... | G..... | A..... | E..... | F..... | G..... | c..... | G..... |
76
+ harp 3 | ..G.G. | ..G.G. | ..A.A. | ..A.A. | ..F.F. | ..F.F. | ..G.G. | ..G.G. |
77
+ | ..G.G. | ..G.G. | ..A.A. | ..A.A. | ..F.F. | ..G.G. | ..G.G. | ..G.G. |
78
+ harp 3 | ..c.c. | ..c.c. | ..c.c. | ..c.c. | ..A.A. | ..A.A. | ..A.A. | ..A.A. |
79
+ | ..c.c. | ..c.c. | ..c.c. | ..c.c. | ..A.A. | ..A.A. | ..c.c. | ..c.c. |
80
+ harp 3 | ..e.e. | ..e.e. | ..e.e. | ..e.e. | ..c.c. | ..c.c. | ..d.d. | ..d.d. |
81
+ | ..e.e. | ..e.e. | ..e.e. | ..e.e. | ..c.c. | ..d.d. | ..e.e. | ..e.e. |
82
+ kick | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
83
+ | X..... | X..... | X..... | X..... | X..... | X..... | X..... | X..... |
84
+ brush | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. |
85
+ | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.x. | ..x.X. |
86
+ shaker | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
87
+ | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx | xxXxXx |
88
+ triangle | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
89
+ | X..... | ...... | X..... | ...... | X..... | ...... | X..... | ...... |
90
+ sparkle 5 | ...... | ...... | ...... | ....a. | ...... | ...... | ...... | ....g. |
91
+ | ...... | ...... | ...... | ....e. | ...... | ...... | ...... | ....c. |
92
+ MUSIC
93
+ parts: {
94
+ melody: Tune::Part.new(instrument: :celesta, gain: 0.64, pan: 0.12),
95
+ counter: Tune::Part.new(instrument: :kalimba, gain: 0.41, pan: -0.3),
96
+ harp: Tune::Part.new(instrument: :harp, gain: 0.35, pan: -0.15),
97
+ bass: Tune::Part.new(instrument: :bass, gain: 1.03, mono: true),
98
+ blink: Tune::Part.new(instrument: :glass, gain: 0.35, pan: 0.45),
99
+ kick: Tune::Part.new(instrument: :kick, gain: 0.78, from: 1),
100
+ brush: Tune::Part.new(instrument: :brush, gain: 0.71, pan: 0.1, from: 1),
101
+ shaker: Tune::Part.new(instrument: :shaker, gain: 0.43, pan: 0.35, from: 2),
102
+ triangle: Tune::Part.new(instrument: :triangle, gain: 0.25, pan: -0.4, from: 4),
103
+ sparkle: Tune::Part.new(instrument: :glass, gain: 0.3, pan: 0.5, from: 5)
104
+ })
105
+ end
106
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ module Tunes
5
+ # A koto piece for the jade snake, in the scale's minor, running over
6
+ # a harp that flows in eighths, with wind chimes now and then. In a
7
+ # game a frame drum beats under it with a woodblock between.
8
+ JADE = Tune.new(:jade, <<~MUSIC, tempo: 88, steps: 4, again: "a", fade_out: 0.5, echo: 0.3, quickens: 0.3,
9
+ intro
10
+ strings 3 | A.e.c.e.A.e.c.e. | A.e.c.e.A.e.c.e. |
11
+ bass 2 | A.......E....... | A.......E....... |
12
+ chime 5 | ................ | ............e... |
13
+ frame | X.......x....... | X.......x...x.x. |
14
+ block | ......x.......x. | ......x.......x. |
15
+
16
+ a
17
+ melody 4 | A.c.d.e.....d.c. | d.......G....... | A.c.d.e.g...e... | d............... |
18
+ | e.g.a.....g.e.d. | e.......d....... | c.d.c.A.....G... | E............... |
19
+ strings 3 | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | G.d.A.d.G.d.A.d. |
20
+ | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | E.d.G.d.E.d.G.d. |
21
+ bass 2 | A.......E....... | G.......D....... | F.......c....... | G.......D....... |
22
+ | A.......E....... | G.......D....... | F.......c....... | E.......G....... |
23
+ frame | X.......x....... | X.......x....... | X.......x....... | X.......x....... |
24
+ | X.......x....... | X.......x....... | X.......x....... | X.......x...x.x. |
25
+ block | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
26
+ | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
27
+ shaker | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
28
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
29
+ chime 5 | ................ | ................ | ................ | ........e....... |
30
+ | ................ | ................ | ................ | ................ |
31
+
32
+ a2
33
+ melody 4 | A.c.d.e.....d.c. | d.......G.A.c.d. | e.g.e.d.c....... | d.......e.d.c.d. |
34
+ | e.g.a.....g.e.d. | e.......g.a.g.e. | d.c.d.e.....c.A. | G.......E....... |
35
+ counter 5 | ................ | ................ | ............A.G. | ................ |
36
+ | ................ | ................ | ................ | ....D...E...G... |
37
+ strings 3 | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | G.d.A.d.G.d.A.d. |
38
+ | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | E.d.G.d.E.d.G.d. |
39
+ bass 2 | A.......E....... | G.......D....... | F.......c....... | G.......D....... |
40
+ | A.......E....... | G.......D....... | F.......c....... | E.......G....... |
41
+ frame | X.......x....... | X.......x....... | X.......x....... | X.......x....... |
42
+ | X.......x....... | X.......x....... | X.......x....... | X.......x...x.x. |
43
+ block | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
44
+ | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
45
+ shaker | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
46
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
47
+ chime 5 | ................ | ................ | ................ | ........e....... |
48
+ | ................ | ................ | ................ | ................ |
49
+
50
+ b
51
+ melody 4 | a.......g.e..... | g.......d....... | e.......c.d.e.g. | a............... |
52
+ | a.......g.d..... | g.......e.d..... | c.d.e.g.a...g... | d............... |
53
+ strings 3 | F.c.A.c.F.c.A.c. | G.d.A.d.G.d.A.d. | A.e.c.e.A.e.c.e. | A.e.c.e.A.e.c.e. |
54
+ | D.A.F.A.D.A.F.A. | E.d.G.d.E.d.G.d. | F.c.A.c.F.c.A.c. | G.d.A.d.G.d.A.d. |
55
+ bass 2 | F.......c....... | G.......D....... | A.......E....... | A.......c....... |
56
+ | D.......A....... | E.......G....... | F.......c....... | G.......D....... |
57
+ counter 5 | ................ | ................ | ................ | ....E...D...C... |
58
+ | ................ | ................ | ................ | ....G...A...c... |
59
+ chime 5 | ................ | ................ | ................ | ........g....... |
60
+ | ................ | ................ | ................ | ................ |
61
+ frame | X.......x....... | X.......x....... | X.......x....... | X.......x....... |
62
+ | X.......x....... | X.......x....... | X.......x....... | X.......x.x.x.x. |
63
+ block | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
64
+ | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
65
+ shaker | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
66
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
67
+ sparkle 5 | ................ | ........g....... | ................ | ............a... |
68
+ | ................ | ........e....... | ................ | ................ |
69
+
70
+ a3
71
+ melody 4 | A.c.d.e.....d.c. | d.......G....... | A.c.d.e.g...e... | d............... |
72
+ | e.g.a.....g.e.d. | e.......d....... | c.d.c.A.....G... | E............... |
73
+ strings 3 | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | G.d.A.d.G.d.A.d. |
74
+ | A.e.c.e.A.e.c.e. | G.d.A.d.G.d.A.d. | F.c.A.c.F.c.A.c. | E.d.G.d.E.d.G.d. |
75
+ bass 2 | A.......E....... | G.......D....... | F.......c....... | G.......D....... |
76
+ | A.......E....... | G.......D....... | F.......c....... | E.......G....... |
77
+ counter 5 | ................ | ............E.D. | ................ | ............E.G. |
78
+ | ................ | ................ | ................ | ................ |
79
+ frame | X.......x....... | X.......x....... | X.......x....... | X.......x....... |
80
+ | X.......x....... | X.......x....... | X.......x....... | X.......x...x.x. |
81
+ block | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
82
+ | ......x.......x. | ......x.......x. | ......x.......x. | ......x.......x. |
83
+ shaker | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
84
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. |
85
+ sparkle 5 | ................ | ................ | ............e... | ................ |
86
+ | ................ | ........a....... | ................ | ............g... |
87
+ MUSIC
88
+ parts: {
89
+ melody: Tune::Part.new(instrument: :koto, gain: 0.98, pan: 0.15),
90
+ strings: Tune::Part.new(instrument: :harp, gain: 0.39, pan: -0.2),
91
+ bass: Tune::Part.new(instrument: :bass, gain: 0.86, mono: true),
92
+ chime: Tune::Part.new(instrument: :glass, gain: 0.44, pan: 0.45),
93
+ counter: Tune::Part.new(instrument: :kalimba, gain: 0.55, pan: -0.35, from: 3),
94
+ frame: Tune::Part.new(instrument: :frame_drum, gain: 0.94, from: 1),
95
+ block: Tune::Part.new(instrument: :woodblock, gain: 0.92, pan: 0.25, from: 1),
96
+ shaker: Tune::Part.new(instrument: :shaker, gain: 0.47, pan: -0.3, from: 2),
97
+ sparkle: Tune::Part.new(instrument: :glass, gain: 0.36, pan: 0.5, from: 5)
98
+ })
99
+ end
100
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ module Tunes
5
+ # A slow swing under the lanterns: a nylon guitar over a vibraphone's
6
+ # chords and an upright bass, the chords struck where a pianist would
7
+ # put them. In a game a side stick and hats join, and a kalimba fills
8
+ # the gaps.
9
+ LANTERNS = Tune.new(:lanterns, <<~MUSIC, tempo: 80, steps: 4, swing: 0.6, again: "a", fade_out: 0.5, echo: 0.25, quickens: 0.3,
10
+ intro
11
+ chords 3 | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
12
+ chords 3 | G-----G--------- | A-----A--------- | A-----A--------- | A-----A--------- |
13
+ chords 3 | c-----c--------- | c-----c--------- | d-----d--------- | d-----d--------- |
14
+ chords 3 | d-----d--------- | d-----d--------- | g-----g--------- | g-----g--------- |
15
+ bass 2 | D.......A....... | G.......d....... | c.......G....... | c.......E...G... |
16
+ kick | X.........x..... | X.........x..... | X.........x..... | X.........x..x.. |
17
+ rim | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X.x. |
18
+ hat | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
19
+
20
+ a
21
+ melody 4 | ..e...d.c...A... | G.......A.c..... | ..e...g.a...g... | e.......d.c.A... |
22
+ | ..e...d.c...A... | G.......A.c.d... | e...g...a...g.e. | d.......c....... |
23
+ chords 3 | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
24
+ | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
25
+ chords 3 | G-----G--------- | A-----A--------- | A-----A--------- | G-----G--------- |
26
+ | G-----G--------- | A-----A--------- | A-----A--------- | A-----A--------- |
27
+ chords 3 | c-----c--------- | c-----c--------- | d-----d--------- | c-----c--------- |
28
+ | c-----c--------- | c-----c--------- | d-----d--------- | d-----d--------- |
29
+ chords 3 | d-----d--------- | d-----d--------- | g-----g--------- | e-----e--------- |
30
+ | d-----d--------- | d-----d--------- | g-----g--------- | g-----g--------- |
31
+ bass 2 | D.......A....... | G.......d....... | c.......G....... | A.......G.....E. |
32
+ | D.......A....... | G.......d....... | c.......G....... | c.......E...G... |
33
+ kick | X.........x..... | X.........x..... | X.........x..... | X.........x..... |
34
+ | X.........x..... | X.........x..... | X.........x..... | X.........x..x.. |
35
+ rim | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X... |
36
+ | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X.x. |
37
+ hat | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
38
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
39
+
40
+ a2
41
+ melody 4 | ..e...d.c...A... | G.......A.c..... | ..e...g.a...g... | e.......d.c.A... |
42
+ | ..e...d.c...A... | G.......A.c.d.e. | g...........e.d. | c............... |
43
+ counter 5 | ................ | ..........G.A... | ................ | ..........E.C... |
44
+ | ................ | ................ | ................ | ....G...E...D... |
45
+ chords 3 | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
46
+ | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
47
+ chords 3 | G-----G--------- | A-----A--------- | A-----A--------- | G-----G--------- |
48
+ | G-----G--------- | A-----A--------- | A-----A--------- | A-----A--------- |
49
+ chords 3 | c-----c--------- | c-----c--------- | d-----d--------- | c-----c--------- |
50
+ | c-----c--------- | c-----c--------- | d-----d--------- | d-----d--------- |
51
+ chords 3 | d-----d--------- | d-----d--------- | g-----g--------- | e-----e--------- |
52
+ | d-----d--------- | d-----d--------- | g-----g--------- | g-----g--------- |
53
+ bass 2 | D.......A....... | G.......d....... | c.......G....... | A.......G.....E. |
54
+ | D.......A....... | G.......d....... | c.......G....... | c.......E...G... |
55
+ kick | X.........x..... | X.........x..... | X.........x..... | X.........x..... |
56
+ | X.........x..... | X.........x..... | X.........x..... | X.........x..x.. |
57
+ rim | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X... |
58
+ | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X.x. |
59
+ hat | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
60
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
61
+ sparkle 5 | ................ | ................ | ............e... | ................ |
62
+ | ................ | ................ | ........g....... | ................ |
63
+
64
+ b
65
+ melody 4 | a.......g...e... | g...e...d....... | c...d...e...g... | a.......g....... |
66
+ | a.......g...e... | g...e...d...c... | A.....c...d.e... | d............... |
67
+ chords 3 | A-----A--------- | G-----G--------- | F-----F--------- | F-----F--------- |
68
+ | A-----A--------- | G-----G--------- | E-----E--------- | F-----F--------- |
69
+ chords 3 | c-----c--------- | A-----A--------- | G-----G--------- | A-----A--------- |
70
+ | c-----c--------- | A-----A--------- | G-----G--------- | A-----A--------- |
71
+ chords 3 | e-----e--------- | d-----d--------- | c-----c--------- | c-----c--------- |
72
+ | e-----e--------- | d-----d--------- | c-----c--------- | c-----c--------- |
73
+ chords 3 | g-----g--------- | e-----e--------- | d-----d--------- | d-----d--------- |
74
+ | g-----g--------- | e-----e--------- | e-----e--------- | d-----d--------- |
75
+ bass 2 | F.......c....... | E.......G....... | D.......A....... | G.......d....... |
76
+ | F.......c....... | E.......G....... | A.......E....... | G.......d.....A. |
77
+ counter 5 | ................ | ..........E.D... | ................ | ..........G.A... |
78
+ | ................ | ..........E.D... | ................ | ....D...E...G... |
79
+ kick | X.........x..... | X.........x..... | X.........x..... | X.........x..... |
80
+ | X.........x..... | X.........x..... | X.........x..... | X.........x..x.. |
81
+ rim | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X... |
82
+ | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X.x. |
83
+ hat | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
84
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
85
+ shaker | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. |
86
+ | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. |
87
+ sparkle 5 | ................ | ........g....... | ................ | ............e... |
88
+ | ................ | ........a....... | ................ | ................ |
89
+
90
+ a3
91
+ melody 4 | ..e...d.c...A... | G.......A.c..... | ..e...g.a...g... | e.......d.c.A... |
92
+ | ..e...d.c...A... | G.......A.c.d... | e...g...a...g.e. | d.......c....... |
93
+ chords 3 | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
94
+ | F-----F--------- | F-----F--------- | E-----E--------- | E-----E--------- |
95
+ chords 3 | G-----G--------- | A-----A--------- | A-----A--------- | G-----G--------- |
96
+ | G-----G--------- | A-----A--------- | A-----A--------- | A-----A--------- |
97
+ chords 3 | c-----c--------- | c-----c--------- | d-----d--------- | c-----c--------- |
98
+ | c-----c--------- | c-----c--------- | d-----d--------- | d-----d--------- |
99
+ chords 3 | d-----d--------- | d-----d--------- | g-----g--------- | e-----e--------- |
100
+ | d-----d--------- | d-----d--------- | g-----g--------- | g-----g--------- |
101
+ bass 2 | D.......A....... | G.......d....... | c.......G....... | A.......G.....E. |
102
+ | D.......A....... | G.......d....... | c.......G....... | c.......E...G... |
103
+ counter 5 | ................ | ................ | ................ | ..........E.C... |
104
+ | ................ | ................ | ................ | ................ |
105
+ kick | X.........x..... | X.........x..... | X.........x..... | X.........x..... |
106
+ | X.........x..... | X.........x..... | X.........x..... | X.........x..x.. |
107
+ rim | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X... |
108
+ | ....X.......X... | ....X.......X... | ....X.......X... | ....X.......X.x. |
109
+ hat | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
110
+ | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.x. | x.x.x.x.x.x.x.xx |
111
+ shaker | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. |
112
+ | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. | ..x...x...x...x. |
113
+ MUSIC
114
+ parts: {
115
+ melody: Tune::Part.new(instrument: :guitar, gain: 1.03, pan: 0.1, mono: true),
116
+ chords: Tune::Part.new(instrument: :vibes, gain: 0.23, pan: -0.2, release: 0.3),
117
+ bass: Tune::Part.new(instrument: :bass, gain: 0.93, mono: true),
118
+ counter: Tune::Part.new(instrument: :kalimba, gain: 0.54, pan: 0.35, from: 3),
119
+ kick: Tune::Part.new(instrument: :kick, gain: 1.01, from: 1),
120
+ rim: Tune::Part.new(instrument: :rim, gain: 0.99, pan: 0.15, from: 1),
121
+ hat: Tune::Part.new(instrument: :hat, gain: 0.58, pan: -0.25, from: 2),
122
+ shaker: Tune::Part.new(instrument: :shaker, gain: 0.74, pan: 0.35, from: 4),
123
+ sparkle: Tune::Part.new(instrument: :glass, gain: 0.42, pan: 0.5, from: 5)
124
+ })
125
+ end
126
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Every tune the songs are made of, each written for the instruments in
5
+ # its own file, and the making of their notes.
6
+ #
7
+ # Like the sounds, they are in C major pentatonic, so nothing the game
8
+ # plays over them can clash. Only the low notes step outside it, to an F
9
+ # under middle C, the root of a chord that every note of the scale still
10
+ # sounds sweet over.
11
+ module Tunes
12
+ SEED = 17
13
+ # Every note is made this loud at its loudest, and the parts set how
14
+ # loud each plays, which together keep the music well under the sounds.
15
+ PEAK = 0.15
16
+ DRUM_VARIANTS = 3
17
+
18
+ module_function
19
+
20
+ def all = { dusk: DUSK, chase: CHASE, fireflies: FIREFLIES, lanterns: LANTERNS, jade: JADE }
21
+
22
+ # Every note the tunes play, made once and handed over as it is made:
23
+ # each instrument at each pitch it is played at, and each drum a few
24
+ # times over, so no two strokes in a row need sound quite alike. The
25
+ # tunes' notes come in the order the tunes are given.
26
+ def each_note(tunes = all.values, rng = Random.new(SEED))
27
+ tunes.flat_map(&:keys).uniq.each do |instrument, pitch|
28
+ (pitch ? 1 : DRUM_VARIANTS).times { yield instrument, pitch, Synth.finish(Instruments.make(instrument, pitch, rng), PEAK) }
29
+ end
30
+ end
31
+
32
+ # The notes as the band keeps them: by instrument and pitch, each with
33
+ # its variants, packed as floats.
34
+ def render(tunes = all.values)
35
+ bank = Hash.new { |hash, key| hash[key] = [] }
36
+ each_note(tunes) { |instrument, pitch, samples| bank[[instrument, pitch]] << samples.pack("e*") }
37
+ bank.to_h
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A small image of palette indices, written as a text grid: one character
5
+ # per pixel, "." for transparent, and a legend naming the color of every
6
+ # other character.
7
+ #
8
+ # A sprite can carry its art for the fine canvas, drawn again at half as
9
+ # big again in the same legend. One that does not is made bigger by
10
+ # Upscale the first time the fine canvas asks for it.
11
+ class Sprite
12
+ CLEAR = 255
13
+
14
+ # The legend can be given whole or its colors written out one by one.
15
+ def self.parse(art, legend = {}, fine: nil, **colors)
16
+ legend = legend.merge(colors)
17
+ new(grid(art, legend), fine: fine && new(grid(fine, legend)))
18
+ end
19
+
20
+ def self.grid(art, legend)
21
+ art.lines(chomp: true).map do |line|
22
+ line.chars.map { |char| char == "." ? CLEAR : Palette[legend.fetch(char)] }.pack("C*")
23
+ end
24
+ end
25
+
26
+ attr_reader :rows, :width, :height
27
+
28
+ def initialize(rows, fine: nil)
29
+ @rows = rows.map(&:freeze).freeze
30
+ @width = rows.first.bytesize
31
+ @height = rows.size
32
+ @fine = fine
33
+ raise ArgumentError, "ragged sprite rows" unless rows.all? { |row| row.bytesize == @width }
34
+ end
35
+
36
+ def fine = @fine ||= Upscale.fine(self)
37
+
38
+ def [](x, y) = @rows[y].getbyte(x)
39
+
40
+ # Swaps rows and columns. Unlike a mirror, a transpose keeps light from
41
+ # the top left coming from the top left, so a sprite facing right
42
+ # becomes the same sprite facing down, lit correctly.
43
+ def transpose
44
+ Sprite.new(Array.new(width) { |x| rows.map { |row| row.getbyte(x) }.pack("C*") }, fine: @fine&.transpose)
45
+ end
46
+
47
+ # Stretched or squashed by nearest neighbor. Only ever on screen for a
48
+ # few frames, in the middle of a squash.
49
+ def scale(width_by, height_by)
50
+ width = [(self.width * width_by).round, 1].max
51
+ height = [(self.height * height_by).round, 1].max
52
+ Sprite.new(Array.new(height) do |y|
53
+ source = rows[[(y / height_by).floor, self.height - 1].min]
54
+ Array.new(width) { |x| source.getbyte([(x / width_by).floor, self.width - 1].min) }.pack("C*")
55
+ end, fine: @fine&.scale(width_by, height_by))
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Holds the scene being played, and what every scene shares: the scores,
5
+ # the stats, the player's preferences, the speaker and the dice. A scene
6
+ # is any object with update(dt), draw(canvas), press(action),
7
+ # point(x, y, kind) for the mouse, focus, the pixel the eye is on, music,
8
+ # what it wants heard, and typing?, whether it is taking a name through
9
+ # type(key). Scenes change through a transition; a scene that animates
10
+ # its own entrance, like the pause panel, can be cut to directly. While
11
+ # they change, the music is the next scene's, so it starts going there
12
+ # at once.
13
+ class Stage
14
+ attr_reader :scores, :stats, :preferences, :speaker, :rng, :scene
15
+ # How many columns wide a new game's field is: as many as the terminal
16
+ # has room for.
17
+ attr_accessor :columns
18
+
19
+ def initialize(scores:, stats:, preferences:, rng:, speaker: Speaker::SILENT)
20
+ @scores = scores
21
+ @stats = stats
22
+ @preferences = preferences
23
+ @speaker = speaker
24
+ @rng = rng
25
+ @scene = nil
26
+ @columns = Courtyard::COLUMNS
27
+ @unwatched = false
28
+ end
29
+
30
+ def show(scene, transition = Fade)
31
+ @transition = transition.new(visible_scene, scene)
32
+ end
33
+
34
+ def cut_to(scene)
35
+ @scene = scene
36
+ end
37
+
38
+ def quit = show(nil)
39
+
40
+ def done? = @scene.nil? && @transition.nil?
41
+
42
+ # Once the game has faded out there is no scene: nothing moves, and the
43
+ # dark frame stays until the loop notices it is done.
44
+ def update(dt)
45
+ if @transition
46
+ @transition.update(dt)
47
+ finish_transition if @transition.done?
48
+ else
49
+ @scene&.update(dt)
50
+ end
51
+ play_music
52
+ end
53
+
54
+ # Asks the speaker for what the scene now showing wants heard: with
55
+ # every update, and whenever the scene changes while nothing moves.
56
+ def play_music
57
+ @speaker.music(visible_scene&.music || Music::NONE)
58
+ end
59
+
60
+ def draw(canvas)
61
+ (@transition || @scene)&.draw(canvas)
62
+ end
63
+
64
+ # Between two scenes nothing is pressed. Focus lost then is not
65
+ # forgotten, though: the scene arriving hears of it, so that a game does
66
+ # not start with nobody looking.
67
+ def press(action)
68
+ return @scene.press(action) unless @transition
69
+
70
+ @unwatched = true if action == :blur
71
+ end
72
+
73
+ def typing? = @transition.nil? && @scene.typing?
74
+
75
+ def type(key)
76
+ @scene.type(key)
77
+ end
78
+
79
+ # A mouse pointer at a pixel, moving or clicking.
80
+ def point(x, y, kind)
81
+ @scene.point(x, y, kind) unless @transition
82
+ end
83
+
84
+ private
85
+
86
+ def visible_scene = @transition ? @transition.to : @scene
87
+
88
+ def finish_transition
89
+ @scene = @transition.to
90
+ @transition = nil
91
+ @scene&.press(:blur) if @unwatched
92
+ @unwatched = false
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module PixelSnake
6
+ # Every game the player has played, added up: how many, how long they
7
+ # took, the fruit eaten and each kind of rare fruit eaten of those that
8
+ # came out, how far the snake has crawled, the longest it has grown and
9
+ # how the games ended, along with the last few games themselves. Kept in
10
+ # a file beside the scores and saved after every game.
11
+ #
12
+ # The medals live here too, each with the day it was earned, saved the
13
+ # moment it is, and those the player has not yet been to see.
14
+ class Stats
15
+ Recent = Data.define(:score, :length, :seconds, :ending, :speed, :date)
16
+
17
+ ENDINGS = %i[wall itself won left].freeze
18
+ RECENT = 30
19
+
20
+ attr_reader :games, :seconds, :fruit, :moves, :longest, :recent
21
+
22
+ def self.default_path(environment = ENV) = File.join(File.dirname(HighScores.default_path(environment)), "stats.json")
23
+
24
+ def initialize(path)
25
+ @file = SaveFile.new(path, Hash)
26
+ take(@file.read)
27
+ end
28
+
29
+ # Nothing played and nothing earned; a medal is kept the moment it is
30
+ # earned, before its game is counted.
31
+ def empty? = games.zero? && @medals.empty?
32
+
33
+ # How many of a kind of rare fruit have been eaten, and come out.
34
+ def eaten(kind) = @rare_eaten.fetch(kind)
35
+
36
+ def seen(kind) = @rare_seen.fetch(kind)
37
+
38
+ # How many games have ended one way: :wall, :itself, :won or :left.
39
+ def ended(how) = @endings.fetch(how)
40
+
41
+ # The day a medal was earned, or nil.
42
+ def earned(medal) = @medals[medal.key]
43
+
44
+ def earned?(medal) = @medals.key?(medal.key)
45
+
46
+ def medals = @medals.size
47
+
48
+ # The medals earned since the player last looked at the case.
49
+ def unseen = @unseen.map { |key| Medal.named(key) }
50
+
51
+ # Keeps a medal not earned before and says whether it was new.
52
+ def award(medal, date: Date.today)
53
+ return false if earned?(medal)
54
+
55
+ @medals[medal.key] = date.iso8601
56
+ @unseen << medal.key
57
+ save
58
+ true
59
+ end
60
+
61
+ def see_medals
62
+ return if @unseen.empty?
63
+
64
+ @unseen = []
65
+ save
66
+ end
67
+
68
+ def record(run, date: Date.today)
69
+ @games += 1
70
+ @seconds += run.seconds
71
+ @fruit += run.fruit
72
+ Game::RARE.each do |kind|
73
+ @rare_eaten[kind] += run.rare_eaten.fetch(kind)
74
+ @rare_seen[kind] += run.rare_seen.fetch(kind)
75
+ end
76
+ @moves += run.moves
77
+ @longest = [longest, run.longest].max
78
+ @endings[run.ending] += 1
79
+ game = Recent.new(run.score, run.length, run.seconds.round(1), run.ending, run.speed, date.iso8601)
80
+ @recent = [*recent, game].last(RECENT)
81
+ save
82
+ end
83
+
84
+ def clear
85
+ take({})
86
+ save
87
+ end
88
+
89
+ private
90
+
91
+ # Whatever is missing from what was saved, or not understood, counts as
92
+ # nothing.
93
+ def take(saved)
94
+ @games = count(saved[:games])
95
+ @seconds = count(saved[:seconds]).to_f
96
+ @fruit = count(saved[:fruit])
97
+ @rare_eaten = Game::RARE.to_h { |kind| [kind, count(saved[:"#{kind}_eaten"])] }
98
+ @rare_seen = Game::RARE.to_h { |kind| [kind, count(saved[:"#{kind}_seen"])] }
99
+ @moves = count(saved[:moves])
100
+ @longest = count(saved[:longest])
101
+ endings = saved[:endings].is_a?(Hash) ? saved[:endings] : {}
102
+ @endings = ENDINGS.to_h { |how| [how, count(endings[how])] }
103
+ @recent = (saved[:recent].is_a?(Array) ? saved[:recent] : []).filter_map { |game| recent_game(game) }.last(RECENT)
104
+ medals = saved[:medals].is_a?(Hash) ? saved[:medals] : {}
105
+ @medals = Medal::KEYS.to_h { |key| [key, Day.read(medals[key])] }.compact
106
+ @unseen = Medal::KEYS & (saved[:unseen].is_a?(Array) ? saved[:unseen] : []).map(&:to_s).map(&:to_sym) & @medals.keys
107
+ end
108
+
109
+ def count(value) = value.is_a?(Numeric) && !value.negative? ? value : 0
110
+
111
+ def recent_game(saved)
112
+ return unless saved.is_a?(Hash) && Day.read(saved[:date])
113
+
114
+ ending = ENDINGS.find { |how| how.to_s == saved[:ending] } || :left
115
+ Recent.new(count(saved[:score]), count(saved[:length]), count(saved[:seconds]).to_f, ending,
116
+ Game.speed_named(saved[:speed]), Day.read(saved[:date]))
117
+ end
118
+
119
+ def save
120
+ @file.write(to_h)
121
+ end
122
+
123
+ # Each kind of rare fruit as two counts of its own, as the golden fruit's
124
+ # were kept before there were other kinds.
125
+ def to_h
126
+ rare = Game::RARE.flat_map { |kind| [[:"#{kind}_eaten", eaten(kind)], [:"#{kind}_seen", seen(kind)]] }.to_h
127
+ { games:, seconds: seconds.round(1), fruit:, **rare, moves:, longest:, endings: @endings, recent: recent.map(&:to_h),
128
+ medals: @medals, unseen: @unseen }
129
+ end
130
+ end
131
+ end