Class: Text2048::CursesView::Tile

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/text2048/curses_view/tile.rb

Overview

Shows tiles in curses. rubocop:disable ClassLength

Constant Summary collapse

DEFAULT_HEIGHT =
7
DEFAULT_WIDTH =
17

Constants included from Colorize

Colorize::COLORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#colorize

Constructor Details

#initialize(tile, row, col, color, scale = 1) ⇒ Tile

Returns a new instance of Tile.



30
31
32
33
34
35
36
37
38
39
# File 'lib/text2048/curses_view/tile.rb', line 30

def initialize(tile, row, col, color, scale = 1)
  klass = self.class
  @value = tile.value
  @height = klass.height(scale)
  @width = klass.width(scale)
  @row = (@height + 1) * row + 2
  @col = (@width + 1) * col + 1
  @color = color
  @scale = scale
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



15
16
17
# File 'lib/text2048/curses_view/tile.rb', line 15

def color
  @color
end

#heightObject (readonly)

Returns the value of attribute height.



14
15
16
# File 'lib/text2048/curses_view/tile.rb', line 14

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/text2048/curses_view/tile.rb', line 13

def width
  @width
end

Class Method Details

.height(scale) ⇒ Object



26
27
28
# File 'lib/text2048/curses_view/tile.rb', line 26

def self.height(scale)
  (DEFAULT_HEIGHT * scale).to_i
end

.width(scale) ⇒ Object



22
23
24
# File 'lib/text2048/curses_view/tile.rb', line 22

def self.width(scale)
  @width = (DEFAULT_WIDTH * scale).to_i
end

Instance Method Details

#draw_boxObject



54
55
56
57
58
59
60
61
# File 'lib/text2048/curses_view/tile.rb', line 54

def draw_box
  draw_square
  [box_upper_left, box_upper_right,
   box_lower_left, box_lower_right].each do |row, col|
    setpos(row, col)
    addstr('+')
  end
end

#draw_numberObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/text2048/curses_view/tile.rb', line 72

def draw_number
  return unless @value
  if @scale >= 1
    draw_lcd_number
  else
    setpos(@row + @height / 2, @col)
    colorize(@color) do
      addstr @value.to_s.center(@width)
    end
  end
end

#fill_blackObject



67
68
69
70
# File 'lib/text2048/curses_view/tile.rb', line 67

def fill_black
  colorize(COLOR_BLACK + 100) { fill }
  refresh
end

#fill_tile_colorObject



63
64
65
# File 'lib/text2048/curses_view/tile.rb', line 63

def fill_tile_color
  colorize(@color + 100) { fill }
end

#popObject



48
49
50
51
52
# File 'lib/text2048/curses_view/tile.rb', line 48

def pop
  colorize(@color + 100) do
    draw_box
  end
end

#showObject



41
42
43
44
45
46
# File 'lib/text2048/curses_view/tile.rb', line 41

def show
  draw_box
  fill_tile_color
  draw_number
  self
end