# # Class used to represent a Room or Location in a Map. # class Room attr_accessor :name attr_accessor :objects attr_accessor :tasks attr_reader :exits attr_accessor :darkness attr_accessor :x, :y DIRECTIONS = [ 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', ] 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 ] } def [](dir) return @exits[dir] end def []=(dir, connection) @exits[dir] = connection end # # Return a direction from the vector of the exit that would take # us to the 'b' room more cleanly. # 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 def vector_to_dir(dx, dy) return Room::vector_to_dir( dx, dy ) end # # Given an 'adjacent' room, return the most direct exit from this # room to room b. # def exit_to(b) dx = (b.x - @x) dy = (b.y - @y) return vector_to_dir(dx, dy) end # 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 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 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 # # Copy a room to another # def copy(b) @name = b.name @objects = b.objects @tasks = b.tasks @darkness = b.darkness end def initialize(x, y, name = 'Room') @exits = Array.new( DIRECTIONS.size ) @darkness = false @name = name @x = x @y = y @objects = '' @tasks = '' end def to_s "\"#{@name}\"" end end