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:



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

Returns the value of attribute carrying_capacity.



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

def carrying_capacity
  @carrying_capacity
end

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

Parameters:

Raises:

  • (ArgumentError)


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_weightFloat

Get the total carried weight of the Inventory.

Returns:

  • (Float)

    The total carried weight, in kilograms.



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.

Returns:

  • (Boolean)

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



126
127
128
# File 'lib/adventure/inventory.rb', line 126

def dragging?
  carried_weight > @drag_weight
end

#eachObject

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.

Returns:

  • (Boolean)

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

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.



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.

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.



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.

Parameters:

  • item (String)

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

Returns:



158
159
160
161
# File 'lib/adventure/inventory.rb', line 158

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.



77
78
79
# File 'lib/adventure/inventory.rb', line 77

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

#to_fFloat

Alias to carried_weight

Returns:

  • (Float)

    The total carried weight, in kilograms.



108
109
110
# File 'lib/adventure/inventory.rb', line 108

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.



94
95
96
# File 'lib/adventure/inventory.rb', line 94

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.



69
70
71
# File 'lib/adventure/inventory.rb', line 69

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