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,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Five pixels tall, for the HUD, labels and buttons. Letters are four wide
5
+ # with a few exceptions that need the room or look better without it. On
6
+ # the fine canvas they are seven tall and five wide, with the same round
7
+ # shoulders.
8
+ Font::SMALL = Font.parse(<<~SHEET, fine: Font.parse(<<~FINE, space: 3))
9
+ A B C D E F G H I J K L M
10
+ .##. ###. .### ###. #### #### .### #..# ### ..## #..# #... #...#
11
+ #..# #..# #... #..# #... #... #... #..# .#. ...# #.#. #... ##.##
12
+ #### ###. #... #..# ###. ###. #.## #### .#. ...# ##.. #... #.#.#
13
+ #..# #..# #... #..# #... #... #..# #..# .#. #..# #.#. #... #...#
14
+ #..# ###. .### ###. #### #... .### #..# ### .##. #..# #### #...#
15
+
16
+ N O P Q R S T U V W X Y Z
17
+ #..# .##. ###. .##. ###. .### ### #..# #...# #...# #..# #.# ####
18
+ ##.# #..# #..# #..# #..# #... .#. #..# #...# #...# #..# #.# ...#
19
+ #.## #..# ###. #..# ###. .##. .#. #..# .#.#. #.#.# .##. .#. .##.
20
+ #..# #..# #... #.#. #.#. ...# .#. #..# .#.#. ##.## #..# .#. #...
21
+ #..# .##. #... .#.# #..# ###. .#. .##. ..#.. #...# #..# .#. ####
22
+
23
+ 0 1 2 3 4 5 6 7 8 9
24
+ .##. .#. ###. ###. #..# #### .##. #### .##. .##.
25
+ #..# ##. ...# ...# #..# #... #... ...# #..# #..#
26
+ #..# .#. .##. .##. #### ###. ###. ..#. .##. .###
27
+ #..# .#. #... ...# ...# ...# #..# .#.. #..# ...#
28
+ .##. ### #### ###. ...# ###. .##. .#.. .##. .##.
29
+
30
+ . , : ! ? - + / ' ×
31
+ . . . # ### ... ... ..# # ...
32
+ . . # # ..# ... .#. ..# # #.#
33
+ . . . # .#. ### ### .#. . .#.
34
+ . # # . ... ... .#. #.. . #.#
35
+ # # . # .#. ... ... #.. . ...
36
+ SHEET
37
+ A B C D E F G H I J K L M
38
+ .###. ####. .#### ####. ##### ##### .#### #...# ### ...## #...# #.... #...#
39
+ #...# #...# #.... #...# #.... #.... #.... #...# .#. ....# #..#. #.... ##.##
40
+ #...# #...# #.... #...# #.... #.... #.... #...# .#. ....# #.#.. #.... #.#.#
41
+ ##### ####. #.... #...# ####. ####. #.### ##### .#. ....# ##... #.... #.#.#
42
+ #...# #...# #.... #...# #.... #.... #...# #...# .#. #...# #.#.. #.... #...#
43
+ #...# #...# #.... #...# #.... #.... #...# #...# .#. #...# #..#. #.... #...#
44
+ #...# ####. .#### ####. ##### #.... .#### #...# ### .###. #...# ##### #...#
45
+
46
+ N O P Q R S T U V W X Y Z
47
+ #...# .###. ####. .###. ####. .#### ##### #...# #...# #...# #...# #...# #####
48
+ ##..# #...# #...# #...# #...# #.... ..#.. #...# #...# #...# #...# #...# ....#
49
+ #.#.# #...# #...# #...# #...# #.... ..#.. #...# #...# #...# .#.#. .#.#. ...#.
50
+ #..## #...# ####. #...# ####. .###. ..#.. #...# #...# #.#.# ..#.. ..#.. ..#..
51
+ #...# #...# #.... #.#.# #.#.. ....# ..#.. #...# .#.#. #.#.# .#.#. ..#.. .#...
52
+ #...# #...# #.... #..#. #..#. ....# ..#.. #...# .#.#. ##.## #...# ..#.. #....
53
+ #...# .###. #.... .##.# #...# ####. ..#.. .###. ..#.. #...# #...# ..#.. #####
54
+
55
+ 0 1 2 3 4 5 6 7 8 9
56
+ .###. .#. ####. ####. #...# ##### .###. ##### .###. .###.
57
+ #...# ##. ....# ....# #...# #.... #.... ....# #...# #...#
58
+ #...# .#. ....# ....# #...# #.... #.... ...#. #...# #...#
59
+ #...# .#. .###. .###. ##### ####. ####. ..#.. .###. .####
60
+ #...# .#. #.... ....# ....# ....# #...# .#... #...# ....#
61
+ #...# .#. #.... ....# ....# ....# #...# .#... #...# ....#
62
+ .###. ### ##### ####. ....# ####. .###. .#... .###. .###.
63
+
64
+ . , : ! ? - + / ' ×
65
+ . .. . # .###. .... ..... ....# # .....
66
+ . .. . # #...# .... ..#.. ...#. # #...#
67
+ . .. # # ....# .... ..#.. ...#. . .#.#.
68
+ . .. . # ..##. #### ##### ..#.. . ..#..
69
+ . .. . # ..#.. .... ..#.. .#... . .#.#.
70
+ . .# # . ..... .... ..#.. .#... . #...#
71
+ # #. . # ..#.. .... ..... #.... . .....
72
+ FINE
73
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # Plays the attract demo. Before each tick it picks the way to go: never
5
+ # into a wall or the body, never into a pocket too small to hold the
6
+ # snake, and otherwise toward the food (a rare fruit first, while there
7
+ # is one), with a little whim so no two demos are alike.
8
+ class Autopilot
9
+ def initialize(game, rng)
10
+ @game = game
11
+ @rng = rng
12
+ end
13
+
14
+ def steer
15
+ heading = @game.snake.heading
16
+ choices = [heading, Direction.new(heading.dy, -heading.dx), Direction.new(-heading.dy, heading.dx)]
17
+ @game.steer(choices.max_by { |direction| merit(direction) })
18
+ end
19
+
20
+ private
21
+
22
+ # Room to move matters most, up to as much room as the snake is long;
23
+ # after that, closeness to what it is after.
24
+ def merit(direction)
25
+ snake = @game.snake
26
+ target = snake.head + direction
27
+ return -Float::INFINITY if !@game.board.include?(target) || snake.blocks?(target)
28
+
29
+ room = @game.board.reachable(target, snake.body[0...-1], limit: snake.length).size - 1
30
+ goal = @game.rare&.tile || @game.food
31
+ [room, snake.length].min * 10 - (goal ? target.distance(goal) : 0) + @rng.rand
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A medal just earned, let down into the band on its ribbon from above
5
+ # the screen, its name beside it. It drops with a little bounce, and as
6
+ # it lands it rings, catches the light and throws a few sparkles; it
7
+ # hangs a while, then is drawn back up out of sight as its name fades.
8
+ # The medal and its name are centered together in points, and the medal
9
+ # hangs at that point on either canvas, where its sparkles fly from.
10
+ class Award
11
+ DOWN = 0.5
12
+ LAND = DOWN * 0.55
13
+ STAY = 2.6
14
+ UP = 0.4
15
+ # The medal's top while it hangs: its ribbon runs on above the screen.
16
+ TOP = -5
17
+ GAP = 4
18
+ FADE = 0.2
19
+ SPARKLES = %i[white cream gold amber].freeze
20
+
21
+ def initialize(medal, particles, speaker)
22
+ @medal = medal
23
+ @particles = particles
24
+ @speaker = speaker
25
+ @x = (App::WIDTH - MedalView::WIDTH - GAP - Font::SMALL.width(medal.name)) / 2
26
+ @time = 0.0
27
+ end
28
+
29
+ def done? = @time >= DOWN + STAY + UP
30
+
31
+ def update(dt)
32
+ before = @time
33
+ @time += dt
34
+ land if (before...@time).cover?(LAND)
35
+ end
36
+
37
+ def draw(canvas)
38
+ density = canvas.density
39
+ font = density.pick(Font::SMALL, Font::SMALL.fine)
40
+ width = density.pick(MedalView::WIDTH, MedalView::FINE_WIDTH)
41
+ gap = density.pick(GAP, 6)
42
+ x = density.at(@x)
43
+ MedalView.paint(canvas.pixels, density, @medal, x, (top * density.scale).round, earned: true, glint:)
44
+ cover = (Ease.progress(@time, LAND, FADE) * (1 - Ease.progress(@time, DOWN + STAY, FADE)) * 16).round
45
+ font.write(canvas.pixels, @medal.name, x + width + gap, density.pick(Hud::TEXT_Y, Hud::FINE_TEXT_Y), Palette[:gold], cover)
46
+ end
47
+
48
+ private
49
+
50
+ def top
51
+ down = Ease.out_back(Ease.progress(@time, 0.0, DOWN))
52
+ up = Ease.in_back(Ease.progress(@time, DOWN + STAY, UP))
53
+ above = -MedalView::HEIGHT - 1
54
+ (above + (TOP - above) * (down - up)).round
55
+ end
56
+
57
+ def land
58
+ @speaker.play(:medal, at: @x + MedalView::WIDTH / 2)
59
+ @particles.burst(@x + MedalView::WIDTH / 2, TOP + MedalView::DISC_Y + 7, SPARKLES, count: 14, speed: 46, gravity: 90.0, sparkle: true)
60
+ end
61
+
62
+ def glint
63
+ moment = @time - LAND
64
+ (Ease.in_out_sine(moment / 0.5) * (MedalView.glint_length + 2)).round - 1 if moment.between?(0, 0.5)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The field the snake lives on: a grid of tiles with walls all around.
5
+ class Board
6
+ attr_reader :columns, :rows, :tiles
7
+
8
+ def initialize(columns, rows)
9
+ @columns = columns
10
+ @rows = rows
11
+ @tiles = (0...rows).flat_map { |row| (0...columns).map { |column| Tile.new(column, row) } }.freeze
12
+ end
13
+
14
+ def include?(tile) = tile.column.between?(0, columns - 1) && tile.row.between?(0, rows - 1)
15
+
16
+ def free_tiles(snake) = tiles - snake.body
17
+
18
+ # The tiles that can be reached from the start without crossing a
19
+ # blocked one, the start included. A limit stops the search once that
20
+ # many have been found.
21
+ def reachable(start, blocked, limit: tiles.size)
22
+ blocked = blocked.to_set
23
+ seen = Set[start]
24
+ frontier = [start]
25
+ until frontier.empty? || seen.size > limit
26
+ frontier.pop.neighbors.each do |tile|
27
+ next if seen.include?(tile) || blocked.include?(tile) || !include?(tile)
28
+
29
+ seen << tile
30
+ frontier << tile
31
+ end
32
+ end
33
+ seen
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # A grid of palette indices, one byte per pixel, kept as one string per row
5
+ # so whole rows can be compared, copied and recolored in C. All drawing
6
+ # clips to the edges.
7
+ #
8
+ # The standard canvas is drawn on in its pixels, which are the points the
9
+ # scenes are laid out in. The fine canvas's pixels are drawn on through
10
+ # Fine instead.
11
+ #
12
+ # A canvas wider than the scenes' frame of points, for a terminal with
13
+ # room for more of the courtyard, has the frame in its middle: drawing
14
+ # is counted from the frame's left edge, the origin, so the scenes need
15
+ # not know, and what reaches to the canvas's edges asks where they are.
16
+ class Canvas
17
+ BAYER = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]].freeze
18
+
19
+ attr_reader :width, :height, :rows, :origin
20
+
21
+ def initialize(width, height, color = 0, origin: 0)
22
+ @width = width
23
+ @height = height
24
+ @origin = origin
25
+ @rows = Array.new(height) { (color.chr * width).b }
26
+ @clip = [0, 0, width, height]
27
+ end
28
+
29
+ # The canvas's edges, counted as its drawing is.
30
+ def left = -@origin
31
+
32
+ def right = @width - @origin
33
+
34
+ def density = Density::STANDARD
35
+
36
+ # The pixels to draw on exactly, which on this canvas are its own.
37
+ def pixels = self
38
+
39
+ # Words in a font, from x if they are aligned left, up to it if they are
40
+ # aligned right, or in the middle of the width from x if centered.
41
+ def write(font, text, x, y, color, cover: 16, align: :left, within: 0)
42
+ x -= font.width(text) if align == :right
43
+ x += (within - font.width(text)) / 2 if align == :center
44
+ font.write(self, text, x, y, color, cover)
45
+ end
46
+
47
+ def [](x, y) = @rows[y].getbyte(x + @origin)
48
+
49
+ def clear(color = 0) = fill(left, 0, @width, @height, color)
50
+
51
+ def plot(x, y, color)
52
+ x += @origin
53
+ left, top, right, bottom = @clip
54
+ @rows[y].setbyte(x, color) if x >= left && x < right && y >= top && y < bottom
55
+ end
56
+
57
+ def fill(x, y, width, height, color)
58
+ x += @origin
59
+ left, top, right, bottom = @clip
60
+ x0 = x.clamp(left, right)
61
+ x1 = (x + width).clamp(left, right)
62
+ return if x1 <= x0
63
+
64
+ span = color.chr * (x1 - x0)
65
+ (y.clamp(top, bottom)...(y + height).clamp(top, bottom)).each do |row|
66
+ @rows[row].bytesplice(x0, x1 - x0, span)
67
+ end
68
+ end
69
+
70
+ # Draws a sprite with its transparent pixels left out. A map recolors it
71
+ # on the way, which is how flashes, glints and silhouettes are drawn.
72
+ def draw(sprite, x, y, map = Palette::IDENTITY)
73
+ x += @origin
74
+ left, top, right, bottom = @clip
75
+ first = [left - x, 0].max
76
+ last = [right - x, sprite.width].min
77
+ sprite.rows.each_with_index do |source, sy|
78
+ row_y = y + sy
79
+ next if row_y < top || row_y >= bottom
80
+
81
+ row = @rows[row_y]
82
+ sx = first
83
+ while sx < last
84
+ color = source.getbyte(sx)
85
+ row.setbyte(x + sx, map.getbyte(color)) unless color == Sprite::CLEAR
86
+ sx += 1
87
+ end
88
+ end
89
+ end
90
+
91
+ # Draws only the share of a sprite's pixels picked by the ordered
92
+ # dither: none at cover 0, all of them at 16. Things appear and vanish
93
+ # this way instead of blending.
94
+ def draw_faded(sprite, x, y, cover, map = Palette::IDENTITY)
95
+ sprite.rows.each_with_index do |source, sy|
96
+ thresholds = BAYER[(y + sy) & 3]
97
+ source.each_byte.with_index do |color, sx|
98
+ plot(x + sx, y + sy, map.getbyte(color)) if color != Sprite::CLEAR && thresholds[(x + sx) & 3] < cover
99
+ end
100
+ end
101
+ end
102
+
103
+ # Limits drawing to a rectangle for the length of the block.
104
+ def clip(x, y, width, height)
105
+ x += @origin
106
+ outer = @clip
107
+ left, top, right, bottom = outer
108
+ @clip = [x.clamp(left, right), y.clamp(top, bottom), (x + width).clamp(left, right), (y + height).clamp(top, bottom)]
109
+ yield
110
+ ensure
111
+ @clip = outer
112
+ end
113
+
114
+ def recolor(map)
115
+ @rows.each { |row| row.tr!(Palette::IDENTITY, map) }
116
+ end
117
+
118
+ # Recolors one rectangle, as when a shadow darkens the ground under it.
119
+ def shade(x, y, width, height, map)
120
+ x += @origin
121
+ left, top, right, bottom = @clip
122
+ (y.clamp(top, bottom)...(y + height).clamp(top, bottom)).each do |row_y|
123
+ row = @rows[row_y]
124
+ (x.clamp(left, right)...(x + width).clamp(left, right)).each do |column|
125
+ row.setbyte(column, map.getbyte(row.getbyte(column)))
126
+ end
127
+ end
128
+ end
129
+
130
+ # Moves the whole picture, filling what it uncovers with a color. This
131
+ # is the camera shake: flat art moves cheaply, since a cell inside a
132
+ # flat area looks the same after it moves.
133
+ def shift(dx, dy, color = 0)
134
+ blank = (color.chr * @width).b
135
+ before = @rows.map(&:dup)
136
+ @rows.each_with_index do |row, y|
137
+ source = (y - dy).between?(0, @height - 1) ? before[y - dy] : blank
138
+ moved =
139
+ if dx.positive? then blank.byteslice(0, dx) + source.byteslice(0, @width - dx)
140
+ else source.byteslice(-dx, @width + dx) + blank.byteslice(0, -dx)
141
+ end
142
+ row.bytesplice(0, @width, moved)
143
+ end
144
+ end
145
+
146
+ # Darkens everything by up to two ramp steps as the amount goes from 0
147
+ # to 1, each step arriving as an ordered dither, so a dim falls like
148
+ # dusk rather than a lamp switched off.
149
+ def dim(amount)
150
+ level = (amount * 32).round
151
+ dither(Palette::DARKER[1], level.clamp(0, 16))
152
+ dither(Palette::DARKER[1], (level - 16).clamp(0, 16))
153
+ end
154
+
155
+ # Recolors the share of pixels picked by a 4×4 ordered dither: none at
156
+ # level 0, every pixel at level 16.
157
+ def dither(map, level)
158
+ return if level <= 0
159
+ return recolor(map) if level >= 16
160
+
161
+ @rows.each_with_index do |row, y|
162
+ thresholds = BAYER[y & 3]
163
+ x = 0
164
+ while x < @width
165
+ row.setbyte(x, map.getbyte(row.getbyte(x))) if thresholds[x & 3] < level
166
+ x += 1
167
+ end
168
+ end
169
+ end
170
+
171
+ def copy(other)
172
+ other.rows.each_with_index { |row, y| @rows[y].bytesplice(0, @width, row, 0, @width) }
173
+ end
174
+
175
+ # Lays a smaller canvas over this one, every pixel of it, with its top
176
+ # left at x, y.
177
+ def paste(other, x, y)
178
+ x += @origin
179
+ left, top, right, bottom = @clip
180
+ first = [left - x, 0].max
181
+ last = [right - x, other.width].min
182
+ return if last <= first
183
+
184
+ other.rows.each_with_index do |row, sy|
185
+ @rows[y + sy].bytesplice(x + first, last - first, row, first, last - first) if (y + sy).between?(top, bottom - 1)
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # One of the settings: a few values to step through, the one in use shown
5
+ # between two arrows while its row is chosen. A step slides the new value
6
+ # in from the side it was stepped toward as the old one slides out the
7
+ # other, and the arrow on that side gives a little kick.
8
+ class Choice
9
+ SLIDE = 0.22
10
+ KICK = 0.18
11
+
12
+ def initialize(values, value)
13
+ @values = values
14
+ @index = values.index(value)
15
+ @before = value
16
+ @by = 1
17
+ @time = 0.0
18
+ @stepped = -SLIDE
19
+ end
20
+
21
+ def value = @values[@index]
22
+
23
+ def step(by)
24
+ @before = value
25
+ @index = (@index + by) % @values.size
26
+ @by = by
27
+ @stepped = @time
28
+ end
29
+
30
+ def update(dt)
31
+ @time += dt
32
+ end
33
+
34
+ # Across the width from x, with the text at y. Values only show between
35
+ # the arrows, and slide a whole width, so the two never meet.
36
+ def draw(canvas, x, y, width, color, arrows:)
37
+ sliding = Ease.out_cubic(Ease.progress(@time, @stepped, SLIDE))
38
+ canvas.clip(x + 5, y - 1, width - 10, Font::SMALL.height + 2) do
39
+ write(canvas, value, x, y, width, ((1 - sliding) * width * @by).round, color)
40
+ write(canvas, @before, x, y, width, (-sliding * width * @by).round, color) if sliding < 1
41
+ end
42
+ draw_arrows(canvas, x, y, width, color) if arrows
43
+ end
44
+
45
+ private
46
+
47
+ def write(canvas, text, x, y, width, shift, color)
48
+ Font::SMALL.draw(canvas, text, x + shift, y, color, align: :center, within: width)
49
+ end
50
+
51
+ # The arrow on the side of the last step jumps out a pixel and flashes.
52
+ def draw_arrows(canvas, x, y, width, color)
53
+ kicking = @time - @stepped < KICK
54
+ bright = Palette::LIGHTER[2].getbyte(color)
55
+ left = kicking && @by.negative?
56
+ right = kicking && @by.positive?
57
+ canvas.draw(LEFT, x - (left ? 1 : 0), y, Palette.solid(left ? bright : color))
58
+ canvas.draw(RIGHT, x + width - RIGHT.width + (right ? 1 : 0), y, Palette.solid(right ? bright : color))
59
+ end
60
+
61
+ LEFT = Sprite.parse(<<~ART, { "a" => :ink }, fine: <<~FINE)
62
+ ..a
63
+ .aa
64
+ aaa
65
+ .aa
66
+ ..a
67
+ ART
68
+ ...a
69
+ ..aa
70
+ .aaa
71
+ aaaa
72
+ .aaa
73
+ ..aa
74
+ ...a
75
+ FINE
76
+ RIGHT = Sprite.parse(<<~ART, { "a" => :ink }, fine: <<~FINE)
77
+ a..
78
+ aa.
79
+ aaa
80
+ aa.
81
+ a..
82
+ ART
83
+ a...
84
+ aa..
85
+ aaa.
86
+ aaaa
87
+ aaa.
88
+ aa..
89
+ a...
90
+ FINE
91
+ end
92
+ end
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # The works around the settings panel. At each top corner a brass gear
5
+ # and a silver one turn in mesh, half hidden behind the panel as if the
6
+ # panel were the face of a machine, and a slotted screw holds each of its
7
+ # corners. When the settings open, the gears rise from behind the panel
8
+ # spinning and coast down to a slow turn, and the screws spin home one
9
+ # after another, all without a sound. Every change of a setting turns
10
+ # the gears on a tooth. On the fine canvas the gears are worked out at
11
+ # their own size, with rounder teeth.
12
+ class Clockwork
13
+ BRASS = Gear.new(teeth: 10, radius: 12, metal: :brass, holes: 4)
14
+ SILVER = Gear.new(teeth: 6, radius: 8, metal: :silver)
15
+ FINE = { BRASS => Gear.new(teeth: 10, radius: 18, metal: :brass, holes: 4, scale: 1.5),
16
+ SILVER => Gear.new(teeth: 6, radius: 12, metal: :silver, scale: 1.5) }.freeze
17
+ # Where the gears sit: the brass this far in from the panel's corner,
18
+ # and the silver up and in from it, close enough for their teeth to
19
+ # meet.
20
+ INSET = 20
21
+ SILVER_AT = [16, -6].freeze
22
+ RISE_FROM = 0.15
23
+ RISE = 0.5
24
+ BELOW = 16
25
+ # Turns spun on the way up, and how quickly that spin dies away.
26
+ SPIN = 1.2
27
+ COAST = 0.35
28
+ IDLE = 0.05
29
+ EASE = 14.0
30
+ SCREW_FROM = 0.4
31
+ SCREW = 0.45
32
+ SCREW_STAGGER = 0.09
33
+ SCREW_TURNS = 2
34
+ GLEAM = 0.15
35
+ # The slots the screws come to rest at, one each way.
36
+ RESTS = [3, 0, 1, 2].freeze
37
+
38
+ def initialize(x, y, width, height)
39
+ left = [x + INSET, y - 4]
40
+ right = [x + width - 1 - INSET, y - 4]
41
+ @trains = [[left, [left[0] + SILVER_AT[0], left[1] + SILVER_AT[1]], 1],
42
+ [right, [right[0] - SILVER_AT[0], right[1] + SILVER_AT[1]], -1]]
43
+ @screws = [[x + 1, y + 1], [x + width - 6, y + 1], [x + 1, y + height - 6], [x + width - 6, y + height - 6]]
44
+ @time = 0.0
45
+ @turned = 0
46
+ @shown = 0.0
47
+ end
48
+
49
+ def update(dt)
50
+ @time += dt
51
+ @shown += (@turned - @shown) * (1 - Math.exp(-EASE * dt))
52
+ end
53
+
54
+ # A setting changed: the works move on by a tooth.
55
+ def turn
56
+ @turned += 1
57
+ end
58
+
59
+ # Behind the panel, so the panel hides their lower halves.
60
+ def draw_gears(canvas)
61
+ below = ((1 - Ease.out_back(Ease.progress(@time, RISE_FROM, RISE))) * BELOW).round
62
+ @trains.each do |brass, silver, way|
63
+ turned = way * brass_angle
64
+ draw_gear(canvas, SILVER, silver, mesh(brass, silver, turned), below)
65
+ draw_gear(canvas, BRASS, brass, turned, below)
66
+ end
67
+ end
68
+
69
+ def draw_screws(canvas)
70
+ @screws.each_with_index do |(x, y), index|
71
+ from = SCREW_FROM + index * SCREW_STAGGER
72
+ left = 1 - Ease.out_cubic(Ease.progress(@time, from, SCREW))
73
+ canvas.draw(HEADS[(RESTS[index] - (left * SCREW_TURNS * HEADS.size).round) % HEADS.size], x, y)
74
+ canvas.plot(x + 1, y + 1, Palette[:cream]) if (@time - from - SCREW).between?(0, GLEAM)
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ # How far the brass gears have turned: a spin that dies away as they
81
+ # rise, a slow turn ever after, and a tooth for every change.
82
+ def brass_angle
83
+ spun = @time < RISE_FROM ? 0 : 1 - Math.exp(-(@time - RISE_FROM) / COAST)
84
+ Math::PI * 2 * (SPIN * spun + IDLE * @time) + BRASS.pitch * @shown
85
+ end
86
+
87
+ # The silver gear's angle when the brass one is at its own: turning the
88
+ # other way, as many teeth at a time, with a gap of its own wherever a
89
+ # brass tooth reaches into it.
90
+ def mesh(brass, silver, turned)
91
+ toward = Math.atan2(silver[1] - brass[1], silver[0] - brass[0])
92
+ toward + Math::PI - (turned - toward + BRASS.pitch / 2) * BRASS.teeth.fdiv(SILVER.teeth)
93
+ end
94
+
95
+ # Around its axle at a point, in the canvas's pixels.
96
+ def draw_gear(canvas, gear, (x, y), angle, below)
97
+ density = canvas.density
98
+ gear = FINE.fetch(gear) if density.fine?
99
+ canvas.pixels.draw(gear.sprite(angle), density.at(x) - gear.radius, density.at(y + below) - gear.radius)
100
+ end
101
+
102
+ LEGEND = { "g" => :gold, "a" => :amber, "o" => :orange, "w" => :wine }.freeze
103
+
104
+ # A screw head with its slot each way it can be read at this size, a
105
+ # quarter of a half turn apart.
106
+ HEADS = [
107
+ Sprite.parse(<<~ART, LEGEND, fine: <<~FINE),
108
+ .ggg.
109
+ gaaao
110
+ gwwwo
111
+ gaaao
112
+ .ooo.
113
+ ART
114
+ ..ggg..
115
+ .gaaao.
116
+ gaaaaao
117
+ gwwwwwo
118
+ gaaaaao
119
+ .oaaao.
120
+ ..ooo..
121
+ FINE
122
+ Sprite.parse(<<~ART, LEGEND, fine: <<~FINE),
123
+ .ggg.
124
+ gwaao
125
+ gawao
126
+ gaawo
127
+ .ooo.
128
+ ART
129
+ ..ggg..
130
+ .gwaao.
131
+ gawaaao
132
+ gaawaao
133
+ gaaawao
134
+ .oaawo.
135
+ ..ooo..
136
+ FINE
137
+ Sprite.parse(<<~ART, LEGEND, fine: <<~FINE),
138
+ .ggg.
139
+ gawao
140
+ gawao
141
+ gawao
142
+ .ooo.
143
+ ART
144
+ ..ggg..
145
+ .gawao.
146
+ gaawaao
147
+ gaawaao
148
+ gaawaao
149
+ .oawao.
150
+ ..ooo..
151
+ FINE
152
+ Sprite.parse(<<~ART, LEGEND, fine: <<~FINE)
153
+ .ggg.
154
+ gaawo
155
+ gawao
156
+ gwaao
157
+ .ooo.
158
+ ART
159
+ ..ggg..
160
+ .gaawo.
161
+ gaaawao
162
+ gaawaao
163
+ gawaaao
164
+ .owaao.
165
+ ..ooo..
166
+ FINE
167
+ ].freeze
168
+ end
169
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PixelSnake
4
+ # What each key means to the game. Arrows, WASD and hjkl all steer.
5
+ module Controls
6
+ ACTIONS = {
7
+ up: :up, "w" => :up, "k" => :up,
8
+ down: :down, "s" => :down, "j" => :down,
9
+ left: :left, "a" => :left, "h" => :left,
10
+ right: :right, "d" => :right, "l" => :right,
11
+ page_up: :page_up, page_down: :page_down, home: :top, end: :bottom,
12
+ enter: :confirm, " " => :confirm,
13
+ escape: :back,
14
+ "p" => :pause,
15
+ interrupt: :quit,
16
+ f3: :debug,
17
+ focus_out: :blur
18
+ }.freeze
19
+
20
+ def self.action(key) = ACTIONS[key.is_a?(String) ? key.downcase : key]
21
+
22
+ # While a scene takes a name, these keys write in it instead: every
23
+ # character, and backspace.
24
+ def self.writes?(key) = key.is_a?(String) || key == :backspace
25
+ end
26
+ end