Class: Pacman::Sprites
- Inherits:
-
Object
- Object
- Pacman::Sprites
- Defined in:
- lib/pacman/sprites.rb
Overview
Procedurally drawn actor sprites. A board cell is scale character rows by
scale * 2 columns; half-block characters double the vertical resolution,
giving a square 2k x 2k pixel grid to draw circles and skirts on.
Constant Summary collapse
- MOUTH_COSINE =
0.75
Class Method Summary collapse
- .cherry(scale:) ⇒ Object
- .eaten(scale:) ⇒ Object
- .frightened(scale:) ⇒ Object
- .ghost(scale:) ⇒ Object
- .player(scale:, direction:) ⇒ Object
Instance Method Summary collapse
- #cherry ⇒ Object
- #draw(texture: nil) ⇒ Object
- #eye?(y, x) ⇒ Boolean
- #ghost_body?(y, x) ⇒ Boolean
-
#initialize(scale) ⇒ Sprites
constructor
A new instance of Sprites.
- #pacman?(y, x, direction) ⇒ Boolean
Constructor Details
#initialize(scale) ⇒ Sprites
Returns a new instance of Sprites.
44 45 46 47 48 49 |
# File 'lib/pacman/sprites.rb', line 44 def initialize(scale) @scale = scale @pixels = scale * 2 @center = (@pixels - 1) / 2.0 @radius = scale - 0.15 end |
Class Method Details
.cherry(scale:) ⇒ Object
31 32 33 34 35 |
# File 'lib/pacman/sprites.rb', line 31 def self.cherry(scale:) return ["● "] if scale == 1 new(scale).cherry end |
.eaten(scale:) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/pacman/sprites.rb', line 37 def self.eaten(scale:) return ["▀▀"] if scale == 1 sprite = new(scale) sprite.draw { |y, x| sprite.eye?(y, x) } end |
.frightened(scale:) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/pacman/sprites.rb', line 24 def self.frightened(scale:) return ["▒▒"] if scale == 1 sprite = new(scale) sprite.draw(texture: "▒") { |y, x| sprite.ghost_body?(y, x) && !sprite.eye?(y, x) } end |
.ghost(scale:) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/pacman/sprites.rb', line 17 def self.ghost(scale:) return ["██"] if scale == 1 sprite = new(scale) sprite.draw { |y, x| sprite.ghost_body?(y, x) && !sprite.eye?(y, x) } end |
.player(scale:, direction:) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/pacman/sprites.rb', line 10 def self.player(scale:, direction:) return ["██"] if scale == 1 sprite = new(scale) sprite.draw { |y, x| sprite.pacman?(y, x, direction) } end |
Instance Method Details
#cherry ⇒ Object
59 60 61 62 |
# File 'lib/pacman/sprites.rb', line 59 def cherry body = draw { |y, x| berry?(y, x) } add_stems(body) end |
#draw(texture: nil) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/pacman/sprites.rb', line 51 def draw(texture: nil) (0...scale).map do |row| (0...pixels).map do |col| cell_char(yield(row * 2, col), yield(row * 2 + 1, col), texture) end.join end end |
#eye?(y, x) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pacman/sprites.rb', line 75 def eye?(y, x) return false if scale < 2 size = [scale / 2, 1].max row = (pixels * 0.35).floor return false unless (row...(row + size)).cover?(y) left = (pixels * 0.3).floor right = (pixels * 0.65).floor (left...(left + size)).cover?(x) || (right...(right + size)).cover?(x) end |
#ghost_body?(y, x) ⇒ Boolean
68 69 70 71 72 73 |
# File 'lib/pacman/sprites.rb', line 68 def ghost_body?(y, x) return skirt?(x) && (x - center).abs <= radius if y == pixels - 1 return circle?(y, x) if y < scale (x - center).abs <= radius end |
#pacman?(y, x, direction) ⇒ Boolean
64 65 66 |
# File 'lib/pacman/sprites.rb', line 64 def pacman?(y, x, direction) circle?(y, x) && !mouth?(y, x, direction) end |