Class: Room
- Inherits:
-
Object
- Object
- Room
- Defined in:
- lib/IFMapper/Room.rb,
lib/IFMapper/locales/en/Messages.rb,
lib/IFMapper/locales/es/Messages.rb,
lib/IFMapper/locales/es/Messages_iso-8859-1.rb
Overview
Class used to represent a Room or Location in a Map.
Direct Known Subclasses
Constant Summary collapse
- DIR_TO_VECTOR =
{ 0 => [ 0, -1 ], 1 => [ 1, -1 ], 2 => [ 1, 0 ], 3 => [ 1, 1 ], 4 => [ 0, 1 ], 5 => [ -1, 1 ], 6 => [ -1, 0 ], 7 => [ -1, -1 ] }
- DIRECTIONS =
[ 'n', 'ne', 'e', 'se', 's', 'so', 'o', 'no', ]
- DIRECTIONS_ENGLISH =
[ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', ]
Instance Attribute Summary collapse
-
#comment ⇒ Object
Room comment.
-
#darkness ⇒ Object
Isxxxxxxxxxx room in darkness?.
-
#desc ⇒ Object
Room description.
-
#exits ⇒ Object
readonly
An array of 8 possible exits in room.
-
#name ⇒ Object
Name of room.
-
#objects ⇒ Object
Objects found in room.
-
#tasks ⇒ Object
Tasks that need to be performed in room.
-
#x ⇒ Object
Room location in grid.
-
#y ⇒ Object
Room location in grid.
Class Method Summary collapse
-
.vector_to_dir(dx, dy) ⇒ Object
Return a direction from the vector of the exit that would take us to the ‘b’ room more cleanly.
Instance Method Summary collapse
- #[](dir) ⇒ Object
- #[]=(dir, connection) ⇒ Object
-
#copy(b) ⇒ Object
Copy a room to another.
-
#exit_to(b) ⇒ Object
Given an ‘adjacent’ room, return the most direct exit from this room to room b.
-
#initialize(x, y, name = 'Room') ⇒ Room
constructor
A new instance of Room.
- #inspect ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(vars) ⇒ Object
-
#next_to?(b) ⇒ Boolean
Check if two rooms are next to each other.
-
#num_doors ⇒ Object
Return the number of doors present in room.
-
#num_exits ⇒ Object
Return the number of exits present in room.
- #to_s ⇒ Object
- #vector_to_dir(dx, dy) ⇒ Object
Constructor Details
#initialize(x, y, name = 'Room') ⇒ Room
Returns a new instance of Room.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/IFMapper/Room.rb', line 143 def initialize(x, y, name = 'Room') @exits = Array.new( DIRECTIONS.size ) @darkness = false @name = name @x = x @y = y @desc = nil @objects = '' @tasks = '' @comment = nil end |
Instance Attribute Details
#comment ⇒ Object
Room comment
13 14 15 |
# File 'lib/IFMapper/Room.rb', line 13 def comment @comment end |
#darkness ⇒ Object
Isxxxxxxxxxx room in darkness?
10 11 12 |
# File 'lib/IFMapper/Room.rb', line 10 def darkness @darkness end |
#desc ⇒ Object
Room description
12 13 14 |
# File 'lib/IFMapper/Room.rb', line 12 def desc @desc end |
#exits ⇒ Object (readonly)
An array of 8 possible exits in room
9 10 11 |
# File 'lib/IFMapper/Room.rb', line 9 def exits @exits end |
#name ⇒ Object
Name of room
6 7 8 |
# File 'lib/IFMapper/Room.rb', line 6 def name @name end |
#objects ⇒ Object
Objects found in room
7 8 9 |
# File 'lib/IFMapper/Room.rb', line 7 def objects @objects end |
#tasks ⇒ Object
Tasks that need to be performed in room
8 9 10 |
# File 'lib/IFMapper/Room.rb', line 8 def tasks @tasks end |
#x ⇒ Object
Room location in grid
11 12 13 |
# File 'lib/IFMapper/Room.rb', line 11 def x @x end |
#y ⇒ Object
Room location in grid
11 12 13 |
# File 'lib/IFMapper/Room.rb', line 11 def y @y end |
Class Method Details
.vector_to_dir(dx, dy) ⇒ Object
Return a direction from the vector of the exit that would take us to the ‘b’ room more cleanly.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/IFMapper/Room.rb', line 82 def self.vector_to_dir(dx, dy) if dx == 0 return 4 if dy > 0 return 0 if dy < 0 raise "vector_to_dir: dx == 0 and dy == 0" elsif dx > 0 return 1 if dy < 0 return 2 if dy == 0 return 3 else return 7 if dy < 0 return 6 if dy == 0 return 5 end end |
Instance Method Details
#[](dir) ⇒ Object
51 52 53 |
# File 'lib/IFMapper/Room.rb', line 51 def [](dir) return @exits[dir] end |
#[]=(dir, connection) ⇒ Object
55 56 57 |
# File 'lib/IFMapper/Room.rb', line 55 def []=(dir, connection) @exits[dir] = connection end |
#copy(b) ⇒ Object
Copy a room to another
134 135 136 137 138 139 140 |
# File 'lib/IFMapper/Room.rb', line 134 def copy(b) @name = b.name @objects = b.objects @tasks = b.tasks @darkness = b.darkness @desc = b.desc end |
#exit_to(b) ⇒ Object
Given an ‘adjacent’ room, return the most direct exit from this room to room b.
106 107 108 109 110 |
# File 'lib/IFMapper/Room.rb', line 106 def exit_to(b) dx = (b.x - @x) dy = (b.y - @y) return vector_to_dir(dx, dy) end |
#inspect ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/IFMapper/Room.rb', line 160 def inspect puts to_s puts "Exits:" @exits.each { |c| puts "\t#{c}" if c != nil } end |
#marshal_dump ⇒ Object
47 48 49 |
# File 'lib/IFMapper/Room.rb', line 47 def marshal_dump [ @name, @objects, @tasks, @exits, @darkness, @x, @y, @desc, @comment ] end |
#marshal_load(vars) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/IFMapper/Room.rb', line 26 def marshal_load(vars) @name = vars.shift @objects = vars.shift @tasks = vars.shift @exits = vars.shift @darkness = vars.shift @x = vars.shift @y = vars.shift @desc = nil @comment = nil if not vars.empty? and vars[0].kind_of?(String) @desc = vars.shift @desc.gsub!(/(\w)\s*\n/, '\1 ') @desc.sub!(/\n+$/, '') @desc.strip! end if not vars.empty? and vars[0].kind_of?(String) @comment = vars.shift end end |
#next_to?(b) ⇒ Boolean
Check if two rooms are next to each other. If so, return the exit that would take us from this room to the other. Otherwise, return nil
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/IFMapper/Room.rb', line 115 def next_to?(b) if not b.kind_of?(Room) raise "next_to?(b): #{b} is not a room." end if self == b raise "next_to? comparing same room #{self}" end if b.x == @x and b.y == @y raise "#{self} and #{b} in same location." end dx = (b.x - @x) dy = (b.y - @y) return nil if dx.abs > 1 or dy.abs > 1 return vector_to_dir(dx, dy) end |
#num_doors ⇒ Object
Return the number of doors present in room
62 63 64 65 66 67 68 69 |
# File 'lib/IFMapper/Room.rb', line 62 def num_doors num = 0 @exits.each { |e| next if not e num += 1 if e.door? } return num end |
#num_exits ⇒ Object
Return the number of exits present in room
74 75 76 |
# File 'lib/IFMapper/Room.rb', line 74 def num_exits return @exits.nitems end |
#to_s ⇒ Object
156 157 158 |
# File 'lib/IFMapper/Room.rb', line 156 def to_s "\"#{@name}\"" end |
#vector_to_dir(dx, dy) ⇒ Object
98 99 100 |
# File 'lib/IFMapper/Room.rb', line 98 def vector_to_dir(dx, dy) return Room::vector_to_dir( dx, dy ) end |