class Twisty::Room

This class models the locations within the game.

Attributes

desc[R]

(String) Description printed when the player types “look”

doors[R]

(Hash{String => Symbol}) Names and keys in Engine.rooms of the locations the player can move from

items[RW]

(Array[String]) Items in the room. Does not include Items stored inside other Items

name[R]

(String) Short name shown in the prompt

Public Class Methods

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

Initializer. Please use Twisty::Engine::define_room() instead.

id

(Symbol) that represents the Room in Engine.rooms()

name

(String) Name shown to the player in the prompt

desc

(String) Long description shown when the player types “look”

# File lib/twisty/room.rb, line 39
def initialize(id, name, desc)
        @id = id
        @name = name
        @desc = desc
        @doors = {}
        @items = []
end

Public Instance Methods

add_door(to, name) click to toggle source

Adds an exit from this room to another NOTE For most purposes Engine.add_door() would be more convinient NOTE The “door” only works in one direction and that the exit is referred to by the String name, not the name of the other Room.

to

(Symbol) Key in Engine.rooms of the location the player will be moved to if “walk” is sucessfully executed

name

(String) Name displayed by “look” and typed as part of “walk”. This can be a word such as “East” instead of the destination's actual name

# File lib/twisty/room.rb, line 88
def add_door(to, name)
        if name.split().length != 1
                raise GameError.new "Door name must be one word"
        elsif @doors.has_key? name
                raise GameError.new "Door with name #{name} already exist in #{@id}"
        else
                @doors[to] = name
        end

        return nil
end
add_item(id) click to toggle source

Places an Item within a the Room. Note that is must first be defined with Twisty::Engine::define_item()

id

(Symbol) Key in Engine.items for the Item that will be added to the Room

# File lib/twisty/room.rb, line 106
def add_item(id)
        if @items.include? id
                raise GameError.new "Duplicate item in room #{@id}"
        else
                if $engine.items.include? id
                        @items << id
                else
                        raise GameError.new "Attempt to add invalid item #{id} to room #{@id}"
                end
        end
end
clone(other) click to toggle source

(Room)

Creates another Item identical to this instance

other

(Symbol) Index of the new instance of Item

# File lib/twisty/room.rb, line 53
def clone(other)
        return engine.define_room(other, @name, @desc)
end
enter_event() click to toggle source

(Boolean)

Executed when the player attempts to enter the Room. This can be redefined on a per-instance basis using Room.on_enter(&block). If this returns false the player will be blocked from entering

# File lib/twisty/room.rb, line 63
def enter_event
        return true
end
exit_event() click to toggle source

(Boolean)

Executed when the player attempts to exit the Room. This can be redefined on a per-instance basis using on_exit(&block). If this returns false the player will be blocked from exiting

# File lib/twisty/room.rb, line 73
def exit_event
        return true
end
look() click to toggle source

Prints the description of the Room and its contents / exits if applicable.

# File lib/twisty/room.rb, line 141
def look
        puts @desc

        if @items.length == 0
                puts "Nothing is located here"
        elsif @items.length == 1
                puts

                puts "Located here is:"
                puts engine.items[@items[0]].name
        else
                puts

                puts "Located here are:"
                @items.each{ |id| puts engine.items[id].name }
        end

                puts

        if doors.length == 0
                puts "There is no exit"
        elsif doors.length == 1
                puts "The only exit is: #{@doors.values[0]}"
        else
                puts "The exits are:"
                @doors.values.each{|name| puts name}
        end

        return nil
end
on_enter(&block) click to toggle source

Used to redefine enter_event

&block

(Proc => Boolean) Code executed on entering the Room. NOTE If this returns false the player “walk” will fail

# File lib/twisty/room.rb, line 123
def on_enter(&block)
        self.define_singleton_method(:enter_event, &block)
        return nil
end
on_exit(&block) click to toggle source

Used to redefine exit_event

&block

(Proc => Boolean) Code executed on exiting the Room. NOTE If this returns false the player “walk” will fail

# File lib/twisty/room.rb, line 133
def on_exit(&block)
        self.define_singleton_method(:exit_event, &block)
        return nil
end