class ItemLibrary::DoorObject

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/door_object.rb, line 66
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?
    { close: { disabled: someone_blocking_the_doorway?, disabled_text: t('object.door.door_blocked') } }
  else
    { open: {}, lock: { disabled: !entity.item_count(:"#{key_name}").positive?,
                        disabled_text: t('object.door.key_required') } }
  end
end
close!() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 38
def close!
  @state = :closed
end
closed?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 26
def closed?
  @state == :closed
end
interactable?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 86
def interactable?
  true
end
lock!() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 14
def lock!
  @locked = true
end
locked?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 18
def locked?
  @locked
end
lockpick_dc() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 171
def lockpick_dc
  (@properties[:lockpick_dc].presence || 10)
end
opaque?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 6
def opaque?
  closed? && !dead?
end
open!() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 34
def open!
  @state = :opened
end
opened?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 30
def opened?
  @state == :opened
end
passable?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 22
def passable?
  opened? || dead?
end
resolve(entity, action, _other_params, opts = {}) click to toggle source

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

# File lib/natural_20/item_library/door_object.rb, line 92
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 :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/door_object.rb, line 42
def token
  return '`' if dead?

  pos_x, pos_y = position
  t = if map.wall?(pos_x - 1, pos_y) || map.wall?(pos_x + 1, pos_y)
        opened? ? '-' : '='
      else
        opened? ? '|' : '║'
      end

  [t]
end
token_closed() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 59
def token_closed
  @properties[:token_closed].presence || '='
end
token_opened() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 55
def token_opened
  @properties[:token_open].presence || '-'
end
unlock!() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 10
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/door_object.rb, line 127
def use!(entity, result)
  case (result[:action])
  when :open
    open! if closed?
  when :close
    return unless opened?

    if someone_blocking_the_doorway?
      return Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                                    sub_type: :close_failed, result: :failed, reason: 'Cannot close door since something is in the doorway')
    end

    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: 'Door unlocked using lockpick.')
  when :lockpick_fail
    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: 'Lockpicking failed and the theives tools are now broken')
  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.door.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.door.lock'))
  when :door_locked
    Natural20::EventManager.received_event(source: self, user: entity, event: :object_interaction,
                                           sub_type: :open_failed, result: :failed, reason: 'Cannot open door since door 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/door_object.rb, line 181
def on_take_damage(battle, damage_params); end
setup_other_attributes() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 183
def setup_other_attributes
  @state = @properties[:state]&.to_sym || :closed
  @locked = @properties[:locked]
  @key_name = @properties[:key]
end
someone_blocking_the_doorway?() click to toggle source
# File lib/natural_20/item_library/door_object.rb, line 177
def someone_blocking_the_doorway?
  !!map.entity_at(*position)
end