Class: Room

Inherits:
Object
  • Object
show all
Defined in:
lib/room.rb

Constant Summary collapse

DEFAULT_COMMANDS =
[["look".commandify, :look], ["l".commandify, :look], ["look_around".commandify, :look]]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_command(regexp, method) ⇒ Object



216
217
218
# File 'lib/room.rb', line 216

def add_command regexp, method
  commands << [regexp, method]
end

.commandsObject



159
160
161
162
# File 'lib/room.rb', line 159

def commands
  $commands ||= {}
  $commands[key] ||= DEFAULT_COMMANDS.dup
end

.do(action) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/room.rb', line 187

def do action
  if action == "debug!"
    Printer.puts
    Printer.puts $state.inspect
  elsif action == "restart!"
    $state = { :here => $starting_room }
    Printer.puts
    Printer.puts "    ....."
    Printer.puts "Your mind, a wild monkey."
    Printer.puts "        ....,.,"
    Room.do "look"
  elsif action == "reload!"
    reload!
    Printer.puts
    Printer.puts "A great wave of relief washes over you."
  elsif here
    here.do action
  else
    Printer.puts
    Printer.puts "Where am I?"
  end
end

.dup(*cmds) ⇒ Object



210
211
212
213
214
# File 'lib/room.rb', line 210

def dup *cmds
  cmds.each do |cmd|
    alias_method cmd, $last_command
  end
end

.go(key, look = true) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/room.rb', line 178

def go key, look = true
  if room key
    $state[:here] = key
    "\n" + here.look if look
  else
    here.unknown_room key
  end
end

.hereObject



174
175
176
# File 'lib/room.rb', line 174

def here
  room $state[:here]
end

.inherited(subclass) ⇒ Object



220
221
222
223
224
# File 'lib/room.rb', line 220

def inherited subclass
  room_keys[subclass.key] = subclass
  $starting_room ||= subclass.key
  $state[:here] ||= $starting_room
end

.keyObject



155
156
157
# File 'lib/room.rb', line 155

def key
  self.to_s.underscore
end

.method_added(name) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/room.rb', line 226

def method_added name
  if public_instance_methods.include? name.to_s
    $last_command = name
    
    if /^look(.+)/ =~ name.to_s
      %w{ look l look_at look_at_the examine ex }.each do |prefix|
        add_command "#{prefix}#{$1}".commandify, name
      end
    elsif /^enter(.+)/ =~ name.to_s
      %w{ enter enter_the go_into go_into_the }.each do |prefix|
        add_command "#{prefix}#{$1}".commandify, name
      end
    else
      add_command name.to_s.commandify, name
    end
  end
end

.room(key) ⇒ Object



164
165
166
167
168
# File 'lib/room.rb', line 164

def room key
  $state[:rooms] ||= {}
  $state[:rooms][key] = room_keys[key].new if $state[:rooms][key].nil? && room_keys[key]
  $state[:rooms][key]
end

.room_keysObject



170
171
172
# File 'lib/room.rb', line 170

def room_keys
  $room_keys ||= {}
end

Instance Method Details

#do(action) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/room.rb', line 123

def do action
  Printer.puts
  if action.strip == ""
  elsif (r = self.class.commands.detect { |c, m| c =~ action })
    command, method = r
    args = command.match(action).to_a.drop(1)
    Printer.puts self.send(method, *args).to_s.rstrip
  else
    Printer.puts huh?(action)
  end
end

#go(key) ⇒ Object



95
96
97
# File 'lib/room.rb', line 95

def go key
  Room.go key
end

#have?(item) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/room.rb', line 111

def have? item
  inventory.include? item
end

#huh?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/room.rb', line 143

def huh? action = nil
  "I don't understand."
end

#immediate(*text) ⇒ Object



135
136
137
# File 'lib/room.rb', line 135

def immediate *text
  Printer.puts *text
end

#inventoryObject



107
108
109
# File 'lib/room.rb', line 107

def inventory
  $state[:inventory] ||= []
end

#lookObject



139
140
141
# File 'lib/room.rb', line 139

def look
  "A nondescript room."
end

#lose(item) ⇒ Object



119
120
121
# File 'lib/room.rb', line 119

def lose item
  inventory.delete item
end

#no_echoObject



103
104
105
# File 'lib/room.rb', line 103

def no_echo
  $secretive = true
end

#quietly_go(key) ⇒ Object



99
100
101
# File 'lib/room.rb', line 99

def quietly_go key
  Room.go key, false
end

#take(item) ⇒ Object



115
116
117
# File 'lib/room.rb', line 115

def take item
  inventory << item
end

#unknown_room(key) ⇒ Object



147
148
149
150
151
152
# File 'lib/room.rb', line 147

def unknown_room key
  "" |
  "The fourth wall falls over and you realize you didn't really" |
  "want to go to '#{key}' anyway. You decide to let the author" |
  "know about this terrible oversight as soon as possible."
end