Class: Core::Game::Inventory
Overview
strength is an arbitrary unit; in this context it’s 1 str == 1 kg of carrying
Instance Attribute Summary collapse
-
#capacity ⇒ Object
Returns the value of attribute capacity.
-
#strength ⇒ Object
Returns the value of attribute strength.
Instance Method Summary collapse
- #add(item) ⇒ Object
-
#initialize(capacity, strength) ⇒ Inventory
constructor
A new instance of Inventory.
- #remove(item, times = 1) ⇒ Object
- #weight ⇒ Object
Methods inherited from Array
Constructor Details
#initialize(capacity, strength) ⇒ Inventory
Returns a new instance of Inventory.
6 7 8 9 10 11 |
# File 'lib/game/inventory.rb', line 6 def initialize(capacity, strength) super(capacity) @capacity = capacity @strength = strength self.clear end |
Instance Attribute Details
#capacity ⇒ Object
Returns the value of attribute capacity.
5 6 7 |
# File 'lib/game/inventory.rb', line 5 def capacity @capacity end |
#strength ⇒ Object
Returns the value of attribute strength.
5 6 7 |
# File 'lib/game/inventory.rb', line 5 def strength @strength end |
Instance Method Details
#add(item) ⇒ Object
12 13 14 15 16 |
# File 'lib/game/inventory.rb', line 12 def add(item) if self.size + 1 < @capacity and item.weight + weight < @strength self.push(item) end end |
#remove(item, times = 1) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/game/inventory.rb', line 17 def remove(item, times=1) removed = 0 self.each { |other_item| if other_item.name == item.name self.delete_at(self.index(other_item)) removed += 1 end if removed >= times return end } end |
#weight ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/game/inventory.rb', line 29 def weight weight = 0 self.each { |item| weight += item.weight } return weight end |