Class: SpellAction

Inherits:
Natural20::Action show all
Extended by:
Natural20::ActionDamage
Defined in:
lib/natural_20/actions/spell_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 included from Natural20::ActionDamage

damage_event

Methods inherited from Natural20::Action

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

Constructor Details

This class inherits a constructor from Natural20::Action

Instance Attribute Details

#at_levelObject

Returns the value of attribute at_level.



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

def at_level
  @at_level
end

#other_paramsObject

Returns the value of attribute other_params.



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

def other_params
  @other_params
end

#spellObject

Returns the value of attribute spell.



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

def spell
  @spell
end

#spell_actionObject

Returns the value of attribute spell_action.



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

def spell_action
  @spell_action
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

Class Method Details

.apply!(battle, item) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/natural_20/actions/spell_action.rb', line 56

def self.apply!(battle, item)
  Natural20::Spell.descendants.each do |klass|
    klass.apply!(battle, item)
  end
  case item[:type]
  when :spell_damage
    damage_event(item, battle)
    consume_resource(battle, item)
  when :spell_miss
    consume_resource(battle, item)
    Natural20::EventManager.received_event({ attack_roll: item[:attack_roll],
                                             attack_name: item[:attack_name],
                                             advantage_mod: item[:advantage_mod],
                                             as_reaction: !!item[:as_reaction],
                                             adv_info: item[:adv_info],
                                             source: item[:source], target: item[:target], event: :miss })
  end
end

.build(session, source) ⇒ Object



25
26
27
28
# File 'lib/natural_20/actions/spell_action.rb', line 25

def self.build(session, source)
  action = SpellAction.new(session, source, :spell)
  action.build_map
end

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

Returns:

  • (Boolean)


6
7
8
9
10
# File 'lib/natural_20/actions/spell_action.rb', line 6

def self.can?(entity, battle, options = {})
  return false unless entity.has_spells?

  battle.nil? || !battle.ongoing? || can_cast?(entity, battle, options[:spell])
end

.can_cast?(entity, battle, spell) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
# File 'lib/natural_20/actions/spell_action.rb', line 14

def self.can_cast?(entity, battle, spell)
  return true unless spell

  spell_details = battle.session.load_spell(spell)
  amt, resource = spell_details[:casting_time].split(':')

  return true if resource == ('action') && battle.total_actions(entity).positive?

  false
end

.consume_resource(battle, item) ⇒ Object

Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/natural_20/actions/spell_action.rb', line 77

def self.consume_resource(battle, item)
  amt, resource = item.dig(:spell, :casting_time).split(':')
  spell_level = item.dig(:spell, :level)
  case resource
  when 'action'
    battle.consume(item[:source], :action)
  when 'reaction'
    battle.consume(item[:source], :reaction)
  end

  item[:source].consume_spell_slot!(spell_level) if spell_level.positive?
end

Instance Method Details

#build_mapObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/natural_20/actions/spell_action.rb', line 30

def build_map
  OpenStruct.new({
                   action: self,
                   param: [
                     {
                       type: :select_spell
                     }
                   ],
                   next: lambda { |spell_choice|
                           spell, at_level = spell_choice
                           @spell = session.load_spell(spell)
                           raise "spell not found #{spell}" unless @spell

                           self.at_level = at_level
                           @spell_action = @spell[:spell_class].constantize.new(@source, spell, @spell)
                           @spell_action.build_map(self)
                         }
                 })
end

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



50
51
52
53
54
# File 'lib/natural_20/actions/spell_action.rb', line 50

def resolve(_session, _map = nil, opts = {})
  battle = opts[:battle]
  @result = spell_action.resolve(@source, battle, self)
  self
end