class Twisty::Room
This class models the locations within the game.
Attributes
(String
) Description printed when the player types “look”
(Hash
{String
=> Symbol
}) Names and keys in Engine.rooms
of the locations the player can move from
(Array
[String
]) Items in the room. Does not include Items stored inside other Items
(String
) Short name shown in the prompt
Public Class Methods
Initializer. Please use Twisty::Engine::define_room() instead.
- id
-
(
Symbol
) that represents theRoom
inEngine.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
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 inEngine.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
Places an Item
within a the Room
. Note that is must first be defined with Twisty::Engine::define_item()
- id
-
(
Symbol
) Key inEngine.items
for theItem
that will be added to theRoom
# 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
(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
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
Used to redefine enter_event
- &block
-
(
Proc
=>Boolean
) Code executed on entering theRoom
. NOTE If this returnsfalse
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
Used to redefine exit_event
- &block
-
(
Proc
=>Boolean
) Code executed on exiting theRoom
. NOTE If this returnsfalse
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