Class: GrappleAction

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

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

Constructor Details

This class inherits a constructor from Natural20::Action

Instance Attribute Details

#targetObject

Returns the value of attribute target.



2
3
4
# File 'lib/natural_20/actions/grapple_action.rb', line 2

def target
  @target
end

Class Method Details

.apply!(battle, item) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/natural_20/actions/grapple_action.rb', line 97

def self.apply!(battle, item)
  case (item[:type])
  when :grapple
    if item[:success]
      item[:target].grappled_by!(item[:source])
      Natural20::EventManager.received_event(event: :grapple_success,
                                             target: item[:target], source: item[:source],
                                             source_roll: item[:source_roll],
                                             target_roll: item[:target_roll])
    else
      Natural20::EventManager.received_event(event: :grapple_failure,
                                             target: item[:target], source: item[:source],
                                             source_roll: item[:source_roll],
                                             target_roll: item[:target_roll])
    end

    battle.consume(item[:source], :action)
  end
end

.build(session, source) ⇒ Object



38
39
40
41
# File 'lib/natural_20/actions/grapple_action.rb', line 38

def self.build(session, source)
  action = GrappleAction.new(session, source, :grapple)
  action.build_map
end

.can?(entity, battle, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/natural_20/actions/grapple_action.rb', line 4

def self.can?(entity, battle, _options = {})
  (battle.nil? || entity.total_actions(battle).positive?) && !entity.grappling?
end

Instance Method Details

#build_mapObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/natural_20/actions/grapple_action.rb', line 17

def build_map
  OpenStruct.new({
                   action: self,
                   param: [
                     {
                       type: :select_target,
                       range: 5,
                       target_types: %i[enemies allies],
                       num: 1
                     }
                   ],
                   next: lambda { |target|
                     self.target = target
                     OpenStruct.new({
                                      param: nil,
                                      next: -> { self }
                                    })
                   }
                 })
end

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

Build the attack roll information

Parameters:

Options Hash (opts):



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
# File 'lib/natural_20/actions/grapple_action.rb', line 48

def resolve(_session, _map, opts = {})
  target = opts[:target] || @target
  battle = opts[:battle]
  raise 'target is a required option for :attack' if target.nil?

  return if (target.size_identifier - @source.size_identifier) > 1

  strength_roll = @source.athletics_check!(battle)
  athletics_stats = (@target.athletics_proficient? ? @target.proficiency_bonus : 0) + @target.str_mod
  acrobatics_stats = (@target.acrobatics_proficient? ? @target.proficiency_bonus : 0) + @target.dex_mod

  grapple_success = false
  if @target.incapacitated? || !battle.opposing?(@source, target)
    grapple_success = true
  else
    contested_roll = if athletics_stats > acrobatics_stats
                       @target.athletics_check!(battle,
                                                description: t('die_roll.contest'))
                     else
                       @target.acrobatics_check!(
                         opts[:battle], description: t('die_roll.contest')
                       )
                     end
    grapple_success = strength_roll.result >= contested_roll.result
  end

  @result = if grapple_success
              [{
                source: @source,
                target: target,
                type: :grapple,
                success: true,
                battle: battle,
                source_roll: strength_roll,
                target_roll: contested_roll
              }]
            else
              [{
                source: @source,
                target: target,
                type: :grapple,
                success: false,
                battle: battle,
                source_roll: strength_roll,
                target_roll: contested_roll
              }]
            end
end

#to_sObject



8
9
10
# File 'lib/natural_20/actions/grapple_action.rb', line 8

def to_s
  @action_type.to_s.humanize
end

#validateObject



12
13
14
15
# File 'lib/natural_20/actions/grapple_action.rb', line 12

def validate
  @errors << 'target is a required option for :attack' if target.nil?
  @errors << t('validation.shove.invalid_target_size') if (target.size_identifier - @source.size_identifier) > 1
end