Class: Twisty::Room

Inherits:
Entity show all
Defined in:
lib/twisty/room.rb

Overview

This class models the locations within the game.

Instance Attribute Summary collapse

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

#==, #engine

Constructor Details

#initialize(id, name, desc) ⇒ Room

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”



39
40
41
42
43
44
45
# File 'lib/twisty/room.rb', line 39

def initialize(id, name, desc)
  @id = id
  @name = name
  @desc = desc
  @doors = {}
  @items = []
end

Instance Attribute Details

#descObject (readonly)

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



25
26
27
# File 'lib/twisty/room.rb', line 25

def desc
  @desc
end

#doorsObject (readonly)

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



28
29
30
# File 'lib/twisty/room.rb', line 28

def doors
  @doors
end

#itemsObject

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



31
32
33
# File 'lib/twisty/room.rb', line 31

def items
  @items
end

#nameObject (readonly)

(String) Short name shown in the prompt



23
24
25
# File 'lib/twisty/room.rb', line 23

def name
  @name
end

Instance Method Details

#add_door(to, name) ⇒ Object

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


88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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


106
107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object

(Room)

Creates another Item identical to this instance

other

(Symbol) Index of the new instance of Item



53
54
55
# File 'lib/twisty/room.rb', line 53

def clone(other)
  return engine.define_room(other, @name, @desc)
end

#enter_eventObject

(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



63
64
65
# File 'lib/twisty/room.rb', line 63

def enter_event
  return true
end

#exit_eventObject

(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



73
74
75
# File 'lib/twisty/room.rb', line 73

def exit_event
  return true
end

#lookObject

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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) ⇒ Object

Used to redefine enter_event

&block

(Proc => Boolean) Code executed on entering the Room.

*NOTE* If this returns +false+ the player "walk" will fail


123
124
125
126
# File 'lib/twisty/room.rb', line 123

def on_enter(&block)
  self.define_singleton_method(:enter_event, &block)
  return nil
end

#on_exit(&block) ⇒ Object

Used to redefine exit_event

&block

(Proc => Boolean) Code executed on exiting the Room.

*NOTE* If this returns +false+ the player "walk" will fail


133
134
135
136
# File 'lib/twisty/room.rb', line 133

def on_exit(&block)
  self.define_singleton_method(:exit_event, &block)
  return nil
end