module AdventureRL::Modifiers::Inventory
Constants
- DEFAULT_INVENTORY_ID
Public Class Methods
# File lib/AdventureRL/Modifiers/Inventory.rb, line 6 def initialize *args @inventory = {} super end
Public Instance Methods
Add any object to this Inventory
. Pass an optional id
, which can be used to access or remove the object afterwards.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 14 def add_object object, id = DEFAULT_INVENTORY_ID @inventory[id] = [] unless (@inventory[id]) @inventory[id] << object end
Returns true, if object with id
has been added to this Inventory
. id
can also be the object itself.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 24 def added_object? id return ( @inventory.key?(id) || get_objects.include?(id) ) end
Returns the last object with the given id
. If no id
is passed, return the last object with the unnamed id
.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 44 def get_object id = DEFAULT_INVENTORY_ID return @inventory[id].last end
Returns all its objects. If optional argument id
is passed, then return all objects with that id
.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 37 def get_objects id = nil return @inventory.values.flatten unless (id) return @inventory[id] end
Removes the last object with the given id
. id
can also be the object to be removed itself. If no id
is given, remove the last object with the unnamed id
.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 81 def remove_object id = DEFAULT_INVENTORY_ID, call_removed_method = true if (@inventory.key? id) object = @inventory[id].delete_at(-1) object.removed if (call_removed_method && object.methods.include?(:removed)) return object end key = (@inventory.detect do |k, val| next val.include?(id) end || []) .first return nil unless (@inventory.key? key) object = @inventory[key].delete id object.removed if (call_removed_method && object.methods.include?(:removed)) return object end
Removes all objects with the given id
. id
can also be an added object itself; all objects with the same id
will be removed. If no id
is given, remove all objects.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 53 def remove_objects id = nil unless (id) get_objects.each do |object| object.removed if (object.methods.include? :removed) end return @inventory.clear end if (@inventory.key? id) objects = @inventory.delete(id) objects.each do |object| object.removed if (object.methods.include? :removed) end return objects end return @inventory.delete((@inventory.detect do |key, val| if (id == val) @inventory[key].each do |object| object.removed if (object.methods.include? :removed) end next true end next false end || []).first) end
When removed is called on an object that has an Inventory
, then also call remove_objects
on that object.
# File lib/AdventureRL/Modifiers/Inventory.rb, line 99 def removed remove_objects end