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
This inventory's maximum carrying capacity, in kilograms.
-
#drag_weight ⇒ Object
readonly
This inventory's maximum dragging/lifting weight, in kilograms.
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.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/adventure/inventory.rb', line 46 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)
This inventory's maximum carrying capacity, in kilograms.
32 33 34 |
# File 'lib/adventure/inventory.rb', line 32 def @carrying_capacity end |
#drag_weight ⇒ Object (readonly)
This inventory's maximum dragging/lifting weight, in kilograms.
34 35 36 |
# File 'lib/adventure/inventory.rb', line 34 def drag_weight @drag_weight end |
Instance Method Details
#add(item) ⇒ Object
Include a new item to the Adventure::Inventory.
142 143 144 145 146 |
# File 'lib/adventure/inventory.rb', line 142 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.
99 100 101 |
# File 'lib/adventure/inventory.rb', line 99 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.
124 125 126 |
# File 'lib/adventure/inventory.rb', line 124 def dragging? carried_weight > @drag_weight end |
#each ⇒ Object
Run each blocks for each item in the Inventory.
60 61 62 |
# File 'lib/adventure/inventory.rb', line 60 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.
115 116 117 |
# File 'lib/adventure/inventory.rb', line 115 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.
135 136 137 |
# File 'lib/adventure/inventory.rb', line 135 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.
85 86 87 |
# File 'lib/adventure/inventory.rb', line 85 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.
156 157 158 159 |
# File 'lib/adventure/inventory.rb', line 156 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.
75 76 77 |
# File 'lib/adventure/inventory.rb', line 75 def to_a @items.map(&:name) end |
#to_f ⇒ Float
Alias to carried_weight
106 107 108 |
# File 'lib/adventure/inventory.rb', line 106 def to_f carried_weight end |
#to_i ⇒ Integer
Return the number of items within the Inventory.
92 93 94 |
# File 'lib/adventure/inventory.rb', line 92 def to_i @items.count end |
#to_s ⇒ String
Return a String with the number of items within.
67 68 69 |
# File 'lib/adventure/inventory.rb', line 67 def to_s "Inventory containing #{@items.count} item#{'s' if @items.count > 1}" end |