class RuneRb::World::Pathfinder

Attributes

entity[RW]
run_queue[RW]
run_toggle[RW]

Public Class Methods

new(entity) click to toggle source
# File app/world/walking.rb, line 11
def initialize(entity)
  @entity = entity
  @waypoints = []
  @run_toggle = false
  @run_queue = false
end

Public Instance Methods

add_step(x, y) click to toggle source
# File app/world/walking.rb, line 32
def add_step(x, y)
  reset if empty?
  last = @waypoints.last
  dx = x - last.x
  dy = y - last.y
  max = [dx.abs, dy.abs].max
  (0...max).each {
    dx += 0 <=> dx
    dy += 0 <=> dy
    add_step_internal x - dx, y - dy
  }
end
empty?() click to toggle source
# File app/world/walking.rb, line 24
def empty?
  @waypoints.empty?
end
finish() click to toggle source
# File app/world/walking.rb, line 28
def finish
  @waypoints.shift
end
next_movement() click to toggle source
# File app/world/walking.rb, line 45
def next_movement
  if @entity.teleport_location != nil
    reset
    @entity.teleporting = true
    @entity.location = @entity.teleport_location
    @entity.teleport_location = nil
  else
    walk_point = next_point
    run_point = (@run_toggle || @run_queue) ? next_point : nil
    
    @entity.update_energy(run_point != nil) if @entity.instance_of?(RuneRb::Model::Player)
    
    @entity.sprites[0] = walk_point ? walk_point.dir : -1
    @entity.sprites[1] = run_point ? run_point.dir : -1
  end
  
  dx = @entity.location.x - entity.last_location.get_region_x * 8
  dy = @entity.location.y - entity.last_location.get_region_y * 8
  @entity.region_change = dx < 16 || dx >= 88 || dy < 16 || dy >= 88
end
reset() click to toggle source
# File app/world/walking.rb, line 18
def reset
  @run_queue = false
  @waypoints.clear
  @waypoints << Point.new(entity.location.x, entity.location.y, -1)
end
running?() click to toggle source
# File app/world/walking.rb, line 66
def running?
  @run_toggle || @run_queue
end

Private Instance Methods

add_step_internal(x, y) click to toggle source
# File app/world/walking.rb, line 87
def add_step_internal(x, y)
  return if @waypoints.size >= MAXIMUM_SIZE
  last = @waypoints.last
  dir = direction(x - last.x, y - last.y)
  @waypoints << Point.new(x, y, dir) if dir > -1
end
direction(dx, dy) click to toggle source
# File app/world/walking.rb, line 72
def direction(dx, dy)
  x = (dx <=> 0) + 1
  y = (dy <=> 0) + 1
  DIRECTIONS[x][y]
end
next_point() click to toggle source
# File app/world/walking.rb, line 78
def next_point
  p = @waypoints.shift
  return nil if p == nil || p.dir == -1
  dx = DIRECTION_DELTA_X[p.dir]
  dy = DIRECTION_DELTA_Y[p.dir]
  @entity.location = @entity.location.transform(dx, dy, 0)
  p
end