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, player) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gemwarrior/inventory.rb', line 84

def add_item(cur_loc, item_name, player)
  cur_loc.items.each do |i|
    if i.name.eql?(item_name)
      if i.takeable
        items.push(i)
        cur_loc.remove_item(item_name)

        # stats
        player.items_taken += 1

        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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gemwarrior/inventory.rb', line 36

def describe_item(item_name)
  if has_item?(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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gemwarrior/inventory.rb', line 48

def equip_item(item_name)
  if has_item?(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

#has_item?(item_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_item?(item_name)
  items.map(&:name).include?(item_name)
end

#list_contentsObject



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

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

#remove_item(item_name) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/gemwarrior/inventory.rb', line 103

def remove_item(item_name)
  if has_item?(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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gemwarrior/inventory.rb', line 66

def unequip_item(item_name)
  if has_item?(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