Class: GroundInteractAction

Inherits:
Natural20::Action show all
Defined in:
lib/natural_20/actions/ground_interact_action.rb

Overview

typed: true

Instance Attribute Summary collapse

Attributes inherited from Natural20::Action

#action_type, #errors, #result, #session, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Natural20::Action

#initialize, #label, #name, #to_s, #validate

Constructor Details

This class inherits a constructor from Natural20::Action

Instance Attribute Details

#ground_itemsObject

Returns the value of attribute ground_items.



3
4
5
# File 'lib/natural_20/actions/ground_interact_action.rb', line 3

def ground_items
  @ground_items
end

#targetObject

Returns the value of attribute target.



3
4
5
# File 'lib/natural_20/actions/ground_interact_action.rb', line 3

def target
  @target
end

Class Method Details

.apply!(battle, item) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/natural_20/actions/ground_interact_action.rb', line 62

def self.apply!(battle, item)
    entity = item[:source]
    case (item[:type])
    when :pickup
      item[:actions].each do |g, action|
        g.use!(nil, action)
      end

      if item[:cost] == :action
        battle&.consume!(entity, :action, 1)
      else
        battle&.consume!(entity, :free_object_interaction, 1) || battle&.consume!(entity, :action, 1)
      end
    end
end

.build(session, source) ⇒ Object



9
10
11
12
# File 'lib/natural_20/actions/ground_interact_action.rb', line 9

def self.build(session, source)
  action = GroundInteractAction.new(session, source, :ground_interact)
  action.build_map
end

.can?(entity, battle) ⇒ Boolean



5
6
7
# File 'lib/natural_20/actions/ground_interact_action.rb', line 5

def self.can?(entity, battle)
  battle.nil? || (entity.total_actions(battle).positive? || entity.free_object_interaction?(battle)) && items_on_the_ground_count(entity, battle).positive?
end

.items_on_the_ground_count(entity, battle) ⇒ Integer



17
18
19
20
21
# File 'lib/natural_20/actions/ground_interact_action.rb', line 17

def self.items_on_the_ground_count(entity, battle)
  return 0 unless battle.map

  battle.map.items_on_the_ground(entity).inject(0) { |total, item| total + item[1].size }
end

Instance Method Details

#build_mapObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/natural_20/actions/ground_interact_action.rb', line 23

def build_map
  OpenStruct.new({
                   action: self,
                   param: [
                     {
                       type: :select_ground_items
                     }
                   ],
                   next: lambda { |object|
                     self.ground_items = object

                     OpenStruct.new({
                                      param: nil,
                                      next: lambda {
                                              self
                                            }
                                    })
                   }
                 })
end

#resolve(_session, map = nil, opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/natural_20/actions/ground_interact_action.rb', line 44

def resolve(_session, map = nil, opts = {})
  battle = opts[:battle]

  actions = ground_items.map do |g, items|
    [g, { action: :pickup, items: items, source: @source, target: g, battle: opts[:battle] }]
  end.to_h

  @result << {
    source: @source,
    actions: actions,
    map: map,
    battle: battle,
    type: :pickup
  }

  self
end