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,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A word in the big font, finished like a painted sign: filled with a ramp
5
+ # that is lightest at the top, bevelled where the light from the upper
6
+ # left catches each stroke, outlined, and lifted off what is behind it by
7
+ # a shadow down and to the right. A glint is a diagonal band of light run
8
+ # across the fill. On the fine canvas the word is painted again in the
9
+ # fine letters, in the middle of where the standard one would stand.
10
+ class Lettering
11
+ STYLES = {
12
+ gold: { ramp: %i[cream gold gold amber amber amber orange orange red], outline: :wine },
13
+ jade: { ramp: %i[lime leaf leaf fern fern fern jade jade pine], outline: :deep },
14
+ stone: { ramp: %i[white cream sand sand sand ash ash steel steel], outline: :ink }
15
+ }.freeze
16
+ GLINT = 3
17
+
18
+ # The size and the sprite are the standard word's, which is how the
19
+ # scenes lay it out.
20
+ attr_reader :width, :height, :sprite
21
+
22
+ def initialize(text, style = :gold, font: Font::BIG)
23
+ @text = text
24
+ @style = STYLES.fetch(style)
25
+ @font = font
26
+ @standard = Sign.new(text, font, @style, GLINT)
27
+ @width = @standard.width
28
+ @height = @standard.height
29
+ @sprite = @standard.sprite
30
+ end
31
+
32
+ # The word as painted for a density, which the countdown squashes.
33
+ def sign(density) = density.fine? ? fine : @standard
34
+
35
+ def draw(canvas, x, y, glint: nil, map: Palette::IDENTITY)
36
+ density = canvas.density
37
+ sign = sign(density)
38
+ left = density.at(x) + (density.at(x + width) - density.at(x) - sign.width) / 2
39
+ top = density.at(y) + (density.at(y + height) - density.at(y) - sign.height) / 2
40
+ canvas.pixels.draw(sign.sprite, left, top, map)
41
+ sign.shine(canvas.pixels, left, top, density.at(glint)) if glint
42
+ end
43
+
44
+ private
45
+
46
+ def fine = @fine ||= Sign.new(@text, @font.fine, @style, (GLINT * Density::FINE.scale).floor)
47
+
48
+ # The word painted in one font.
49
+ class Sign
50
+ attr_reader :width, :height, :sprite
51
+
52
+ def initialize(text, font, style, glint)
53
+ @style = style
54
+ @glint = glint
55
+ @letter_height = font.height
56
+ @width = font.width(text) + 3
57
+ @height = font.height + 3
58
+ @ink = mask(text, font)
59
+ @fill = rows { |x, y| fill_color(x, y) if ink?(x, y) }
60
+ @sprite = Sprite.new(rows { |x, y| ink?(x, y) ? @fill[y].getbyte(x) : edge_color(x, y) })
61
+ end
62
+
63
+ def shine(canvas, x, y, position)
64
+ lighter = Palette::LIGHTER[2]
65
+ @fill.each_with_index do |row, sy|
66
+ (position - sy...position - sy + @glint).each do |sx|
67
+ next unless sx.between?(0, @width - 1)
68
+
69
+ color = row.getbyte(sx)
70
+ canvas.plot(x + sx, y + sy, lighter.getbyte(color)) unless color == Sprite::CLEAR
71
+ end
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def mask(text, font)
78
+ canvas = Canvas.new(@width, @height)
79
+ font.write(canvas, text, 1, 1, 1, 16)
80
+ canvas.rows.map { |row| row.bytes.map { |color| color == 1 } }
81
+ end
82
+
83
+ def rows
84
+ Array.new(@height) { |y| Array.new(@width) { |x| yield(x, y) || Sprite::CLEAR }.pack("C*") }
85
+ end
86
+
87
+ def ink?(x, y) = x >= 0 && y >= 0 && x < @width && y < @height && @ink[y][x]
88
+
89
+ # The ramp runs down the letters; edges facing the light are a step
90
+ # brighter and edges facing away a step darker.
91
+ def fill_color(x, y)
92
+ ramp = @style[:ramp]
93
+ step = ((y - 1) * ramp.size / @letter_height).clamp(0, ramp.size - 1)
94
+ lit = !ink?(x, y - 1) || !ink?(x - 1, y)
95
+ shaded = !ink?(x, y + 1) || !ink?(x + 1, y)
96
+ step -= 1 if lit && !shaded
97
+ step += 1 if shaded && !lit
98
+ Palette[ramp[step.clamp(0, ramp.size - 1)]]
99
+ end
100
+
101
+ def edge_color(x, y)
102
+ around = [-1, 0, 1].product([-1, 0, 1])
103
+ if around.any? { |dx, dy| ink?(x + dx, y + dy) }
104
+ Palette[@style[:outline]]
105
+ elsif around.any? { |dx, dy| ink?(x + dx - 1, y + dy - 1) }
106
+ Palette[:ink]
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Something worth doing in a game, earned once and kept for good: its
5
+ # name and what it asks. The first row of the case is the courtyard's
6
+ # small wonders, the second its long games.
7
+ Medal = Data.define(:key, :name, :goal)
8
+
9
+ class Medal
10
+ ALL = [
11
+ new(:first_gold, "FIRST GOLD", "EAT A GOLDEN FRUIT"),
12
+ new(:just_in_time, "JUST IN TIME", "EAT A RARE FRUIT ON ITS LAST SPARKLE"),
13
+ new(:ouroboros, "OUROBOROS", "FOLLOW YOUR OWN TAIL FOR TEN MOVES"),
14
+ new(:fire_eater, "FIRE EATER", "EAT THREE FRUIT UNDER ONE PEPPER"),
15
+ new(:slow_dance, "SLOW DANCE", "EAT THREE FRUIT UNDER ONE PEAR"),
16
+ new(:full_basket, "FULL BASKET", "EAT ALL FOUR RARE FRUIT IN ONE GAME"),
17
+ new(:four_figures, "FOUR FIGURES", "SCORE 1,000 IN ONE GAME"),
18
+ new(:quicksilver, "QUICKSILVER", "REACH ×5 AT FAST SPEED"),
19
+ new(:gold_rush, "GOLD RUSH", "EAT THREE GOLDEN FRUIT IN ONE GAME"),
20
+ new(:top_lantern, "TOP LANTERN", "REACH ×9"),
21
+ new(:white_heat, "WHITE HEAT", "EAT A FRUIT UNDER A PEPPER AT ×9"),
22
+ new(:great_coil, "GREAT COIL", "FILL A QUARTER OF THE COURTYARD")
23
+ ].freeze
24
+
25
+ KEYS = ALL.map(&:key).freeze
26
+
27
+ def self.named(key) = ALL.find { |medal| medal.key == key }
28
+ end
29
+ end
@@ -0,0 +1,428 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # How a medal looks: a brass disc on a short ribbon of its own colors,
5
+ # its picture struck into the face, or the same in dark iron on a grey
6
+ # ribbon while it is still to be earned. The light comes from the top
7
+ # left: the rim catches it on that side and the bevel inside the rim on
8
+ # the other, and the struck picture is bright along its far edge, where
9
+ # the light falls into it. A glint crosses a brass one corner to corner.
10
+ # On the fine canvas the disc is twenty-two pixels across, its rim two
11
+ # deep, and its picture drawn again with room for what it shows.
12
+ module MedalView
13
+ WIDTH = 15
14
+ HEIGHT = 24
15
+ # The disc's top row, under the ribbon and its ring.
16
+ DISC_Y = 9
17
+ FINE_WIDTH = 22
18
+ FINE_HEIGHT = 36
19
+ FINE_DISC_Y = 14
20
+ GLINT_STEPS = [1, 2, 1].freeze
21
+
22
+ module_function
23
+
24
+ # Placed in points and drawn in the canvas's pixels. A glint is how far
25
+ # its band has come across the standard disc, in diagonals from the top
26
+ # left corner; the fine disc's band goes as far across its own.
27
+ def draw(canvas, medal, x, y, earned:, glint: nil)
28
+ density = canvas.density
29
+ paint(canvas.pixels, density, medal, density.at(x), density.at(y), earned:, glint:)
30
+ end
31
+
32
+ # The same, at a pixel of a canvas of pixels.
33
+ def paint(canvas, density, medal, x, y, earned:, glint: nil)
34
+ sprite = (earned ? BRASS : IRON).fetch(medal.key)
35
+ canvas.draw(density.pick(sprite, sprite.fine), x, y)
36
+ return unless earned && glint
37
+
38
+ draw_glint(canvas, x, y + density.pick(DISC_Y, FINE_DISC_Y), (glint * density.scale).round,
39
+ density.pick(DIAGONALS, FINE_DIAGONALS))
40
+ end
41
+
42
+ # Diagonals across the disc, for a glint to come all the way over.
43
+ def glint_length = WIDTH * 2
44
+
45
+ def draw_glint(canvas, x, y, front, diagonals)
46
+ GLINT_STEPS.each_with_index do |steps, offset|
47
+ diagonals[front - offset]&.each { |dx, dy| canvas.shade(x + dx, y + dy, 1, 1, Palette::LIGHTER[steps]) }
48
+ end
49
+ end
50
+
51
+ DISC = <<~ART.lines(chomp: true).freeze
52
+ .....ooooo.....
53
+ ...ooLLLLLoo...
54
+ ..oLLrrrrrLLo..
55
+ .oLrrfffffrlRo.
56
+ .oLrffffffflRo.
57
+ oLrffffffffflRo
58
+ oLrffffffffflRo
59
+ oLrffffffffflRo
60
+ oLrffffffffflRo
61
+ oLrffffffffflRo
62
+ .oLrffffffflRo.
63
+ .oLllfffffllRo.
64
+ ..oRRlllllRRo..
65
+ ...ooRRRRRoo...
66
+ .....ooooo.....
67
+ ART
68
+ RIBBON = <<~ART.lines(chomp: true).freeze
69
+ ....EabbbaE....
70
+ ....EabbbaE....
71
+ ....EabbbaE....
72
+ ....EabbbaE....
73
+ ....EabbbaE....
74
+ ....EabbbaE....
75
+ ....EabbbaE....
76
+ .....Eabaa.....
77
+ ......ggg......
78
+ ART
79
+ FINE_DISC = <<~ART.lines(chomp: true).freeze
80
+ .......oooooooo.......
81
+ .....ooLLLLLLLLoo.....
82
+ ....oLLLLLLLLLLLLo....
83
+ ...oLLLrrrrrrrrLLLo...
84
+ ..oLLrrffffffffrrRRo..
85
+ .oLLrfffffffffffflRRo.
86
+ .oLLrfffffffffffflRRo.
87
+ oLLrfffffffffffffflRRo
88
+ oLLrfffffffffffffflRRo
89
+ oLLrfffffffffffffflRRo
90
+ oLLrfffffffffffffflRRo
91
+ oLLrfffffffffffffflRRo
92
+ oLLrfffffffffffffflRRo
93
+ oLLrfffffffffffffflRRo
94
+ oLLrfffffffffffffflRRo
95
+ .oLLrfffffffffffflRRo.
96
+ .oLLrfffffffffffflRRo.
97
+ ..oLRllffffffffllRRo..
98
+ ...oRRRllllllllRRRo...
99
+ ....oRRRRRRRRRRRRo....
100
+ .....ooRRRRRRRRoo.....
101
+ .......oooooooo.......
102
+ ART
103
+ FINE_RIBBON = <<~ART.lines(chomp: true).freeze
104
+ ......EaabbbbaaE......
105
+ ......EaabbbbaaE......
106
+ ......EaabbbbaaE......
107
+ ......EaabbbbaaE......
108
+ ......EaabbbbaaE......
109
+ ......EaabbbbaaE......
110
+ ......EaabbbbaaE......
111
+ ......EaabbbbaaE......
112
+ ......EaabbbbaaE......
113
+ ......EaabbbbaaE......
114
+ ......EaabbbbaaE......
115
+ ......EaabbbbaaE......
116
+ .......Eaabbaa........
117
+ .........gggg.........
118
+ ART
119
+ PICTURES = {
120
+ first_gold: <<~ART,
121
+ ....x..
122
+ ...x...
123
+ .xxxxx.
124
+ xxxxxxx
125
+ xxxxxxx
126
+ .xxxxx.
127
+ ..xxx..
128
+ ART
129
+ just_in_time: <<~ART,
130
+ .xxxxx.
131
+ .x...x.
132
+ ..x.x..
133
+ ...x...
134
+ ..x.x..
135
+ .xxxxx.
136
+ .xxxxx.
137
+ ART
138
+ ouroboros: <<~ART,
139
+ ..xxxx.
140
+ .x...xx
141
+ x....xx
142
+ x......
143
+ x.....x
144
+ .x...x.
145
+ ..xxx..
146
+ ART
147
+ fire_eater: <<~ART,
148
+ .....xx
149
+ ....x..
150
+ ..xxxx.
151
+ .xxxxx.
152
+ .xxxx..
153
+ .xx....
154
+ x......
155
+ ART
156
+ slow_dance: <<~ART,
157
+ ...x...
158
+ ..xx...
159
+ ..xx...
160
+ .xxxx..
161
+ xxxxxx.
162
+ xxxxxx.
163
+ .xxxx..
164
+ ART
165
+ full_basket: <<~ART,
166
+ ..xxx..
167
+ .x...x.
168
+ .x...x.
169
+ xxxxxxx
170
+ xx.x.xx
171
+ .xxxxx.
172
+ ..xxx..
173
+ ART
174
+ four_figures: <<~ART,
175
+ ...x...
176
+ ..xxx..
177
+ xxxxxxx
178
+ .xxxxx.
179
+ ..xxx..
180
+ .xx.xx.
181
+ .x...x.
182
+ ART
183
+ quicksilver: <<~ART,
184
+ ....xx.
185
+ ...xx..
186
+ ..xx...
187
+ .xxxxx.
188
+ ...xx..
189
+ ..xx...
190
+ .xx....
191
+ ART
192
+ gold_rush: <<~ART,
193
+ .xxx...
194
+ x..xx..
195
+ ...xxx.
196
+ ..x.xx.
197
+ .x...x.
198
+ x......
199
+ .......
200
+ ART
201
+ top_lantern: <<~ART,
202
+ .......
203
+ x.x.xxx
204
+ .x..x.x
205
+ x.x.xxx
206
+ ......x
207
+ ....xxx
208
+ .......
209
+ ART
210
+ white_heat: <<~ART,
211
+ ...x...
212
+ ..xx...
213
+ ..xxx..
214
+ .xxxx.x
215
+ .xxxxxx
216
+ xxx.xxx
217
+ .xxxxx.
218
+ ART
219
+ great_coil: <<~ART
220
+ xxxxxxx
221
+ x.....x
222
+ x.xxx.x
223
+ x.x.x.x
224
+ x.x...x
225
+ x.xxxxx
226
+ x......
227
+ ART
228
+ }.freeze
229
+ FINE_PICTURES = {
230
+ first_gold: <<~ART,
231
+ ......xx..
232
+ .....x....
233
+ ..xxx.xxx.
234
+ .xxxxxxxxx
235
+ .xxxxxxxxx
236
+ .xxxxxxxxx
237
+ .xxxxxxxxx
238
+ ..xxxxxxx.
239
+ ...xxxxx..
240
+ ..........
241
+ ART
242
+ just_in_time: <<~ART,
243
+ .xxxxxxxx.
244
+ ..x....x..
245
+ ..x....x..
246
+ ...x..x...
247
+ ....xx....
248
+ ....xx....
249
+ ...x..x...
250
+ ..x.xx.x..
251
+ ..xxxxxx..
252
+ .xxxxxxxx.
253
+ ART
254
+ ouroboros: <<~ART,
255
+ ...xxxx...
256
+ ..x....x..
257
+ .x......x.
258
+ x......xxx
259
+ x.....xxxx
260
+ x......x..
261
+ x.........
262
+ .x......x.
263
+ ..x....x..
264
+ ...xxxx...
265
+ ART
266
+ fire_eater: <<~ART,
267
+ ........xx
268
+ .......x..
269
+ ....xxxx..
270
+ ...xxxxx..
271
+ ..xxxxxx..
272
+ ..xxxxx...
273
+ .xxxxx....
274
+ .xxxx.....
275
+ xxx.......
276
+ x.........
277
+ ART
278
+ slow_dance: <<~ART,
279
+ .....x....
280
+ ....xx....
281
+ ....xx....
282
+ ...xxxx...
283
+ ...xxxx...
284
+ ..xxxxxx..
285
+ .xxxxxxxx.
286
+ .xxxxxxxx.
287
+ .xxxxxxxx.
288
+ ..xxxxxx..
289
+ ART
290
+ full_basket: <<~ART,
291
+ ...xxxx...
292
+ ..x....x..
293
+ .x......x.
294
+ xxxxxxxxxx
295
+ .x.x.x.x..
296
+ .xxxxxxxx.
297
+ ..x.x.x.x.
298
+ .xxxxxxxx.
299
+ ..xxxxxx..
300
+ ..........
301
+ ART
302
+ four_figures: <<~ART,
303
+ ....xx....
304
+ ....xx....
305
+ ...xxxx...
306
+ xxxxxxxxxx
307
+ .xxxxxxxx.
308
+ ..xxxxxx..
309
+ ..xxxxxx..
310
+ .xxx..xxx.
311
+ .xx....xx.
312
+ x........x
313
+ ART
314
+ quicksilver: <<~ART,
315
+ .....xxxx.
316
+ ....xxxx..
317
+ ...xxxx...
318
+ ..xxxx....
319
+ .xxxxxxxx.
320
+ ....xxxx..
321
+ ...xxxx...
322
+ ..xxx.....
323
+ .xx.......
324
+ x.........
325
+ ART
326
+ gold_rush: <<~ART,
327
+ ..xxxxx...
328
+ .x....xxx.
329
+ x......xx.
330
+ ......x.x.
331
+ .....x...x
332
+ ....x.....
333
+ ...x......
334
+ ..x.......
335
+ .x........
336
+ x.........
337
+ ART
338
+ top_lantern: <<~ART,
339
+ ..........
340
+ .....xxx..
341
+ ....x...x.
342
+ ....x...x.
343
+ x.x..xxxx.
344
+ .x......x.
345
+ x.x.....x.
346
+ ....xxxx..
347
+ ..........
348
+ ..........
349
+ ART
350
+ white_heat: <<~ART,
351
+ ....x.....
352
+ ....xx....
353
+ ...xxx....
354
+ ...xxxx.x.
355
+ ..xxxxxxx.
356
+ ..xxx.xxx.
357
+ .xxx..xxxx
358
+ .xx....xxx
359
+ .xx....xx.
360
+ ..xxxxxx..
361
+ ART
362
+ great_coil: <<~ART
363
+ xxxxxxxxxx
364
+ x........x
365
+ x.xxxxxx.x
366
+ x.x....x.x
367
+ x.x.xx.x.x
368
+ x.x.x..x.x
369
+ x.x.xxxx.x
370
+ x.x......x
371
+ x.xxxxxxxx
372
+ x.........
373
+ ART
374
+ }.freeze
375
+ # Each medal's ribbon, its edges and its middle stripe.
376
+ RIBBONS = {
377
+ first_gold: %i[crimson gold], just_in_time: %i[tide foam], ouroboros: %i[pine leaf], fire_eater: %i[red orange],
378
+ slow_dance: %i[steel white], full_basket: %i[plum blush], four_figures: %i[violet gold], quicksilver: %i[teal white],
379
+ gold_rush: %i[amber cream], top_lantern: %i[crimson amber], white_heat: %i[orange cream], great_coil: %i[jade lime]
380
+ }.freeze
381
+ # The disc's outline, lit rim, shaded rim, the bevel in shadow and in
382
+ # light, the face, the struck picture and its lit edge, and the ring.
383
+ METALS = {
384
+ brass: { "o" => :wine, "L" => :cream, "R" => :amber, "r" => :amber, "l" => :white, "f" => :gold,
385
+ picture: :orange, edge: :cream, ring: :gold },
386
+ iron: { "o" => :ink, "L" => :pewter, "R" => :slate, "r" => :slate, "l" => :pewter, "f" => :cobble,
387
+ picture: :indigo, edge: :pewter, ring: :pewter }
388
+ }.freeze
389
+
390
+ # A medal put together once for each canvas: ribbon, then the disc over
391
+ # its ring, then the picture, lit edge first, in the middle of the face.
392
+ def made(key, metal, ribbon)
393
+ Sprite.new(assembled(RIBBON, DISC, PICTURES.fetch(key), metal, ribbon, [WIDTH, HEIGHT], DISC_Y).rows,
394
+ fine: assembled(FINE_RIBBON, FINE_DISC, FINE_PICTURES.fetch(key), metal, ribbon, [FINE_WIDTH, FINE_HEIGHT],
395
+ FINE_DISC_Y))
396
+ end
397
+
398
+ def assembled(ribbon_art, disc, picture, metal, ribbon, size, disc_y)
399
+ colors = METALS.fetch(metal)
400
+ edge, middle = ribbon
401
+ canvas = Canvas.new(*size, Sprite::CLEAR)
402
+ shade = Palette::NAMES[Palette::DARKER[1].getbyte(Palette[edge])]
403
+ canvas.draw(Sprite.parse(ribbon_art.join("\n"), "E" => shade, "a" => edge, "b" => middle, "g" => colors[:ring]), 0, 0)
404
+ canvas.draw(Sprite.parse(disc.join("\n"), colors.slice(*%w[o L R r l f])), 0, disc_y)
405
+ face = Sprite.parse(picture, "x" => colors[:picture])
406
+ left = (size.first - face.width) / 2
407
+ top = disc_y + (disc.size - face.height) / 2
408
+ canvas.draw(Sprite.parse(picture, "x" => colors[:edge]), left + 1, top + 1)
409
+ canvas.draw(face, left, top)
410
+ Sprite.new(canvas.rows)
411
+ end
412
+
413
+ BRASS = PICTURES.keys.to_h { |key| [key, made(key, :brass, RIBBONS.fetch(key))] }.freeze
414
+ IRON = PICTURES.keys.to_h { |key| [key, made(key, :iron, %i[slate cobble])] }.freeze
415
+
416
+ # A disc's pixels inside its outline, by which diagonal they are on.
417
+ def diagonals(disc)
418
+ disc.each_with_index.flat_map do |line, dy|
419
+ line.each_char.with_index.filter_map { |char, dx| [dx, dy] unless [".", "o"].include?(char) }
420
+ end.group_by(&:sum).freeze
421
+ end
422
+
423
+ DIAGONALS = diagonals(DISC)
424
+ FINE_DIAGONALS = diagonals(FINE_DISC)
425
+
426
+ private_class_method :made, :assembled, :diagonals
427
+ end
428
+ end