Module: DugTunnels
- Defined in:
- lib/algorithms/dug_tunnels.rb
Instance Method Summary collapse
- #can_dig(x, y) ⇒ Object
- #dig(x, y, dir) ⇒ Object
- #dug_all? ⇒ Boolean
- #generate ⇒ Object
- #init_panels ⇒ Object
- #next_panel(x, y, dir) ⇒ Object
- #next_xy(x, y, dir) ⇒ Object
- #random_direction ⇒ Object
Instance Method Details
#can_dig(x, y) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/algorithms/dug_tunnels.rb', line 49 def can_dig(x, y) (0..3).each do |dir| unless out_of_map?(*next_xy(x, y, dir)) return true if next_panel(x, y, dir).wall? end end return false end |
#dig(x, y, dir) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/algorithms/dug_tunnels.rb', line 62 def dig(x, y, dir) xy = next_xy(x, y, dir) panels(*xy).set_kind(:floor) @dug_count += 1 diff = [xy[0] - x, xy[1] - y] if diff[0] == 0 panels(x, xy[1] - 1).set_kind(:floor) if diff[1] > 0 panels(x, xy[1] + 1).set_kind(:floor) if diff[1] < 0 end if diff[1] == 0 panels(xy[0] - 1, y).set_kind(:floor) if diff[0] > 0 panels(xy[0] + 1, y).set_kind(:floor) if diff[0] < 0 end end |
#dug_all? ⇒ Boolean
58 59 60 |
# File 'lib/algorithms/dug_tunnels.rb', line 58 def dug_all? @dug_count >= (size_x+2)* (size_y+2) / 4 - 1 end |
#generate ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/algorithms/dug_tunnels.rb', line 10 def generate @dug_count = 0 init_panels # start point panels(*random_even_xy).set_kind(:floor) until dug_all? xy = random_floor_xy(even_hash_panels) while can_dig(*xy) dir = random_direction next_xy = next_xy(*xy, dir) next_panel = panels(*next_xy) if next_panel.wall? && !out_of_map?(*next_xy) dig(*xy, dir) xy = next_xy end end end end |
#init_panels ⇒ Object
4 5 6 7 8 |
# File 'lib/algorithms/dug_tunnels.rb', line 4 def init_panels each_panels do |panel, x, y| panel.set_kind(:wall) end end |
#next_panel(x, y, dir) ⇒ Object
45 46 47 |
# File 'lib/algorithms/dug_tunnels.rb', line 45 def next_panel(x, y, dir) panels(*next_xy(x, y, dir)) end |
#next_xy(x, y, dir) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/algorithms/dug_tunnels.rb', line 36 def next_xy(x, y, dir) [ [x-2,y], [x+2,y], [x,y-2], [x,y+2], ].at(dir) end |
#random_direction ⇒ Object
32 33 34 |
# File 'lib/algorithms/dug_tunnels.rb', line 32 def random_direction rand(4) end |