Class: Gemwarrior::Inventory

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

Constant Summary collapse

ERROR_ITEM_REMOVE_INVALID =

CONSTANTS ERRORS

'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 thing, wouldn\'t it? Well, it\'s not so great for you right now.'
ERROR_ITEM_ADD_INVALID =
'That item does not exist here.'
ERROR_ITEM_DESCRIBE_INVALID =
'You do not possess that.'
ERROR_ITEM_EQUIP_INVALID =
'You do not have anything called that to equip.'
ERROR_ITEM_EQUIP_NONWEAPON =
'That cannot be equipped as a weapon.'
ERROR_ITEM_UNEQUIP_INVALID =
'You do not have anything called that to unequip.'
ERROR_ITEM_UNEQUIP_NONWEAPON =
'That cannot be unequipped.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Inventory.



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

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

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



17
18
19
# File 'lib/gemwarrior/inventory.rb', line 17

def items
  @items
end

#weaponObject

Returns the value of attribute weapon.



17
18
19
# File 'lib/gemwarrior/inventory.rb', line 17

def weapon
  @weapon
end

Instance Method Details

#add_item(cur_loc, item_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gemwarrior/inventory.rb', line 80

def add_item(cur_loc, item_name)
  cur_loc.items.each do |i|
    if i.name.eql?(item_name)
      if i.takeable
        items.push(i)
        cur_loc.remove_item(item_name)
        return "Added #{item_name} to your increasing collection of bits of tid."
      else
        return ERROR_ITEM_ADD_UNTAKEABLE
      end
    end
  end
  ERROR_ITEM_ADD_INVALID
end

#describe_item(item_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gemwarrior/inventory.rb', line 32

def describe_item(item_name)
  if items.map(&:name).include?(item_name)
    items.each do |i|
      if i.name.eql?(item_name)
        return i.description
      end
    end
  else
    ERROR_ITEM_DESCRIBE_INVALID
  end
end

#equip_item(item_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gemwarrior/inventory.rb', line 44

def equip_item(item_name)
  if items.map(&:name).include?(item_name)
    items.each do |i|
      if i.name.eql?(item_name)
        if i.equippable
          i.equipped = true
          self.weapon = i
          return "The #{i.name} has taken charge, and been equipped."
        else
          ERROR_ITEM_EQUIP_NONWEAPON
        end
      end
    end
  else
    ERROR_ITEM_EQUIP_INVALID
  end
end

#list_contentsObject



24
25
26
27
28
29
30
# File 'lib/gemwarrior/inventory.rb', line 24

def list_contents
  if items.empty?
    return contents_text = '[empty]'
  else
    return contents_text = "#{items.map(&:name).join ', '}"
  end
end

#remove_item(item_name) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/gemwarrior/inventory.rb', line 95

def remove_item(item_name)
  if items.map(&:name).include?(item_name)
    items.reject! { |item| item.name == item_name }
    return "The #{item_name} has been thrown on the ground, but far out of reach, and you're much too lazy to go get it now, so it's as good as gone."
  else
    ERROR_ITEM_REMOVE_INVALID
  end
end

#unequip_item(item_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gemwarrior/inventory.rb', line 62

def unequip_item(item_name)
  if items.map(&:name).include?(item_name)
    items.each do |i|
      if i.name.eql?(item_name)
        if i.equippable
          i.equipped = false
          self.weapon = nil
          return "The #{i.name} has been demoted to unequipped."
        else
          ERROR_ITEM_UNEQUIP_NONWEAPON
        end
      end
    end
  else
    ERROR_ITEM_UNEQUIP_INVALID
  end
end