Class: Path

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-doom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(startx, starty, path = "") ⇒ Path

Returns a new instance of Path.



841
842
843
844
# File 'lib/ruby-doom.rb', line 841

def initialize(startx, starty, path="")
  @path = path
  @start = Point.new(startx, starty)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



840
841
842
# File 'lib/ruby-doom.rb', line 840

def path
  @path
end

#startObject (readonly)

Returns the value of attribute start.



840
841
842
# File 'lib/ruby-doom.rb', line 840

def start
  @start
end

Instance Method Details

#add(p, count = 1) ⇒ Object



845
846
847
# File 'lib/ruby-doom.rb', line 845

def add(p,count=1)
  count.times {@path += p }
end

#nethack(size = Nethack::DEFAULT_SIZE) ⇒ Object



873
874
875
876
877
# File 'lib/ruby-doom.rb', line 873

def nethack(size=Nethack::DEFAULT_SIZE)
  n = Nethack.new(@start, size)
  visit(n)
  return n.render
end

#segment_countObject



851
852
853
# File 'lib/ruby-doom.rb', line 851

def segment_count
  segments.size
end

#segmentsObject



848
849
850
# File 'lib/ruby-doom.rb', line 848

def segments
  @path.split(/\//)
end

#to_sObject



878
879
880
# File 'lib/ruby-doom.rb', line 878

def to_s
  @path
end

#visit(visitor) ⇒ Object



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/ruby-doom.rb', line 854

def visit(visitor)
  cur = @start
  segments.each do |x|
    dir = x[0].chr
    len = x.slice(1, x.length-1).to_i
    if dir == "e"
      cur = cur.translate(len, 0)
    elsif dir == "n"
      cur = cur.translate(0, len)
    elsif dir == "w"
      cur = cur.translate(-len, 0)
    elsif dir == "s"
      cur = cur.translate(0, -len)
    else
      raise "Unrecognized direction " + dir.to_s + " in segment " + x.to_s
    end
    visitor.line_to(cur)
  end
end