Module: Swarm::Console

Extended by:
Console
Includes:
Curses, Color
Included in:
Console
Defined in:
lib/swarm/console.rb

Defined Under Namespace

Modules: Color, NormalKey, VIKey Classes: PopUp

Constant Summary

Constants included from Color

Color::BLACK, Color::BLUE, Color::GREEN, Color::GREY, Color::PURPLE, Color::RED, Color::SILVER, Color::WHITE, Color::YELLOW

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#key_infoObject (readonly)

Returns the value of attribute key_info



62
63
64
# File 'lib/swarm/console.rb', line 62

def key_info
  @key_info
end

Instance Method Details

#closeObject

Turn line buffering on and close the Curses session.



141
142
143
144
145
146
# File 'lib/swarm/console.rb', line 141

def close
  return if closed?

  nocbreak
  close_screen
end

#draw(tile) ⇒ Object

Parameters:

  • color (Integer)

    0 to 8

  • x (Integer)
  • y (Integer)


129
130
131
132
133
# File 'lib/swarm/console.rb', line 129

def draw(tile)
  setpos *tile.location

  attron(color_pair tile.color) { addstr tile.icon }
end

#heightInteger

Returns height of console

Returns:

  • (Integer)

    height of console



115
116
117
# File 'lib/swarm/console.rb', line 115

def height
  @height ||= lines
end

#init_colorsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/swarm/console.rb', line 85

def init_colors
  start_color
  use_default_colors

            #id     #fg     #bg
  init_pair BLACK,  BLACK,  BLACK
  init_pair SILVER, SILVER, SILVER
  init_pair WHITE,  WHITE,  WHITE
  init_pair GREY,   GREY,   GREY
  init_pair RED,    RED,    BLACK
  init_pair PURPLE, PURPLE, BLACK
  init_pair GREEN,  GREEN,  BLACK
  init_pair BLUE,   BLUE,   BLACK
  init_pair YELLOW, YELLOW, BLACK
end

#init_keysObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/swarm/console.rb', line 66

def init_keys
  nonl
  noecho # don't echo keypresses
  stdscr.nodelay = -1 # don't wait for input, just listen for it
  stdscr.keypad true

  @key_info = {pause: '<SPACEBAR>', quit: ?q}

  if ENV.fetch('SWARM', 'VIM_MODE') == 'easy'
    extend NormalKey

    @key_info.merge! north: '<UP>', south: '<DOWN>', west: '<LEFT>', east: '<RIGHT>'
  else
    extend VIKey

    @key_info.merge! north: ?k, south: ?j, west: ?h, east: ?l
  end
end

#keypress {|command| ... } ⇒ Object

Yield Parameters:

  • command (String)

    yields command on keypress



102
103
104
# File 'lib/swarm/console.rb', line 102

def keypress
  command = getch and yield(command)
end

#openObject

Initialize the Curses environment, configure it and then clean up when the caller is done. 9 different bg/fg pairs are initialized for the colored Tile squares (0-8): (black, red, green, yellow, blue, magenta, cyan, white, grey)



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/swarm/console.rb', line 152

def open
  init_screen
  init_colors
  init_keys

  curs_set 0 # hide the cursor
  cbreak # turn off line buffering

  yield

  close
end

#update(tiles) ⇒ Object

Redraw all tiles given and refresh the console

Parameters:

  • tiles (Array<Tile, ...>)


121
122
123
124
# File 'lib/swarm/console.rb', line 121

def update(tiles)
  tiles.each &method(:draw)
  refresh
end

#widthInteger

Returns width of console

Returns:

  • (Integer)

    width of console



110
111
112
# File 'lib/swarm/console.rb', line 110

def width
  @width ||= cols
end

#wipeObject



135
136
137
138
# File 'lib/swarm/console.rb', line 135

def wipe
  clear
  refresh
end