class Gemwarrior::Inventory

Constants

ERROR_ITEM_ADD_INVALID
ERROR_ITEM_ADD_UNTAKEABLE
ERROR_ITEM_DESCRIBE_INVALID
ERROR_ITEM_EQUIP_INVALID
ERROR_ITEM_EQUIP_NONARMAMENT
ERROR_ITEM_REMOVE_INVALID

CONSTANTS

ERROR_ITEM_UNEQUIP_INVALID
ERROR_ITEM_UNEQUIP_NONARMAMENT
VOWELS

Attributes

armor[RW]
items[RW]
weapon[RW]

Public Class Methods

new(items = [], weapon = nil, armor = nil) click to toggle source
# File lib/gemwarrior/inventory.rb, line 23
def initialize(items = [], weapon = nil, armor = nil)
  self.items  = items
  self.weapon = weapon
  self.armor  = armor
end

Public Instance Methods

add_item(item_name, cur_loc = nil, player = nil) click to toggle source
# File lib/gemwarrior/inventory.rb, line 203
def add_item(item_name, cur_loc = nil, player = nil)
  if cur_loc.nil?
    self.items.push(item_name)
  else
    cur_loc.items.each do |i|
      if i.name.eql?(item_name)
        if i.takeable
          self.items.push(i)
          cur_loc.remove_item(item_name)

          # stats
          player.items_taken += 1

          return "#{"Added".colorize(:green)} #{item_name.colorize(:yellow)} #{"to your increasing collection of bits of tid".colorize(:green)}."
        else
          return ERROR_ITEM_ADD_UNTAKEABLE.colorize(:red)
        end
      end
    end
  end
  ERROR_ITEM_ADD_INVALID.colorize(:red)
end
an_words() click to toggle source
# File lib/gemwarrior/inventory.rb, line 29
def an_words
  ['herb']
end
article_chooser(word) click to toggle source
# File lib/gemwarrior/inventory.rb, line 33
def article_chooser(word)
  (VOWELS.include?(word.to_s[0]) or an_words.include?(word.to_s)) ? 'an' : 'a'
end
contains_battle_item?() click to toggle source
# File lib/gemwarrior/inventory.rb, line 125
def contains_battle_item?
  battle_item_found = false
  self.items.each do |i|
    battle_item_found = true if i.useable_battle
  end
  battle_item_found
end
contains_item?(item_name) click to toggle source
# File lib/gemwarrior/inventory.rb, line 121
def contains_item?(item_name)
  self.items.map{ |i| i.name.downcase }.include?(item_name.downcase)
end
contents() click to toggle source

non-English-like contents of inventory for simple lists

# File lib/gemwarrior/inventory.rb, line 42
def contents
  if is_empty?
    nil
  else
    item_hash = {}
    self.items.map(&:name).each do |i|
      i_sym = i.to_sym
      if item_hash.keys.include? i_sym
        item_hash[i_sym] += 1
      else
        item_hash[i_sym] = 1
      end
    end

    # one item? return string
    if item_hash.length == 1
      i = item_hash.keys.join
      q = item_hash.values.join.to_i
      return q > 1 ? "#{i}s x#{q}" : i
    # multiple items? return array of strings to mush together
    else
      item_arr = []
      item_hash.each do |i, q|
        if q > 1
          item_arr.push("#{i}s x#{q}")
        else
          item_arr.push(i)
        end
      end

      return item_arr.join(', ')
    end
  end
end
describe_item(item_name, world) click to toggle source
# File lib/gemwarrior/inventory.rb, line 141
def describe_item(item_name, world)
  if contains_item?(item_name)
    self.items.each do |i|
      if i.name.eql?(item_name)
        if GameOptions.data['debug_mode']
          return i.describe_detailed(world)
        else
          return i.describe(world)
        end
      end
    end
  else
    ERROR_ITEM_DESCRIBE_INVALID
  end
end
drop_item(item_name, cur_loc) click to toggle source
# File lib/gemwarrior/inventory.rb, line 226
def drop_item(item_name, cur_loc)
  if contains_item?(item_name)
    remove_item(item_name)
    cur_loc.add_item(item_name)
    "You dropped #{item_name.colorize(:yellow)}."
  else
    ERROR_ITEM_REMOVE_INVALID
  end
end
equip_item(item_name) click to toggle source
# File lib/gemwarrior/inventory.rb, line 157
def equip_item(item_name)
  if contains_item?(item_name)
    self.items.each do |i|
      if i.name.eql?(item_name)
        if i.equippable
          i.equipped = true
          if i.is_weapon
            self.weapon = i
            return "The #{i.name.colorize(:yellow)} has taken charge, and been equipped."
          elsif i.is_armor
            self.armor = i
            return "The #{i.name.colorize(:yellow)} has fortified you, and been equipped."
          end
        else
          return ERROR_ITEM_EQUIP_NONARMAMENT
        end
      end
    end
  else
    ERROR_ITEM_EQUIP_INVALID
  end
end
is_empty?() click to toggle source
# File lib/gemwarrior/inventory.rb, line 37
def is_empty?
  self.items.nil? || self.items.empty?
end
list_battle_items() click to toggle source
# File lib/gemwarrior/inventory.rb, line 133
def list_battle_items
  battle_items = []
  self.items.each do |i|
    battle_items.push(i) if i.useable_battle
  end
  battle_items
end
list_contents() click to toggle source

English-like sentence summary of inventory

# File lib/gemwarrior/inventory.rb, line 78
def list_contents
  if is_empty?
    'You possess nothing.'
  else
    # build hash of inventory's items
    item_hash = {}
    self.items.map(&:name).each do |i|
      i_sym = i.to_sym
      if item_hash.keys.include? i_sym
        item_hash[i_sym] += 1
      else
        item_hash[i_sym] = 1
      end
    end

    # one item? return string
    if item_hash.length == 1
      i = item_hash.keys.join
      q = item_hash.values.join.to_i
      if q > 1
        return "You have #{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}."
      else
        return "You have #{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}."
      end
    # multiple items? return array of strings to mush together
    else
      item_list_text = 'You have '
      item_arr = []
      item_hash.each do |i, q|
        if q > 1
          item_arr.push("#{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}")
        else
          item_arr.push("#{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}")
        end
      end

      item_arr[-1].replace("and #{item_arr[-1]}.")

      return item_list_text << item_arr.join(', ')
    end
  end
end
remove_item(item_name) click to toggle source
# File lib/gemwarrior/inventory.rb, line 236
def remove_item(item_name)
  self.items.delete_at(self.items.map(&:name).index(item_name) || self.items.length)
  unless self.weapon.nil?
    self.weapon = nil if self.weapon.name.eql?(item_name)
  end
end
unequip_item(item_name) click to toggle source
# File lib/gemwarrior/inventory.rb, line 180
def unequip_item(item_name)
  if contains_item?(item_name)
    self.items.each do |i|
      if i.name.eql?(item_name)
        if i.equippable
          i.equipped = false
          if i.is_weapon
            self.weapon = nil
            return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
          elsif i.is_armor
            self.armor = nil
            return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
          end
        else
          return ERROR_ITEM_UNEQUIP_NONARMAMENT
        end
      end
    end
  else
    ERROR_ITEM_UNEQUIP_INVALID
  end
end