class SpellAction

Attributes

at_level[RW]
other_params[RW]
spell[RW]
spell_action[RW]
target[RW]

Public Class Methods

apply!(battle, item) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source

@param battle [Natural20::Battle] @param spell [Symbol]

# 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) click to toggle source

@param battle [Natural20::Battle] @param item [Hash]

# 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

Public Instance Methods

build_map() click to toggle source
# 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 = {}) click to toggle source
# 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