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,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# The framed box behind menus and messages: an ink outline with clipped
|
|
5
|
+
# corners, a bevel lit from the top left, a dark fill, and a brass stud
|
|
6
|
+
# in each corner catching the same light. It is placed in points and its
|
|
7
|
+
# edges drawn in pixels, a pixel each at either density.
|
|
8
|
+
module Panel
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def draw(canvas, x, y, width, height)
|
|
12
|
+
density = canvas.density
|
|
13
|
+
left = density.at(x)
|
|
14
|
+
top = density.at(y)
|
|
15
|
+
width = density.at(x + width) - left
|
|
16
|
+
height = density.at(y + height) - top
|
|
17
|
+
paint(canvas.pixels, left, top, width, height, density.pick(STUD, STUD.fine))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def paint(canvas, x, y, width, height, stud)
|
|
21
|
+
ink = Palette[:ink]
|
|
22
|
+
canvas.fill(x + 1, y, width - 2, height, ink)
|
|
23
|
+
canvas.fill(x, y + 1, width, height - 2, ink)
|
|
24
|
+
canvas.fill(x + 1, y + 1, width - 2, height - 2, Palette[:violet])
|
|
25
|
+
canvas.fill(x + 2, y + 2, width - 3, height - 3, Palette[:night])
|
|
26
|
+
canvas.fill(x + 2, y + 2, width - 4, height - 4, Palette[:indigo])
|
|
27
|
+
far_x = x + width - 2 - stud.width
|
|
28
|
+
far_y = y + height - 2 - stud.height
|
|
29
|
+
[[x + 2, y + 2], [far_x, y + 2], [x + 2, far_y], [far_x, far_y]].each do |stud_x, stud_y|
|
|
30
|
+
canvas.draw(stud, stud_x, stud_y)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
STUD = Sprite.parse(<<~ART, { "g" => :gold, "a" => :amber, "o" => :orange, "w" => :wine }, fine: <<~FINE)
|
|
35
|
+
ga
|
|
36
|
+
ao
|
|
37
|
+
ART
|
|
38
|
+
gga
|
|
39
|
+
gao
|
|
40
|
+
aow
|
|
41
|
+
FINE
|
|
42
|
+
|
|
43
|
+
private_class_method :paint
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# Bits flung out by events: juice when a fruit bursts, scales when the
|
|
5
|
+
# snake comes apart, dust where a fruit lands, confetti for a new best.
|
|
6
|
+
# A bit steps down its ramp of colors as it ages and is gone at the end
|
|
7
|
+
# of it; confetti keeps its one color and flutters as it falls.
|
|
8
|
+
#
|
|
9
|
+
# The fine canvas has room to show a bit in flight. It is drawn where it
|
|
10
|
+
# is on the finer grid, and while it is young and fast a short tail runs
|
|
11
|
+
# back the way it came: a pixel of its own color, then one a step down
|
|
12
|
+
# its ramp. A burst thrown to sparkle opens there with a star in its own
|
|
13
|
+
# light, which its bits fly out from under, and every third bit catches
|
|
14
|
+
# the light once while it is still bright, one after another: a little
|
|
15
|
+
# cross with a white middle.
|
|
16
|
+
class Particles
|
|
17
|
+
# A bit's glint is the share of its life at which it catches the light,
|
|
18
|
+
# if it does.
|
|
19
|
+
Bit = Struct.new(:x, :y, :vx, :vy, :gravity, :drag, :age, :life, :colors, :flutter, :glint, keyword_init: true)
|
|
20
|
+
# Where a sparkling burst began, how long ago, and the map that puts
|
|
21
|
+
# the star in its light.
|
|
22
|
+
Star = Struct.new(:x, :y, :age, :light)
|
|
23
|
+
|
|
24
|
+
# How much of its way a bit trails, in seconds, how many pixels at
|
|
25
|
+
# most, and for how much of its life.
|
|
26
|
+
TAIL = 0.032
|
|
27
|
+
LONGEST_TAIL = 2
|
|
28
|
+
TAILED = 0.65
|
|
29
|
+
# Each of a star's frames lasts this long. Meanwhile bits this near
|
|
30
|
+
# its middle, in points, stay under it.
|
|
31
|
+
STAR_FRAME = 1.0 / 30
|
|
32
|
+
REACH = 6
|
|
33
|
+
# The shares of their lives at which the bits of a sparkling burst
|
|
34
|
+
# catch the light, in turn, and for how long each.
|
|
35
|
+
GLINTS = [0.18, 0.25, 0.32, 0.39].freeze
|
|
36
|
+
GLINT = 0.06
|
|
37
|
+
|
|
38
|
+
def initialize(rng)
|
|
39
|
+
@rng = rng
|
|
40
|
+
@bits = []
|
|
41
|
+
@stars = []
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Bits thrown out in every direction from a point, a little upward.
|
|
45
|
+
def burst(x, y, colors, count:, speed:, gravity: 160.0, life: 0.45..0.8, sparkle: false)
|
|
46
|
+
@stars << Star.new(x, y, 0.0, light(colors)) if sparkle
|
|
47
|
+
count.times do |index|
|
|
48
|
+
angle = @rng.rand * Math::PI * 2
|
|
49
|
+
pace = speed * @rng.rand(0.45..1.0)
|
|
50
|
+
glint = GLINTS[index / 3 % GLINTS.size] if sparkle && (index % 3).zero?
|
|
51
|
+
@bits << Bit.new(x:, y:, vx: Math.cos(angle) * pace, vy: Math.sin(angle) * pace - speed * 0.4, gravity:, drag: 0.0,
|
|
52
|
+
age: 0.0, life: @rng.rand(life), colors:, flutter: 0.0, glint:)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Paper in the palette's brightest colors, tossed up from below the
|
|
57
|
+
# top edge so it rains down across the width.
|
|
58
|
+
def confetti(count, left:, width:, top:)
|
|
59
|
+
count.times do
|
|
60
|
+
color = %i[gold amber cream lime leaf red orange].sample(random: @rng)
|
|
61
|
+
@bits << Bit.new(x: left + @rng.rand(width), y: top - @rng.rand(30), vx: @rng.rand(-20.0..20.0),
|
|
62
|
+
vy: @rng.rand(-40.0..10.0), gravity: 90.0, drag: 1.5, age: 0.0, life: @rng.rand(2.8..3.8),
|
|
63
|
+
colors: [color], flutter: @rng.rand(2.0..4.0))
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def update(dt)
|
|
68
|
+
@bits.each do |bit|
|
|
69
|
+
bit.age += dt
|
|
70
|
+
bit.vy += bit.gravity * dt
|
|
71
|
+
bit.vx -= bit.vx * bit.drag * dt
|
|
72
|
+
bit.vy -= bit.vy * bit.drag * dt
|
|
73
|
+
bit.x += bit.vx * dt + Math.sin(bit.age * bit.flutter * 3) * bit.flutter * dt * 6
|
|
74
|
+
bit.y += bit.vy * dt
|
|
75
|
+
end
|
|
76
|
+
@bits.reject! { |bit| bit.age >= bit.life }
|
|
77
|
+
@stars.each { |star| star.age += dt }
|
|
78
|
+
@stars.reject! { |star| frame(star) >= STAR.size }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def draw(canvas)
|
|
82
|
+
return draw_in_flight(canvas.pixels, canvas.density.scale) if canvas.density.fine?
|
|
83
|
+
|
|
84
|
+
@bits.each do |bit|
|
|
85
|
+
color = Palette[bit.colors[step(bit)]]
|
|
86
|
+
if bit.flutter.positive?
|
|
87
|
+
flat = flat?(bit)
|
|
88
|
+
canvas.fill(bit.x.round, bit.y.round, flat ? 2 : 1, flat ? 1 : 2, color)
|
|
89
|
+
else
|
|
90
|
+
canvas.plot(bit.x.round, bit.y.round, color)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
# How far down its ramp a bit has come.
|
|
98
|
+
def step(bit) = (bit.age / bit.life * bit.colors.size).floor
|
|
99
|
+
|
|
100
|
+
# Which of its frames a star has opened to.
|
|
101
|
+
def frame(star) = (star.age / STAR_FRAME).floor
|
|
102
|
+
|
|
103
|
+
# Paper shows its face or its edge as it turns.
|
|
104
|
+
def flat?(bit) = Math.sin(bit.age * bit.flutter * 3).positive?
|
|
105
|
+
|
|
106
|
+
# The star is drawn in cream and gold: this puts it in the first two
|
|
107
|
+
# colors of a burst's ramp, after white.
|
|
108
|
+
def light(colors)
|
|
109
|
+
arms, tips = (colors - [:white]).first(2)
|
|
110
|
+
lit = { Palette[:cream] => Palette[arms], Palette[:gold] => Palette[tips] }
|
|
111
|
+
Palette.map { |index| lit.fetch(index, index) }
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The bits in the fine canvas's pixels, and the stars over them.
|
|
115
|
+
def draw_in_flight(pixels, scale)
|
|
116
|
+
@bits.each do |bit|
|
|
117
|
+
next if under_a_star?(bit)
|
|
118
|
+
|
|
119
|
+
x = (bit.x * scale).round
|
|
120
|
+
y = (bit.y * scale).round
|
|
121
|
+
color = Palette[bit.colors[step(bit)]]
|
|
122
|
+
if bit.flutter.positive?
|
|
123
|
+
flat = flat?(bit)
|
|
124
|
+
pixels.fill(x, y, flat ? 3 : 1, flat ? 1 : 3, color)
|
|
125
|
+
else
|
|
126
|
+
draw_tail(pixels, bit, x, y, scale, color) if bit.age < bit.life * TAILED
|
|
127
|
+
pixels.plot(x, y, color)
|
|
128
|
+
draw_glint(pixels, x, y, color) if glinting?(bit)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
@stars.each do |star|
|
|
132
|
+
art = STAR[frame(star)]
|
|
133
|
+
pixels.draw(art, (star.x * scale).round - art.width / 2, (star.y * scale).round - art.height / 2, star.light)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def under_a_star?(bit)
|
|
138
|
+
@stars.any? { |star| (bit.x - star.x).abs + (bit.y - star.y).abs < REACH }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# No further back than the bit has come.
|
|
142
|
+
def draw_tail(pixels, bit, x, y, scale, color)
|
|
143
|
+
trailed = [TAIL, bit.age].min
|
|
144
|
+
back_x = -bit.vx * scale * trailed
|
|
145
|
+
back_y = -bit.vy * scale * trailed
|
|
146
|
+
reach = Math.hypot(back_x, back_y)
|
|
147
|
+
length = [reach, LONGEST_TAIL].min.round
|
|
148
|
+
return if length.zero?
|
|
149
|
+
|
|
150
|
+
after = Palette[bit.colors[[step(bit) + 1, bit.colors.size - 1].min]]
|
|
151
|
+
pixels.plot(x + (back_x / reach * 2).round, y + (back_y / reach * 2).round, after) if length > 1
|
|
152
|
+
pixels.plot(x + (back_x / reach).round, y + (back_y / reach).round, color)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def glinting?(bit) = bit.glint && (bit.age - bit.glint * bit.life).between?(0, GLINT)
|
|
156
|
+
|
|
157
|
+
def draw_glint(pixels, x, y, color)
|
|
158
|
+
pixels.fill(x - 1, y, 3, 1, color)
|
|
159
|
+
pixels.fill(x, y - 1, 1, 3, color)
|
|
160
|
+
pixels.plot(x, y, Palette[:white])
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
LIGHT = { "W" => :white, "c" => :cream, "g" => :gold }.freeze
|
|
164
|
+
|
|
165
|
+
# A star opening: a bud of light, then the star. Fine art only, there
|
|
166
|
+
# being no star on the standard canvas.
|
|
167
|
+
STAR = [
|
|
168
|
+
Sprite.parse(<<~ART, LIGHT),
|
|
169
|
+
..c..
|
|
170
|
+
.cWc.
|
|
171
|
+
cWWWc
|
|
172
|
+
.cWc.
|
|
173
|
+
..c..
|
|
174
|
+
ART
|
|
175
|
+
Sprite.parse(<<~ART, LIGHT)
|
|
176
|
+
....g....
|
|
177
|
+
....c....
|
|
178
|
+
....c....
|
|
179
|
+
...cWc...
|
|
180
|
+
gccWWWccg
|
|
181
|
+
...cWc...
|
|
182
|
+
....c....
|
|
183
|
+
....c....
|
|
184
|
+
....g....
|
|
185
|
+
ART
|
|
186
|
+
].freeze
|
|
187
|
+
end
|
|
188
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# The part of the body inside one tile, between the side toward the head
|
|
5
|
+
# and the side toward the tail. A straight piece runs edge to edge; a
|
|
6
|
+
# corner bends around the tile corner between its two sides, so pieces of
|
|
7
|
+
# either kind meet their neighbors with the same cross-section.
|
|
8
|
+
#
|
|
9
|
+
# For each pixel a piece knows how far along the body it lies, measured
|
|
10
|
+
# from the head side (0 to the tile's size), how far across it lies from
|
|
11
|
+
# the center line, and which way the surface there faces. That is enough
|
|
12
|
+
# to draw the body at any width under one light, in a tile of any size.
|
|
13
|
+
class Piece
|
|
14
|
+
Spot = Data.define(:x, :y, :along, :across, :facing_x, :facing_y)
|
|
15
|
+
|
|
16
|
+
def self.between(front, back, size = Courtyard::TILE)
|
|
17
|
+
@pieces ||= {}
|
|
18
|
+
@pieces[[front, back, size]] ||= new(front, back, size)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :front, :back, :spots, :size
|
|
22
|
+
|
|
23
|
+
def initialize(front, back, size)
|
|
24
|
+
@front = front
|
|
25
|
+
@back = back
|
|
26
|
+
@size = size
|
|
27
|
+
@spots = []
|
|
28
|
+
size.times do |y|
|
|
29
|
+
size.times do |x|
|
|
30
|
+
spot = locate(x, y)
|
|
31
|
+
@spots << spot if spot.across.abs < size / 2.0
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
@spots.freeze
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def straight? = front.opposite?(back)
|
|
38
|
+
|
|
39
|
+
# Whether the body goes on past this edge of the tile into the next.
|
|
40
|
+
def open_toward?(direction) = direction == front || direction == back
|
|
41
|
+
|
|
42
|
+
# Whether a body of the given half width covers a pixel of the tile.
|
|
43
|
+
def covers?(x, y, half_width) = @spots.any? { |spot| spot.x == x && spot.y == y && spot.across.abs < half_width }
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def locate(x, y)
|
|
48
|
+
center_x = x + 0.5
|
|
49
|
+
center_y = y + 0.5
|
|
50
|
+
straight? ? along_line(x, y, center_x, center_y) : around_corner(x, y, center_x, center_y)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def along_line(x, y, center_x, center_y)
|
|
54
|
+
half = size / 2.0
|
|
55
|
+
if front.dy.zero?
|
|
56
|
+
across = center_y - half
|
|
57
|
+
along = front.dx.positive? ? size - center_x : center_x
|
|
58
|
+
Spot.new(x, y, along, across, 0, across <=> 0)
|
|
59
|
+
else
|
|
60
|
+
across = center_x - half
|
|
61
|
+
along = front.dy.positive? ? size - center_y : center_y
|
|
62
|
+
Spot.new(x, y, along, across, across <=> 0, 0)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# A corner bends around the corner of the tile its two sides share, with
|
|
67
|
+
# the center line a quarter circle half a tile out.
|
|
68
|
+
def around_corner(x, y, center_x, center_y)
|
|
69
|
+
pivot_x = front.dx.positive? || back.dx.positive? ? size : 0
|
|
70
|
+
pivot_y = front.dy.positive? || back.dy.positive? ? size : 0
|
|
71
|
+
offset_x = center_x - pivot_x
|
|
72
|
+
offset_y = center_y - pivot_y
|
|
73
|
+
distance = Math.hypot(offset_x, offset_y)
|
|
74
|
+
# Seen from the pivot, the front edge lies the opposite way from the
|
|
75
|
+
# back one; the angle from it says how far round the bend a pixel is.
|
|
76
|
+
angle = Math.acos(((-offset_x * back.dx - offset_y * back.dy) / distance).clamp(-1.0, 1.0))
|
|
77
|
+
across = distance - size / 2.0
|
|
78
|
+
side = across <=> 0
|
|
79
|
+
Spot.new(x, y, angle / (Math::PI / 2) * size, across, offset_x / distance * side, offset_y / distance * side)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# Steers a game as it was steered before: each turn of a recording, on
|
|
5
|
+
# the move it was made, the way the autopilot steers a game of its own.
|
|
6
|
+
class Playback
|
|
7
|
+
def initialize(game, turns)
|
|
8
|
+
@game = game
|
|
9
|
+
@turns = turns
|
|
10
|
+
@next = 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Called before each tick.
|
|
14
|
+
def steer
|
|
15
|
+
move, way = @turns[@next]
|
|
16
|
+
return unless move == @game.moves
|
|
17
|
+
|
|
18
|
+
@game.steer(way)
|
|
19
|
+
@next += 1
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# The player's choices from the settings: the snake's skin, the speed,
|
|
5
|
+
# whether a crash shakes the screen, how loud the sounds and the music
|
|
6
|
+
# are, the song the music plays, and the name games are signed with.
|
|
7
|
+
# Kept in a file and saved whenever one of them changes.
|
|
8
|
+
#
|
|
9
|
+
# The sounds and the music can be switched off and back on, and come
|
|
10
|
+
# back as loud as they were last heard. Only the volumes themselves are
|
|
11
|
+
# kept, so after a start with either off, it comes back at medium.
|
|
12
|
+
class Preferences
|
|
13
|
+
attr_reader :skin, :speed, :volume, :music, :song, :name
|
|
14
|
+
|
|
15
|
+
def self.default_path(environment = ENV)
|
|
16
|
+
config = environment["XDG_CONFIG_HOME"].to_s
|
|
17
|
+
config = File.join(Dir.home, ".config") if config.empty?
|
|
18
|
+
File.join(config, "pixelsnake", "settings.json")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Whoever is logged in, as the game can write it.
|
|
22
|
+
def self.default_name(environment = ENV)
|
|
23
|
+
name = Name.clean(environment["USER"] || environment["LOGNAME"])
|
|
24
|
+
name.empty? ? "PLAYER" : name
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Anything missing from the file, or not understood, is the default.
|
|
28
|
+
def initialize(path, name: "PLAYER")
|
|
29
|
+
@file = SaveFile.new(path, Hash)
|
|
30
|
+
saved = @file.read
|
|
31
|
+
@skin = Skin.named(saved[:skin])
|
|
32
|
+
@speed = Game.speed_named(saved[:speed])
|
|
33
|
+
@shake = saved[:shake] != false
|
|
34
|
+
@volume = Speaker.volume_named(saved[:volume])
|
|
35
|
+
@music = Speaker.volume_named(saved[:music])
|
|
36
|
+
@heard_volume = @volume == :off ? :medium : @volume
|
|
37
|
+
@heard_music = @music == :off ? :medium : @music
|
|
38
|
+
@song = Song.named(saved[:song])
|
|
39
|
+
@name = Name.clean(saved[:name])
|
|
40
|
+
@name = name if @name.empty?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def shake? = @shake
|
|
44
|
+
|
|
45
|
+
def skin=(skin)
|
|
46
|
+
@skin = skin
|
|
47
|
+
save
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def speed=(speed)
|
|
51
|
+
@speed = speed
|
|
52
|
+
save
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def shake=(shake)
|
|
56
|
+
@shake = shake
|
|
57
|
+
save
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def volume=(volume)
|
|
61
|
+
@volume = volume
|
|
62
|
+
@heard_volume = volume unless volume == :off
|
|
63
|
+
save
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def music=(music)
|
|
67
|
+
@music = music
|
|
68
|
+
@heard_music = music unless music == :off
|
|
69
|
+
save
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def song=(song)
|
|
73
|
+
@song = song
|
|
74
|
+
save
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def switch_sounds
|
|
78
|
+
self.volume = volume == :off ? @heard_volume : :off
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def switch_music
|
|
82
|
+
self.music = music == :off ? @heard_music : :off
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def name=(name)
|
|
86
|
+
@name = name
|
|
87
|
+
save
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def save
|
|
93
|
+
@file.write(skin: skin.name, speed:, shake: @shake, volume:, music:, song: song.name, name:)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PixelSnake
|
|
4
|
+
# A rare fruit on the stones: it twinkles into being with a flash of its
|
|
5
|
+
# own light and a chime of its own, sits in a ring of sparkles that go
|
|
6
|
+
# out one by one as its time runs down, and if nobody comes for it, it
|
|
7
|
+
# fades away in a little shimmer, chiming down. Each kind has its look:
|
|
8
|
+
# the golden fruit in gold, the plum in its bloom, the ember pepper in
|
|
9
|
+
# sparks that flicker like a fire, and the silver pear in a slow, cool
|
|
10
|
+
# glitter.
|
|
11
|
+
class RareFruit
|
|
12
|
+
APPEAR = 0.4
|
|
13
|
+
FADE = 0.45
|
|
14
|
+
GLINT_EVERY = 1.3
|
|
15
|
+
|
|
16
|
+
# How a kind looks: the fruit, its ring's sparkles bright and dim, how
|
|
17
|
+
# fast they twinkle, and the shimmer it fades in.
|
|
18
|
+
Look = Data.define(:sprite, :bright, :dim, :flicker, :shimmer)
|
|
19
|
+
|
|
20
|
+
# The ring around the tile, clockwise from the top; the first ones go
|
|
21
|
+
# out first.
|
|
22
|
+
RING = [[4, -3], [9, -1], [11, 4], [9, 9], [3, 11], [-2, 9], [-4, 3], [-2, -2]].freeze
|
|
23
|
+
|
|
24
|
+
def initialize(courtyard, particles, speaker)
|
|
25
|
+
@courtyard = courtyard
|
|
26
|
+
@particles = particles
|
|
27
|
+
@speaker = speaker
|
|
28
|
+
@time = 0.0
|
|
29
|
+
@rare = nil
|
|
30
|
+
@left = 0.0
|
|
31
|
+
@appeared = -APPEAR
|
|
32
|
+
@faded = nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def update(dt)
|
|
36
|
+
@time += dt
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Keeps up with the game after each tick: a new rare fruit appears, and
|
|
40
|
+
# one that is gone without being eaten fades, even as another comes out
|
|
41
|
+
# on the same move. Left is the share of its time still to run.
|
|
42
|
+
def follow(rare, left)
|
|
43
|
+
return @left = left if rare.equal?(@rare)
|
|
44
|
+
|
|
45
|
+
fade(@rare) if @rare
|
|
46
|
+
appear(rare) if rare
|
|
47
|
+
@rare = rare
|
|
48
|
+
@left = left
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Taken by the snake: no fading, the scene bursts it.
|
|
52
|
+
def eaten
|
|
53
|
+
@rare = nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def draw(canvas)
|
|
57
|
+
draw_fading(canvas) if @faded && @time - @faded.last < FADE
|
|
58
|
+
return unless @rare
|
|
59
|
+
|
|
60
|
+
look = LOOKS.fetch(@rare.kind)
|
|
61
|
+
x, y = @courtyard.corner(@rare.tile.column, @rare.tile.row)
|
|
62
|
+
draw_ring(canvas, x, y, look)
|
|
63
|
+
canvas.draw(Fruit::SHADOW, x + 2, y + 6)
|
|
64
|
+
growing = Ease.progress(@time, @appeared + 0.08, APPEAR - 0.08)
|
|
65
|
+
if growing < 1
|
|
66
|
+
draw_arrival(canvas, x, y, growing, look)
|
|
67
|
+
else
|
|
68
|
+
canvas.draw(look.sprite, x, y)
|
|
69
|
+
canvas.draw(Fruit::SPARK, x + 1, y + 2) if (@time - @appeared) % GLINT_EVERY < 0.12
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def appear(rare)
|
|
76
|
+
@appeared = @time
|
|
77
|
+
@speaker.play(:twinkle, Game::RARE.index(rare.kind), at: @courtyard.corner(rare.tile.column, rare.tile.row).first + 4)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def fade(rare)
|
|
81
|
+
@faded = [rare, @time]
|
|
82
|
+
x, y = @courtyard.corner(rare.tile.column, rare.tile.row)
|
|
83
|
+
@particles.burst(x + 4, y + 3, LOOKS.fetch(rare.kind).shimmer, count: 6, speed: 18, gravity: -30.0, life: 0.4..0.7)
|
|
84
|
+
@speaker.play(:fizzle, at: x + 4)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# A star of the kind's light first, then the fruit springing out of it.
|
|
88
|
+
def draw_arrival(canvas, x, y, growing, look)
|
|
89
|
+
canvas.draw(STAR, x - 1, y, STARLIGHT.fetch(@rare.kind)) if @time - @appeared < 0.16
|
|
90
|
+
return if growing.zero?
|
|
91
|
+
|
|
92
|
+
sprite = look.sprite.scale(Ease.out_back(growing), Ease.out_back(growing))
|
|
93
|
+
canvas.draw(sprite, x + (look.sprite.width - sprite.width) / 2, y + (look.sprite.height - sprite.height) / 2)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Each sparkle twinkles on its own beat, now and then flaring into a
|
|
97
|
+
# little cross. As time runs out they go out, clockwise from the top.
|
|
98
|
+
def draw_ring(canvas, x, y, look)
|
|
99
|
+
lit = (@left * RING.size).ceil
|
|
100
|
+
RING.each_with_index do |(dx, dy), index|
|
|
101
|
+
next if index < RING.size - lit || @time - @appeared < 0.1 + index * 0.03
|
|
102
|
+
|
|
103
|
+
twinkle = (Math.sin(@time * look.flicker + index * 1.7) + 1) / 2
|
|
104
|
+
if twinkle > 0.9
|
|
105
|
+
canvas.draw(GLEAM, x + dx - 1, y + dy - 1, STARLIGHT.fetch(@rare.kind))
|
|
106
|
+
else
|
|
107
|
+
canvas.plot(x + dx, y + dy, Palette[twinkle > 0.45 ? look.bright : look.dim])
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def draw_fading(canvas)
|
|
113
|
+
rare, since = @faded
|
|
114
|
+
x, y = @courtyard.corner(rare.tile.column, rare.tile.row)
|
|
115
|
+
cover = ((1 - Ease.progress(@time, since, FADE)) * 16).round
|
|
116
|
+
canvas.draw_faded(LOOKS.fetch(rare.kind).sprite, x, y - (Ease.out_cubic(Ease.progress(@time, since, FADE)) * 3).round,
|
|
117
|
+
cover)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
LEGEND = {
|
|
121
|
+
"w" => :wine, "o" => :orange, "a" => :amber, "g" => :gold, "c" => :cream, "W" => :white,
|
|
122
|
+
"d" => :deep, "f" => :fern, "l" => :leaf, "j" => :jade, "L" => :lime,
|
|
123
|
+
"P" => :plum, "m" => :mauve, "p" => :orchid, "b" => :blush, "r" => :red, "R" => :crimson,
|
|
124
|
+
"n" => :sand, "h" => :ash, "t" => :steel
|
|
125
|
+
}.freeze
|
|
126
|
+
|
|
127
|
+
def self.art(grid, fine = nil) = Sprite.parse(grid, LEGEND, fine:)
|
|
128
|
+
|
|
129
|
+
GOLDEN = art(<<~ART, <<~FINE)
|
|
130
|
+
.....ll.
|
|
131
|
+
....dfl.
|
|
132
|
+
..wwdw..
|
|
133
|
+
.wWcggw.
|
|
134
|
+
.wcggaw.
|
|
135
|
+
.wggaaw.
|
|
136
|
+
.waaaow.
|
|
137
|
+
..woow..
|
|
138
|
+
ART
|
|
139
|
+
........ll..
|
|
140
|
+
.......lLlf.
|
|
141
|
+
......dllf..
|
|
142
|
+
...wwwdwww..
|
|
143
|
+
..wccgwgggw.
|
|
144
|
+
.wcWcggggaaw
|
|
145
|
+
.wccggggaaaw
|
|
146
|
+
.wgggggaaaow
|
|
147
|
+
.wggggaaaoow
|
|
148
|
+
.wggaaaaooow
|
|
149
|
+
..waaaoooow.
|
|
150
|
+
...wwwwwww..
|
|
151
|
+
FINE
|
|
152
|
+
|
|
153
|
+
# Round and dusky, with the bloom catching the light.
|
|
154
|
+
PLUM = art(<<~ART, <<~FINE)
|
|
155
|
+
......l.
|
|
156
|
+
.....dl.
|
|
157
|
+
...wwdw.
|
|
158
|
+
..wbpmmw
|
|
159
|
+
.wbpmmmw
|
|
160
|
+
.wpmmmPw
|
|
161
|
+
.wmmmPPw
|
|
162
|
+
..wPPww.
|
|
163
|
+
ART
|
|
164
|
+
.........l..
|
|
165
|
+
........ll..
|
|
166
|
+
.......dl...
|
|
167
|
+
....wwwdww..
|
|
168
|
+
...wbbppmmw.
|
|
169
|
+
..wbppmmmmmw
|
|
170
|
+
.wbpmmmmmmPw
|
|
171
|
+
.wpmmmmmmPPw
|
|
172
|
+
.wmmmmmmPPPw
|
|
173
|
+
.wmmmmmPPPw.
|
|
174
|
+
..wPmPPPPw..
|
|
175
|
+
...wwwwww...
|
|
176
|
+
FINE
|
|
177
|
+
|
|
178
|
+
# A chili, its cap still green, ripened to the color of embers.
|
|
179
|
+
PEPPER = art(<<~ART, <<~FINE)
|
|
180
|
+
.Ll.....
|
|
181
|
+
.ljdw...
|
|
182
|
+
..wgaw..
|
|
183
|
+
..wraow.
|
|
184
|
+
...wraow
|
|
185
|
+
...wrrRw
|
|
186
|
+
....wRw.
|
|
187
|
+
.....w..
|
|
188
|
+
ART
|
|
189
|
+
..Ll........
|
|
190
|
+
.Lljd.......
|
|
191
|
+
..ljjww.....
|
|
192
|
+
...wgaaw....
|
|
193
|
+
...wgaaow...
|
|
194
|
+
...wcgaoow..
|
|
195
|
+
....wcaooow.
|
|
196
|
+
.....waoorw.
|
|
197
|
+
.....wraorw.
|
|
198
|
+
......wrRw..
|
|
199
|
+
......wRw...
|
|
200
|
+
.......w....
|
|
201
|
+
FINE
|
|
202
|
+
|
|
203
|
+
# A pear gone to silver, the light on its shoulder.
|
|
204
|
+
PEAR = art(<<~ART, <<~FINE)
|
|
205
|
+
.....ll.
|
|
206
|
+
....dl..
|
|
207
|
+
...wdw..
|
|
208
|
+
..wcnw..
|
|
209
|
+
..wnhw..
|
|
210
|
+
.wcnnhw.
|
|
211
|
+
.wnnhtw.
|
|
212
|
+
..wwww..
|
|
213
|
+
ART
|
|
214
|
+
........l...
|
|
215
|
+
.......ll...
|
|
216
|
+
......dl....
|
|
217
|
+
.....wdw....
|
|
218
|
+
....wcnhw...
|
|
219
|
+
....wcnhw...
|
|
220
|
+
...wcnnnhw..
|
|
221
|
+
..wcnnnnhhw.
|
|
222
|
+
.wcWnnnnnhtw
|
|
223
|
+
.wcnnnnnhhtw
|
|
224
|
+
..wnnnnhhtw.
|
|
225
|
+
...wwwwwww..
|
|
226
|
+
FINE
|
|
227
|
+
|
|
228
|
+
STAR = art(<<~ART)
|
|
229
|
+
....c....
|
|
230
|
+
....c....
|
|
231
|
+
...cWc...
|
|
232
|
+
ccWWWWWcc
|
|
233
|
+
...cWc...
|
|
234
|
+
....c....
|
|
235
|
+
....c....
|
|
236
|
+
ART
|
|
237
|
+
|
|
238
|
+
GLEAM = art(<<~ART, <<~FINE)
|
|
239
|
+
.c.
|
|
240
|
+
cWc
|
|
241
|
+
.c.
|
|
242
|
+
ART
|
|
243
|
+
..c..
|
|
244
|
+
..c..
|
|
245
|
+
ccWcc
|
|
246
|
+
..c..
|
|
247
|
+
..c..
|
|
248
|
+
FINE
|
|
249
|
+
|
|
250
|
+
LOOKS = {
|
|
251
|
+
golden: Look.new(GOLDEN, :gold, :amber, 4.2, %i[white cream gold amber]),
|
|
252
|
+
plum: Look.new(PLUM, :blush, :orchid, 3.0, %i[white blush orchid mauve]),
|
|
253
|
+
pepper: Look.new(PEPPER, :amber, :orange, 9.0, %i[gold amber orange red]),
|
|
254
|
+
pear: Look.new(PEAR, :white, :sand, 2.2, %i[white cream sand ash])
|
|
255
|
+
}.freeze
|
|
256
|
+
|
|
257
|
+
# The star and the flares in each kind's own light: cream and white for
|
|
258
|
+
# the gold, blush for the plum, amber for the pepper, white for the pear.
|
|
259
|
+
STARLIGHT = {
|
|
260
|
+
golden: Palette::IDENTITY,
|
|
261
|
+
plum: Palette.map { |index| { Palette[:cream] => Palette[:blush] }.fetch(index, index) },
|
|
262
|
+
pepper: Palette.map { |index| { Palette[:cream] => Palette[:amber], Palette[:white] => Palette[:gold] }.fetch(index, index) },
|
|
263
|
+
pear: Palette.map { |index| { Palette[:cream] => Palette[:sand] }.fetch(index, index) }
|
|
264
|
+
}.freeze
|
|
265
|
+
|
|
266
|
+
private_class_method :art
|
|
267
|
+
end
|
|
268
|
+
end
|