Class: Gemwarrior::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/gemwarrior/inventory.rb

Constant Summary collapse

ERROR_ITEM_REMOVE_INVALID =

CONSTANTS

'Your inventory does not contain that item, so you cannot drop it.'
ERROR_ITEM_ADD_UNTAKEABLE =
'That would be great if you could take that, wouldn\'t it? Huh!'
ERROR_ITEM_ADD_INVALID =
'That item cannot be taken or does not exist.'
ERROR_ITEM_DESCRIBE_INVALID =
'That does not seem to be in the inventory.'
ERROR_ITEM_EQUIP_INVALID =
'You do not possess anything called that to equip.'
ERROR_ITEM_EQUIP_NONARMAMENT =
'That item cannot be equipped.'
ERROR_ITEM_UNEQUIP_INVALID =
'You do not possess anything called that to unequip.'
ERROR_ITEM_UNEQUIP_NONARMAMENT =
'That item cannot be unequipped.'
VOWELS =
'aeiou'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = [], weapon = nil, armor = nil) ⇒ Inventory

Returns a new instance of Inventory.



23
24
25
26
27
# File 'lib/gemwarrior/inventory.rb', line 23

def initialize(items = [], weapon = nil, armor = nil)
  self.items  = items
  self.weapon = weapon
  self.armor  = armor
end

Instance Attribute Details

#armorObject

Returns the value of attribute armor.



19
20
21
# File 'lib/gemwarrior/inventory.rb', line 19

def armor
  @armor
end

#itemsObject

Returns the value of attribute items.



19
20
21
# File 'lib/gemwarrior/inventory.rb', line 19

def items
  @items
end

#weaponObject

Returns the value of attribute weapon.



19
20
21
# File 'lib/gemwarrior/inventory.rb', line 19

def weapon
  @weapon
end

Instance Method Details

#add_item(item_name, cur_loc = nil, player = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 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_wordsObject



29
30
31
# File 'lib/gemwarrior/inventory.rb', line 29

def an_words
  ['herb']
end

#article_chooser(word) ⇒ Object



33
34
35
# File 'lib/gemwarrior/inventory.rb', line 33

def article_chooser(word)
  (VOWELS.include?(word[0]) or an_words.include?(word)) ? 'an' : 'a'
end

#contains_battle_item?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# 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) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/gemwarrior/inventory.rb', line 121

def contains_item?(item_name)
  self.items.map{ |i| i.name.downcase }.include?(item_name.downcase)
end

#contentsObject

non-English-like contents of inventory for simple lists



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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) ⇒ Object



226
227
228
229
230
231
232
233
234
# 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) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/gemwarrior/inventory.rb', line 37

def is_empty?
  self.items.nil? || self.items.empty?
end

#list_battle_itemsObject



133
134
135
136
137
138
139
# 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_contentsObject

English-like sentence summary of inventory



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



236
237
238
239
240
241
# 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) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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