Class: Adventure::Inventory
- Inherits:
-
Object
- Object
- Adventure::Inventory
- Includes:
- Enumerable
- Defined in:
- lib/adventure/inventory.rb
Overview
Represents an ingame inventory.
Extends/includes the Enumerable mixin.
Constant Summary collapse
- KG_PER_STR =
How many
kgcan be carryied by point of Strength score, before encumberance sets it. 7.0- DRAG_KG_PER_STR =
How many
kgcan be dragged by point of Strength score. 14.0- SIZE_MODIFIER =
Size modifier for the maximum carrying capacity.
{ fine: 0.125, diminutive: 0.25, tiny: 0.5, small: 0.75, medium: 1.0, large: 2.0, huge: 4.0, gargantuan: 8.0, colossal: 16.0 }.freeze
- SIZES =
Valid sizes.
%w[fine diminutive tiny small medium large huge gargantuan colossal].freeze
Instance Attribute Summary collapse
-
#carrying_capacity ⇒ Object
readonly
Returns the value of attribute carrying_capacity.
-
#drag_weight ⇒ Object
readonly
Returns the value of attribute drag_weight.
Instance Method Summary collapse
-
#add(item) ⇒ Object
Include a new item to the Inventory.
-
#carried_weight ⇒ Float
Get the total carried weight of the Inventory.
-
#dragging? ⇒ Boolean
Get whether or not this Inventory is carrying items with a total weight over its Drag limit --- i.e. the Inventory cannot be carried, only dragged.
-
#each ⇒ Object
Run
eachblocks for each item in the Inventory. -
#encumbered? ⇒ Boolean
Get whether or not this Inventory is carrying items with a total weight over its Carrying Capacity --- i.e. the Inventory imposes encumberance.
-
#get(item) ⇒ Item?
Get the first Item whose name includes the given String.
-
#initialize(*items, **opts) ⇒ Inventory
constructor
Create a new instance of Inventory.
-
#join(separator = ', ') ⇒ String
Fetches the Inventory as an Array (through the inner #to_a method) and returns such Array joined by the given separator.
-
#remove(item) ⇒ Item?
Remove the first Item whose name includes the given String.
-
#to_a ⇒ Array<String>
Return an Array equivalent of the Inventory.
-
#to_f ⇒ Float
Alias to carried_weight.
-
#to_i ⇒ Integer
Return the number of items within the Inventory.
-
#to_s ⇒ String
Return a String with the number of items within.
Constructor Details
#initialize(*items, **opts) ⇒ Inventory
Create a new instance of Inventory.
Accepts only Adventure::Item's as contents.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/adventure/inventory.rb', line 48 def initialize(*items, **opts) # Populate Inventory only with {Item}s @items = items.grep(Item) # Get parameters str = opts.key?(:str) ? opts[:str].to_f : 10 size = opts.key?(:size) ? opts[:size].to_sym : :medium size = :medium unless SIZES.include? size modifier = opts.key?(:modifier) ? opts[:modifier].to_f : 1.0 # Set capacities @carrying_capacity = str * KG_PER_STR * SIZE_MODIFIER[size] * modifier @drag_weight = str * DRAG_KG_PER_STR * SIZE_MODIFIER[size] * modifier end |
Instance Attribute Details
#carrying_capacity ⇒ Object (readonly)
Returns the value of attribute carrying_capacity.
33 34 35 |
# File 'lib/adventure/inventory.rb', line 33 def @carrying_capacity end |
#drag_weight ⇒ Object (readonly)
Returns the value of attribute drag_weight.
36 37 38 |
# File 'lib/adventure/inventory.rb', line 36 def drag_weight @drag_weight end |
Instance Method Details
#add(item) ⇒ Object
Include a new item to the Adventure::Inventory.
144 145 146 147 148 |
# File 'lib/adventure/inventory.rb', line 144 def add(item) raise ArgumentError, 'Item to be added is not an Item.' unless item.is_a? Item @items.push item end |
#carried_weight ⇒ Float
Get the total carried weight of the Inventory.
101 102 103 |
# File 'lib/adventure/inventory.rb', line 101 def carried_weight @items.sum(&:weight) end |
#dragging? ⇒ Boolean
Get whether or not this Inventory is carrying items with a total weight over its Drag limit --- i.e. the Inventory cannot be carried, only dragged.
126 127 128 |
# File 'lib/adventure/inventory.rb', line 126 def dragging? carried_weight > @drag_weight end |
#each ⇒ Object
Run each blocks for each item in the Inventory.
62 63 64 |
# File 'lib/adventure/inventory.rb', line 62 def each(&) @items.each(&) end |
#encumbered? ⇒ Boolean
Get whether or not this Inventory is carrying items with a total weight over its Carrying Capacity --- i.e. the Inventory imposes encumberance.
117 118 119 |
# File 'lib/adventure/inventory.rb', line 117 def encumbered? carried_weight > @carrying_capacity end |
#get(item) ⇒ Item?
Get the first Adventure::Item whose name includes the given String.
This method is case-insensitive.
137 138 139 |
# File 'lib/adventure/inventory.rb', line 137 def get(item) @items.find { |i| i.name.downcase.include? item.downcase } end |
#join(separator = ', ') ⇒ String
Fetches the Inventory as an Array (through the inner #to_a method) and returns such Array joined by the given separator.
87 88 89 |
# File 'lib/adventure/inventory.rb', line 87 def join(separator = ', ') to_a.join(separator) end |
#remove(item) ⇒ Item?
Remove the first Adventure::Item whose name includes the given String.
This method is case-insensitive and finds the Adventure::Item through the #get method.
158 159 160 161 |
# File 'lib/adventure/inventory.rb', line 158 def remove(item) trash = get(item) @items.delete trash end |
#to_a ⇒ Array<String>
Return an Array equivalent of the Inventory. This Array is a list of the name of each Item within.
77 78 79 |
# File 'lib/adventure/inventory.rb', line 77 def to_a @items.map(&:name) end |
#to_f ⇒ Float
Alias to carried_weight
108 109 110 |
# File 'lib/adventure/inventory.rb', line 108 def to_f carried_weight end |
#to_i ⇒ Integer
Return the number of items within the Inventory.
94 95 96 |
# File 'lib/adventure/inventory.rb', line 94 def to_i @items.count end |
#to_s ⇒ String
Return a String with the number of items within.
69 70 71 |
# File 'lib/adventure/inventory.rb', line 69 def to_s "Inventory containing #{@items.count} item#{'s' if @items.count > 1}" end |