Class: Gaminator::Runner

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/gaminator/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(game_class, options = {}) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gaminator/runner.rb', line 9

def initialize(game_class, options = {})
  if options[:rows] && options[:cols]
    resizeterm(options[:rows], options[:cols])
  end

  init_screen
  start_color
  cbreak
  noecho
  stdscr.nodelay = 1
  curs_set(0)

  [
    COLOR_WHITE, COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
    COLOR_MAGENTA, COLOR_YELLOW
  ].each do |color|
    init_pair(color, color, COLOR_BLACK)
  end

  if colors > 8
    (8..colors-1).each do |color|
      init_pair(color, color, COLOR_BLACK)
    end
  end

  @plane_width = cols
  @plane_height = lines - 5
  @plane = Window.new(@plane_height, @plane_width, 0, 0)
  @plane.box("|", "-")

  @textbox_width = cols
  @textbox_height = 5
  @textbox = Window.new(@textbox_height, @textbox_width, @plane_height, 0)
  @textbox.box("|", "-")

  @game = game_class.new(@plane_width - 2, @plane_height - 2)
end

Instance Method Details

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gaminator/runner.rb', line 47

def run
  begin
    loop do
      tick_game

      render_objects
      render_textbox

      @plane.refresh
      @textbox.refresh

      handle_input

      clear_plane
      clear_textbox

      sleep(@game.sleep_time)
    end
  ensure
    close_screen
    puts
    puts @game.exit_message
    puts
  end
end