class Twisty::Item

This class models objects that (movable and otherwise) that the character can interact with.

Attributes

contents[R]

(Array[Symbol]) Other Item this instance is currently storiing

desc[R]

(String) Description printed when the Item is looked at.

fixed[R]

(Boolean) Indicates if the Item can not be moved

name[R]

(String) One-word name of the Item

storage[R]

(Integer) Number of other Items this instance can store

Public Class Methods

new(id, name, desc, fixed, storage) click to toggle source

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 to false the Item 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

add_item(item) click to toggle source

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
clone(other) click to toggle source

(Item)

Creates another Item identical to this instance

other

(Symbol) Index of the new instance of Item

# 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) click to toggle source

(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
del_item(item) click to toggle source

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

# 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() click to toggle source

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

# File lib/twisty/item.rb, line 78
def drop_event
        return true
end
look() click to toggle source

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
on_drop(&block) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

(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

# File lib/twisty/item.rb, line 90
def put_content_event(item)
        return true
end
take_content_event(item) click to toggle source

(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

# File lib/twisty/item.rb, line 101
def take_content_event(item)
        return true
end
take_event() click to toggle source

(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

# File lib/twisty/item.rb, line 68
def take_event
        return true
end