Class: MapNode

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

Overview

Simple class used as an adapter for our FXMap<->AStar

Constant Summary collapse

MAX_SCALE =
9
@@pmap =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ MapNode

Returns a new instance of MapNode.



183
184
185
186
# File 'lib/IFMapper/AStar.rb', line 183

def initialize(x, y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



176
177
178
# File 'lib/IFMapper/AStar.rb', line 176

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



176
177
178
# File 'lib/IFMapper/AStar.rb', line 176

def y
  @y
end

Class Method Details

.map(pmap) ⇒ Object



179
180
181
# File 'lib/IFMapper/AStar.rb', line 179

def self.map(pmap)
  @@pmap = pmap
end

Instance Method Details

#cost(successor) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/IFMapper/AStar.rb', line 237

def cost( successor )
  g  = get_map(@x,@y)

  dx = (@x - successor.x).abs
  dy = (@y - successor.y).abs
  if dx + dy == 1
     g += 10  # straight move
   else
     g += 14  # diagonal move
  end
  return g
end

#distance_estimate(goal) ⇒ Object



198
199
200
201
202
# File 'lib/IFMapper/AStar.rb', line 198

def distance_estimate( goal )
  dx = @x - goal.x
  dy = @y - goal.y
  return dx*dx + dy*dy
end

#get_map(x, y) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/IFMapper/AStar.rb', line 206

def get_map(x, y)
  if x < 0 or y < 0 or x >= @@pmap.size or y >= @@pmap[0].size
    return MAX_SCALE
  else
    t = @@pmap.at(x).at(y)
    return MAX_SCALE   if t.kind_of?(Room)
    return MAX_SCALE-1 if t.kind_of?(Connection)
    return 1
  end
end

#inspectObject



229
230
231
# File 'lib/IFMapper/AStar.rb', line 229

def inspect
  "pos: #{@x}, #{@y}"
end

#is_goal?(goal) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/IFMapper/AStar.rb', line 193

def is_goal?( goal )
  return true  if @x == goal.x and @y == goal.y
  return false
end

#is_same?(node) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
# File 'lib/IFMapper/AStar.rb', line 188

def is_same?(node)
  return true  if node and @x == node.x and @y == node.y
  return false
end

#successors(astar, parent = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/IFMapper/AStar.rb', line 217

def successors( astar, parent = nil )
  info = nil
  info = parent.info if parent
  Room::DIR_TO_VECTOR.each_value { |x, y|
    new_node = MapNode.new(@x + x, @y + y)
    if get_map( new_node.x, new_node.y ) < MAX_SCALE and 
 not new_node.is_same?(info)
	astar.add_successor(new_node)
    end
  }
end

#to_sObject



233
234
235
# File 'lib/IFMapper/AStar.rb', line 233

def to_s
  inspect
end