Class: Twisty::Item
Overview
This class models objects that (movable and otherwise) that the character can interact with.
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
(
Array[Symbol]) Other Item this instance is currently storiing. -
#desc ⇒ Object
readonly
(
String) Description printed when the Item is looked at. -
#fixed ⇒ Object
readonly
(
Boolean) Indicates if the Item can not be moved. -
#name ⇒ Object
readonly
(
String) One-word name of the Item. -
#storage ⇒ Object
readonly
(
Integer) Number of other Items this instance can store.
Attributes inherited from Entity
Instance Method Summary collapse
-
#add_item(item) ⇒ Object
Stores another Item represented by the Symbol item and stores it within this Item if: * storage > 0 * contents.size < 0 * insert_event() returns true.
-
#clone(other) ⇒ Object
(
Item). -
#contains_item?(item) ⇒ Boolean
(
Boolean). -
#del_item(item) ⇒ Object
Removes an Item contained within this instance NOTE This does not place the Item in the player’s inventory.
-
#drop_event ⇒ Object
(
Boolean). -
#initialize(id, name, desc, fixed, storage) ⇒ Item
constructor
Initialiser.
-
#look ⇒ Object
Prints the description and the names of the contents if applicable.
-
#on_drop(&block) ⇒ Object
Redefines Item::drop_event for this specific instance.
-
#on_put_content(&block) ⇒ Object
Redefines Item::put_content_event for this specific instance.
-
#on_take(&block) ⇒ Object
Redefines Item::take_event for this specific instance.
-
#on_take_content(&block) ⇒ Object
Redefines Item::take_content_event for this specific instance.
-
#put_content_event(item) ⇒ Object
(
Boolean). -
#take_content_event(item) ⇒ Object
(
Boolean). -
#take_event ⇒ Object
(
Boolean).
Methods inherited from Entity
Constructor Details
#initialize(id, name, desc, fixed, storage) ⇒ Item
Initialiser. Please use Engine.define_item() instead
- id
-
(
Symbol) Key in Engine.items of the Item - name
-
(
String) One-word name of the Item - desc
-
A long description of the Item printed when the player uses
"look at item"
- fixed
-
(
Boolean) If set tofalsethe Item can not be taken - storage
-
(
Integer) The number of other Items this instance can hold
43 44 45 46 47 48 49 50 |
# 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 |
Instance Attribute Details
#contents ⇒ Object (readonly)
(Array[Symbol]) Other Item this instance is currently storiing
33 34 35 |
# File 'lib/twisty/item.rb', line 33 def contents @contents end |
#desc ⇒ Object (readonly)
(String) Description printed when the Item is looked at.
27 28 29 |
# File 'lib/twisty/item.rb', line 27 def desc @desc end |
#fixed ⇒ Object (readonly)
(Boolean) Indicates if the Item can not be moved
29 30 31 |
# File 'lib/twisty/item.rb', line 29 def fixed @fixed end |
#name ⇒ Object (readonly)
(String) One-word name of the Item
25 26 27 |
# File 'lib/twisty/item.rb', line 25 def name @name end |
#storage ⇒ Object (readonly)
(Integer) Number of other Items this instance can store
31 32 33 |
# File 'lib/twisty/item.rb', line 31 def storage @storage end |
Instance Method Details
#add_item(item) ⇒ Object
Stores another Item represented by the Symbol item and stores it within this Item if:
-
storage > 0
-
contents.size < 0
-
insert_event() returns true
138 139 140 141 142 143 144 145 146 147 148 149 |
# 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 |
#clone(other) ⇒ Object
(Item)
Creates another Item identical to this instance
- other
-
(
Symbol) Index of the new instance of Item
58 59 60 61 |
# File 'lib/twisty/item.rb', line 58 def clone(other) return engine.define_item(other, @name, @desc, :fixed => @fixed, :storage => @storage) end |
#contains_item?(item) ⇒ Boolean
(Boolean)
Returns true if this item contains another represented by the symbol item
128 129 130 |
# File 'lib/twisty/item.rb', line 128 def contains_item?(item) return @contents.include?(item) if @storage > 0 end |
#del_item(item) ⇒ Object
Removes an Item contained within this instance NOTE This does not place the Item in the player’s inventory
- item
-
(
Symbol) Key in Engine.items of the Item being placed in this
instance of Item
157 158 159 160 161 162 163 164 |
# 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 |
#drop_event ⇒ Object
(Boolean)
Executed when the Item is picked up. This function can be redefined on a per-instance basis using on_drop(&block). If this returns false drop will fail.
78 79 80 |
# File 'lib/twisty/item.rb', line 78 def drop_event return true end |
#look ⇒ Object
Prints the description and the names of the contents if applicable
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# 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 |
#on_drop(&block) ⇒ Object
Redefines Item::drop_event for this specific instance
- &block
-
(
Proc=>Boolean) The new version of Item.drop_event for
this specific instance *NOTE* Must return a +Boolean+
181 182 183 184 |
# File 'lib/twisty/item.rb', line 181 def on_drop(&block) self.define_singleton_method(:drop_event, &block) return nil end |
#on_put_content(&block) ⇒ Object
Redefines Item::put_content_event for this specific instance
- &block
-
(
Proc=>Boolean) The new version of
Item.put_content_event for this specific instance.
*NOTE* Must return a +Boolean+
192 193 194 195 |
# File 'lib/twisty/item.rb', line 192 def on_put_content(&block) self.define_singleton_method(:put_content_event, &block) return nil end |
#on_take(&block) ⇒ Object
Redefines Item::take_event for this specific instance
- &block
-
(
Proc=>Boolean) The new version of Item.take_event for
this specific instance *NOTE* Must return a +Boolean+
171 172 173 174 |
# File 'lib/twisty/item.rb', line 171 def on_take(&block) self.define_singleton_method(:take_event, &block) return nil end |
#on_take_content(&block) ⇒ Object
Redefines Item::take_content_event for this specific instance
- &block
-
(
Proc=>Boolean) The new version of
Item.take_content_event for this specific instance.
*NOTE* Must return a +Boolean+
203 204 205 206 |
# File 'lib/twisty/item.rb', line 203 def on_take_content(&block) self.define_singleton_method(:take_content_event, &block) return nil end |
#put_content_event(item) ⇒ Object
(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 in Engine.items of the item being added
90 91 92 |
# File 'lib/twisty/item.rb', line 90 def put_content_event(item) return true end |
#take_content_event(item) ⇒ Object
(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 in Engine.items of the item being taken
101 102 103 |
# File 'lib/twisty/item.rb', line 101 def take_content_event(item) return true end |
#take_event ⇒ Object
(Boolean)
Executed when the Item is picked up. This function can be redefined on a per-instance basis using on_take(&block). If this returns false take will fail
68 69 70 |
# File 'lib/twisty/item.rb', line 68 def take_event return true end |