Class: UseItemAction

Inherits:
Natural20::Action show all
Defined in:
lib/natural_20/actions/use_item_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

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

#target_itemObject

Returns the value of attribute target_item.



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

def target_item
  @target_item
end

Class Method Details

.apply!(battle, item) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/natural_20/actions/use_item_action.rb', line 46

def self.apply!(battle, item)
  case item[:type]
  when :use_item
    Natural20::EventManager.received_event({ event: :use_item, source: item[:source], item: item[:item] })
    item[:item].use!(item[:target], item)
    item[:source].deduct_item(item[:item].name, 1) if item[:item].consumable?
    battle.entity_state_for(item[:source])[:action] -= 1 if battle
  end
end

.build(session, source) ⇒ Object



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

def self.build(session, source)
  action = UseItemAction.new(session, source, :attack)
  action.build_map
end

.can?(entity, battle) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#build_mapObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/natural_20/actions/use_item_action.rb', line 14

def build_map
  OpenStruct.new({
                   action: self,
                   param: [
                     {
                       type: :select_item
                     }
                   ],
                   next: lambda { |item|
                           item_details = session.load_equipment(item)
                           raise "item #{item_details[:name]} not usable!" unless item_details[:usable]

                           @target_item = item_details[:item_class].constantize.new(item, item_details)
                           @target_item.build_map(self)
                         }
                 })
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/natural_20/actions/use_item_action.rb', line 32

def resolve(_session, map = nil, opts = {})
  battle = opts[:battle]
  result_payload = {
    source: @source,
    target: target,
    map: map,
    battle: battle,
    type: :use_item,
    item: target_item
  }.merge(target_item.resolve(@source, battle))
  @result = [result_payload]
  self
end