Class: Room

Inherits:
Object
  • Object
show all
Defined in:
lib/IFMapper/Room.rb,
lib/IFMapper/locales/en/Messages.rb,
lib/IFMapper/locales/es/Messages.rb

Overview

Class used to represent a Room or Location in a Map.

Direct Known Subclasses

FXRoom

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

Class Method Summary collapse

Instance Method Summary collapse

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

#commentObject

Room comment



13
14
15
# File 'lib/IFMapper/Room.rb', line 13

def comment
  @comment
end

#darknessObject

Isxxxxxxxxxx room in darkness?



10
11
12
# File 'lib/IFMapper/Room.rb', line 10

def darkness
  @darkness
end

#descObject

Room description



12
13
14
# File 'lib/IFMapper/Room.rb', line 12

def desc
  @desc
end

#exitsObject (readonly)

An array of 8 possible exits in room



9
10
11
# File 'lib/IFMapper/Room.rb', line 9

def exits
  @exits
end

#nameObject

Name of room



6
7
8
# File 'lib/IFMapper/Room.rb', line 6

def name
  @name
end

#objectsObject

Objects found in room



7
8
9
# File 'lib/IFMapper/Room.rb', line 7

def objects
  @objects
end

#tasksObject

Tasks that need to be performed in room



8
9
10
# File 'lib/IFMapper/Room.rb', line 8

def tasks
  @tasks
end

#xObject

Room location in grid



11
12
13
# File 'lib/IFMapper/Room.rb', line 11

def x
  @x
end

#yObject

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

#inspectObject



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_dumpObject



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

Returns:

  • (Boolean)


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_doorsObject

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_exitsObject

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_sObject



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