Class: Connection

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

Overview

Class used to represent a connection between one room and another

Direct Known Subclasses

FXConnection

Constant Summary collapse

FREE =

Type constants

0
LOCKED_DOOR =
1
SPECIAL =
2
CLOSED_DOOR =
3
BOTH =

Direction constants

0
AtoB =
1
BtoA =

NO LONGER USED.

2
EXIT_TEXT =

Text near connection (to indicate other dir)

[
  '',
  'U',
  'D',
  'I',
  'O'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roomA, roomB, dir = BOTH, type = FREE) ⇒ Connection

Returns a new instance of Connection.



92
93
94
95
96
97
98
99
# File 'lib/IFMapper/Connection.rb', line 92

def initialize( roomA, roomB, dir = BOTH, type = FREE )
  @room     = []
  @room[0]  = roomA
  @room[1]  = roomB
  @dir      = dir
  @type     = type
  @exitText = [0, 0]
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

#exitTextObject

Returns the value of attribute exitText.



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

def exitText
  @exitText
end

#roomObject

Returns the value of attribute room.



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

def room
  @room
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#dirsObject

Return the connected direction index for each room



133
134
135
136
137
138
139
140
141
# File 'lib/IFMapper/Connection.rb', line 133

def dirs
  dirA = @room[0].exits.index(self)
  if @room[1]
    dirB = @room[1].exits.rindex(self)
  else
    dirB = nil
  end
  return [dirA, dirB]
end

#door?Boolean

Return true if connection is a door

Returns:

  • (Boolean)


164
165
166
167
# File 'lib/IFMapper/Connection.rb', line 164

def door?
  return true if @type == CLOSED_DOOR or @type == LOCKED_DOOR
  return false
end

#exitAtextObject



65
66
67
# File 'lib/IFMapper/Connection.rb', line 65

def exitAtext
  return @exitText[0]
end

#exitAtext=(x) ⇒ Object



57
58
59
# File 'lib/IFMapper/Connection.rb', line 57

def exitAtext=(x)
  @exitText[0] = x
end

#exitBtextObject



69
70
71
# File 'lib/IFMapper/Connection.rb', line 69

def exitBtext
  return @exitText[1]
end

#exitBtext=(x) ⇒ Object



61
62
63
# File 'lib/IFMapper/Connection.rb', line 61

def exitBtext=(x)
  @exitText[1] = x
end

#flipObject

Flip A and B rooms. This is mainly used to make a oneway AtoB connection become a BtoA connection.



124
125
126
127
128
# File 'lib/IFMapper/Connection.rb', line 124

def flip
  return unless @room[1]
  @room.reverse!
  @exitText.reverse!
end

#index(room) ⇒ Object

Given a room, return the index of that room in the room[] array If room is not present in connection, return nil.



115
116
117
118
# File 'lib/IFMapper/Connection.rb', line 115

def index(room)
  @room.each_with_index { |r, idx| return idx if r == room }
  return nil
end

#loop?Boolean

Return true if connection is a self-looping connection

Returns:

  • (Boolean)


154
155
156
157
158
159
# File 'lib/IFMapper/Connection.rb', line 154

def loop?
  return false if @room[0] != @room[1]
  dirA, dirB = dirs
  return true if dirA == dirB
  return false
end

#marshal_dumpObject



88
89
90
# File 'lib/IFMapper/Connection.rb', line 88

def marshal_dump
  [ @type, @dir, @room, @exitText ]
end

#marshal_load(vars) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/IFMapper/Connection.rb', line 73

def marshal_load(vars)
  if vars[2].kind_of?(Array)
    @type, @dir, @room, @exitText = vars
  else
    @type     = vars[0]
    @dir      = vars[1]
    @room     = [vars[2], vars[3]]
    @exitText = [vars[4], vars[5]]
    if @dir == BtoA
	@dir = AtoB
	flip
    end
  end
end

#opposite(room) ⇒ Object

Given a room, return the opposite room in the connection. If room passed is not present in connection, raise an exception.



105
106
107
108
109
# File 'lib/IFMapper/Connection.rb', line 105

def opposite(room)
  idx = self.index(room)
  raise "Room #{room} not part of connection #{c}" unless idx
  return @room[idx ^ 1]
end

#roomAObject



49
50
51
# File 'lib/IFMapper/Connection.rb', line 49

def roomA
  return @room[0]
end

#roomA=(x) ⇒ Object



41
42
43
# File 'lib/IFMapper/Connection.rb', line 41

def roomA=(x)
  @room[0] = x
end

#roomBObject



53
54
55
# File 'lib/IFMapper/Connection.rb', line 53

def roomB
  return @room[1]
end

#roomB=(x) ⇒ Object



45
46
47
# File 'lib/IFMapper/Connection.rb', line 45

def roomB=(x)
  @room[1] = x
end

#stub?Boolean

Return true if connection is a stub exit (ie. an exit leading nowhere)

Returns:

  • (Boolean)


146
147
148
149
# File 'lib/IFMapper/Connection.rb', line 146

def stub?
  return true unless @room[1]
  return false
end

#to_sObject

For debugging purposes, print ourselves nicely



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/IFMapper/Connection.rb', line 172

def to_s
  dirA = dirB = ''
  a = @room[0]? @room[0].name : 'nil'
  b = @room[1]? @room[1].name : 'nil'
  if @exitText[0] > 0
    dirA = EXIT_TEXT[@exitText[0]]
  else
    if @room[0]
	idx  = @room[0].exits.index(self)
	dirA = Room::DIRECTIONS[idx] if idx
	dirA = dirA.upcase
    end
  end
  if @exitText[1] > 0
    dirB = EXIT_TEXT[@exitText[1]]
  else
    if @room[1]
	idx  = @room[1].exits.rindex(self)
	dirB = Room::DIRECTIONS[idx] if idx
	dirB = dirB.upcase
    end
  end
  if @dir == AtoB
    sym = '  --> '
  else
    sym = ' <-> '
  end
  "#{a} #{dirA}#{sym}#{b} #{dirB}"
end