class Twisty::Item
This class models objects that (movable and otherwise) that the character can interact with.
Attributes
(Array
[Symbol
]) Other Item
this instance is currently storiing
(String
) Description printed when the Item
is looked at.
(Boolean
) Indicates if the Item
can not be moved
(String
) One-word name of the Item
(Integer
) Number of other Items this instance can store
Public Class Methods
Initialiser. Please use Engine.define_item()
instead
- id
-
(
Symbol
) Key inEngine.items
of theItem
- name
-
(
String
) One-word name of theItem
- desc
-
A long description of the
Item
printed when the player uses “look at item” - fixed
-
(
Boolean
) If set tofalse
theItem
can not be taken - storage
-
(
Integer
) The number of other Items this instance can hold
# File lib/twisty/item.rb, line 43 def initialize(id, name, desc, fixed, storage) @id = id @name = name @desc = desc @fixed = fixed @storage = storage @contents = [] end
Public Instance Methods
Stores another Item
represented by the Symbol item and stores it within this Item
if:
* storage > 0 * contents.size < 0 * insert_event() returns true
# File lib/twisty/item.rb, line 138 def add_item(item) if @storage == 0 raise GameError.new "#{@id} is not a container" else #Skip if already contained if contains_item?(item) == false @contents << item end end return nil end
(Boolean
)
Returns true
if this item contains another represented by the symbol item
# File lib/twisty/item.rb, line 128 def contains_item?(item) return @contents.include?(item) if @storage > 0 end
Removes an Item
contained within this instance NOTE This does not place the Item
in the player's inventory
- item
-
(
Symbol
) Key inEngine.items
of theItem
being placed in this instance ofItem
# File lib/twisty/item.rb, line 157 def del_item(item) #Skip if already contained if contains_item?(item) @contents.delete item end return nil end
Prints the description and the names of the contents if applicable
# File lib/twisty/item.rb, line 107 def look puts @desc if @storage > 0 puts "Contents:" if @contents.length == 0 puts "Nothing" else @contents.each{|id| puts engine.items[id].name} end end return nil end
Redefines Item::drop_event for this specific instance
- &block
-
(
Proc
=>Boolean
) The new version ofItem.drop_event
for this specific instance NOTE Must return aBoolean
# File lib/twisty/item.rb, line 181 def on_drop(&block) self.define_singleton_method(:drop_event, &block) return nil end
Redefines Item::put_content_event for this specific instance
- &block
-
(
Proc
=>Boolean
) The new version ofItem.put_content_event
for this specific instance. NOTE Must return aBoolean
# File lib/twisty/item.rb, line 192 def on_put_content(&block) self.define_singleton_method(:put_content_event, &block) return nil end
Redefines Item::take_event for this specific instance
- &block
-
(
Proc
=>Boolean
) The new version ofItem.take_event
for this specific instance NOTE Must return aBoolean
# File lib/twisty/item.rb, line 171 def on_take(&block) self.define_singleton_method(:take_event, &block) return nil end
Redefines Item::take_content_event for this specific instance
- &block
-
(
Proc
=>Boolean
) The new version ofItem.take_content_event
for this specific instance. NOTE Must return aBoolean
# File lib/twisty/item.rb, line 203 def on_take_content(&block) self.define_singleton_method(:take_content_event, &block) return nil end
(Boolean
)
Executed when another Item
is placed inside this instance. This function can be redefined on a per-instance basis using on_add(&block). If this return false add_item
will fail
- item
-
(
Symbol
) Key inEngine.items
of the item being added
# File lib/twisty/item.rb, line 90 def put_content_event(item) return true end
(Boolean
)
Executed an Item
inside this instance of Item
is removed by the player. If this returns false “take X from Y” will fail.
- item
-
(
Symbol
) Key inEngine.items
of the item being taken
# File lib/twisty/item.rb, line 101 def take_content_event(item) return true end