Class: BettyNiño
- Inherits:
-
Object
- Object
- BettyNiño
- Defined in:
- lib/pieces.rb
Overview
A tetrimino piece
Direct Known Subclasses
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#orientation ⇒ Object
Returns the value of attribute orientation.
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
Instance Method Summary collapse
-
#display(y, x) ⇒ Object
Draw piece in a preview box (hold/next).
-
#draw(oy, ox, win) ⇒ Object
Draw piece at its current position on the game board.
-
#ghost ⇒ Object
Find ghost (landing preview) position.
-
#initialize(board, shape) ⇒ BettyNiño
constructor
A new instance of BettyNiño.
-
#move!(delta = -1)) ⇒ Object
Move horizontally.
-
#niños(orientation = @orientation) ⇒ Object
Get cell positions for given orientation.
-
#rotate!(clockwise: true) ⇒ Object
Rotate using Super Rotation System.
-
#slide! ⇒ Object
Move down one row.
-
#valid? ⇒ Boolean
Check if current position is valid.
Constructor Details
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
95 96 97 |
# File 'lib/pieces.rb', line 95 def center @center end |
#orientation ⇒ Object
Returns the value of attribute orientation.
95 96 97 |
# File 'lib/pieces.rb', line 95 def orientation @orientation end |
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
94 95 96 |
# File 'lib/pieces.rb', line 94 def shape @shape end |
Instance Method Details
#display(y, x) ⇒ Object
Draw piece in a preview box (hold/next)
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/pieces.rb', line 171 def display(y, x) color = COLOR_PAIR[@shape] win = Curses.stdscr win.attron(Curses.color_pair(color)) BLUEPRINT[@shape][Face.↑].each do |dy, dx| win.setpos(y + 1 + dy, x + dx * 2) win.addstr("██") end win.attroff(Curses.color_pair(color)) end |
#draw(oy, ox, win) ⇒ Object
Draw piece at its current position on the game board
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pieces.rb', line 157 def draw(oy, ox, win) color = COLOR_PAIR[@shape] niños.each do |niño| next if niño.y <= SKYLINE_INDEX win.setpos(oy + 1 + niño.y - SKYLINE_INDEX, ox + 19 + niño.x * 2) win.attron(Curses.color_pair(color)) win.addstr("██") win.attroff(Curses.color_pair(color)) end end |
#ghost ⇒ Object
Find ghost (landing preview) position
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/pieces.rb', line 145 def ghost original = @center @center = Point[@center.y, @center.x] @center = @center + Point[1, 0] while can_drop? niños ensure @center = original end |
#move!(delta = -1)) ⇒ Object
Move horizontally. Returns truthy if successful.
105 106 107 108 |
# File 'lib/pieces.rb', line 105 def move!(delta = -1) niños.all? { |niño| @board.valid?(niño + Point[0, delta]) } .tif { @center = @center + Point[0, delta] } end |
#niños(orientation = @orientation) ⇒ Object
Get cell positions for given orientation
140 141 142 |
# File 'lib/pieces.rb', line 140 def niños(orientation = @orientation) BLUEPRINT[@shape][orientation].map { |dy, dx| @center + Point[dy, dx] } end |
#rotate!(clockwise: true) ⇒ Object
Rotate using Super Rotation System
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/pieces.rb', line 122 def rotate!(clockwise: true) current = @orientation target = (current + (clockwise ? 1 : -1)) % 4 offsets = OFFSET[@shape == :I ? :I : :JLSTZ] target_niños = niños(target) offsets[[current, target]].any? do |offset| offset_point = offset.to_p target_niños.all? { |niño| @board.valid?(niño + offset_point) } .tif do @center = @center + offset_point @orientation = target end end end |
#slide! ⇒ Object
Move down one row. Returns truthy if successful.
111 112 113 114 |
# File 'lib/pieces.rb', line 111 def niños.all? { |niño| @board.valid?(niño + Point[1, 0]) } .tif { @center = @center + Point[1, 0] } end |
#valid? ⇒ Boolean
Check if current position is valid
117 118 119 |
# File 'lib/pieces.rb', line 117 def valid? niños.all? { |niño| @board.valid?(niño) } end |