Class: Map
- Inherits:
-
Object
- Object
- Map
- Defined in:
- lib/cave_gen/map.rb
Overview
класс карты, в котором и происходит всё веселье
Instance Method Summary collapse
-
#check_point_to_floor(y_coord, x_coord) ⇒ Object
проверка точки - может ли она быть полом (считается по окружению) вне зависимости от результата, после проверки точка будет считаться “использованной”, т.е.
-
#end_generation? ⇒ Boolean
метод проверки генерации.
-
#generate_map ⇒ Object
метод содания карты.
-
#get_next_point(y_coord, x_coord) ⇒ Object
алгоритм выбора следующей точки.
-
#get_rand_point ⇒ Object
выбор первой случайной точки.
-
#initialize(height = 20, width = 20, error = 100, opacity = 7) ⇒ Map
constructor
изначально карта принимает два параментра для инициализации - высоты и ширину, образуя массив каждый элемент массива - тайл.
- #is_floor?(y_coord, x_coord) ⇒ Boolean
-
#show_map ⇒ Object
показываем всю карту.
- #to_s ⇒ Object
- #to_symbol_array ⇒ Object
Constructor Details
#initialize(height = 20, width = 20, error = 100, opacity = 7) ⇒ Map
изначально карта принимает два параментра для инициализации - высоты и ширину, образуя массив каждый элемент массива - тайл. Счётчик остановки используется, чтобы не делать бесконечного цикла
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cave_gen/map.rb', line 8 def initialize(height = 20, width = 20, error = 100, opacity = 7) @height = height @width = width @stop = 0 @error = error @opacity_challenge = opacity rows, cols = @width, @height @grid = Array.new(cols) cols.times do |y| @grid[y] = Array.new(rows) rows.times do |x| @grid[y][x] = Wall.new(y, x) end end end |
Instance Method Details
#check_point_to_floor(y_coord, x_coord) ⇒ Object
проверка точки - может ли она быть полом (считается по окружению) вне зависимости от результата, после проверки точка будет считаться “использованной”, т.е. на неё больше не пойдут
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cave_gen/map.rb', line 54 def check_point_to_floor(y_coord, x_coord) @grid[y_coord][x_coord].touched = 1 (y_coord-1..y_coord+1).each do |y| (x_coord-1..x_coord+1).each do |x| if @grid[y][x].is_floor? @grid[y_coord][x_coord].opacity += 1 end end end if @grid[y_coord][x_coord].opacity < @opacity_challenge @grid[y_coord][x_coord] = Floor.new(y_coord, x_coord, @grid[y_coord][x_coord].opacity) end end |
#end_generation? ⇒ Boolean
метод проверки генерации. Завершает её, если число остановки (количества ошибок) стало слишком большим.
115 116 117 118 119 120 121 |
# File 'lib/cave_gen/map.rb', line 115 def end_generation? if @stop < @error return true else return false end end |
#generate_map ⇒ Object
метод содания карты. Берётся случайная первая точка, после чего выбирается направление и осуществляется проверка на возможность проложить пол. Циклично, до тех пор, пока не накопится счётик остановки
38 39 40 41 42 43 44 45 |
# File 'lib/cave_gen/map.rb', line 38 def generate_map @curr_point = get_rand_point while end_generation? do check_point_to_floor(@curr_point[0], @curr_point[1]) @curr_point = get_next_point(@curr_point[0], @curr_point[1]) end end |
#get_next_point(y_coord, x_coord) ⇒ Object
алгоритм выбора следующей точки. Место наибольшей сложности
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cave_gen/map.rb', line 69 def get_next_point(y_coord, x_coord) while end_generation? do directions = Array.new if y_coord - 1 > 0 if @grid[y_coord - 1][x_coord].touched != 1 directions << 0 end end if x_coord + 1 < @width - 1 if @grid[y_coord][x_coord + 1].touched != 1 directions << 1 end end if y_coord + 1 < @height - 1 if @grid[y_coord + 1][x_coord].touched != 1 directions << 2 end end if x_coord - 1 > 0 if @grid[y_coord][x_coord - 1].touched != 1 directions << 3 end end unless directions.empty? direction = directions.sample else y_coord, x_coord = get_rand_point @stop += 10 next end case direction when 0 return [y_coord - 1, x_coord] when 1 return [y_coord, x_coord + 1] when 2 return [y_coord + 1, x_coord] when 3 return [y_coord, x_coord - 1] end end end |
#get_rand_point ⇒ Object
выбор первой случайной точки
48 49 50 |
# File 'lib/cave_gen/map.rb', line 48 def get_rand_point @rand_point = [rand(1..@height-2), rand(1..@width-2)] end |
#is_floor?(y_coord, x_coord) ⇒ Boolean
123 124 125 |
# File 'lib/cave_gen/map.rb', line 123 def is_floor?(y_coord, x_coord) return @grid[y_coord][x_coord].is_floor? end |
#show_map ⇒ Object
показываем всю карту
27 28 29 30 31 32 33 34 |
# File 'lib/cave_gen/map.rb', line 27 def show_map @grid.each do |y| y.each do |x| x.print_tile end print "\n" end end |
#to_s ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cave_gen/map.rb', line 127 def to_s string = '' @grid.each do |y| y.each do |x| string = string + x.get_tile end string = string + "\n" end return string end |
#to_symbol_array ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/cave_gen/map.rb', line 138 def to_symbol_array string = Array.new @grid.each_with_index do |y, y_index| y.each_with_index do |x, x_index| string[y_index][y_index] = x.get_tile end end return string end |