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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +122 -0
- data/bin/pixelsnake +8 -0
- data/lib/pixelsnake/abacus.rb +102 -0
- data/lib/pixelsnake/app.rb +130 -0
- data/lib/pixelsnake/art/big_font.rb +196 -0
- data/lib/pixelsnake/art/heads.rb +68 -0
- data/lib/pixelsnake/art/small_font.rb +73 -0
- data/lib/pixelsnake/autopilot.rb +34 -0
- data/lib/pixelsnake/award.rb +67 -0
- data/lib/pixelsnake/board.rb +36 -0
- data/lib/pixelsnake/canvas.rb +189 -0
- data/lib/pixelsnake/choice.rb +92 -0
- data/lib/pixelsnake/clockwork.rb +169 -0
- data/lib/pixelsnake/controls.rb +26 -0
- data/lib/pixelsnake/countdown.rb +80 -0
- data/lib/pixelsnake/courtyard.rb +306 -0
- data/lib/pixelsnake/day.rb +23 -0
- data/lib/pixelsnake/death.rb +115 -0
- data/lib/pixelsnake/debug_overlay.rb +53 -0
- data/lib/pixelsnake/demo.rb +85 -0
- data/lib/pixelsnake/density.rb +47 -0
- data/lib/pixelsnake/ease.rb +27 -0
- data/lib/pixelsnake/fade.rb +30 -0
- data/lib/pixelsnake/feats.rb +86 -0
- data/lib/pixelsnake/fine.rb +78 -0
- data/lib/pixelsnake/fireflies.rb +63 -0
- data/lib/pixelsnake/floating_text.rb +36 -0
- data/lib/pixelsnake/font.rb +62 -0
- data/lib/pixelsnake/fruit.rb +149 -0
- data/lib/pixelsnake/game.rb +294 -0
- data/lib/pixelsnake/gear.rb +132 -0
- data/lib/pixelsnake/high_scores.rb +91 -0
- data/lib/pixelsnake/hud.rb +217 -0
- data/lib/pixelsnake/iris.rb +76 -0
- data/lib/pixelsnake/jukebox.rb +143 -0
- data/lib/pixelsnake/keyboard.rb +96 -0
- data/lib/pixelsnake/laurel.rb +76 -0
- data/lib/pixelsnake/leaf.rb +73 -0
- data/lib/pixelsnake/lettering.rb +111 -0
- data/lib/pixelsnake/medal.rb +29 -0
- data/lib/pixelsnake/medal_view.rb +428 -0
- data/lib/pixelsnake/menu.rb +213 -0
- data/lib/pixelsnake/meter.rb +108 -0
- data/lib/pixelsnake/metronome.rb +24 -0
- data/lib/pixelsnake/name.rb +22 -0
- data/lib/pixelsnake/name_field.rb +156 -0
- data/lib/pixelsnake/odometer.rb +76 -0
- data/lib/pixelsnake/palette.rb +64 -0
- data/lib/pixelsnake/panel.rb +45 -0
- data/lib/pixelsnake/particles.rb +188 -0
- data/lib/pixelsnake/piece.rb +82 -0
- data/lib/pixelsnake/playback.rb +22 -0
- data/lib/pixelsnake/preferences.rb +96 -0
- data/lib/pixelsnake/rare_fruit.rb +268 -0
- data/lib/pixelsnake/recording.rb +44 -0
- data/lib/pixelsnake/ribbon.rb +110 -0
- data/lib/pixelsnake/save_file.rb +51 -0
- data/lib/pixelsnake/scenes/game_over.rb +249 -0
- data/lib/pixelsnake/scenes/medal_case.rb +268 -0
- data/lib/pixelsnake/scenes/pause.rb +171 -0
- data/lib/pixelsnake/scenes/play.rb +326 -0
- data/lib/pixelsnake/scenes/scores.rb +420 -0
- data/lib/pixelsnake/scenes/settings.rb +344 -0
- data/lib/pixelsnake/scenes/size_notice.rb +38 -0
- data/lib/pixelsnake/scenes/statistics.rb +420 -0
- data/lib/pixelsnake/scenes/title.rb +152 -0
- data/lib/pixelsnake/screen.rb +163 -0
- data/lib/pixelsnake/shed.rb +40 -0
- data/lib/pixelsnake/skin.rb +36 -0
- data/lib/pixelsnake/snake.rb +53 -0
- data/lib/pixelsnake/snake_view.rb +393 -0
- data/lib/pixelsnake/sound/band.rb +354 -0
- data/lib/pixelsnake/sound/instruments.rb +143 -0
- data/lib/pixelsnake/sound/mixer.rb +60 -0
- data/lib/pixelsnake/sound/music.rb +15 -0
- data/lib/pixelsnake/sound/pulse.rb +149 -0
- data/lib/pixelsnake/sound/song.rb +22 -0
- data/lib/pixelsnake/sound/sounds.rb +225 -0
- data/lib/pixelsnake/sound/speaker.rb +199 -0
- data/lib/pixelsnake/sound/synth.rb +277 -0
- data/lib/pixelsnake/sound/tune.rb +145 -0
- data/lib/pixelsnake/sound/tunes/dusk.rb +154 -0
- data/lib/pixelsnake/sound/tunes/fireflies.rb +106 -0
- data/lib/pixelsnake/sound/tunes/jade.rb +100 -0
- data/lib/pixelsnake/sound/tunes/lanterns.rb +126 -0
- data/lib/pixelsnake/sound/tunes.rb +40 -0
- data/lib/pixelsnake/sprite.rb +58 -0
- data/lib/pixelsnake/stage.rb +95 -0
- data/lib/pixelsnake/stats.rb +131 -0
- data/lib/pixelsnake/terminal.rb +84 -0
- data/lib/pixelsnake/terrarium.rb +206 -0
- data/lib/pixelsnake/tile.rb +32 -0
- data/lib/pixelsnake/toolbox.rb +251 -0
- data/lib/pixelsnake/trophy.rb +99 -0
- data/lib/pixelsnake/tuft.rb +55 -0
- data/lib/pixelsnake/upscale.rb +60 -0
- data/lib/pixelsnake/version.rb +5 -0
- data/lib/pixelsnake.rb +143 -0
- metadata +146 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# A column of pixel buttons. The chosen one is a lit lantern among dark
|
|
5
|
+
# ones. When the choice moves, the light slides over with a little
|
|
6
|
+
# overshoot and the lantern catches from its heart outward, hot for a
|
|
7
|
+
# moment before it settles. While it rests, a glint runs across it every
|
|
8
|
+
# few seconds, the same light that crosses the logo. A press sinks the
|
|
9
|
+
# lantern and flares it; a tap sinks it for a moment and lights it again
|
|
10
|
+
# from the heart, for a button that does something without leaving. The
|
|
11
|
+
# buttons ease in one after another, and the light knocks softly as it
|
|
12
|
+
# moves, a little higher going up. The scene around the menu draws its
|
|
13
|
+
# panel.
|
|
14
|
+
#
|
|
15
|
+
# Each label is written in the middle of its button, unless the menu is
|
|
16
|
+
# given a block to write the buttons itself. Without slots, the rows sit
|
|
17
|
+
# straight on the panel and only the lantern stands out. The light starts
|
|
18
|
+
# on the first button unless told another.
|
|
19
|
+
#
|
|
20
|
+
# Buttons are placed in points and their edges drawn in pixels, a pixel
|
|
21
|
+
# each at either density; the words go through the canvas as the scene's
|
|
22
|
+
# do, so they land where the buttons are.
|
|
23
|
+
class Menu
|
|
24
|
+
BUTTON = 11
|
|
25
|
+
GAP = 3
|
|
26
|
+
SLIDE = 0.18
|
|
27
|
+
CATCH = 0.24
|
|
28
|
+
ARRIVE = 0.4
|
|
29
|
+
STAGGER = 0.07
|
|
30
|
+
TAP = 0.12
|
|
31
|
+
GLINT = 0.55
|
|
32
|
+
GLINT_EVERY = 3.6
|
|
33
|
+
GLINT_REST = 1.2
|
|
34
|
+
GLINT_STEPS = [1, 2, 1].freeze
|
|
35
|
+
|
|
36
|
+
def initialize(labels, x:, y:, width:, speaker:, delay: 0.0, gap: GAP, slots: true, at: 0, &write)
|
|
37
|
+
@labels = labels
|
|
38
|
+
@x = x
|
|
39
|
+
@y = y
|
|
40
|
+
@width = width
|
|
41
|
+
@speaker = speaker
|
|
42
|
+
@delay = delay
|
|
43
|
+
@gap = gap
|
|
44
|
+
@slots = slots
|
|
45
|
+
@write = write || method(:write_label)
|
|
46
|
+
@time = 0.0
|
|
47
|
+
@index = at
|
|
48
|
+
@slide_from = at.to_f
|
|
49
|
+
@moved = -SLIDE
|
|
50
|
+
@lit = delay + ARRIVE / 2
|
|
51
|
+
@pressed = false
|
|
52
|
+
@tapped = -TAP
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def selected = @labels[@index]
|
|
56
|
+
|
|
57
|
+
# Whether the buttons have begun to come in.
|
|
58
|
+
def up? = @time >= @delay
|
|
59
|
+
|
|
60
|
+
def height = @labels.size * (BUTTON + @gap) - @gap
|
|
61
|
+
|
|
62
|
+
# The middle of the chosen button, where the eye is.
|
|
63
|
+
def focus = [@x + @width / 2, button_y(@index) + BUTTON / 2]
|
|
64
|
+
|
|
65
|
+
def move(by)
|
|
66
|
+
select((@index + by) % @labels.size, by)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Selects whatever button is under a point, and says whether there was
|
|
70
|
+
# one.
|
|
71
|
+
def point(x, y)
|
|
72
|
+
row = @labels.each_index.find do |index|
|
|
73
|
+
x.between?(@x, @x + @width - 1) && y.between?(button_y(index), button_y(index) + BUTTON - 1)
|
|
74
|
+
end
|
|
75
|
+
select(row, row - @index) if row && row != @index
|
|
76
|
+
!row.nil?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def press
|
|
80
|
+
@pressed = true
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def tap
|
|
84
|
+
@tapped = @time
|
|
85
|
+
@lit = @time
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def update(dt)
|
|
89
|
+
@time += dt
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def draw(canvas)
|
|
93
|
+
canvas.clip(@x, @y, @width, height + 1) do
|
|
94
|
+
@labels.each_index { |index| draw_slot(canvas, index) } if @slots
|
|
95
|
+
draw_lantern(canvas)
|
|
96
|
+
@labels.each_index { |index| write(canvas, index) }
|
|
97
|
+
draw_glint(canvas)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A button's box in the canvas's pixels, from its left and top in
|
|
102
|
+
# points.
|
|
103
|
+
def box(canvas, x, y)
|
|
104
|
+
density = canvas.density
|
|
105
|
+
left = density.at(x)
|
|
106
|
+
top = density.at(y)
|
|
107
|
+
[left, top, density.at(x + @width) - left, density.at(y + BUTTON) - top]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
def select(index, by)
|
|
113
|
+
@slide_from = lantern_row
|
|
114
|
+
@index = index
|
|
115
|
+
@moved = @time
|
|
116
|
+
@lit = @time + SLIDE / 2
|
|
117
|
+
@speaker.play(:move, by.negative? ? 0 : 1)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def pressed? = @pressed || @time - @tapped < TAP
|
|
121
|
+
|
|
122
|
+
def button_y(row) = (@y + row * (BUTTON + @gap)).round
|
|
123
|
+
|
|
124
|
+
# Where the lantern is, in rows; between two rows while it slides.
|
|
125
|
+
def lantern_row
|
|
126
|
+
@slide_from + (@index - @slide_from) * Ease.out_back(Ease.progress(@time, @moved, SLIDE))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def lantern_top = button_y(lantern_row) + (pressed? ? 1 : 0)
|
|
130
|
+
|
|
131
|
+
# Each button slides in on its own beat from past the right edge of the
|
|
132
|
+
# menu, where it is out of sight until then, and slows into place. It
|
|
133
|
+
# does not overshoot: past its place it would be cut off at the left.
|
|
134
|
+
def arrival(index) = ((1 - Ease.out_cubic(Ease.progress(@time, @delay + index * STAGGER, ARRIVE))) * @width).round
|
|
135
|
+
|
|
136
|
+
# A button at rest sits dark and a little sunk into the panel.
|
|
137
|
+
def draw_slot(canvas, index)
|
|
138
|
+
x, y, width, height = box(canvas, @x + arrival(index), button_y(index))
|
|
139
|
+
pixels = canvas.pixels
|
|
140
|
+
pixels.fill(x, y, width, height, Palette[:violet])
|
|
141
|
+
pixels.fill(x, y, width - 1, height - 1, Palette[:ink])
|
|
142
|
+
pixels.fill(x + 1, y + 1, width - 2, height - 2, Palette[:night])
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Lit along its top and left, shaded along its bottom and right, with
|
|
146
|
+
# its heart a step brighter than its body.
|
|
147
|
+
def draw_lantern(canvas)
|
|
148
|
+
x, y, width, height = box(canvas, @x + arrival(@index), lantern_top)
|
|
149
|
+
pixels = canvas.pixels
|
|
150
|
+
pixels.fill(x, y, width, height, Palette[:ink])
|
|
151
|
+
pixels.fill(x, y, width - 1, height - 1, Palette[:amber])
|
|
152
|
+
pixels.fill(x + 1, y + 1, width - 2, height - 2, Palette[:crimson])
|
|
153
|
+
draw_heart(pixels, canvas.density, x, y, width, height)
|
|
154
|
+
pixels.fill(x + 1, y + height - 2, width - 2, 1, Palette[:wine])
|
|
155
|
+
pixels.fill(x + width - 2, y + 1, 1, height - 2, Palette[:wine])
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The heart catches from the middle outward like a wick: a steady red
|
|
159
|
+
# behind two bright edges that run out to its ends. Pressed, all of it
|
|
160
|
+
# flares.
|
|
161
|
+
def draw_heart(canvas, density, x, y, width, height)
|
|
162
|
+
catching = Ease.progress(@time, @lit, CATCH)
|
|
163
|
+
return if catching.zero?
|
|
164
|
+
|
|
165
|
+
inset = density.pick(3, 4)
|
|
166
|
+
edge = density.pick(2, 3)
|
|
167
|
+
whole = width - inset * 2
|
|
168
|
+
heart = (whole * Ease.out_cubic(catching)).round
|
|
169
|
+
left = x + inset + (whole - heart) / 2
|
|
170
|
+
canvas.fill(left, y + inset, heart, height - inset * 2, Palette[@pressed ? :orange : :red])
|
|
171
|
+
return if catching == 1
|
|
172
|
+
|
|
173
|
+
canvas.fill(left, y + inset, edge, height - inset * 2, Palette[:orange])
|
|
174
|
+
canvas.fill(left + heart - edge, y + inset, edge, height - inset * 2, Palette[:orange])
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Where the words on a button go and in what color, for whatever writes
|
|
178
|
+
# them: the button's left edge, the top of the text, steel on a dark
|
|
179
|
+
# button, gold on the lantern, cream while it is pressed.
|
|
180
|
+
def write(canvas, index)
|
|
181
|
+
chosen = index == @index
|
|
182
|
+
color = if !chosen then :steel
|
|
183
|
+
elsif pressed? then :cream
|
|
184
|
+
else :gold
|
|
185
|
+
end
|
|
186
|
+
y = button_y(index) + 3 + (chosen && pressed? ? 1 : 0)
|
|
187
|
+
@write.call(canvas, index, @x + arrival(index), y, Palette[color])
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def write_label(canvas, index, x, y, color)
|
|
191
|
+
Font::SMALL.draw(canvas, @labels[index], x, y, color, align: :center, within: @width)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# A slanted band of light crossing the lantern, label and all: two
|
|
195
|
+
# steps brighter at its core, one at its edges, and never on the
|
|
196
|
+
# outline, so the lantern's shape stays put while the light moves.
|
|
197
|
+
def draw_glint(canvas)
|
|
198
|
+
moment = @time - @lit - GLINT_REST
|
|
199
|
+
return if pressed? || moment.negative? || moment % GLINT_EVERY > GLINT
|
|
200
|
+
|
|
201
|
+
x, top, width, height = box(canvas, @x, lantern_top)
|
|
202
|
+
pixels = canvas.pixels
|
|
203
|
+
front = (Ease.in_out_sine(moment % GLINT_EVERY / GLINT) * (width + height)).round
|
|
204
|
+
pixels.clip(x, top, width - 1, height - 1) do
|
|
205
|
+
height.times do |row|
|
|
206
|
+
GLINT_STEPS.each_with_index do |steps, offset|
|
|
207
|
+
pixels.shade(x + front - row - offset, top + row, 1, 1, Palette::LIGHTER[steps])
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# How loud the sounds or the music are, as three bars rising from left to
|
|
5
|
+
# right, lit as far as the volume goes: none for off, all three for high.
|
|
6
|
+
# A new volume lights the bars one after another, each flashing as it
|
|
7
|
+
# catches, or puts them out one after another from the top. While its row
|
|
8
|
+
# is chosen an arrow stands at each side, and as with a setting the arrow
|
|
9
|
+
# toward a step kicks out a pixel; an arrow with nowhere left to go is
|
|
10
|
+
# dark.
|
|
11
|
+
class Meter
|
|
12
|
+
VOLUMES = Speaker::VOLUMES.keys
|
|
13
|
+
HEIGHTS = [2, 3, 5].freeze
|
|
14
|
+
BAR = 3
|
|
15
|
+
GAP = 2
|
|
16
|
+
SPAN = HEIGHTS.size * (BAR + GAP) - GAP
|
|
17
|
+
# The bars on the fine canvas, in its pixels.
|
|
18
|
+
FINE_HEIGHTS = [3, 5, 7].freeze
|
|
19
|
+
FINE_BAR = 4
|
|
20
|
+
FINE_GAP = 3
|
|
21
|
+
# Each bar lights or goes out this long after the one before it.
|
|
22
|
+
EACH = 0.05
|
|
23
|
+
FLASH = 0.15
|
|
24
|
+
|
|
25
|
+
attr_reader :volume
|
|
26
|
+
|
|
27
|
+
def initialize(volume)
|
|
28
|
+
@volume = volume
|
|
29
|
+
@from = level
|
|
30
|
+
@changed = -FLASH
|
|
31
|
+
@by = 0
|
|
32
|
+
@time = 0.0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The volume a step down or up, or nothing past either end.
|
|
36
|
+
def beside(by)
|
|
37
|
+
index = level + by
|
|
38
|
+
VOLUMES[index] if index.between?(0, VOLUMES.size - 1)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Goes to a new volume from whatever is lit, and kicks the arrow on the
|
|
42
|
+
# side it was stepped toward, if it was stepped.
|
|
43
|
+
def turn(volume, by: 0)
|
|
44
|
+
@from = lit
|
|
45
|
+
@volume = volume
|
|
46
|
+
@changed = @time
|
|
47
|
+
@by = by
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def update(dt)
|
|
51
|
+
@time += dt
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Across the width from x, with the bars standing on the line of text
|
|
55
|
+
# whose top is at y. Chosen, it sits on the lantern, in its colors. It
|
|
56
|
+
# is placed in points and drawn in the canvas's pixels.
|
|
57
|
+
def draw(canvas, x, y, width, chosen:)
|
|
58
|
+
density = canvas.density
|
|
59
|
+
pixels = canvas.pixels
|
|
60
|
+
left = density.at(x)
|
|
61
|
+
top = density.at(y)
|
|
62
|
+
width = density.at(x + width) - left
|
|
63
|
+
bar = density.pick(BAR, FINE_BAR)
|
|
64
|
+
gap = density.pick(GAP, FINE_GAP)
|
|
65
|
+
heights = density.pick(HEIGHTS, FINE_HEIGHTS)
|
|
66
|
+
line = density.pick(Font::SMALL, Font::SMALL.fine).height
|
|
67
|
+
start = left + (width - (heights.size * (bar + gap) - gap)) / 2
|
|
68
|
+
heights.each_with_index do |height, index|
|
|
69
|
+
pixels.fill(start + index * (bar + gap), top + line - height, bar, height, color(index, chosen))
|
|
70
|
+
end
|
|
71
|
+
draw_arrows(pixels, density, left, top, width) if chosen
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def level = VOLUMES.index(@volume)
|
|
77
|
+
|
|
78
|
+
# How many bars are lit: one more, or one fewer, every EACH from the
|
|
79
|
+
# change, the first at once, until the volume is reached.
|
|
80
|
+
def lit
|
|
81
|
+
steps = ((@time - @changed) / EACH).floor + 1
|
|
82
|
+
level > @from ? [@from + steps, level].min : [@from - steps, level].max
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def color(bar, chosen)
|
|
86
|
+
return Palette[chosen ? :wine : :violet] if bar >= lit
|
|
87
|
+
|
|
88
|
+
color = Palette[chosen ? :cream : :ash]
|
|
89
|
+
flashing?(bar) ? Palette::LIGHTER[1].getbyte(color) : color
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# A bar that has just caught, going up, is a step brighter for a moment.
|
|
93
|
+
def flashing?(bar) = bar >= @from && @time - @changed - (bar - @from) * EACH < FLASH
|
|
94
|
+
|
|
95
|
+
def draw_arrows(canvas, density, x, y, width)
|
|
96
|
+
kicking = @time - @changed < Choice::KICK ? @by : 0
|
|
97
|
+
left = density.pick(Choice::LEFT, Choice::LEFT.fine)
|
|
98
|
+
right = density.pick(Choice::RIGHT, Choice::RIGHT.fine)
|
|
99
|
+
[[left, x, -1], [right, x + width - right.width, 1]].each do |arrow, arrow_x, side|
|
|
100
|
+
color = if kicking == side then Palette[:white]
|
|
101
|
+
elsif beside(side).nil? then Palette[:wine]
|
|
102
|
+
else Palette[:cream]
|
|
103
|
+
end
|
|
104
|
+
canvas.draw(arrow, arrow_x + (kicking == side ? side : 0), y, Palette.solid(color))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# Keeps time for a game: turns elapsed seconds into ticks at the game's
|
|
5
|
+
# own pace, and knows how far along it is toward the next one, which is
|
|
6
|
+
# how far the snake is drawn between tiles.
|
|
7
|
+
class Metronome
|
|
8
|
+
def initialize(game)
|
|
9
|
+
@game = game
|
|
10
|
+
@elapsed = game.interval
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Yields once for every tick that is due.
|
|
14
|
+
def advance(dt)
|
|
15
|
+
@elapsed += dt
|
|
16
|
+
while @elapsed >= @game.interval && !@game.over?
|
|
17
|
+
@elapsed -= @game.interval
|
|
18
|
+
yield
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def progress = @game.over? ? 1.0 : (@elapsed / @game.interval).clamp(0.0, 1.0)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# What a player can be called: up to ten capitals, digits and marks the
|
|
5
|
+
# small font can write, with single spaces between words.
|
|
6
|
+
module Name
|
|
7
|
+
LIMIT = 10
|
|
8
|
+
|
|
9
|
+
# Whether a character can go on the end of a name.
|
|
10
|
+
def self.takes?(name, char)
|
|
11
|
+
return false if name.size >= LIMIT || !Font::SMALL.writes?(char)
|
|
12
|
+
|
|
13
|
+
char != " " || !(name.empty? || name.end_with?(" "))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Any text made into a name the game can write, such as one read from
|
|
17
|
+
# a file or the login name.
|
|
18
|
+
def self.clean(text)
|
|
19
|
+
text.to_s.upcase.each_char.with_object(+"") { |char, name| name << char if takes?(name, char) }.rstrip
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# A name being written. It starts with the name the player went by last
|
|
5
|
+
# time, marked as selected: Enter keeps it, and the first key replaces
|
|
6
|
+
# it. The letters stay centered in the field, gliding over as the name
|
|
7
|
+
# grows or shrinks; a new letter drops into place a shade brighter and
|
|
8
|
+
# settles, and letters rubbed out fall away. The caret after them glows
|
|
9
|
+
# steadily while letters come and breathes once they stop. Each letter
|
|
10
|
+
# ticks as it is written and each rubbed out a little lower; a letter
|
|
11
|
+
# the name cannot take shakes the field and is turned down with a low
|
|
12
|
+
# note instead. Its glide is kept in points; on the fine canvas the fine
|
|
13
|
+
# letters glide the same way about their own middle.
|
|
14
|
+
class NameField
|
|
15
|
+
DROP = 0.16
|
|
16
|
+
FALL = 0.18
|
|
17
|
+
STEADY = 0.6
|
|
18
|
+
BREATHE = 1.8
|
|
19
|
+
SHAKE = 0.3
|
|
20
|
+
GLIDE = 26.0
|
|
21
|
+
GLOW = %i[cream gold amber orange].freeze
|
|
22
|
+
|
|
23
|
+
attr_reader :text
|
|
24
|
+
|
|
25
|
+
# The name it starts from is copied: what is written here is the
|
|
26
|
+
# field's own until it is kept.
|
|
27
|
+
def initialize(text, width:, speaker:)
|
|
28
|
+
@text = text.dup
|
|
29
|
+
@width = width
|
|
30
|
+
@speaker = speaker
|
|
31
|
+
@selected = !text.empty?
|
|
32
|
+
@time = 0.0
|
|
33
|
+
@dropped = Array.new(text.size, -DROP)
|
|
34
|
+
@falling = []
|
|
35
|
+
@typed = -STEADY
|
|
36
|
+
@refused = -SHAKE
|
|
37
|
+
@left = home
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def type(key)
|
|
41
|
+
@typed = @time
|
|
42
|
+
return erase if key == :backspace
|
|
43
|
+
|
|
44
|
+
char = key.upcase
|
|
45
|
+
if @selected
|
|
46
|
+
return refuse unless Name.takes?("", char)
|
|
47
|
+
|
|
48
|
+
clear
|
|
49
|
+
end
|
|
50
|
+
write(char)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def refuse
|
|
54
|
+
@refused = @time
|
|
55
|
+
@speaker.play(:refuse)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def update(dt)
|
|
59
|
+
@time += dt
|
|
60
|
+
@left += (home - @left) * (1 - Math.exp(-GLIDE * dt))
|
|
61
|
+
@falling.reject! { |_, _, at| @time - at > FALL }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The field's left edge is at x, and its text at y, in points. Once the
|
|
65
|
+
# name is kept, the caret goes.
|
|
66
|
+
def draw(canvas, x, y, color, caret: true)
|
|
67
|
+
density = canvas.density
|
|
68
|
+
pixels = canvas.pixels
|
|
69
|
+
font = density.pick(Font::SMALL, Font::SMALL.fine)
|
|
70
|
+
left = density.at(x) + glided(density, font, density.at(x + @width) - density.at(x)) + shake(density)
|
|
71
|
+
top = density.at(y)
|
|
72
|
+
margin = density.pick(2, 3)
|
|
73
|
+
pixels.fill(left - margin, top - margin, font.width(@text) + margin * 2, font.height + margin * 2, Palette[:steel]) if @selected
|
|
74
|
+
@text.each_char.with_index do |char, index|
|
|
75
|
+
landing = Ease.progress(@time, @dropped[index], DROP)
|
|
76
|
+
lift = ((1 - Ease.out_back(landing)) * density.pick(4, 6)).round
|
|
77
|
+
font.write(pixels, char, left, top - lift, landing < 1 ? Palette::LIGHTER[1].getbyte(color) : color, 16)
|
|
78
|
+
left += font.advance(char)
|
|
79
|
+
end
|
|
80
|
+
draw_falling(canvas, x, y, color)
|
|
81
|
+
pixels.fill(left, top - 1, 1, font.height + 2, caret_color) if caret && !@selected
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def write(char)
|
|
87
|
+
return refuse unless Name.takes?(@text, char)
|
|
88
|
+
|
|
89
|
+
@text << char
|
|
90
|
+
@dropped << @time
|
|
91
|
+
@speaker.play(:key, @text.size % 3)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Backspace takes a selected name away whole, or else the last letter.
|
|
95
|
+
def erase
|
|
96
|
+
return refuse if @text.empty?
|
|
97
|
+
|
|
98
|
+
@selected ? clear : rub_out
|
|
99
|
+
@speaker.play(:erase)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def rub_out
|
|
103
|
+
fall(@text.size - 1)
|
|
104
|
+
@text.chop!
|
|
105
|
+
@dropped.pop
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# A selected name falls away all at once.
|
|
109
|
+
def clear
|
|
110
|
+
@selected = false
|
|
111
|
+
@text.size.times { |index| fall(index) }
|
|
112
|
+
@text.clear
|
|
113
|
+
@dropped.clear
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def fall(index)
|
|
117
|
+
before = Font::SMALL.width(@text[0, index + 1]) - Font::SMALL.width(@text[index])
|
|
118
|
+
@falling << [@text[index], @left.round + before, @time]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Where the text starts when it and the caret are centered.
|
|
122
|
+
def home = (@width - Font::SMALL.width(@text) - 2) / 2.0
|
|
123
|
+
|
|
124
|
+
# Where the text starts now, in the pixels of a field so wide: on the
|
|
125
|
+
# fine canvas as far from its own middle as the standard text is from
|
|
126
|
+
# its middle.
|
|
127
|
+
def glided(density, font, width)
|
|
128
|
+
return @left.round unless density.fine?
|
|
129
|
+
|
|
130
|
+
((width - font.width(@text) - 2) / 2.0 + (@left - home) * density.scale).round
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def shake(density = Density::STANDARD)
|
|
134
|
+
since = @time - @refused
|
|
135
|
+
since < SHAKE ? (Math.sin(since * 70) * 2.5 * density.scale * (1 - since / SHAKE)).round : 0
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def draw_falling(canvas, x, y, color)
|
|
139
|
+
@falling.each do |char, from, at|
|
|
140
|
+
falling = Ease.progress(@time, at, FALL)
|
|
141
|
+
drop = (Ease.in_quad(falling) * 4).round
|
|
142
|
+
Font::SMALL.draw(canvas, char, x + from, y + drop, color, cover: ((1 - falling) * 16).round)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Bright and still while letters are coming, then slowly breathing
|
|
147
|
+
# down the gold ramp and back.
|
|
148
|
+
def caret_color
|
|
149
|
+
resting = @time - @typed - STEADY
|
|
150
|
+
return Palette[GLOW.first] if resting.negative?
|
|
151
|
+
|
|
152
|
+
depth = (1 - Math.cos(resting / BREATHE * Math::PI * 2)) / 2
|
|
153
|
+
Palette[GLOW[(depth * (GLOW.size - 1)).round]]
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# A number whose digits roll to their new values instead of snapping, one
|
|
5
|
+
# after another from the right, like a mechanical counter. Leading zeros
|
|
6
|
+
# are drawn dim. A number that outgrows its places gets another on the
|
|
7
|
+
# left, which rolls up from nought. It is drawn in the pixels of a canvas
|
|
8
|
+
# at its density, in the fine letters on the fine canvas.
|
|
9
|
+
class Odometer
|
|
10
|
+
ROLL = 0.3
|
|
11
|
+
STAGGER = 0.07
|
|
12
|
+
# How far apart the digits stand and how far one rolls, in pixels.
|
|
13
|
+
PITCH = 5
|
|
14
|
+
TRAVEL = 7
|
|
15
|
+
FINE_PITCH = 6
|
|
16
|
+
FINE_TRAVEL = 10
|
|
17
|
+
|
|
18
|
+
def initialize(value, places: 5)
|
|
19
|
+
@time = 0.0
|
|
20
|
+
@digits = digits_of(value, places)
|
|
21
|
+
@rolls = Array.new(@digits.size)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# In the pixels of a density.
|
|
25
|
+
def width(density = Density::STANDARD) = @digits.size * density.pick(PITCH, FINE_PITCH) - 1
|
|
26
|
+
|
|
27
|
+
def value=(value)
|
|
28
|
+
target = digits_of(value, @digits.size)
|
|
29
|
+
grown = target.size - @digits.size
|
|
30
|
+
@digits = Array.new(grown, "0") + @digits
|
|
31
|
+
@rolls = Array.new(grown) + @rolls
|
|
32
|
+
changed = (0...target.size).select { |place| target[place] != @digits[place] }
|
|
33
|
+
changed.reverse.each_with_index do |place, order|
|
|
34
|
+
@rolls[place] = [@digits[place], target[place], @time + order * STAGGER]
|
|
35
|
+
end
|
|
36
|
+
@digits = target
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def update(dt)
|
|
40
|
+
@time += dt
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# From a pixel of a canvas of pixels, at a density.
|
|
44
|
+
def draw(canvas, x, y, color, dim, density = Density::STANDARD)
|
|
45
|
+
font = density.pick(Font::SMALL, Font::SMALL.fine)
|
|
46
|
+
pitch = density.pick(PITCH, FINE_PITCH)
|
|
47
|
+
significant = @digits.index { |digit| digit != "0" } || @digits.size - 1
|
|
48
|
+
@digits.each_with_index do |digit, place|
|
|
49
|
+
left = x + place * pitch
|
|
50
|
+
tint = place < significant ? dim : color
|
|
51
|
+
roll = @rolls[place]
|
|
52
|
+
if roll && @time < roll[2] + ROLL
|
|
53
|
+
draw_rolling(canvas, font, pitch, density.pick(TRAVEL, FINE_TRAVEL), left, y, *roll, tint)
|
|
54
|
+
else
|
|
55
|
+
draw_digit(canvas, font, pitch, digit, left, y, tint)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def digits_of(value, places) = value.to_s.rjust(places, "0").chars
|
|
63
|
+
|
|
64
|
+
def draw_rolling(canvas, font, pitch, travel, x, y, from, to, start, color)
|
|
65
|
+
offset = (Ease.out_back(Ease.progress(@time, start, ROLL)) * travel).round
|
|
66
|
+
canvas.clip(x, y, pitch - 1, font.height) do
|
|
67
|
+
draw_digit(canvas, font, pitch, from, x, y - offset, color)
|
|
68
|
+
draw_digit(canvas, font, pitch, to, x, y - offset + travel, color)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def draw_digit(canvas, font, pitch, digit, x, y, color)
|
|
73
|
+
font.write(canvas, digit, x + (pitch - 1 - font.width(digit)) / 2, y, color, 16)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# The 32 colors of a stone courtyard at dusk, as ramps from dark to light.
|
|
5
|
+
# Shadows lean violet and teal, highlights lean sand, lime and gold. The
|
|
6
|
+
# last two ramps are only for snakes in other skins; each borrows its
|
|
7
|
+
# darkest and lightest colors from the ramps before it.
|
|
8
|
+
#
|
|
9
|
+
# Everything on screen is an index into this palette. A recoloring is a
|
|
10
|
+
# map: a string whose byte i is the index that color i becomes, which
|
|
11
|
+
# String#tr! applies to a whole row of pixels at once.
|
|
12
|
+
module Palette
|
|
13
|
+
RAMPS = {
|
|
14
|
+
night: { ink: 0x0b0a17, night: 0x161428, indigo: 0x221e3b, violet: 0x2f2a4f },
|
|
15
|
+
stone: { slate: 0x2a2a46, cobble: 0x34365a, pewter: 0x3d4166, steel: 0x565b7c, ash: 0x7f7f95, sand: 0xb8aa98 },
|
|
16
|
+
jade: { deep: 0x0c2a33, pine: 0x12524d, jade: 0x1f7d58, fern: 0x3fa85a, leaf: 0x8fd160, lime: 0xdff07c },
|
|
17
|
+
ember: { wine: 0x3a1230, crimson: 0x7d1f3a, red: 0xc4372f, orange: 0xec6b2e, amber: 0xffaa4a, gold: 0xffdc8a },
|
|
18
|
+
light: { cream: 0xfff4cf, white: 0xfffdf5 },
|
|
19
|
+
lagoon: { tide: 0x15456b, teal: 0x1d7590, lagoon: 0x34a9b4, foam: 0x8ce0cf },
|
|
20
|
+
orchid: { plum: 0x5a2468, mauve: 0x8c3a8c, orchid: 0xc45aa8, blush: 0xf39ac0 }
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# Where each ramp continues past its ends, so a fade always reaches ink
|
|
24
|
+
# and a flash always reaches white.
|
|
25
|
+
BELOW = { ink: :ink, slate: :indigo, deep: :night, wine: :night, cream: :gold, tide: :deep, plum: :wine }.freeze
|
|
26
|
+
ABOVE = { violet: :steel, sand: :cream, lime: :cream, gold: :cream, white: :white, foam: :white, blush: :cream }.freeze
|
|
27
|
+
|
|
28
|
+
NAMES = RAMPS.values.flat_map(&:keys).freeze
|
|
29
|
+
RGB = RAMPS.values.flat_map(&:values).freeze
|
|
30
|
+
SIZE = NAMES.size
|
|
31
|
+
|
|
32
|
+
def self.[](name) = NAMES.index(name) || raise(KeyError, "no color named #{name}")
|
|
33
|
+
|
|
34
|
+
def self.rgb(index) = [RGB[index] >> 16, (RGB[index] >> 8) & 0xff, RGB[index] & 0xff]
|
|
35
|
+
|
|
36
|
+
def self.map(&block) = Array.new(SIZE, &block).pack("C*").freeze
|
|
37
|
+
|
|
38
|
+
IDENTITY = map { |index| index }
|
|
39
|
+
SOLID = Array.new(SIZE) { |color| map { color } }.freeze
|
|
40
|
+
|
|
41
|
+
# A map that paints everything in one color: text, shadows, silhouettes.
|
|
42
|
+
def self.solid(color) = SOLID[color]
|
|
43
|
+
|
|
44
|
+
def self.neighbor(name, offset, beyond)
|
|
45
|
+
ramp = RAMPS.values.find { |colors| colors.key?(name) }.keys
|
|
46
|
+
position = ramp.index(name) + offset
|
|
47
|
+
position.between?(0, ramp.size - 1) ? ramp[position] : beyond.fetch(name)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Maps for 0, 1, 2... steps along the ramps, until every color has
|
|
51
|
+
# arrived at the end.
|
|
52
|
+
def self.walk(offset, beyond)
|
|
53
|
+
step = map { |index| self[neighbor(NAMES[index], offset, beyond)] }
|
|
54
|
+
maps = [IDENTITY]
|
|
55
|
+
maps << maps.last.tr(IDENTITY, step).freeze until maps.last == maps.last.tr(IDENTITY, step)
|
|
56
|
+
maps.freeze
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
DARKER = walk(-1, BELOW)
|
|
60
|
+
LIGHTER = walk(+1, ABOVE)
|
|
61
|
+
|
|
62
|
+
private_class_method :neighbor, :walk
|
|
63
|
+
end
|
|
64
|
+
end
|