class ItemLibrary::Chest

Attributes

key_name[R]
locked[R]
state[R]

Public Instance Methods

available_interactions(entity, battle = nil) click to toggle source

Returns available interaction with this object @param entity [Natural20::PlayerCharacter] @return [Array]

# File lib/natural_20/item_library/chest.rb, line 107
def available_interactions(entity, battle = nil)
  interaction_actions = {}
  if locked?
    interaction_actions[:unlock] = { disabled: !entity.item_count(:"#{key_name}").positive?,
                                     disabled_text: t('object.door.key_required') }
    if entity.item_count('thieves_tools').positive? && entity.proficient?('thieves_tools')
      interaction_actions[:lockpick] =
        { disabled: !entity.action?(battle), disabled_text: t('object.door.action_required') }
    end
    return interaction_actions
  end

  if opened?
    %i[close store loot]
  else
    { open: {}, lock: { disabled: !entity.item_count(:"#{key_name}").positive?,
                        disabled_text: t('object.door.key_required') } }
  end
end
build_map(action, action_object) click to toggle source

Builds a custom UI map @param action [Symbol] The item specific action @param action_object [InteractAction] @return [OpenStruct]

# File lib/natural_20/item_library/chest.rb, line 12
def build_map(action, action_object)
  case action
  when :store
    OpenStruct.new({
                     action: action_object,
                     param: [
                       {
                         type: :select_items,
                         label: action_object.source.items_label,
                         items: action_object.source.inventory
                       }
                     ],
                     next: lambda { |items|
                             action_object.other_params = items
                             OpenStruct.new({
                                              param: nil,
                                              next: lambda {
                                                      action_object
                                                    }
                                            })
                           }
                   })
  when :loot
    OpenStruct.new({
                     action: action_object,
                     param: [
                       {
                         type: :select_items,
                         label: items_label,
                         items: inventory
                       }
                     ],
                     next: lambda { |items|
                             action_object.other_params = items
                             OpenStruct.new({
                                              param: nil,
                                              next: lambda {
                                                      action_object
                                                    }
                                            })
                           }
                   })
  end
end
close!() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 89
def close!
  @state = :closed
end
closed?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 77
def closed?
  @state == :closed
end
color() click to toggle source
Calls superclass method
# File lib/natural_20/item_library/chest.rb, line 100
def color
  opened? ? :white : super
end
interactable?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 127
def interactable?
  true
end
lock!() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 65
def lock!
  @locked = true
end
locked?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 69
def locked?
  @locked
end
lockpick_dc() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 216
def lockpick_dc
  (@properties[:lockpick_dc].presence || 10)
end
opaque?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 57
def opaque?
  false
end
open!() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 85
def open!
  @state = :opened
end
opened?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 81
def opened?
  @state == :opened
end
passable?() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 73
def passable?
  true
end
resolve(entity, action, other_params, opts = {}) click to toggle source

@param entity [Natural20::Entity] @param action [InteractAction]

# File lib/natural_20/item_library/chest.rb, line 133
def resolve(entity, action, other_params, opts = {})
  return if action.nil?

  case action
  when :open
    if !locked?
      {
        action: action
      }
    else
      {
        action: :door_locked
      }
    end
  when :loot, :store
    { action: action, items: other_params, source: entity, target: self, battle: opts[:battle] }
  when :close
    {
      action: action
    }
  when :lockpick
    lock_pick_roll = entity.lockpick!(opts[:battle])

    if lock_pick_roll.result >= lockpick_dc
      { action: :lockpick_success, roll: lock_pick_roll, cost: :action }
    else
      { action: :lockpick_fail, roll: lock_pick_roll, cost: :action }
    end
  when :unlock
    entity.item_count(:"#{key_name}").positive? ? { action: :unlock } : { action: :unlock_failed }
  when :lock
    entity.item_count(:"#{key_name}").positive? ? { action: :lock } : { action: :lock_failed }
  end
end
token() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 93
def token
  return '`' if dead?

  t = opened? ? "\u2610" : "\u2610"
  [t]
end
unlock!() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 61
def unlock!
  @locked = false
end
use!(entity, result) click to toggle source

@param entity [Natural20::Entity] @param result [Hash]

# File lib/natural_20/item_library/chest.rb, line 170
def use!(entity, result)
  case result[:action]
  when :store
    store(result[:battle], result[:source], result[:target], result[:items])
  when :loot
    retrieve(result[:battle], result[:source], result[:target], result[:items])
  when :open
    open! if closed?
  when :close
    return unless opened?

    close!
  when :lockpick_success
    return unless locked?

    unlock!
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :unlock, result: :success, lockpick: true, roll: result[:roll], reason: t(:"object.chest.unlock"))
  when :lockpick_fail
    return unless locked?

    entity.deduct_item('thieves_tools')
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :unlock, result: :failed, roll: result[:roll], reason: t('object.lockpick_failed'))

  when :unlock
    return unless locked?

    unlock!
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :unlock, result: :success, reason: t(:"object.chest.unlock"))
  when :lock
    return unless unlocked?

    lock!
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :lock, result: :success, reason: t(:"object.chest.lock"))
  when :door_locked
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :open_failed, result: :failed, reason: 'Cannot open chest since chest is locked.')
  when :unlock_failed
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :unlock_failed, result: :failed, reason: 'Correct Key missing.')
  end
end

Protected Instance Methods

on_take_damage(battle, damage_params) click to toggle source
# File lib/natural_20/item_library/chest.rb, line 222
def on_take_damage(battle, damage_params); end
setup_other_attributes() click to toggle source
# File lib/natural_20/item_library/chest.rb, line 224
def setup_other_attributes
  @state = @properties[:state]&.to_sym || :closed
  @locked = @properties[:locked]
  @key_name = @properties[:key]
end