Class: Adventure::Inventory

Inherits:
Object
  • Object
show all
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 kg can be carryied by point of Strength score, before encumberance sets it.

7.0
DRAG_KG_PER_STR =

How many kg can 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

Instance Method Summary collapse

Constructor Details

#initialize(*items, **opts) ⇒ Inventory

Create a new instance of Inventory.

Accepts only Adventure::Item's as contents.

Parameters:

  • items (Array<Item>)

    A list of comma-separated Adventure::Items.

  • opts (Hash)

    A list of optional parameters.

Options Hash (**opts):

  • str (Float)

    The Strength score of this Adventure::Inventory's holder.

  • size (Symbol)

    The size of this Adventure::Inventory's holder.

  • modifier (Float)

    Any other miscellaneous modifier to the carrying capacity.

See Also:



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_capacityObject (readonly)

This inventory's maximum carrying capacity, in kilograms.



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

def carrying_capacity
  @carrying_capacity
end

#drag_weightObject (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.

Parameters:

Raises:

  • (ArgumentError)


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_weightFloat

Get the total carried weight of the Inventory.

Returns:

  • (Float)

    The total carried weight, in kilograms.



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.

Returns:

  • (Boolean)

    Whether or not this Inventory must be dragged to be moved.



124
125
126
# File 'lib/adventure/inventory.rb', line 124

def dragging?
  carried_weight > @drag_weight
end

#eachObject

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.

Returns:

  • (Boolean)

    Whether or not this Inventory is emposing 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.

Parameters:

  • item (String)

    A full or partial name to search an Adventure::Item by.

Returns:

  • (Item, nil)

    Either the first Adventure::Item whose name matches the given String, or nil if there is no match.



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.

Parameters:

  • separator (String) (defaults to: ', ')

    The string to join the array by.

Returns:

  • (String)

    A string of the name of each Adventure::Item, separated by the given String.



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.

Parameters:

  • item (String)

    A full or partial name to search the Adventure::Item to be removed by.

Returns:



156
157
158
159
# File 'lib/adventure/inventory.rb', line 156

def remove(item)
  trash = get(item)
  @items.delete trash
end

#to_aArray<String>

Return an Array equivalent of the Inventory. This Array is a list of the name of each Item within.

Returns:

  • (Array<String>)

    An Array of Strings, each String being the name of an Adventure::Item within.



75
76
77
# File 'lib/adventure/inventory.rb', line 75

def to_a
  @items.map(&:name)
end

#to_fFloat

Alias to carried_weight

Returns:

  • (Float)

    The total carried weight, in kilograms.



106
107
108
# File 'lib/adventure/inventory.rb', line 106

def to_f
  carried_weight
end

#to_iInteger

Return the number of items within the Inventory.

Returns:

  • (Integer)

    The number of items within the Inventory.



92
93
94
# File 'lib/adventure/inventory.rb', line 92

def to_i
  @items.count
end

#to_sString

Return a String with the number of items within.

Returns:

  • (String)

    The String "Inventory containing X item(s)", with X being the item count and "item" word being pluralized accordingly.



67
68
69
# File 'lib/adventure/inventory.rb', line 67

def to_s
  "Inventory containing #{@items.count} item#{'s' if @items.count > 1}"
end