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,268 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The medals hung in two rows on brass rails, on a panel over the dimmed
5
+ # courtyard: the courtyard's small wonders above, its long games below,
6
+ # those earned in brass on their own ribbons and the rest in dark iron.
7
+ # One is chosen, a soft light behind it, and told under the rails: its
8
+ # name, the day it was earned or that it is still to come, and what it
9
+ # asks. The arrows, Home and End and the pointer choose, and a medal
10
+ # chosen swings a little on its ribbon before it settles.
11
+ #
12
+ # As the case opens the title drops in between two sprigs of laurel that
13
+ # grow up beside it, and the medals are hung on the rails one after
14
+ # another. Those earned since the case was last opened flash as they
15
+ # land and throw sparkles, the first of them chosen; after that the
16
+ # light crosses the brass ones in turn.
17
+ class MedalCase
18
+ PANEL_X = 22
19
+ PANEL_Y = 23
20
+ PANEL_WIDTH = 192
21
+ PANEL_HEIGHT = 93
22
+ INSIDE = PANEL_X + 6
23
+ INSIDE_WIDTH = PANEL_WIDTH - 12
24
+ ROW = 6
25
+ RAILS = [PANEL_Y + 6, PANEL_Y + 34].freeze
26
+ STEP = 29
27
+ FIRST = PANEL_X + (PANEL_WIDTH - (ROW - 1) * STEP - MedalView::WIDTH) / 2
28
+ RULE_Y = PANEL_Y + 66
29
+ NAME_Y = RULE_Y + 6
30
+ GOAL_Y = RULE_Y + 15
31
+ # Between the title and each sprig.
32
+ ASIDE = 8
33
+ HANG_FROM = 0.3
34
+ HANG = 0.35
35
+ STAGGER = 0.05
36
+ DROP = 8
37
+ SWING = 0.9
38
+ GLINT = 0.5
39
+ GLINT_EVERY = 1.4
40
+ SPARKLES = %i[white cream gold amber].freeze
41
+
42
+ def initialize(stage)
43
+ @stage = stage
44
+ @stats = stage.stats
45
+ @courtyard = Courtyard.new(stage.rng, hud: false)
46
+ @title = Lettering.new("MEDALS")
47
+ @laurels = [Laurel.new((App::WIDTH - @title.width) / 2 - ASIDE - Laurel::WIDTH, PANEL_Y + 1, :left, glint_from: 1.6),
48
+ Laurel.new((App::WIDTH + @title.width) / 2 + ASIDE, PANEL_Y + 1, :right, glint_from: 3.6)]
49
+ @sparkles = Particles.new(stage.rng)
50
+ @fireflies = Fireflies.new(stage.rng, 6)
51
+ @new = @stats.unseen.sort_by { |medal| Medal::ALL.index(medal) }
52
+ @stats.see_medals
53
+ @earned = Medal::ALL.each_index.select { |index| @stats.earned?(Medal::ALL[index]) }
54
+ @chosen = Medal::ALL.index(@new.first) || 0
55
+ @chose = -SWING
56
+ @flashed = 0
57
+ @time = 0.0
58
+ end
59
+
60
+ def focus = [App::WIDTH / 2, App::HEIGHT / 2]
61
+
62
+ def music = @stage.preferences.song.in_the_menus
63
+
64
+ def typing? = false
65
+
66
+ def update(dt)
67
+ @time += dt
68
+ [@courtyard, *@laurels, @sparkles, @fireflies].each { |part| part.update(dt) }
69
+ flash while @flashed < @new.size && @time >= landed(Medal::ALL.index(@new[@flashed]))
70
+ end
71
+
72
+ def draw(canvas)
73
+ @courtyard.draw(canvas)
74
+ canvas.recolor(Palette::DARKER[2])
75
+ @laurels.each { |laurel| laurel.draw(canvas) }
76
+ drop = ((1 - Ease.out_back(Ease.progress(@time, 0.0, 0.45))) * 30).round
77
+ @title.draw(canvas, (App::WIDTH - @title.width) / 2, PANEL_Y - @title.height - 3 - drop)
78
+ Panel.draw(canvas, PANEL_X, PANEL_Y, PANEL_WIDTH, PANEL_HEIGHT)
79
+ RAILS.each { |y| draw_rail(canvas, y) }
80
+ canvas.clip(PANEL_X + 3, PANEL_Y + 3, PANEL_WIDTH - 6, PANEL_HEIGHT - 6) do
81
+ Medal::ALL.each_index { |index| draw_medal(canvas, index) }
82
+ end
83
+ draw_count(canvas)
84
+ draw_told(canvas)
85
+ @sparkles.draw(canvas)
86
+ @fireflies.draw(canvas)
87
+ end
88
+
89
+ def press(action)
90
+ case action
91
+ when :left then choose(@chosen - 1)
92
+ when :right then choose(@chosen + 1)
93
+ when :up then choose(@chosen - ROW)
94
+ when :down then choose(@chosen + ROW)
95
+ when :top then choose(0)
96
+ when :bottom then choose(Medal::ALL.size - 1)
97
+ when :back, :confirm then leave
98
+ end
99
+ end
100
+
101
+ # The pointer chooses the medal it is over; a click off the panel goes
102
+ # back.
103
+ def point(x, y, kind)
104
+ index = medal_at(x, y)
105
+ case kind
106
+ when :move, :drag then choose(index) if index
107
+ when :click then on_panel?(x, y) ? (choose(index) if index) : leave
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def leave
114
+ @stage.speaker.play(:back)
115
+ @stage.show(Title.new(@stage))
116
+ end
117
+
118
+ # A step off the end of the case, or up from its top row, goes nowhere.
119
+ def choose(index)
120
+ return if index == @chosen || !index.between?(0, Medal::ALL.size - 1)
121
+
122
+ @chosen = index
123
+ @chose = @time
124
+ @stage.speaker.play(:tick, 1)
125
+ end
126
+
127
+ # A medal just earned flashes as it lands and throws a few sparkles.
128
+ def flash
129
+ x, y = spot(Medal::ALL.index(@new[@flashed]))
130
+ @sparkles.burst(x + MedalView::WIDTH / 2, y + MedalView::DISC_Y + 7, SPARKLES, count: 12, speed: 40, gravity: 70.0, sparkle: true)
131
+ @flashed += 1
132
+ end
133
+
134
+ def spot(index) = [FIRST + index % ROW * STEP, RAILS[index / ROW] + 1]
135
+
136
+ def landed(index) = HANG_FROM + index * STAGGER + HANG
137
+
138
+ def medal_at(x, y)
139
+ Medal::ALL.each_index.find do |index|
140
+ left, top = spot(index)
141
+ x.between?(left - 3, left + MedalView::WIDTH + 2) && y.between?(top, top + MedalView::HEIGHT)
142
+ end
143
+ end
144
+
145
+ def on_panel?(x, y)
146
+ x.between?(PANEL_X, PANEL_X + PANEL_WIDTH - 1) && y.between?(PANEL_Y, PANEL_Y + PANEL_HEIGHT - 1)
147
+ end
148
+
149
+ # A brass rod with a stud at each end, a shadow under it. It is laid
150
+ # in pixels, its lit top two deep on the fine canvas, so the rod is
151
+ # whole there.
152
+ def draw_rail(canvas, y)
153
+ density = canvas.density
154
+ pixels = canvas.pixels
155
+ left = density.at(INSIDE + 2)
156
+ width = density.at(INSIDE + INSIDE_WIDTH - 2) - left
157
+ top = density.at(y)
158
+ lit = density.pick(1, 2)
159
+ pixels.fill(left, top, width, lit, Palette[:gold])
160
+ pixels.fill(left, top + lit, width, 1, Palette[:orange])
161
+ pixels.fill(left, top + lit + 1, width, 1, Palette[:night])
162
+ [INSIDE, INSIDE + INSIDE_WIDTH - 4].each { |x| canvas.draw(STUD, x, y - 1) }
163
+ end
164
+
165
+ # Each medal drops onto its rail on its own beat. The chosen one has a
166
+ # light behind it and swings, its ribbon bending, until it settles.
167
+ def draw_medal(canvas, index)
168
+ hanging = Ease.progress(@time, landed(index) - HANG, HANG)
169
+ return if hanging.zero?
170
+
171
+ x, y = spot(index)
172
+ y -= ((1 - Ease.out_back(hanging)) * DROP).round
173
+ medal = Medal::ALL[index]
174
+ earned = @earned.include?(index)
175
+ canvas.draw(HALO, x + MedalView::WIDTH / 2 - HALO.width / 2, y + MedalView::DISC_Y + 7 - HALO.height / 2) if index == @chosen
176
+ swing = index == @chosen ? self.swing : 0
177
+ [[0, 5, 0], [5, MedalView::DISC_Y - 5, swing / 2], [MedalView::DISC_Y, MedalView::HEIGHT, swing]].each do |top, rows, dx|
178
+ canvas.clip(x + dx - 1, y + top, MedalView::WIDTH + 2, rows) do
179
+ MedalView.draw(canvas, medal, x + dx, y, earned:, glint: glint(index))
180
+ end
181
+ end
182
+ end
183
+
184
+ # Out to one side and back, a little less each time.
185
+ def swing
186
+ moment = @time - @chose
187
+ return 0 if moment > SWING
188
+
189
+ (Math.sin(moment * Math::PI * 4) * 2.2 * (1 - moment / SWING)).round
190
+ end
191
+
192
+ # The brass ones take turns to catch the light, one every so often; a
193
+ # new one catches it as it lands.
194
+ def glint(index)
195
+ return unless @earned.include?(index)
196
+
197
+ moment = @time - landed(index)
198
+ return front(moment) if @new.include?(Medal::ALL[index]) && moment.between?(0, GLINT)
199
+
200
+ turn = (@time - landed(Medal::ALL.size - 1) - 1.0) / GLINT_EVERY
201
+ return if turn.negative? || @earned[turn.floor % @earned.size] != index
202
+
203
+ moment = (turn - turn.floor) * GLINT_EVERY
204
+ front(moment) if moment < GLINT
205
+ end
206
+
207
+ def front(moment) = (Ease.in_out_sine(moment / GLINT) * (MedalView.glint_length + 2)).round - 1
208
+
209
+ # The rule under the rails, broken in the middle by how many there are,
210
+ # the gap fitted to the words in the canvas's own letters.
211
+ def draw_count(canvas)
212
+ density = canvas.density
213
+ pixels = canvas.pixels
214
+ font = density.pick(Font::SMALL, Font::SMALL.fine)
215
+ text = "#{@stats.medals} OF #{Medal::ALL.size}"
216
+ width = font.width(text)
217
+ left = density.at(INSIDE)
218
+ middle = density.at(PANEL_X + PANEL_WIDTH / 2)
219
+ rule = density.at(RULE_Y)
220
+ margin = density.pick(5, 7)
221
+ violet = Palette[:violet]
222
+ pixels.fill(left, rule, middle - width / 2 - margin - left, 1, violet)
223
+ pixels.fill(middle + width / 2 + margin, rule, density.at(INSIDE + INSIDE_WIDTH) - middle - width / 2 - margin, 1, violet)
224
+ font.write(pixels, text, middle - width / 2, rule - density.pick(2, 3), Palette[:steel], 16)
225
+ end
226
+
227
+ # The chosen medal's name, when it was earned and what it asks.
228
+ def draw_told(canvas)
229
+ medal = Medal::ALL[@chosen]
230
+ day = @stats.earned(medal)
231
+ Font::SMALL.draw(canvas, medal.name, INSIDE + 1, NAME_Y, Palette[day ? :gold : :ash])
232
+ told = day ? Day.told(day) : "NOT YET"
233
+ Font::SMALL.draw(canvas, told, INSIDE + INSIDE_WIDTH - 1, NAME_Y, Palette[day ? :leaf : :steel], align: :right)
234
+ Font::SMALL.draw(canvas, medal.goal, INSIDE + 1, GOAL_Y, Palette[day ? :cream : :steel])
235
+ end
236
+
237
+ STUD = Sprite.parse(<<~ART, "w" => :wine, "a" => :amber, "g" => :gold)
238
+ gaaw
239
+ aaaw
240
+ aaaw
241
+ wwww
242
+ ART
243
+ # A soft light behind the chosen medal.
244
+ HALO = Sprite.parse(<<~ART, "v" => :violet)
245
+ ......vvvvvvvvv......
246
+ ....vvvvvvvvvvvvv....
247
+ ...vvvvvvvvvvvvvvv...
248
+ ..vvvvvvvvvvvvvvvvv..
249
+ .vvvvvvvvvvvvvvvvvvv.
250
+ .vvvvvvvvvvvvvvvvvvv.
251
+ vvvvvvvvvvvvvvvvvvvvv
252
+ vvvvvvvvvvvvvvvvvvvvv
253
+ vvvvvvvvvvvvvvvvvvvvv
254
+ vvvvvvvvvvvvvvvvvvvvv
255
+ vvvvvvvvvvvvvvvvvvvvv
256
+ vvvvvvvvvvvvvvvvvvvvv
257
+ vvvvvvvvvvvvvvvvvvvvv
258
+ vvvvvvvvvvvvvvvvvvvvv
259
+ vvvvvvvvvvvvvvvvvvvvv
260
+ .vvvvvvvvvvvvvvvvvvv.
261
+ .vvvvvvvvvvvvvvvvvvv.
262
+ ..vvvvvvvvvvvvvvvvv..
263
+ ...vvvvvvvvvvvvvvv...
264
+ ....vvvvvvvvvvvvv....
265
+ ......vvvvvvvvv......
266
+ ART
267
+ end
268
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The game held still under a dim, with a panel to resume or leave, and
5
+ # to set the sounds and the music without leaving the game. On the way
6
+ # in the dim dithers over the courtyard and the panel drops from above;
7
+ # on the way out the panel lifts away and the dim clears, and the game
8
+ # counts down before it moves again.
9
+ #
10
+ # The sounds and the music each have a row with their tool from the
11
+ # settings, in its colors while they are on, and a meter. Left and right
12
+ # turn one down and up, and Enter switches it off, or back on as loud as
13
+ # it was. Every change is saved at once, and heard: a sound plays at the
14
+ # new volume, and the music answers through the door.
15
+ class Pause
16
+ CHOICES = %w[RESUME SOUND MUSIC MENU].freeze
17
+ PANEL_X = 76
18
+ PANEL_Y = 44
19
+ PANEL_WIDTH = 84
20
+ PADDING = 6
21
+ ROW_WIDTH = PANEL_WIDTH - PADDING * 2
22
+ PANEL_HEIGHT = CHOICES.size * (Menu::BUTTON + Menu::GAP) - Menu::GAP + PADDING * 2
23
+ # Where a row's tool, name and meter go, from the row's left edge. The
24
+ # tool and the name stand where they do in the settings, and the meter
25
+ # ends as far from the right edge as the tool starts from the left.
26
+ TOOL_X = 3
27
+ LABEL_X = 12
28
+ METER_X = 40
29
+ METER_WIDTH = ROW_WIDTH - METER_X - TOOL_X
30
+ ENTER = 0.32
31
+ LEAVE = 0.22
32
+ # The panel drops in from just showing at the top of the screen, and
33
+ # lifts away until it is gone.
34
+ DROP = 90
35
+ LIFT = PANEL_Y + PANEL_HEIGHT + 1
36
+
37
+ def initialize(stage, play)
38
+ @stage = stage
39
+ @play = play
40
+ @preferences = stage.preferences
41
+ @speaker = stage.speaker
42
+ @title = Lettering.new("PAUSED", :stone)
43
+ @menu = Menu.new(CHOICES, x: PANEL_X + PADDING, y: PANEL_Y + PADDING, width: ROW_WIDTH, speaker: @speaker,
44
+ delay: 0.12, &method(:write_row))
45
+ @toolbox = Toolbox.new(@preferences)
46
+ @meters = { "SOUND" => Meter.new(@preferences.volume), "MUSIC" => Meter.new(@preferences.music) }
47
+ @time = 0.0
48
+ @leaving = nil
49
+ end
50
+
51
+ def focus = @menu.focus
52
+
53
+ # The game's music carries on behind the panel, as through a door.
54
+ def music = @music ||= @play.music.muffle
55
+
56
+ def typing? = false
57
+
58
+ def update(dt)
59
+ @time += dt
60
+ [@menu, @toolbox, *@meters.values].each { |part| part.update(dt) }
61
+ @play.resume if @leaving && @time >= @leaving + LEAVE
62
+ end
63
+
64
+ def draw(canvas)
65
+ @play.draw(canvas)
66
+ canvas.dim(@leaving ? 1 - Ease.progress(@time, @leaving, LEAVE) : Ease.progress(@time, 0.0, ENTER))
67
+ offset = drop
68
+ @title.draw(canvas, (App::WIDTH - @title.width) / 2, PANEL_Y - @title.height - 2 + offset)
69
+ Panel.draw(canvas, PANEL_X, PANEL_Y + offset, PANEL_WIDTH, PANEL_HEIGHT)
70
+ @menu.draw(canvas) if offset.zero?
71
+ end
72
+
73
+ def press(action)
74
+ return if @leaving
75
+
76
+ case action
77
+ when :up then @menu.move(-1)
78
+ when :down then @menu.move(1)
79
+ when :left then turn(-1)
80
+ when :right then turn(1)
81
+ when :confirm then choose
82
+ when :pause, :back then leave
83
+ end
84
+ end
85
+
86
+ # Hovering selects a button and clicking chooses it. On a row with a
87
+ # meter, a click past the name turns the meter down on its left half
88
+ # and up on its right half.
89
+ def point(x, y, kind)
90
+ return if @leaving
91
+ return unless @menu.point(x, y) && kind == :click
92
+
93
+ along = x - PANEL_X - PADDING
94
+ if @meters.key?(@menu.selected) && along > LABEL_X + Font::SMALL.width(@menu.selected)
95
+ turn(along < METER_X + METER_WIDTH / 2 ? -1 : 1)
96
+ else
97
+ choose
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def choose
104
+ case @menu.selected
105
+ when "RESUME" then leave
106
+ when "MENU"
107
+ @menu.press
108
+ @play.abandon
109
+ @speaker.play(:press)
110
+ @stage.show(Title.new(@stage), Iris)
111
+ else switch(@menu.selected)
112
+ end
113
+ end
114
+
115
+ def switch(row)
116
+ row == "SOUND" ? @preferences.switch_sounds : @preferences.switch_music
117
+ changed(row)
118
+ end
119
+
120
+ # A step down or up the meter, as far as its ends.
121
+ def turn(by)
122
+ row = @menu.selected
123
+ volume = @meters[row]&.beside(by)
124
+ return unless volume
125
+
126
+ row == "SOUND" ? @preferences.volume = volume : @preferences.music = volume
127
+ changed(row, by)
128
+ end
129
+
130
+ # The meter follows, the lantern sinks for a moment, and a sound plays
131
+ # at the new volume to judge it by; the music answers by itself.
132
+ def changed(row, by = 0)
133
+ @meters[row].turn(volume(row), by:)
134
+ @menu.tap
135
+ @speaker.play(:press)
136
+ end
137
+
138
+ def volume(row) = row == "SOUND" ? @preferences.volume : @preferences.music
139
+
140
+ def leave
141
+ @leaving = @time
142
+ @speaker.play(:resume)
143
+ end
144
+
145
+ # How far above its place the panel is: it drops in with an overshoot
146
+ # and leaves by lifting back up after a little dip.
147
+ def drop
148
+ if @leaving
149
+ -(Ease.in_back(Ease.progress(@time, @leaving, LEAVE)) * LIFT).round
150
+ else
151
+ -((1 - Ease.out_back(Ease.progress(@time, 0.0, ENTER))) * DROP).round
152
+ end
153
+ end
154
+
155
+ # RESUME and MENU are written in the middle of their buttons; the sounds
156
+ # and the music have their tool, awake while they are on, their name
157
+ # and their meter.
158
+ def write_row(canvas, index, x, y, color)
159
+ row = CHOICES[index]
160
+ meter = @meters[row]
161
+ if meter
162
+ chosen = row == @menu.selected
163
+ @toolbox.draw(canvas, row, x + TOOL_X, y, lit: chosen, awake: meter.volume != :off)
164
+ Font::SMALL.draw(canvas, row, x + LABEL_X, y, color)
165
+ meter.draw(canvas, x + METER_X, y, METER_WIDTH, chosen:)
166
+ else
167
+ Font::SMALL.draw(canvas, row, x, y, color, align: :center, within: ROW_WIDTH)
168
+ end
169
+ end
170
+ end
171
+ end