Class: Core::Game::Inventory

Inherits:
Array show all
Defined in:
lib/game/inventory.rb

Overview

strength is an arbitrary unit; in this context it’s 1 str == 1 kg of carrying

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#to_color, #x, #y

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

#capacityObject

Returns the value of attribute capacity.



5
6
7
# File 'lib/game/inventory.rb', line 5

def capacity
  @capacity
end

#strengthObject

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

#weightObject



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