7
8
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
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/action_ui.rb', line 7
def action_ui(action, entity)
return :stop if action == :stop
cont = action.build_map
loop do
param = cont.param&.map do |p|
case (p[:type])
when :look
self
when :movement
move_path, jump_index = move_ui(entity, p)
return nil if move_path.nil?
[move_path, jump_index]
when :target, :select_target
targets = attack_ui(entity, action, p)
return nil if targets.nil? || targets.empty?
p[:num] > 1 ? targets : targets.first
when :select_spell
spell_slots_ui(entity)
spell = prompt.select("#{entity.name} cast Spell", per_page: TTY_PROMPT_PER_PAGE) do |q|
spell_choice(entity, battle, q)
q.choice t(:back).colorize(:blue), :back
end
return nil if spell == :back
spell
when :select_weapon
action.using || action.npc_action
when :select_item
item = prompt.select("#{entity.name} use item", per_page: TTY_PROMPT_PER_PAGE) do ||
entity.usable_items.each do |d|
if d[:consumable]
.choice "#{d[:label].colorize(:blue)} (#{d[:qty]})", d[:name]
else
.choice d[:label].colorize(:blue).to_s, d[:name]
end
end
.choice t(:back).colorize(:blue), :back
end
return nil if item == :back
item
when :select_ground_items
selected_items = prompt.multi_select("Items on the ground around #{entity.name}") do ||
map.items_on_the_ground(entity).each do |ground_item|
ground, items = ground_item
items.each do |t|
item_label = t("object.#{t.label}", default: t.label)
.choice t('inventory.inventory_items', name: item_label, qty: t.qty), [ground, t]
end
end
end
return nil if selected_items.empty?
item_selection = {}
selected_items.each do |s_item|
ground, item = s_item
qty = how_many?(item)
item_selection[ground] ||= []
item_selection[ground] << [item, qty]
end
item_selection.map do |k, v|
[k, v]
end
when :select_object
target_objects = entity.usable_objects(map, battle)
item = prompt.select("#{entity.name} interact with") do ||
target_objects.each do |d|
.choice d.name.humanize.to_s, d
end
.choice t(:manual_target), :manual_target
.choice t(:back).colorize(:blue), :back
end
return nil if item == :back
if item == :manual_target
item = target_ui(entity, num_select: 1, validation: lambda { |selected|
selected_entities = map.thing_at(*selected)
return false if selected_entities.empty?
selected_entities.detect do |selected_entity|
target_objects.include?(selected_entity)
end
}).first
end
item
when :select_items
selected_items = prompt.multi_select(p[:label], per_page: TTY_PROMPT_PER_PAGE) do ||
p[:items].each do |m|
item_label = t("object.#{m.label}", default: m.label)
if m.try(:equipped)
.choice t('inventory.equiped_items', name: item_label), m
else
.choice t('inventory.inventory_items', name: item_label, qty: m.qty), m
end
end
.choice t(:back).colorize(:blue), :back
end
return nil if selected_items.include?(:back)
selected_items = selected_items.map do |m|
count = how_many?(m)
[m, count]
end
selected_items
when :interact
object_action = prompt.select("#{entity.name} will") do ||
interactions = p[:target].available_interactions(entity)
class_key = p[:target].class.to_s
if interactions.is_a?(Array)
interactions.each do |k|
.choice t(:"object.#{class_key}.#{k}", default: k.to_s.humanize), k
end
else
interactions.each do |k, options|
label = options[:label] || t(:"object.#{class_key}.#{k}", default: k.to_s.humanize)
if options[:disabled]
.choice label, k, disabled: options[:disabled_text]
else
.choice label, k
end
end
end
.choice 'Back', :back
end
return nil if item == :back
object_action
when :show_inventory
inventory_ui(entity)
else
raise "unknown #{p[:type]}"
end
end
cont = cont.next.call(*param)
break if param.nil?
end
@action = cont
end
|