Class: BettyNiño

Inherits:
Object
  • Object
show all
Defined in:
lib/pieces.rb

Overview

A tetrimino piece

Direct Known Subclasses

BettyNiñoO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, shape) ⇒ BettyNiño

Returns a new instance of BettyNiño.



97
98
99
100
101
102
# File 'lib/pieces.rb', line 97

def initialize(board, shape)
  @board = board
  @shape = shape
  @orientation = Face.
  @center = Point[20, 4]
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



95
96
97
# File 'lib/pieces.rb', line 95

def center
  @center
end

#orientationObject

Returns the value of attribute orientation.



95
96
97
# File 'lib/pieces.rb', line 95

def orientation
  @orientation
end

#shapeObject (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

#ghostObject

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 slide!
  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

Returns:

  • (Boolean)


117
118
119
# File 'lib/pieces.rb', line 117

def valid?
  niños.all? { |niño| @board.valid?(niño) }
end