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,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # How finely the game is drawn. Every scene is laid out in the points of
5
+ # the standard canvas, 236 by 122, where a point is a pixel. A terminal
6
+ # with room for half as many cells again each way gets the fine canvas
7
+ # instead, three pixels for every two points, and the art and the
8
+ # letters drawn again at that size: the same game, as big on the screen,
9
+ # in finer detail rather than bigger pixels.
10
+ #
11
+ # The width is the frame's. A terminal with a tile's room more each side
12
+ # gets a canvas that much wider, the frame in its middle, and the field
13
+ # grows into it.
14
+ Density = Data.define(:name, :scale, :width, :height) do
15
+ def fine? = name == :fine
16
+
17
+ # The canvas's width with room for the field to be a column wider each
18
+ # side.
19
+ def wide = at(App::WIDTH + 2 * Courtyard::TILE)
20
+
21
+ # The pixel a point falls in.
22
+ def at(point) = (point * scale).floor
23
+
24
+ # The point a pixel falls in, as for the mouse.
25
+ def point(pixel) = (pixel / scale).floor
26
+
27
+ # The standard canvas's value or the fine one's.
28
+ def pick(standard, fine) = fine? ? fine : standard
29
+
30
+ # The terminal cells it takes, a cell showing two pixels, one above the
31
+ # other.
32
+ def columns = width
33
+
34
+ def lines = height / 2
35
+ end
36
+
37
+ Density::STANDARD = Density.new(:standard, 1, 236, 122)
38
+ # Half as many pixels again, and one more row at the foot to make the
39
+ # last cell whole.
40
+ Density::FINE = Density.new(:fine, 1.5, 354, 184)
41
+
42
+ # The finest density a terminal has room for, or nil if it has room for
43
+ # none.
44
+ def Density.fitting(columns, lines)
45
+ [Density::FINE, Density::STANDARD].find { |density| columns >= density.columns && lines >= density.lines }
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Easing curves over t in 0..1. Each starts at 0 and ends at 1; the back
5
+ # curves overshoot on the way.
6
+ module Ease
7
+ BACK = 1.70158
8
+
9
+ module_function
10
+
11
+ def in_quad(t) = t * t
12
+
13
+ def out_quad(t) = 1 - (1 - t) * (1 - t)
14
+
15
+ def out_cubic(t) = 1 - (1 - t)**3
16
+
17
+ def in_out_sine(t) = (1 - Math.cos(Math::PI * t)) / 2
18
+
19
+ def in_back(t) = (BACK + 1) * t**3 - BACK * t**2
20
+
21
+ def out_back(t) = 1 + (BACK + 1) * (t - 1)**3 + BACK * (t - 1)**2
22
+
23
+ # How far through an interval a moment is, held at 0 before it and at 1
24
+ # after it.
25
+ def progress(time, start, length) = ((time - start) / length).clamp(0.0, 1.0)
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The gentle way between scenes: every color steps down its ramp to ink,
5
+ # the scenes swap in the dark, and the new one steps back up.
6
+ class Fade
7
+ HALF = 0.3
8
+
9
+ attr_reader :to
10
+
11
+ def initialize(from, to)
12
+ @from = from
13
+ @to = to
14
+ @time = 0.0
15
+ end
16
+
17
+ def update(dt)
18
+ @time += dt
19
+ end
20
+
21
+ def done? = @time >= 2 * HALF
22
+
23
+ def draw(canvas)
24
+ scene, darkness = @time < HALF ? [@from, @time / HALF] : [@to, 2 - @time / HALF]
25
+ scene ? scene.draw(canvas) : canvas.clear
26
+ steps = Palette::DARKER.size - 1
27
+ canvas.recolor(Palette::DARKER[(Ease.in_out_sine(darkness.clamp(0.0, 1.0)) * steps).round])
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # What a game has done toward the medals, told after every move, and the
5
+ # medals each move has earned: every one at most once a game, whether or
6
+ # not the player has it already. It only watches; the game goes on the
7
+ # same without it.
8
+ class Feats
9
+ # How much the medals that count ask for.
10
+ FRUIT_UNDER_ONE = 3
11
+ TAIL_MOVES = 10
12
+ GOLDEN = 3
13
+ SCORE = 1000
14
+ FAST_LEVEL = 5
15
+
16
+ def initialize(game)
17
+ @game = game
18
+ @earned = []
19
+ @kinds = []
20
+ @golden = 0
21
+ @under = 0
22
+ @chased = 0
23
+ see
24
+ end
25
+
26
+ # The medals the move just made has earned, given what it came to.
27
+ def after(event)
28
+ found = []
29
+ case event
30
+ in Game::Ate(kind: :fruit) then fruit(found)
31
+ in Game::Ate(kind:) then rare(kind, found)
32
+ in Game::Moved then chase(found)
33
+ in Game::Crashed then nil
34
+ end
35
+ found << :four_figures if @game.score >= SCORE
36
+ found << :quicksilver if @game.speed == :fast && @game.level >= FAST_LEVEL
37
+ found << :top_lantern if @game.level == Game::TOP_LEVEL
38
+ found << :great_coil if @game.snake.length * 4 >= @game.board.columns * @game.board.rows
39
+ see
40
+ fresh = found - @earned
41
+ @earned.concat(fresh)
42
+ fresh.map { |key| Medal.named(key) }
43
+ end
44
+
45
+ private
46
+
47
+ # How things stood once the move was made, which is how they stand as
48
+ # the next one begins: what was working, the rare fruit's moves left,
49
+ # the multiplier and the tail.
50
+ def see
51
+ @working = @game.effect
52
+ @rare_left = @game.rare_left
53
+ @level = @game.level
54
+ @tail = @game.snake.body.last
55
+ end
56
+
57
+ # A fruit eaten while a pepper or a pear was working counts toward it.
58
+ def fruit(found)
59
+ @chased = 0
60
+ return unless @working
61
+
62
+ @under += 1
63
+ found << (@working == :pepper ? :fire_eater : :slow_dance) if @under >= FRUIT_UNDER_ONE
64
+ found << :white_heat if @working == :pepper && @level == Game::TOP_LEVEL
65
+ end
66
+
67
+ # The last of the eight sparkles around a rare fruit is lit for the
68
+ # last eighth of its moves.
69
+ def rare(kind, found)
70
+ @chased = 0
71
+ @kinds |= [kind]
72
+ @golden += 1 if kind == :golden
73
+ @under = 0 if Game::WORKS.key?(kind)
74
+ found << :first_gold if kind == :golden
75
+ found << :gold_rush if @golden >= GOLDEN
76
+ found << :full_basket if @kinds.size == Game::RARE.size
77
+ found << :just_in_time if @rare_left * 8 <= Game::RARE_MOVES
78
+ end
79
+
80
+ # The head going where the tail just was, move after move.
81
+ def chase(found)
82
+ @chased = @game.snake.head == @tail ? @chased + 1 : 0
83
+ found << :ouroboros if @chased >= TAIL_MOVES
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The fine canvas as the scenes see it. They draw on it in points just as
5
+ # on the standard canvas, and it puts every point in its pixels, three
6
+ # for every two. Art comes out in its fine version, centered on where the
7
+ # standard art would have been, words in the fine letters, and a line one
8
+ # point wide stays one pixel wide rather than turning one or two by where
9
+ # it falls. What has to be exact to the pixel draws on the pixels
10
+ # themselves, with the density to place it.
11
+ class Fine
12
+ attr_reader :pixels
13
+
14
+ def initialize(pixels)
15
+ @pixels = pixels
16
+ end
17
+
18
+ def density = Density::FINE
19
+
20
+ def width = App::WIDTH
21
+
22
+ def height = App::HEIGHT
23
+
24
+ def clear(color = 0) = @pixels.clear(color)
25
+
26
+ def plot(x, y, color) = @pixels.plot(at(x), at(y), color)
27
+
28
+ def fill(x, y, width, height, color) = @pixels.fill(*box(x, y, width, height), color)
29
+
30
+ def shade(x, y, width, height, map) = @pixels.shade(*box(x, y, width, height), map)
31
+
32
+ def draw(sprite, x, y, map = Palette::IDENTITY)
33
+ fine = sprite.fine
34
+ @pixels.draw(fine, *place(sprite, fine, x, y), map)
35
+ end
36
+
37
+ def draw_faded(sprite, x, y, cover, map = Palette::IDENTITY)
38
+ fine = sprite.fine
39
+ @pixels.draw_faded(fine, *place(sprite, fine, x, y), cover, map)
40
+ end
41
+
42
+ def clip(x, y, width, height, &) = @pixels.clip(*box(x, y, width, height), &)
43
+
44
+ def recolor(map) = @pixels.recolor(map)
45
+
46
+ def dither(map, level) = @pixels.dither(map, level)
47
+
48
+ def dim(amount) = @pixels.dim(amount)
49
+
50
+ # As far one way as the other: a shake must not lean.
51
+ def shift(dx, dy, color = 0) = @pixels.shift((dx * density.scale).round, (dy * density.scale).round, color)
52
+
53
+ def write(font, text, x, y, color, cover: 16, align: :left, within: 0)
54
+ fine = font.fine
55
+ left = at(x)
56
+ left -= fine.width(text) if align == :right
57
+ left += (at(x + within) - at(x) - fine.width(text)) / 2 if align == :center
58
+ fine.write(@pixels, text, left, at(y), color, cover)
59
+ end
60
+
61
+ private
62
+
63
+ def at(point) = density.at(point)
64
+
65
+ # A rectangle of points in pixels, where one point across is one pixel.
66
+ def box(x, y, width, height)
67
+ left = at(x)
68
+ top = at(y)
69
+ [left, top, width == 1 ? 1 : at(x + width) - left, height == 1 ? 1 : at(y + height) - top]
70
+ end
71
+
72
+ # Fine art goes in the middle of the pixels the standard art would
73
+ # have covered.
74
+ def place(sprite, fine, x, y)
75
+ [at(x) + (at(x + sprite.width) - at(x) - fine.width) / 2, at(y) + (at(y + sprite.height) - at(y) - fine.height) / 2]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Fireflies drifting over the courtyard on slow wandering paths, each
5
+ # glowing up and fading on its own rhythm. They are gentle enough to
6
+ # ignore and never stay bright for long.
7
+ class Fireflies
8
+ Fly = Data.define(:x, :y, :phases, :rhythm, :offset)
9
+
10
+ def initialize(rng, count, left: 0, top: 0, width: App::WIDTH, height: App::HEIGHT)
11
+ @time = 0.0
12
+ @flies = Array.new(count) do
13
+ phases = Array.new(4) { rng.rand * Math::PI * 2 }
14
+ Fly.new(left + rng.rand(width), top + rng.rand(height), phases, rng.rand(3.5..6.0), rng.rand * 6)
15
+ end
16
+ end
17
+
18
+ def update(dt)
19
+ @time += dt
20
+ end
21
+
22
+ def draw(canvas)
23
+ @flies.each do |fly|
24
+ glow = GLOWS[brightness(fly)]
25
+ next unless glow
26
+
27
+ x, y = position(fly)
28
+ canvas.draw(glow, x - 1, y - 1)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ # Two slow swells on each axis, so the path wanders without repeating
35
+ # in any way the eye can catch.
36
+ def position(fly)
37
+ a, b, c, d = fly.phases
38
+ t = @time
39
+ x = fly.x + 16 * Math.sin(t * 0.19 + a) + 6 * Math.sin(t * 0.47 + b)
40
+ y = fly.y + 9 * Math.sin(t * 0.15 + c) + 4 * Math.sin(t * 0.58 + d)
41
+ [x.round, y.round]
42
+ end
43
+
44
+ # Dark most of the time, then a soft rise to full glow and a fall.
45
+ def brightness(fly)
46
+ moment = ((@time + fly.offset) % fly.rhythm) / 1.6
47
+ return 0 if moment >= 1
48
+
49
+ (Math.sin(moment * Math::PI) * (GLOWS.size - 1)).round
50
+ end
51
+
52
+ LEGEND = { "c" => :cream, "l" => :lime, "f" => :leaf, "j" => :fern }.freeze
53
+
54
+ # On the fine canvas the brightest of them gathers a soft ring.
55
+ GLOWS = [
56
+ nil,
57
+ Sprite.parse("...\n.j.\n...", LEGEND, fine: ".....\n.....\n..j..\n.....\n....."),
58
+ Sprite.parse("...\n.f.\n...", LEGEND, fine: ".....\n.....\n..f..\n.....\n....."),
59
+ Sprite.parse(".j.\njlj\n.j.", LEGEND, fine: ".....\n..j..\n.jlj.\n..j..\n....."),
60
+ Sprite.parse(".f.\nfcf\n.f.", LEGEND, fine: "..j..\n.jfj.\njfcfj\n.jfj.\n..j..")
61
+ ].freeze
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A few words that rise from where something happened and fade by
5
+ # stepping down their color's ramp, like the points a fruit was worth.
6
+ # A shadow a pixel down and to the right lifts them off the stones.
7
+ class FloatingText
8
+ RISE = 12
9
+ LIFE = 0.95
10
+ FADE = 0.35
11
+
12
+ def initialize(text, center_x, y, color)
13
+ @text = text
14
+ @center = center_x
15
+ @y = y
16
+ @color = Palette[color]
17
+ @age = 0.0
18
+ end
19
+
20
+ def update(dt)
21
+ @age += dt
22
+ end
23
+
24
+ def done? = @age >= LIFE
25
+
26
+ def draw(canvas)
27
+ density = canvas.density
28
+ font = density.pick(Font::SMALL, Font::SMALL.fine)
29
+ x = density.at(@center) - font.width(@text) / 2
30
+ y = density.at(@y - (Ease.out_cubic(Ease.progress(@age, 0.0, LIFE)) * RISE).round)
31
+ steps = (Ease.progress(@age, LIFE - FADE, FADE) * 4).floor
32
+ font.write(canvas.pixels, @text, x + 1, y + 1, Palette[:ink], 16)
33
+ font.write(canvas.pixels, @text, x, y, Palette::DARKER[steps].getbyte(@color), 16)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A bitmap font. Glyphs are drawn on a sheet as text grids: a line naming
5
+ # the characters, then the rows of each glyph side by side in the same
6
+ # order, "#" for ink and "." for paper. Blocks are separated by blank lines.
7
+ # Each font has its letters for the fine canvas too, drawn again with
8
+ # strokes a pixel wide at half as tall again.
9
+ class Font
10
+ def self.parse(sheet, tracking: 1, space: 2, fine: nil)
11
+ glyphs = {}
12
+ sheet.split(/\n\s*\n/).each do |block|
13
+ names, *rows = block.lines.map(&:split)
14
+ names.each_with_index do |name, index|
15
+ glyphs[name] = Sprite.parse(rows.map { |row| row[index] }.join("\n"), "#" => :ink)
16
+ end
17
+ end
18
+ new(glyphs, tracking:, space:, fine:)
19
+ end
20
+
21
+ attr_reader :height, :fine
22
+
23
+ def initialize(glyphs, tracking:, space:, fine: nil)
24
+ @glyphs = glyphs
25
+ @tracking = tracking
26
+ @space = space
27
+ @fine = fine
28
+ @height = glyphs.values.first.height
29
+ end
30
+
31
+ def width(text)
32
+ text.each_char.sum { |char| advance(char) } - @tracking
33
+ end
34
+
35
+ def writes?(char) = char == " " || @glyphs.key?(char)
36
+
37
+ # How far along the next character starts.
38
+ def advance(char) = (char == " " ? @space : @glyphs.fetch(char).width) + @tracking
39
+
40
+ # Text can be drawn part of the way faded, as a dither: cover runs from
41
+ # 0 for none of it to 16 for all. It starts at x, ends at it if aligned
42
+ # right, or is centered in the width from x.
43
+ def draw(canvas, text, x, y, color, cover: 16, align: :left, within: 0)
44
+ canvas.write(self, text, x, y, color, cover:, align:, within:)
45
+ end
46
+
47
+ # The letters themselves, from x on a canvas of pixels.
48
+ def write(canvas, text, x, y, color, cover)
49
+ map = Palette.solid(color)
50
+ text.each_char do |char|
51
+ draw_glyph(canvas, @glyphs.fetch(char), x, y, map, cover) unless char == " "
52
+ x += advance(char)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def draw_glyph(canvas, glyph, x, y, map, cover)
59
+ cover < 16 ? canvas.draw_faded(glyph, x, y, cover, map) : canvas.draw(glyph, x, y, map)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The food, drawn as a ripe fruit on the stones. A new one drops in from
5
+ # above while its shadow grows under it, lands in a puff of dust with a
6
+ # soft knock, and settles with one small bounce. While it waits, the
7
+ # light glints on it now and then. On the fine canvas it is a rounder
8
+ # apple, with a dip where the stem goes in and a leaf of two greens.
9
+ class Fruit
10
+ DROP = 0.45
11
+ LANDING = 0.7
12
+ HEIGHT = 26.0
13
+ GLINT_EVERY = 2.6
14
+ DUST = %i[sand ash steel pewter].freeze
15
+
16
+ def initialize(courtyard, particles, speaker)
17
+ @courtyard = courtyard
18
+ @particles = particles
19
+ @speaker = speaker
20
+ @time = 0.0
21
+ @tile = nil
22
+ @dropped = -DROP
23
+ @landed = true
24
+ end
25
+
26
+ def update(dt)
27
+ @time += dt
28
+ land if !@landed && Ease.progress(@time, @dropped, DROP) >= LANDING
29
+ end
30
+
31
+ def drop(tile)
32
+ @tile = tile
33
+ @dropped = @time
34
+ @landed = false
35
+ end
36
+
37
+ # The last fruit of a full board, eaten with nowhere for another.
38
+ def eaten
39
+ @tile = nil
40
+ @landed = true
41
+ end
42
+
43
+ def draw(canvas)
44
+ return unless @tile
45
+
46
+ x, y = @courtyard.corner(@tile.column, @tile.row)
47
+ falling = Ease.progress(@time, @dropped, DROP)
48
+ falling < 0.5 ? canvas.draw(FAR_SHADOW, x + 3, y + 7) : canvas.draw(SHADOW, x + 2, y + 6)
49
+ canvas.draw(SPRITE, x, y - lift(falling))
50
+ glint(canvas, x, y) if falling >= 1
51
+ end
52
+
53
+ private
54
+
55
+ # A fall under gravity for most of the drop, then one bounce an eighth
56
+ # as high as the fall.
57
+ def lift(falling)
58
+ return (HEIGHT * (1 - Ease.in_quad(falling / LANDING))).round if falling < LANDING
59
+
60
+ bounce = (falling - LANDING) / (1 - LANDING)
61
+ (HEIGHT / 8 * 4 * bounce * (1 - bounce)).round
62
+ end
63
+
64
+ def land
65
+ @landed = true
66
+ x, y = @courtyard.corner(@tile.column, @tile.row)
67
+ @particles.burst(x + 1, y + 7, DUST, count: 2, speed: 22, gravity: 40, life: 0.25..0.4)
68
+ @particles.burst(x + 6, y + 7, DUST, count: 2, speed: 22, gravity: 40, life: 0.25..0.4)
69
+ @speaker.play(:plop, at: x + 4)
70
+ end
71
+
72
+ def glint(canvas, x, y)
73
+ moment = (@time - @dropped) % GLINT_EVERY
74
+ return if moment > 0.25
75
+
76
+ canvas.draw(moment.between?(0.08, 0.17) ? SPARK : SPECK, x + 1, y + 2)
77
+ end
78
+
79
+ LEGEND = {
80
+ "w" => :wine, "c" => :crimson, "r" => :red, "o" => :orange, "a" => :amber, "g" => :gold,
81
+ "d" => :deep, "f" => :fern, "l" => :leaf, "L" => :lime, "s" => :slate, "C" => :cream, "W" => :white
82
+ }.freeze
83
+
84
+ SPRITE = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
85
+ .....ll.
86
+ ....dfl.
87
+ ..wwdw..
88
+ .wgaoow.
89
+ .waoorw.
90
+ .woorrw.
91
+ .wrrrcw.
92
+ ..wccw..
93
+ ART
94
+ ........ll..
95
+ .......lLlf.
96
+ ......dllf..
97
+ ...wwwdwww..
98
+ ..wggowooow.
99
+ .wgCgoooorrw
100
+ .wggoooorrrw
101
+ .wooooorrrcw
102
+ .woooorrrccw
103
+ .woorrrrcccw
104
+ ..wrrrccccw.
105
+ ...wwwwwww..
106
+ FINE
107
+
108
+ SHADOW = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
109
+ .ssss.
110
+ ssssss
111
+ .ssss.
112
+ ART
113
+ .sssssss.
114
+ sssssssss
115
+ sssssssss
116
+ .sssssss.
117
+ FINE
118
+
119
+ FAR_SHADOW = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
120
+ .ss.
121
+ ssss
122
+ ART
123
+ .ssss.
124
+ ssssss
125
+ FINE
126
+
127
+ SPARK = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
128
+ .C.
129
+ CCC
130
+ .C.
131
+ ART
132
+ ..C..
133
+ ..C..
134
+ CCWCC
135
+ ..C..
136
+ ..C..
137
+ FINE
138
+
139
+ SPECK = Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
140
+ ...
141
+ .C.
142
+ ...
143
+ ART
144
+ ...
145
+ .C.
146
+ ...
147
+ FINE
148
+ end
149
+ end