Class: Natural20::CharacterBuilder

Inherits:
Object
  • Object
show all
Includes:
FighterBuilder, InventoryUI, RogueBuilder, WizardBuilder
Defined in:
lib/natural_20/cli/character_builder.rb

Constant Summary collapse

ALL_LANGUAGES =
%w[abyssal
celestial
common
deep_speech
draconic
dwarvish
elvish
giant
gnomish
goblin
halfling
infernal
orc
primordial
sylvan
undercommon]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InventoryUI

#character_sheet, #how_many?, #inventory_ui

Methods included from Weapons

#compute_advantages_and_disadvantages, #damage_modifier, #target_advantage_condition

Methods included from WizardBuilder

#wizard_builder

Methods included from RogueBuilder

#rogue_builder

Methods included from FighterBuilder

#fighter_builder

Constructor Details

#initialize(prompt, session, battle) ⇒ CharacterBuilder

Returns a new instance of CharacterBuilder.



13
14
15
16
17
# File 'lib/natural_20/cli/character_builder.rb', line 13

def initialize(prompt, session, battle)
  @prompt = prompt
  @session = session
  @battle = battle
end

Instance Attribute Details

#battleObject (readonly)

Returns the value of attribute battle.



11
12
13
# File 'lib/natural_20/cli/character_builder.rb', line 11

def battle
  @battle
end

#sessionObject (readonly)

Returns the value of attribute session.



11
12
13
# File 'lib/natural_20/cli/character_builder.rb', line 11

def session
  @session
end

Instance Method Details

#build_characterObject



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
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/natural_20/cli/character_builder.rb', line 19

def build_character
  @values = {
    hit_die: 'inherit',
    classes: {},
    ability: {},
    skills: [],
    level: 1,
    token: ['X'],
    tools: []
  }
  loop do
    ability_method = :random

    @values[:name] = prompt.ask(t('builder.enter_name'), default: @values[:name]) do |q|
      q.required true
      q.modify :capitalize
    end

    @values[:token] = [prompt.ask(t('builder.token'), default: @values[:name][0])]
    @values[:color] = prompt.select(t('builder.token_color')) do |q|
      %i[
        red light_red
        green light_green
        yellow light_yellow
        blue light_blue
        magenta light_magenta
        cyan light_cyan
        white light_white
      ].each do |color|
        q.choice @values[:token].first.colorize(color), color
      end
    end

    description = prompt.multiline(t('builder.description')) do |q|
      q.default t('builder.default_description')
    end

    @values[:description] = description.is_a?(Array) ? description.join("\n") : description

    races = session.load_races
    @values[:race] = prompt.select(t('builder.select_race')) do |q|
      races.each do |race, details|
        q.choice details[:label] || race.humanize, race
      end
    end

    race_detail = races[@values[:race]]
    if race_detail[:subrace]
      @values[:subrace] = prompt.select(t('builder.select_subrace')) do |q|
        race_detail[:subrace].each do |subrace, detail|
          q.choice detail[:label] || t("builder.races.#{subrace}"), subrace
        end
      end
    end
    subrace_detail = race_detail.dig(:subrace, @values[:subrace]&.to_sym)

    known_languages = race_detail.fetch(:languages, []) + (subrace_detail&.fetch(:languages, []) || [])
    language_choice = race_detail.fetch(:language_choice, 0) + (subrace_detail&.fetch(:language_choice, 0) || 0)
    if language_choice.positive?
      language_selector(ALL_LANGUAGES - known_languages, min: language_choice, max: language_choice)
    end

    race_bonus = race_detail[:attribute_bonus] || {}
    subrace_bonus = subrace_detail&.fetch(:attribute_bonus, {}) || {}

    attribute_bonuses = race_bonus.merge!(subrace_bonus)

    k = prompt.select(t('builder.class')) do |q|
      session.load_classes.each do |klass, details|
        q.choice details[:label] || klass.humanize, klass.to_sym
      end
    end

    @values[:classes][k.to_sym] = 1
    @class_properties = session.load_class(k)

    if race_detail[:tool_proficiencies_choice]
      num_tools_choices = race_detail[:tool_proficiencies_choice]
      @values[:tools] = prompt.multi_select(t('builder.select_tools_proficiency'), min: num_tools_choices, max: num_tools_choices) do |q|
        race_detail[:tool_proficiencies].each do |prof|
          q.choice t("builder.tools.#{prof}"), prof
        end
      end
    end

    ability_method = prompt.select(t('builder.ability_score_method')) do |q|
      q.choice t('builder.ability_score.random'), :random
      q.choice t('builder.ability_score.fixed'), :fixed
      # q.choice t('builder.ability_score.point_buy'), :point_buy
    end

    ability_scores = if ability_method == :random
                       6.times.map do |index|
                         r = 4.times.map do |_x|
                           die_roll = Natural20::DieRoll.roll('1d6', battle: battle,
                                                                     description: t('dice_roll.ability_score', roll_num: index + 1))
                           die_roll.result
                         end.sort.reverse
                         puts "#{index + 1}. #{r.join(',')}"

                         r.take(3).sum
                       end.sort.reverse
                     elsif ability_method == :fixed
                       [15, 14, 13, 12, 10, 8]
                     end

    puts t('builder.assign_ability_scores', scores: ability_scores.join(','))

    chosen_score = []

    Natural20::Entity::ATTRIBUTE_TYPES_ABBV.each do |type|
      bonus = attribute_bonuses[type.to_sym] || 0
      ability_choice_str = t("builder.#{type}")
      ability_choice_str += " (+#{bonus})" if bonus.positive?
      score_index = prompt.select(ability_choice_str) do |q|
        ability_scores.each_with_index do |score, index|
          next if chosen_score.include?(index)

          q.choice score, index
        end
      end

      chosen_score << score_index
      @values[:ability][type.to_sym] = ability_scores[score_index] + bonus
    end

    class_skills_selector

    send(:"#{k}_builder", @values)

    @values.merge!(@class_values)
    @pc = Natural20::PlayerCharacter.new(session, @values)
    character_sheet(@pc)
    break if prompt.yes?(t('builder.review'))
  end

  session.save_character(@values[:name], @values)

  @pc
end