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,420 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The player's games added up, on a panel over the dimmed courtyard: how
5
+ # many there have been and for how long, the fruit and gold eaten, how
6
+ # far the snake has crawled, the longest it has grown and how the games
7
+ # ended, each beside a small picture of its own, and under them the plums,
8
+ # peppers and pears eaten of those that came out. Under them the last
9
+ # thirty games stand in a row of bars, the best one crowned. One bar is
10
+ # lit like a lantern and its game told above the row; the arrows, the
11
+ # wheel and the pointer move the light along. As the screen opens the
12
+ # totals count up, each ticking a step up the scale as it lands, and the
13
+ # bars rise after them one by one. A little abacus stands at each side
14
+ # of the title and counts along, a bead for every total that lands on
15
+ # its side of the panel.
16
+ #
17
+ # The totals, the rare fruit and the bars are laid out in the canvas's
18
+ # pixels, so on the fine canvas the lines stay evenly spaced and the
19
+ # bars evenly set, in a grid of whole pixels.
20
+ class Statistics
21
+ PANEL_X = 22
22
+ PANEL_Y = 23
23
+ PANEL_WIDTH = 192
24
+ PANEL_HEIGHT = 93
25
+ INSIDE = PANEL_X + 6
26
+ INSIDE_WIDTH = PANEL_WIDTH - 12
27
+ # The totals stand in two columns of four rows, and the rare fruit in a
28
+ # row of three under them.
29
+ TOTALS = [[:games, "GAMES"], [:fruit, "FRUIT"], [:time, "TIME"], [:golden, "GOLDEN"], [:moves, "CRAWLED"],
30
+ [:longest, "LONGEST"], [:wall, "INTO WALLS"], [:itself, "INTO ITSELF"]].freeze
31
+ RARE = %i[plum pepper pear].freeze
32
+ # Every total in the order they count up: the grid by rows, then the
33
+ # rare fruit.
34
+ COUNTED = [*TOTALS.map(&:first), *RARE].freeze
35
+ COLUMN = 94
36
+ COLUMN_WIDTH = 86
37
+ TOTALS_Y = PANEL_Y + 8
38
+ LINE = 9
39
+ RULE_Y = PANEL_Y + 52
40
+ HEADING_Y = PANEL_Y + 56
41
+ # The bars stand on the ground, a slot each.
42
+ GROUND = PANEL_Y + 83
43
+ TALLEST = 17
44
+ SLOT = 6
45
+ BAR = 4
46
+ # A bar's colors: its lit side, its body, its shaded side and its top.
47
+ PLAIN = %i[ash steel pewter ash].freeze
48
+ BEST = %i[gold amber orange cream].freeze
49
+ LIT = %i[amber red wine gold].freeze
50
+ COUNT_FROM = 0.25
51
+ COUNT = 0.7
52
+ STAGGER = 0.06
53
+ RISE_FROM = 0.55
54
+ RISE = 0.35
55
+ RISE_STAGGER = 0.02
56
+ SPACE = 6
57
+ # Between the title and each abacus.
58
+ ASIDE = 10
59
+ # The lines and the bars on the fine canvas, in its pixels.
60
+ FINE_LINE = 13
61
+ FINE_SLOT = 9
62
+ FINE_BAR = 6
63
+ FINE_TALLEST = 25
64
+
65
+ def initialize(stage)
66
+ @stage = stage
67
+ @stats = stage.stats
68
+ @courtyard = Courtyard.new(stage.rng, hud: false)
69
+ @title = Lettering.new("STATS")
70
+ @abaci = [Abacus.new((App::WIDTH - @title.width) / 2 - ASIDE - Abacus::WIDTH, PANEL_Y - 1, -1),
71
+ Abacus.new((App::WIDTH + @title.width) / 2 + ASIDE, PANEL_Y - 1, 1)]
72
+ @fireflies = Fireflies.new(stage.rng, 6)
73
+ @values = {
74
+ games: @stats.games, fruit: @stats.fruit, time: @stats.seconds, golden: @stats.eaten(:golden),
75
+ moves: @stats.moves, longest: @stats.longest, wall: @stats.ended(:wall), itself: @stats.ended(:itself),
76
+ **RARE.to_h { |kind| [kind, @stats.eaten(kind)] }
77
+ }
78
+ @spread = {}
79
+ @recent = @stats.recent
80
+ @top = [*@recent.map(&:score), 1].max
81
+ @best = @recent.index { |game| game.score == @top }
82
+ @selected = @recent.size - 1
83
+ @landed = 0
84
+ @time = 0.0
85
+ end
86
+
87
+ def focus = [App::WIDTH / 2, App::HEIGHT / 2]
88
+
89
+ def music = @stage.preferences.song.in_the_menus
90
+
91
+ def typing? = false
92
+
93
+ def update(dt)
94
+ @time += dt
95
+ [@courtyard, *@abaci, @fireflies].each { |part| part.update(dt) }
96
+ land while @landed < COUNTED.size && @time >= COUNT_FROM + @landed * STAGGER + COUNT
97
+ end
98
+
99
+ def draw(canvas)
100
+ @courtyard.draw(canvas)
101
+ canvas.recolor(Palette::DARKER[2])
102
+ @abaci.each { |abacus| abacus.draw(canvas) }
103
+ drop = ((1 - Ease.out_back(Ease.progress(@time, 0.0, 0.45))) * 30).round
104
+ @title.draw(canvas, (App::WIDTH - @title.width) / 2, PANEL_Y - @title.height - 3 - drop)
105
+ Panel.draw(canvas, PANEL_X, PANEL_Y, PANEL_WIDTH, PANEL_HEIGHT)
106
+ density = canvas.density
107
+ TOTALS.each_index { |index| draw_total(canvas.pixels, density, index) }
108
+ RARE.each_index { |index| draw_rare(canvas.pixels, density, index) }
109
+ canvas.fill(INSIDE, RULE_Y, INSIDE_WIDTH, 1, Palette[:violet])
110
+ draw_chart(canvas)
111
+ @fireflies.draw(canvas)
112
+ end
113
+
114
+ def press(action)
115
+ case action
116
+ when :left then select(@selected - 1)
117
+ when :right then select(@selected + 1)
118
+ when :top then select(0)
119
+ when :bottom then select(@recent.size - 1)
120
+ when :back, :confirm then leave
121
+ end
122
+ end
123
+
124
+ # The pointer lights the bar it is over and the wheel moves the light;
125
+ # a click off the panel goes back.
126
+ def point(x, y, kind)
127
+ case kind
128
+ when :move, :drag then select(slot_at(x, y))
129
+ when :wheel_up then select(@selected - 1)
130
+ when :wheel_down then select(@selected + 1)
131
+ when :click then on_panel?(x, y) ? select(slot_at(x, y)) : leave
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def leave
138
+ @stage.speaker.play(:back)
139
+ @stage.show(Title.new(@stage))
140
+ end
141
+
142
+ def select(slot)
143
+ return if slot.nil? || @recent.empty?
144
+
145
+ slot = slot.clamp(0, @recent.size - 1)
146
+ return if slot == @selected
147
+
148
+ @selected = slot
149
+ @stage.speaker.play(:tick, 1)
150
+ end
151
+
152
+ # A total has finished counting: it ticks a step higher than the last
153
+ # and the abacus on its side slides a bead across, unless it had
154
+ # nothing to count.
155
+ def land
156
+ if @values.fetch(COUNTED[@landed]).positive?
157
+ @stage.speaker.play(:tally, @landed)
158
+ @abaci[@landed % 2].count
159
+ end
160
+ @landed += 1
161
+ end
162
+
163
+ def on_panel?(x, y)
164
+ x.between?(PANEL_X, PANEL_X + PANEL_WIDTH - 1) && y.between?(PANEL_Y, PANEL_Y + PANEL_HEIGHT - 1)
165
+ end
166
+
167
+ def slot_at(x, y)
168
+ return unless y.between?(GROUND - TALLEST - 6, GROUND + 1)
169
+
170
+ slot = (x - INSIDE).div(SLOT)
171
+ slot if slot.between?(0, @recent.size - 1)
172
+ end
173
+
174
+ def font(density) = density.pick(Font::SMALL, Font::SMALL.fine)
175
+
176
+ # The top of a line of the grid, the rare fruit's line below its four,
177
+ # in pixels.
178
+ def line_top(density, line) = density.at(TOTALS_Y) + line * density.pick(LINE, FINE_LINE)
179
+
180
+ # A small picture centered on the line of text whose top is at y, in
181
+ # the room before the words.
182
+ def draw_icon(canvas, density, icon, x, y)
183
+ icon = density.pick(icon, icon.fine)
184
+ canvas.draw(icon, x + (density.pick(8, 12) - icon.width) / 2, y + density.pick(2, 3) - icon.height / 2)
185
+ end
186
+
187
+ # The picture, the name in steel, and the number counting up to its
188
+ # value on the right.
189
+ def draw_total(canvas, density, index)
190
+ name, label = TOTALS[index]
191
+ x = density.at(INSIDE + index % 2 * COLUMN)
192
+ y = line_top(density, index / 2)
193
+ font = font(density)
194
+ draw_icon(canvas, density, ICONS.fetch(name), x, y)
195
+ font.write(canvas, label, x + density.pick(10, 15), y, Palette[:steel], 16)
196
+ text = figure(name, Ease.out_cubic(Ease.progress(@time, COUNT_FROM + index * STAGGER, COUNT)))
197
+ right = density.at(INSIDE + index % 2 * COLUMN + COLUMN_WIDTH)
198
+ font.write(canvas, text, right - font.width(text), y, Palette[:cream], 16)
199
+ end
200
+
201
+ # Where each rare fruit's picture goes, the three spread from one side
202
+ # of the panel to the other by how wide their counts will be once they
203
+ # have counted up, so that nothing moves while they do.
204
+ def spread(density)
205
+ @spread[density.name] ||= begin
206
+ room = density.pick(10, 15)
207
+ left = density.at(INSIDE)
208
+ widths = RARE.map { |kind| room + font(density).width(figure(kind, 1.0)) }
209
+ gap = (density.at(INSIDE + INSIDE_WIDTH) - left - widths.sum) / (RARE.size - 1)
210
+ widths.each_index.map { |index| left + widths.first(index).sum + gap * index }
211
+ end
212
+ end
213
+
214
+ # A rare fruit's picture and its count of those that came out, counting
215
+ # up after the grid.
216
+ def draw_rare(canvas, density, index)
217
+ kind = RARE[index]
218
+ x = spread(density)[index]
219
+ y = line_top(density, 4)
220
+ draw_icon(canvas, density, ICONS.fetch(kind), x, y)
221
+ text = figure(kind, Ease.out_cubic(Ease.progress(@time, COUNT_FROM + (TOTALS.size + index) * STAGGER, COUNT)))
222
+ font(density).write(canvas, text, x + density.pick(10, 15), y, Palette[:cream], 16)
223
+ end
224
+
225
+ # A total as it reads a share of the way through counting up.
226
+ def figure(name, share)
227
+ value = @values.fetch(name) * share
228
+ case name
229
+ when :time then clock(value)
230
+ when :golden, *RARE then "#{grouped(value.round)} OF #{grouped(@stats.seen(name))}"
231
+ else grouped(value.round)
232
+ end
233
+ end
234
+
235
+ def grouped(number) = number.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
236
+
237
+ # 45S, 12M 05S, 3H 24M.
238
+ def clock(time)
239
+ hours, rest = time.floor.divmod(3600)
240
+ minutes, seconds = rest.divmod(60)
241
+ if hours.positive? then "#{hours}H #{minutes.to_s.rjust(2, "0")}M"
242
+ elsif minutes.positive? then "#{minutes}M #{seconds.to_s.rjust(2, "0")}S"
243
+ else "#{seconds}S"
244
+ end
245
+ end
246
+
247
+ def draw_chart(canvas)
248
+ density = canvas.density
249
+ pixels = canvas.pixels
250
+ Font::SMALL.draw(canvas, "RECENT", INSIDE + 1, HEADING_Y, Palette[:steel])
251
+ canvas.fill(INSIDE, GROUND + 1, INSIDE_WIDTH, 1, Palette[:violet])
252
+ return draw_waiting(canvas) if @recent.empty?
253
+
254
+ @recent.each_index { |slot| draw_bar(pixels, density, slot) }
255
+ return if @time < RISE_FROM
256
+
257
+ pointer = density.pick(POINTER, POINTER.fine)
258
+ x = bar_x(density, @selected) + (density.pick(BAR, FINE_BAR) - pointer.width) / 2
259
+ pixels.draw(pointer, x, density.at(GROUND) + density.pick(3, 4))
260
+ draw_readout(pixels, density)
261
+ end
262
+
263
+ def draw_waiting(canvas)
264
+ Font::SMALL.draw(canvas, "YOUR GAMES WILL STAND HERE", INSIDE, GROUND - 9, Palette[:steel], align: :center,
265
+ within: INSIDE_WIDTH)
266
+ end
267
+
268
+ def bar_x(density, slot) = density.at(INSIDE) + slot * density.pick(SLOT, FINE_SLOT) + 1
269
+
270
+ # Each bar springs up on its own beat, lit from the left and the top
271
+ # like everything else; the best wears the crown once it is up.
272
+ def draw_bar(canvas, density, slot)
273
+ game = @recent[slot]
274
+ rising = Ease.progress(@time, RISE_FROM + slot * RISE_STAGGER, RISE)
275
+ tallest = density.pick(TALLEST, FINE_TALLEST)
276
+ height = ([(game.score.fdiv(@top) * tallest).round, 1].max * Ease.out_back(rising)).round
277
+ return if height <= 0
278
+
279
+ bar = density.pick(BAR, FINE_BAR)
280
+ x = bar_x(density, slot)
281
+ top = density.at(GROUND) - height + 1
282
+ lit, body, shade, cap = colors(slot).map { |name| Palette[name] }
283
+ canvas.fill(x, top, bar, height, body)
284
+ canvas.fill(x, top, 1, height, lit)
285
+ canvas.fill(x + bar - 1, top, 1, height, shade)
286
+ canvas.fill(x, top, bar - 1, 1, cap)
287
+ return unless slot == @best && rising == 1
288
+
289
+ crown = density.pick(Hud::CROWN, Hud::CROWN.fine)
290
+ canvas.draw(crown, x + (bar - crown.width) / 2, top - crown.height - 1)
291
+ end
292
+
293
+ def colors(slot)
294
+ if slot == @selected then LIT
295
+ elsif slot == @best then BEST
296
+ else PLAIN
297
+ end
298
+ end
299
+
300
+ # The lit game told in full: its score, the snake's length, its speed
301
+ # and its day.
302
+ def draw_readout(canvas, density)
303
+ game = @recent[@selected]
304
+ font = font(density)
305
+ space = density.pick(SPACE, 8)
306
+ parts = [[grouped(game.score), :cream], ["LENGTH #{game.length}", :ash], [game.speed.to_s.upcase, :ash],
307
+ [day(game.date), :ash]]
308
+ x = density.at(INSIDE + INSIDE_WIDTH) - 1 - parts.sum { |text, _| font.width(text) } - space * (parts.size - 1)
309
+ parts.each do |text, color|
310
+ font.write(canvas, text, x, density.at(HEADING_Y), Palette[color], 16)
311
+ x += font.width(text) + space
312
+ end
313
+ end
314
+
315
+ def day(date) = Day.told(date)
316
+
317
+ LEGEND = {
318
+ "k" => :ink, "w" => :wine, "r" => :red, "o" => :orange, "a" => :amber, "g" => :gold,
319
+ "C" => :cream, "W" => :white, "v" => :violet, "p" => :pewter, "t" => :steel, "h" => :ash, "n" => :sand,
320
+ "d" => :deep, "P" => :pine, "j" => :jade, "f" => :fern, "l" => :leaf,
321
+ "b" => :blush, "m" => :mauve, "M" => :plum
322
+ }.freeze
323
+
324
+ # Under the lit bar, so even a game with a stub of a bar can be found.
325
+ POINTER = Sprite.parse(<<~ART, LEGEND)
326
+ .gg.
327
+ gaao
328
+ ART
329
+
330
+ ICONS = {
331
+ games: Sprite.parse(<<~ART, LEGEND),
332
+ ..PPP...
333
+ .PllgkP.
334
+ Pllllll.
335
+ ffffffrr
336
+ jjjjjjd.
337
+ .ddddd.r
338
+ ART
339
+ fruit: Hud::FRUIT,
340
+ time: Sprite.parse(<<~ART, LEGEND),
341
+ aaaaa
342
+ .CgC.
343
+ .CgC.
344
+ ..g..
345
+ .C.C.
346
+ .CgC.
347
+ ooooo
348
+ ART
349
+ golden: Sprite.parse(<<~ART, LEGEND),
350
+ ...f.
351
+ .wdw.
352
+ wWCgw
353
+ wCgaw
354
+ wgaow
355
+ .wow.
356
+ ART
357
+ plum: Sprite.parse(<<~ART, LEGEND),
358
+ ...f.
359
+ .wdw.
360
+ wbmmw
361
+ wmmmw
362
+ wmmMw
363
+ .wMw.
364
+ ART
365
+ pepper: Sprite.parse(<<~ART, LEGEND),
366
+ lj....
367
+ .jw...
368
+ .wgaw.
369
+ ..wraw
370
+ ...wrw
371
+ ....w.
372
+ ART
373
+ pear: Sprite.parse(<<~ART, LEGEND),
374
+ ...f.
375
+ ..w..
376
+ .wCw.
377
+ wCnhw
378
+ wnhtw
379
+ .www.
380
+ ART
381
+ moves: Sprite.parse(<<~ART, LEGEND, fine: <<~FINE),
382
+ ..ll....
383
+ .l..f..l
384
+ f....jf.
385
+ ART
386
+ ..lll.......
387
+ .l...f.....l
388
+ f.....f...j.
389
+ .......jjj..
390
+ FINE
391
+ longest: Sprite.parse(<<~ART, LEGEND, fine: <<~FINE),
392
+ .C...C.
393
+ CCCCCCC
394
+ .C...C.
395
+ ART
396
+ ..C.....C..
397
+ .CC.....CC.
398
+ CCCCCCCCCCC
399
+ .CC.....CC.
400
+ ..C.....C..
401
+ FINE
402
+ wall: Sprite.parse(<<~ART, LEGEND),
403
+ nhhvhhh
404
+ httvhtp
405
+ vvvvvvv
406
+ hvnhhvn
407
+ pvhtpvh
408
+ ART
409
+ itself: Sprite.parse(<<~ART, LEGEND)
410
+ .lll...
411
+ l...lgk
412
+ l....ff
413
+ f.....f
414
+ f.....j
415
+ .j...j.
416
+ ..jjj..
417
+ ART
418
+ }.freeze
419
+ end
420
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The first thing on screen: the logo over the attract demo, dimmed, and a
5
+ # menu to start a game, see the scores, look over the stats or the
6
+ # medals, change the settings or leave. Six buttons sit closer than other
7
+ # menus' so the logo, the panel and the hint all fit between the walls.
8
+ # While there are medals the player has not been to see, a star twinkles
9
+ # beside MEDALS.
10
+ class Title
11
+ CHOICES = %w[PLAY SCORES STATS MEDALS SETTINGS QUIT].freeze
12
+ # A game starts with a bright run up; leaving falls away.
13
+ SOUNDS = { "PLAY" => :start, "SCORES" => :press, "STATS" => :press, "MEDALS" => :press, "SETTINGS" => :press,
14
+ "QUIT" => :back }.freeze
15
+ LOGO_Y = 8
16
+ GLINT_EVERY = 4.5
17
+ GLINT_LENGTH = 0.8
18
+ PANEL = { x: 80, y: 26, width: 76 }.freeze
19
+ GAP = 1
20
+ HINT_Y = 109
21
+ PADDING = 5
22
+ HINT = "ENTER SELECT ESC QUIT"
23
+ TWINKLE = 0.35
24
+
25
+ def initialize(stage)
26
+ @stage = stage
27
+ @demo = Demo.new(stage, stage.scores.table(stage.preferences.speed).filter_map(&:replay))
28
+ @logo = Lettering.new("PIXELSNAKE")
29
+ @menu = Menu.new(CHOICES, x: PANEL[:x] + PADDING, y: PANEL[:y] + PADDING, width: PANEL[:width] - PADDING * 2,
30
+ speaker: stage.speaker, delay: 0.3, gap: GAP) do |canvas, index, x, y, color|
31
+ write(canvas, CHOICES[index], x, y, color)
32
+ end
33
+ @new_medals = !stage.stats.unseen.empty?
34
+ @fireflies = Fireflies.new(stage.rng, 9)
35
+ @time = 0.0
36
+ end
37
+
38
+ def focus = @menu.focus
39
+
40
+ def music = @stage.preferences.song.in_the_menus
41
+
42
+ def typing? = false
43
+
44
+ def update(dt)
45
+ @time += dt
46
+ [@demo, @menu, @fireflies].each { |part| part.update(dt) }
47
+ end
48
+
49
+ def draw(canvas)
50
+ @demo.draw(canvas)
51
+ canvas.recolor(Palette::DARKER[1])
52
+ draw_logo(canvas)
53
+ Panel.draw(canvas, PANEL[:x], PANEL[:y], PANEL[:width], @menu.height + PADDING * 2)
54
+ @menu.draw(canvas)
55
+ Font::SMALL.draw(canvas, HINT, 0, HINT_Y, Palette[:steel], align: :center, within: App::WIDTH)
56
+ @fireflies.draw(canvas)
57
+ end
58
+
59
+ def press(action)
60
+ case action
61
+ when :up then @menu.move(-1)
62
+ when :down then @menu.move(1)
63
+ when :confirm then choose
64
+ when :back then quit
65
+ end
66
+ end
67
+
68
+ # Hovering selects a button; clicking one chooses it.
69
+ def point(x, y, kind)
70
+ choose if @menu.point(x, y) && kind == :click
71
+ end
72
+
73
+ private
74
+
75
+ def choose
76
+ @menu.press
77
+ @stage.speaker.play(SOUNDS.fetch(@menu.selected))
78
+ case @menu.selected
79
+ when "PLAY" then @stage.show(Play.new(@stage), Iris)
80
+ when "SCORES" then @stage.show(Scores.new(@stage))
81
+ when "STATS" then @stage.show(Statistics.new(@stage))
82
+ when "MEDALS" then @stage.show(MedalCase.new(@stage))
83
+ when "SETTINGS" then @stage.show(Settings.new(@stage))
84
+ when "QUIT" then @stage.quit
85
+ end
86
+ end
87
+
88
+ def quit
89
+ @stage.speaker.play(:back)
90
+ @stage.quit
91
+ end
92
+
93
+ # A label in the middle of its button, and beside MEDALS a star while
94
+ # there is something new in the case, a few pixels after the word ends.
95
+ def write(canvas, label, x, y, color)
96
+ width = PANEL[:width] - PADDING * 2
97
+ Font::SMALL.draw(canvas, label, x, y, color, align: :center, within: width)
98
+ return unless label == "MEDALS" && @new_medals
99
+
100
+ density = canvas.density
101
+ font = density.pick(Font::SMALL, Font::SMALL.fine)
102
+ left = density.at(x)
103
+ right = left + (density.at(x + width) - left + font.width(label)) / 2
104
+ star = (@time / TWINKLE).floor.even? ? STAR : SPARK
105
+ star = density.pick(star, star.fine)
106
+ canvas.pixels.draw(star, right + density.pick(6, 8) - star.width / 2,
107
+ density.at(y) + density.pick(2, 3) - star.height / 2)
108
+ end
109
+
110
+ # The logo drops in with an overshoot, then every few seconds a glint
111
+ # runs across it.
112
+ def draw_logo(canvas)
113
+ x = (App::WIDTH - @logo.width) / 2
114
+ y = LOGO_Y - ((1 - Ease.out_back(Ease.progress(@time, 0.0, 0.55))) * 34).round
115
+ @logo.draw(canvas, x, y, glint: glint)
116
+ end
117
+
118
+ def glint
119
+ moment = (@time - 1.0) % GLINT_EVERY
120
+ return if @time < 1.0 || moment > GLINT_LENGTH
121
+
122
+ (Ease.in_out_sine(moment / GLINT_LENGTH) * (@logo.width + @logo.height)).round - @logo.height
123
+ end
124
+
125
+ STAR = Sprite.parse(<<~ART, { "a" => :amber, "g" => :gold, "w" => :white }, fine: <<~FINE)
126
+ ..a..
127
+ ..g..
128
+ agwga
129
+ ..g..
130
+ ..a..
131
+ ART
132
+ ...a...
133
+ ...g...
134
+ ..aga..
135
+ aggwgga
136
+ ..aga..
137
+ ...g...
138
+ ...a...
139
+ FINE
140
+ SPARK = Sprite.parse(<<~ART, { "a" => :amber, "g" => :gold }, fine: <<~FINE)
141
+ .a.
142
+ aga
143
+ .a.
144
+ ART
145
+ ..a..
146
+ ..g..
147
+ aggga
148
+ ..g..
149
+ ..a..
150
+ FINE
151
+ end
152
+ end