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,217 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The band above the wall: the score on the left beside a fruit, with the
5
+ # multiplier each fruit is worth on a little lantern after it; the best
6
+ # score on the right beside a crown. Beating the best mid-game carries the
7
+ # best along with the score. When the multiplier rises, its lantern hops
8
+ # and brightens while the digit rolls over.
9
+ #
10
+ # While an ember pepper or a silver pear is working, a badge of its own
11
+ # stands after the lantern, the pepper's saying each fruit counts double,
12
+ # and along its foot a fuse burns down as the moves run out. It hops in
13
+ # as it starts, and the pepper's edge flickers like a flame.
14
+ #
15
+ # A game being watched again says whose it was and when in the middle of
16
+ # the band, after a play mark that turns to two bars while it is held,
17
+ # and how many times faster it is going.
18
+ #
19
+ # It is laid out in the pixels of the canvas, the badges after the score
20
+ # as wide as it really is, so on the fine canvas everything keeps the
21
+ # same spacing around the fine letters. The score and the best keep to
22
+ # the canvas's edges, however wide it is.
23
+ class Hud
24
+ TEXT_Y = 5
25
+ MARGIN = 10
26
+ BADGE_WIDTH = 15
27
+ BADGE_HEIGHT = 9
28
+ WORKING_WIDTH = 19
29
+ HOP = 0.3
30
+ BRIGHT = 0.45
31
+ # The band on the fine canvas, in its pixels.
32
+ FINE_TEXT_Y = 7
33
+ FINE_BADGE_WIDTH = 20
34
+ FINE_BADGE_HEIGHT = 13
35
+ FINE_WORKING_WIDTH = 26
36
+
37
+ def initialize(best)
38
+ @best_so_far = best
39
+ @score = Odometer.new(0)
40
+ @best = Odometer.new(best)
41
+ @multiplier = Odometer.new(1, places: 1)
42
+ @level = 1
43
+ @time = 0.0
44
+ @raised = -BRIGHT
45
+ @effect = nil
46
+ @share = 0.0
47
+ @started = -HOP
48
+ @caption = nil
49
+ @rate = 1
50
+ @held = false
51
+ end
52
+
53
+ attr_writer :caption, :rate, :held
54
+
55
+ # What a pepper or a pear is doing, if either is, and the share of its
56
+ # moves still to come.
57
+ def effect(kind, share)
58
+ @started = @time if kind && kind != @effect
59
+ @effect = kind
60
+ @share = share
61
+ end
62
+
63
+ def score=(value)
64
+ @score.value = value
65
+ @best.value = value if value > @best_so_far
66
+ end
67
+
68
+ def level=(value)
69
+ return if value == @level
70
+
71
+ @level = value
72
+ @multiplier.value = value
73
+ @raised = @time
74
+ end
75
+
76
+ def update(dt)
77
+ @time += dt
78
+ [@score, @best, @multiplier].each { |odometer| odometer.update(dt) }
79
+ end
80
+
81
+ def draw(canvas)
82
+ density = canvas.density
83
+ pixels = canvas.pixels
84
+ text_y = density.pick(TEXT_Y, FINE_TEXT_Y)
85
+ margin = density.at(MARGIN)
86
+ pixels.draw(art(FRUIT, density), pixels.left + margin, text_y - 1)
87
+ score_x = pixels.left + density.at(MARGIN + 8)
88
+ @score.draw(pixels, score_x, text_y, Palette[:cream], Palette[:violet], density)
89
+ badge_x = score_x + @score.width(density) + density.pick(4, 6)
90
+ draw_badge(pixels, density, badge_x, text_y)
91
+ working_x = badge_x + density.pick(BADGE_WIDTH, FINE_BADGE_WIDTH) + density.pick(3, 4)
92
+ draw_working(pixels, density, working_x, text_y) if @effect
93
+ draw_caption(pixels, density, text_y) if @caption
94
+ best_x = pixels.right - margin - @best.width(density)
95
+ pixels.draw(art(CROWN, density), best_x - density.pick(9, 13), text_y - 1)
96
+ @best.draw(pixels, best_x, text_y, Palette[:gold], Palette[:violet], density)
97
+ end
98
+
99
+ private
100
+
101
+ def art(sprite, density) = density.pick(sprite, sprite.fine)
102
+
103
+ def font(density) = density.pick(Font::SMALL, Font::SMALL.fine)
104
+
105
+ # How high a badge hops as it starts or rises.
106
+ def hop(density, since) = (Math.sin(Ease.progress(since, 0.0, HOP) * Math::PI) * density.pick(3, 4)).round
107
+
108
+ # A lantern like the menu's, small enough to sit in the band.
109
+ def draw_badge(canvas, density, x, text_y)
110
+ since = @time - @raised
111
+ bright = since < BRIGHT
112
+ width = density.pick(BADGE_WIDTH, FINE_BADGE_WIDTH)
113
+ height = density.pick(BADGE_HEIGHT, FINE_BADGE_HEIGHT)
114
+ inset = density.pick(2, 3)
115
+ y = text_y - inset - hop(density, since)
116
+ canvas.fill(x, y, width, height, Palette[:ink])
117
+ canvas.fill(x, y, width - 1, height - 1, Palette[bright ? :gold : :amber])
118
+ canvas.fill(x + 1, y + 1, width - 2, height - 2, Palette[bright ? :red : :crimson])
119
+ canvas.fill(x + 1, y + height - 2, width - 2, 1, Palette[:wine])
120
+ canvas.fill(x + width - 2, y + 1, 1, height - 2, Palette[:wine])
121
+ text = Palette[bright ? :cream : :gold]
122
+ font(density).write(canvas, "×", x + density.pick(3, 4), y + inset, text, 16)
123
+ @multiplier.draw(canvas, x + density.pick(7, 10), y + inset, text, text, density)
124
+ end
125
+
126
+ # Dark, with the fruit that is working and a fuse along its foot that
127
+ # burns down from the right, its spark at the end.
128
+ def draw_working(canvas, density, x, text_y)
129
+ look = WORKING.fetch(@effect)
130
+ width = density.pick(WORKING_WIDTH, FINE_WORKING_WIDTH)
131
+ height = density.pick(BADGE_HEIGHT, FINE_BADGE_HEIGHT)
132
+ y = text_y - density.pick(2, 3) - hop(density, @time - @started)
133
+ edge = look.flame && Math.sin(@time * 23) > 0.3 ? look.flame : look.edge
134
+ canvas.fill(x, y, width, height, Palette[:ink])
135
+ canvas.fill(x, y, width - 1, height - 1, Palette[edge])
136
+ canvas.fill(x + 1, y + 1, width - 2, height - 2, Palette[look.body])
137
+ fuse = ((width - 2) * @share).ceil
138
+ canvas.fill(x + 1, y + height - 3, fuse, 1, Palette[look.fuse])
139
+ canvas.plot(x + fuse, y + height - 3, Palette[:white]) if fuse.positive?
140
+ canvas.draw(art(look.icon, density), x + density.pick(2, 3), y + density.pick(0, 1))
141
+ font(density).write(canvas, look.text, x + density.pick(9, 13), y + density.pick(1, 2), Palette[:gold], 16) if look.text
142
+ end
143
+
144
+ def draw_caption(canvas, density, text_y)
145
+ font = font(density)
146
+ mark = art(@held ? HELD : Choice::RIGHT, density)
147
+ rate = @rate > 1 ? "×#{@rate}" : ""
148
+ gap = density.pick(3, 4)
149
+ width = mark.width + gap + font.width(@caption) + (rate.empty? ? 0 : density.pick(4, 6) + font.width(rate))
150
+ x = (density.width - width) / 2
151
+ canvas.draw(mark, x, text_y, Palette.solid(Palette[:gold]))
152
+ font.write(canvas, @caption, x + mark.width + gap, text_y, Palette[:ash], 16)
153
+ font.write(canvas, rate, x + width - font.width(rate), text_y, Palette[:gold], 16) unless rate.empty?
154
+ end
155
+
156
+ LEGEND = {
157
+ "w" => :wine, "c" => :crimson, "r" => :red, "o" => :orange, "g" => :gold, "a" => :amber,
158
+ "d" => :deep, "f" => :fern, "l" => :lime, "j" => :jade, "C" => :cream, "n" => :sand, "h" => :ash, "t" => :steel
159
+ }.freeze
160
+
161
+ HELD = Sprite.parse(<<~ART, "a" => :ink)
162
+ a.a
163
+ a.a
164
+ a.a
165
+ a.a
166
+ a.a
167
+ ART
168
+ FRUIT = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
169
+ ...f.
170
+ .wdw.
171
+ wgorw
172
+ worrw
173
+ wrrcw
174
+ .wcw.
175
+ ART
176
+ .....lf.
177
+ ....df..
178
+ .wwwdww.
179
+ wgCgooow
180
+ wggooorw
181
+ wooorrrw
182
+ worrrrcw
183
+ .wrrrcw.
184
+ ..wwww..
185
+ FINE
186
+
187
+ CROWN = Sprite.parse(<<~ART, LEGEND)
188
+ g.g.g
189
+ gagag
190
+ aaaaa
191
+ ooooo
192
+ ART
193
+
194
+ # How each badge looks: its edge, and the flame it flickers to, its
195
+ # body, its fuse, its fruit, and what it says.
196
+ Look = Data.define(:edge, :flame, :body, :fuse, :icon, :text)
197
+
198
+ WORKING = {
199
+ pepper: Look.new(:orange, :amber, :wine, :amber, Sprite.parse(<<~ART, LEGEND), "×2"),
200
+ l....
201
+ .j...
202
+ .ga..
203
+ .aor.
204
+ ..orr
205
+ ...rc
206
+ ART
207
+ pear: Look.new(:ash, nil, :slate, :sand, Sprite.parse(<<~ART, LEGEND), nil)
208
+ ..l.
209
+ .j..
210
+ .Cn.
211
+ Cnnh
212
+ Cnht
213
+ .hh.
214
+ ART
215
+ }.freeze
216
+ end
217
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The bold way into and out of a game: a circle of light closes on the
5
+ # point the eye was on, and opens again on the point it should go to
6
+ # next, each scene saying where that is. A thin amber rim marks the edge.
7
+ class Iris
8
+ HALF = 0.36
9
+ RIM = 1.5
10
+
11
+ attr_reader :to
12
+
13
+ def initialize(from, to)
14
+ @from = from
15
+ @to = to
16
+ @time = 0.0
17
+ end
18
+
19
+ def update(dt)
20
+ @time += dt
21
+ end
22
+
23
+ def done? = @time >= 2 * HALF
24
+
25
+ def draw(canvas)
26
+ closing = @time < HALF
27
+ scene = closing ? @from : @to
28
+ scene ? scene.draw(canvas) : canvas.clear
29
+ x, y = scene ? scene.focus : [App::WIDTH / 2, App::HEIGHT / 2]
30
+ open = closing ? 1 - Ease.in_quad(@time / HALF) : Ease.out_quad((@time - HALF) / HALF)
31
+ close_around(canvas, x, y, open * Math.sqrt(reach(canvas, x, y)))
32
+ end
33
+
34
+ private
35
+
36
+ # How far the circle has to open from a point, squared, to show every
37
+ # corner of the canvas.
38
+ def reach(canvas, x, y)
39
+ pixels = canvas.pixels
40
+ scale = canvas.density.scale
41
+ [x - pixels.left / scale, pixels.right / scale - x].max**2 + [y, App::HEIGHT - y].max**2
42
+ end
43
+
44
+ # Everything outside the circle goes dark, row by row of pixels; just
45
+ # inside its edge runs the rim.
46
+ def close_around(canvas, x, y, radius)
47
+ density = canvas.density
48
+ pixels = canvas.pixels
49
+ x = density.at(x)
50
+ y = density.at(y)
51
+ radius *= density.scale
52
+ rim_width = RIM * density.scale
53
+ ink = Palette[:ink]
54
+ rim = Palette[:amber]
55
+ pixels.height.times do |row|
56
+ outer = span(radius, row - y)
57
+ inner = span(radius - rim_width, row - y)
58
+ if outer.nil?
59
+ pixels.fill(pixels.left, row, pixels.width, 1, ink)
60
+ next
61
+ end
62
+ pixels.fill(pixels.left, row, x - outer - pixels.left, 1, ink)
63
+ pixels.fill(x + outer, row, pixels.width, 1, ink)
64
+ pixels.fill(x - outer, row, outer - (inner || 0), 1, rim) if radius > rim_width
65
+ pixels.fill(x + (inner || 0), row, outer - (inner || 0), 1, rim) if radius > rim_width
66
+ end
67
+ end
68
+
69
+ # Half the width of the circle at a height above or below its center.
70
+ def span(radius, rise)
71
+ return if radius <= 0 || rise.abs >= radius
72
+
73
+ Math.sqrt(radius * radius - rise * rise).round
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The music page of the settings, shown in place of their list: the
5
+ # songs to choose from, each with the kind of piece it is and a note
6
+ # bobbing beside the one that is playing, then how loud the music is and
7
+ # the way back. A song chosen is put on at once, and switches the music
8
+ # on if it was off; the volume turns and switches as on the pause panel.
9
+ # Whatever changes is saved, heard, and turns the settings' gears.
10
+ class Jukebox
11
+ ROWS = [*Song::ALL.map(&:name), "VOLUME", "BACK"].freeze
12
+ TOOL_X = 3
13
+ LABEL_X = 12
14
+ # The kinds and the meter stand as far in from the row's right edge as
15
+ # the tool does from its left.
16
+ EDGE = 4
17
+ METER_WIDTH = 29
18
+
19
+ def initialize(preferences, speaker, toolbox, x:, y:, width:, gap:, &changed)
20
+ @preferences = preferences
21
+ @speaker = speaker
22
+ @toolbox = toolbox
23
+ @x = x
24
+ @width = width
25
+ @changed = changed
26
+ @meter = Meter.new(preferences.music)
27
+ @menu = Menu.new(ROWS, x:, y:, width:, speaker:, gap:, slots: false, at: ROWS.index(preferences.song.name),
28
+ &method(:write_row))
29
+ @closed = false
30
+ end
31
+
32
+ def closed? = @closed
33
+
34
+ def focus = @menu.focus
35
+
36
+ def update(dt)
37
+ @menu.update(dt)
38
+ @meter.update(dt)
39
+ end
40
+
41
+ def draw(canvas)
42
+ @menu.draw(canvas)
43
+ end
44
+
45
+ def press(action)
46
+ case action
47
+ when :up then @menu.move(-1)
48
+ when :down then @menu.move(1)
49
+ when :left then turn(-1)
50
+ when :right then turn(1)
51
+ when :confirm then choose
52
+ when :back then close
53
+ end
54
+ end
55
+
56
+ # Hovering selects a row and clicking works it. On the volume, a click
57
+ # past its name turns the meter down on its left half and up on its
58
+ # right half.
59
+ def point(x, y, kind)
60
+ return unless @menu.point(x, y) && kind == :click
61
+
62
+ along = x - @x
63
+ if @menu.selected == "VOLUME" && along > LABEL_X + Font::SMALL.width("VOLUME")
64
+ turn(along < meter_x + METER_WIDTH / 2 ? -1 : 1)
65
+ else
66
+ choose
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def meter_x = @width - EDGE - METER_WIDTH
73
+
74
+ def choose
75
+ case @menu.selected
76
+ when "VOLUME" then switch
77
+ when "BACK" then close
78
+ else put_on(Song.named(@menu.selected))
79
+ end
80
+ end
81
+
82
+ def put_on(song)
83
+ @preferences.song = song
84
+ @preferences.switch_music if @preferences.music == :off
85
+ changed
86
+ end
87
+
88
+ # A step down or up the meter, as far as its ends.
89
+ def turn(by)
90
+ volume = @menu.selected == "VOLUME" && @meter.beside(by)
91
+ return unless volume
92
+
93
+ @preferences.music = volume
94
+ changed(by)
95
+ end
96
+
97
+ def switch
98
+ @preferences.switch_music
99
+ changed
100
+ end
101
+
102
+ # The meter follows, the lantern sinks for a moment, the gears turn,
103
+ # and the press is heard over the music changing.
104
+ def changed(by = 0)
105
+ @meter.turn(@preferences.music, by:) unless @meter.volume == @preferences.music
106
+ @menu.tap
107
+ @changed.call
108
+ @speaker.play(:press)
109
+ end
110
+
111
+ def close
112
+ @closed = true
113
+ @speaker.play(:back)
114
+ end
115
+
116
+ # A song's name, gold while it is the one playing, and its kind at the
117
+ # right; the volume with the settings' notes and its meter; the way
118
+ # back with its arrow.
119
+ def write_row(canvas, index, x, y, color)
120
+ row = ROWS[index]
121
+ lit = row == @menu.selected
122
+ case row
123
+ when "VOLUME"
124
+ @toolbox.draw(canvas, "MUSIC", x + TOOL_X, y, lit:, awake: on?)
125
+ Font::SMALL.draw(canvas, row, x + LABEL_X, y, color)
126
+ @meter.draw(canvas, x + meter_x, y, METER_WIDTH, chosen: lit)
127
+ when "BACK"
128
+ @toolbox.draw(canvas, row, x + TOOL_X, y, lit:)
129
+ Font::SMALL.draw(canvas, row, x + LABEL_X, y, color)
130
+ else write_song(canvas, Song.named(row), x, y, lit, color)
131
+ end
132
+ end
133
+
134
+ def write_song(canvas, song, x, y, lit, color)
135
+ playing = song == @preferences.song
136
+ @toolbox.draw(canvas, "SONG", x + TOOL_X, y, lit: on?, awake: on?) if playing
137
+ Font::SMALL.draw(canvas, song.name, x + LABEL_X, y, playing && !lit ? Palette[:gold] : color)
138
+ Font::SMALL.draw(canvas, song.kind, x + @width - EDGE, y, Palette[lit ? :cream : :ash], align: :right)
139
+ end
140
+
141
+ def on? = @preferences.music != :off
142
+ end
143
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A mouse report in terminal cells, counted from zero: a move, a drag,
5
+ # a click, or a turn of the wheel.
6
+ Mouse = Data.define(:column, :line, :kind)
7
+
8
+ # Turns the bytes a terminal sends into keys: symbols for named keys,
9
+ # one-character strings for the rest, and Mouse reports.
10
+ #
11
+ # Esc starts every escape sequence and is also a key of its own. A lone
12
+ # Esc is told apart by waiting a moment: the rest of a sequence arrives in
13
+ # the same burst, so an Esc still alone after LONE_ESCAPE was pressed.
14
+ # A whole sequence the game has no use for, F1 or F5 say, is no key at
15
+ # all, rather than an Esc and the letters after it.
16
+ class Keyboard
17
+ LONE_ESCAPE = 0.03
18
+
19
+ # The last letters of the sequences for the arrows, Home and End, which
20
+ # come in two forms.
21
+ FINALS = { "A" => :up, "B" => :down, "C" => :right, "D" => :left, "H" => :home, "F" => :end }.freeze
22
+ WHEEL = { 64 => :wheel_up, 65 => :wheel_down }.freeze
23
+ SHIFT_ALT_CTRL = 0b11100
24
+
25
+ def initialize
26
+ @pending = "".b
27
+ @waiting_since = nil
28
+ end
29
+
30
+ def read(bytes, now)
31
+ @pending << bytes.b
32
+ keys = []
33
+ until @pending.empty?
34
+ key, length = parse(@pending)
35
+ if length.nil?
36
+ break unless lone_escape?(now)
37
+
38
+ key, length = :escape, 1
39
+ end
40
+ keys << key if key
41
+ @pending = @pending.byteslice(length..)
42
+ end
43
+ @waiting_since = @pending.empty? ? nil : (@waiting_since || now)
44
+ keys
45
+ end
46
+
47
+ private
48
+
49
+ def lone_escape?(now) = @pending.start_with?("\e") && @waiting_since && now - @waiting_since >= LONE_ESCAPE
50
+
51
+ # Returns the key at the start of the bytes and how many bytes it took,
52
+ # or no length when the bytes so far are the start of a longer sequence.
53
+ # A sequence after Esc and [ is any parameters, any intermediates and
54
+ # one last byte; after Esc and O it is one byte.
55
+ def parse(bytes)
56
+ case bytes
57
+ when /\A\e\[<(\d+);(\d+);(\d+)([Mm])/ then [mouse($1.to_i, $2.to_i, $3.to_i, $4), $&.bytesize]
58
+ when /\A\e\[([\x30-\x3f]*)[\x20-\x2f]*([\x40-\x7e])/ then [csi($1, $2), $&.bytesize]
59
+ when /\A\eO([\x20-\x7e])/ then [$1 == "R" ? :f3 : FINALS[$1], 3]
60
+ when /\A\e(\[[\x30-\x3f]*[\x20-\x2f]*|O)?\z/ then nil
61
+ when /\A\e/ then [:escape, 1]
62
+ when /\A[\r\n]/ then [:enter, 1]
63
+ when /\A\x03/ then [:interrupt, 1]
64
+ when /\A[\x7f\b]/ then [:backspace, 1]
65
+ when /\A[\x20-\x7e]/ then [$&, 1]
66
+ else [nil, 1]
67
+ end
68
+ end
69
+
70
+ def csi(parameters, final)
71
+ case [parameters, final]
72
+ in [_, "A" | "B" | "C" | "D" | "F" | "H"] then FINALS[final]
73
+ in ["13", "~"] then :f3
74
+ in ["1" | "7", "~"] then :home
75
+ in ["4" | "8", "~"] then :end
76
+ in ["5", "~"] then :page_up
77
+ in ["6", "~"] then :page_down
78
+ in ["", "I"] then :focus_in
79
+ in ["", "O"] then :focus_out
80
+ else nil
81
+ end
82
+ end
83
+
84
+ # Movement, the left button and the wheel matter to the scenes; the
85
+ # other buttons and every release are left out, and so are Shift, Alt
86
+ # and Ctrl. Moving with the left button held is a drag.
87
+ def mouse(code, column, line, final)
88
+ button = code & ~SHIFT_ALT_CTRL
89
+ kind = if button.anybits?(64) then WHEEL[button]
90
+ elsif button.anybits?(32) then button == 32 ? :drag : :move
91
+ elsif final == "M" && button.zero? then :click
92
+ end
93
+ Mouse.new(column - 1, line - 1, kind) if kind
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A sprig of laurel to stand beside the medals' title, curving in toward
5
+ # it, the one on the right the left one turned round. It grows up from
6
+ # behind the panel as the case opens, leaf by leaf, and after that every
7
+ # few seconds the light runs up its leaves, the two sprigs taking turns.
8
+ class Laurel
9
+ WIDTH = 14
10
+ HEIGHT = 20
11
+ GROW_FROM = 0.2
12
+ GROW = 0.6
13
+ GLINT = 0.6
14
+ GLINT_EVERY = 4.0
15
+
16
+ # Rooted on the ground at y, facing the title from the left or the
17
+ # right, with its first glint at a moment of its own.
18
+ def initialize(x, ground, facing, glint_from:)
19
+ @x = x
20
+ @y = ground - HEIGHT + 1
21
+ @sprite = facing == :left ? SPRIG : TURNED
22
+ @glint_from = glint_from
23
+ @time = 0.0
24
+ end
25
+
26
+ def update(dt)
27
+ @time += dt
28
+ end
29
+
30
+ # Only the part that has grown shows, from the root up.
31
+ def draw(canvas)
32
+ grown = (Ease.out_cubic(Ease.progress(@time, GROW_FROM, GROW)) * HEIGHT).round
33
+ return if grown.zero?
34
+
35
+ canvas.clip(@x, @y + HEIGHT - grown, WIDTH, grown) { canvas.draw(@sprite, @x, @y) }
36
+ draw_glint(canvas)
37
+ end
38
+
39
+ private
40
+
41
+ # Two rows of leaves at a time, a step lighter, climbing from the root.
42
+ def draw_glint(canvas)
43
+ moment = @time - @glint_from
44
+ return if moment.negative? || moment % GLINT_EVERY > GLINT
45
+
46
+ row = HEIGHT - (Ease.in_out_sine(moment % GLINT_EVERY / GLINT) * (HEIGHT + 2)).round
47
+ canvas.clip(@x, @y + row, WIDTH, 2) { canvas.draw(@sprite, @x, @y, Palette::LIGHTER[1]) }
48
+ end
49
+
50
+ LEGEND = { "p" => :pine, "j" => :jade, "f" => :fern, "l" => :leaf, "m" => :lime }.freeze
51
+
52
+ SPRIG = Sprite.parse(<<~ART, LEGEND)
53
+ .......ml.....
54
+ .......lff....
55
+ ......jfj.....
56
+ ...ml.j.......
57
+ ...lffj.lm....
58
+ ....fjjffl....
59
+ .ml..jjjf.....
60
+ .lff.j........
61
+ ..fjjj.lm.....
62
+ .ml.j.ffl.....
63
+ .lffjjjf......
64
+ ..fjj.........
65
+ ..mlj..lm.....
66
+ ..lffjffl.....
67
+ ...fjjjf......
68
+ ....mlj..lm...
69
+ ....lff.ffl...
70
+ .....fjpjf....
71
+ ........p.....
72
+ .........p....
73
+ ART
74
+ TURNED = Sprite.new(SPRIG.rows.map(&:reverse))
75
+ end
76
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The fruit's leaf, torn off as the fruit is bitten: thrown up to one
5
+ # side, it flutters down, turning over as it falls, and darkens away. It
6
+ # is too small a thing for the standard canvas, so only the fine one
7
+ # shows it. Which way it is thrown comes from where the fruit was, and
8
+ # no dice are thrown for it.
9
+ class Leaf
10
+ LIFE = 1.15
11
+ GRAVITY = 90.0
12
+ # It never falls faster than this, and loses its sideways push this
13
+ # quickly.
14
+ SINK = 13.0
15
+ DRAG = 2.2
16
+ # How far it sways from side to side as it falls, and how fast it
17
+ # turns.
18
+ SWAY = 13.0
19
+ TURN = 8.0
20
+ # How long before the end it is a step darker, and then two.
21
+ WORN = [0.3, 0.14].freeze
22
+
23
+ def initialize(x, y)
24
+ dice = Random.new(x.round * 31 + y.round)
25
+ @x = x
26
+ @y = y
27
+ @vx = dice.rand(12.0..22.0) * (dice.rand(2).zero? ? -1 : 1)
28
+ @vy = -dice.rand(34.0..44.0)
29
+ @age = 0.0
30
+ end
31
+
32
+ def update(dt)
33
+ @age += dt
34
+ @vy = [@vy + GRAVITY * dt, SINK].min
35
+ @vx -= @vx * DRAG * dt
36
+ @x += (@vx + Math.sin(@age * TURN) * SWAY) * dt
37
+ @y += @vy * dt
38
+ end
39
+
40
+ def done? = @age >= LIFE
41
+
42
+ def draw(canvas)
43
+ return unless canvas.density.fine?
44
+
45
+ scale = canvas.density.scale
46
+ art = TURNS[(@age * TURN / (Math::PI / 2)).floor % TURNS.size]
47
+ worn = Palette::DARKER[WORN.count { |left| LIFE - @age < left }]
48
+ canvas.pixels.draw(art, (@x * scale).round - 1, (@y * scale).round - 1, worn)
49
+ end
50
+
51
+ COLORS = { "L" => :lime, "l" => :leaf, "f" => :fern }.freeze
52
+
53
+ # Face on, edge on, its other face, edge on.
54
+ TURNS = [
55
+ Sprite.parse(<<~ART, COLORS),
56
+ .lL
57
+ fl.
58
+ ART
59
+ Sprite.parse(<<~ART, COLORS),
60
+ l
61
+ f
62
+ ART
63
+ Sprite.parse(<<~ART, COLORS),
64
+ Ll.
65
+ .lf
66
+ ART
67
+ Sprite.parse(<<~ART, COLORS)
68
+ l
69
+ f
70
+ ART
71
+ ].freeze
72
+ end
73
+ end