Class: StatMenu

Inherits:
Object
  • Object
show all
Includes:
Remedy
Defined in:
lib/terminal_hero/classes/stat_menu.rb

Overview

Custom class created because tty-prompt does not provide menus in this format.

Instance Method Summary collapse

Constructor Details

#initialize(starting_stats, starting_points) ⇒ StatMenu

Returns a new instance of StatMenu.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/terminal_hero/classes/stat_menu.rb', line 19

def initialize(starting_stats, starting_points)
  @starting_stats = starting_stats
  @stats = Utils.depth_two_clone(starting_stats)
  @starting_points = starting_points
  @points = starting_points
  @line_no = 0
  @header = Header.new
  @footer = Footer.new
  @stat_index = @stats.keys
  @header << "Please allocate your character's stat points."
  @header << "Use the left and right arrow keys to assign points, and enter to confirm."
end

Instance Method Details

#add_pointObject



51
52
53
54
55
56
# File 'lib/terminal_hero/classes/stat_menu.rb', line 51

def add_point
  return unless @points.positive?

  @points -= 1
  @stats[@stat_index[@line_no]][:value] += 1
end

#change_line(key) ⇒ Object



68
69
70
71
# File 'lib/terminal_hero/classes/stat_menu.rb', line 68

def change_line(key)
  change = key == :down ? 1 : -1
  @line_no = Utils.collar(0, @line_no + change, @stats.length - 1)
end

#get_display_parametersObject



73
74
75
# File 'lib/terminal_hero/classes/stat_menu.rb', line 73

def get_display_parameters
  return [@stats, @points, @line_no, @header, @footer]
end

#process_input(key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/terminal_hero/classes/stat_menu.rb', line 32

def process_input(key)
  case key
  when :right
    add_point
  # Left arrow key reduces highlighted stat, but not below its starting value
  when :left
    subtract_point
  # Up and down arrow keys to move around list
  when :down, :up
    change_line(key)
  # :control_m represents carriage return
  when :control_m
    return true, @stats if @points.zero?

    @footer << "You must allocate all stat points to continue.".colorize(:red) if @footer.lines.empty?
  end
  return false
end

#subtract_pointObject



58
59
60
61
62
63
64
65
66
# File 'lib/terminal_hero/classes/stat_menu.rb', line 58

def subtract_point
  unless @points < @starting_points &&
         @stats[@stat_index[@line_no]][:value] > @starting_stats[@stat_index[@line_no]][:value]
    return
  end

  @points += 1
  @stats[@stat_index[@line_no]][:value] -= 1
end