Class: World
- Inherits:
-
Object
- Object
- World
- Defined in:
- lib/World.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_to_x_level(amount, position) ⇒ Object
- #add_to_y_level(amount, position) ⇒ Object
- #add_to_z_level(amount, position) ⇒ Object
- #colored_matrix ⇒ Object
- #coordinates_display ⇒ Object
- #current_matrix_width ⇒ Object
- #decrement_x_level(position) ⇒ Object
- #decrement_y_level(position) ⇒ Object
- #decrement_z_level(position) ⇒ Object
- #draw(tile_size) ⇒ Object
- #expanded_row(zoom_length, current_row) ⇒ Object
- #fill_none_tiles(unfilled_matrix) ⇒ Object
- #generate_new_scrolled_down_map(position) ⇒ Object
- #generate_new_scrolled_left_map(position) ⇒ Object
- #generate_new_scrolled_right_map(position) ⇒ Object
- #generate_new_scrolled_up_map(position) ⇒ Object
- #generate_new_zoomed_in_map(position) ⇒ Object
- #generate_new_zoomed_out_map(position) ⇒ Object
- #get_tile_at_position(x, y, current_matrix) ⇒ Object
- #get_tiles_in_neighboring_positions(x, y, current_matrix) ⇒ Object
- #increment_x_level(position) ⇒ Object
- #increment_y_level(position) ⇒ Object
- #increment_z_level(position) ⇒ Object
-
#initialize(theme) ⇒ World
constructor
A new instance of World.
- #initialize_matrix ⇒ Object
- #initialize_name ⇒ Object
- #matrix ⇒ Object
- #matrix_exists_in_position(position) ⇒ Object
- #new_row(zoom_length) ⇒ Object
- #next_matrix_width ⇒ Object
- #regenerate ⇒ Object
- #render_frame_around_map(map) ⇒ Object
- #render_framed_map(tile_size) ⇒ Object
- #render_framed_table_map(tile_size) ⇒ Object
- #render_map(tile_size) ⇒ Object
- #render_map_as_text(tile_size) ⇒ Object
- #render_map_with_info(tile_size) ⇒ Object
- #render_matrix(matrix_to_render, theme, tile_size) ⇒ Object
- #render_table_map(tile_size) ⇒ Object
- #scroll_down ⇒ Object
- #scroll_left ⇒ Object
- #scroll_right ⇒ Object
- #scroll_up ⇒ Object
- #set_name(name) ⇒ Object
- #themed_matrix(theme) ⇒ Object
- #world_name_display ⇒ Object
- #zoom_in ⇒ Object
- #zoom_level_display ⇒ Object
- #zoom_out ⇒ Object
Constructor Details
#initialize(theme) ⇒ World
27 28 29 30 31 32 33 34 |
# File 'lib/World.rb', line 27 def initialize(theme) @theme = theme @pastel = Pastel.new @max_x = 4 @max_y = 4 @max_z = 4 regenerate end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
25 26 27 |
# File 'lib/World.rb', line 25 def name @name end |
Instance Method Details
#add_to_x_level(amount, position) ⇒ Object
253 254 255 256 257 258 259 260 261 |
# File 'lib/World.rb', line 253 def add_to_x_level(amount, position) new_x = (position.x + amount).clamp(-@max_x, @max_x) Point.new( new_x, position.y, position.z, ) end |
#add_to_y_level(amount, position) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/World.rb', line 263 def add_to_y_level(amount, position) new_y = (position.y + amount).clamp(-@max_y, @max_y) Point.new( position.x, new_y, position.z, ) end |
#add_to_z_level(amount, position) ⇒ Object
273 274 275 276 277 278 279 280 281 |
# File 'lib/World.rb', line 273 def add_to_z_level(amount, position) new_z = (position.z + amount).clamp(0, @max_z) Point.new( position.x, position.y, new_z, ) end |
#colored_matrix ⇒ Object
58 59 60 |
# File 'lib/World.rb', line 58 def colored_matrix themed_matrix(@theme) end |
#coordinates_display ⇒ Object
159 160 161 |
# File 'lib/World.rb', line 159 def coordinates_display "x: #{@current_position.x}, y: #{@current_position.y}" end |
#current_matrix_width ⇒ Object
163 164 165 |
# File 'lib/World.rb', line 163 def current_matrix_width matrix.length end |
#decrement_x_level(position) ⇒ Object
233 234 235 |
# File 'lib/World.rb', line 233 def decrement_x_level(position) add_to_x_level(-1, position) end |
#decrement_y_level(position) ⇒ Object
241 242 243 |
# File 'lib/World.rb', line 241 def decrement_y_level(position) add_to_y_level(-1, position) end |
#decrement_z_level(position) ⇒ Object
249 250 251 |
# File 'lib/World.rb', line 249 def decrement_z_level(position) add_to_z_level(-1, position) end |
#draw(tile_size) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/World.rb', line 74 def draw(tile_size) # puts render_framed_map(tile_size) # puts render_framed_table_map(tile_size) # puts render_map(tile_size) puts render_map_with_info(tile_size) # puts render_table_map(tile_size) end |
#expanded_row(zoom_length, current_row) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/World.rb', line 367 def (zoom_length, current_row) new_row = [] current_row.each_with_index{ |row, i| new_row << current_row[i] if new_row.length < zoom_length new_row << Constants::TILE_TYPES[:NONE] end } new_row end |
#fill_none_tiles(unfilled_matrix) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/World.rb', line 383 def fill_none_tiles(unfilled_matrix) new_matrix = [] unfilled_matrix.each_with_index { |row, y| new_row = [] row.each_with_index { |tile, x| new_tile = tile if tile == Constants::TILE_TYPES[:NONE] neighboring_tiles = get_tiles_in_neighboring_positions(x, y, unfilled_matrix); random_neighbor_tile = neighboring_tiles.select{|tile| ![Constants::TILE_TYPES[:NONE]].include?(tile) }.sample new_tile = random_neighbor_tile != Constants::TILE_TYPES[:NONE] ? random_neighbor_tile : Constants::TILE_TYPES.sample end new_row << new_tile } new_matrix << new_row } new_matrix end |
#generate_new_scrolled_down_map(position) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/World.rb', line 354 def generate_new_scrolled_down_map(position) unfilled_new_matrix = [] matrix.dup.map.with_index{ |row, i| unfilled_new_matrix << row.dup if i > 0 } unfilled_new_matrix << new_row(current_matrix_width) filled_new_matrix = fill_none_tiles(unfilled_new_matrix) @matrix_map[position.to_s] = filled_new_matrix end |
#generate_new_scrolled_left_map(position) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/World.rb', line 327 def generate_new_scrolled_left_map(position) unfilled_new_matrix = matrix.dup.map.with_index{ |row, y| new_row = row.dup new_row.pop new_row.unshift(Constants::TILE_TYPES[:NONE]) new_row } filled_new_matrix = fill_none_tiles(unfilled_new_matrix) @matrix_map[position.to_s] = filled_new_matrix end |
#generate_new_scrolled_right_map(position) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/World.rb', line 313 def generate_new_scrolled_right_map(position) unfilled_new_matrix = matrix.dup.map.with_index{ |row, y| new_row = row.dup new_row.shift new_row << Constants::TILE_TYPES[:NONE] new_row } filled_new_matrix = fill_none_tiles(unfilled_new_matrix) @matrix_map[position.to_s] = filled_new_matrix end |
#generate_new_scrolled_up_map(position) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/World.rb', line 341 def generate_new_scrolled_up_map(position) unfilled_new_matrix = [] matrix.dup.map.with_index{ |row, i| unfilled_new_matrix << row.dup if i < (current_matrix_width - 1) } unfilled_new_matrix.unshift(new_row(current_matrix_width)) filled_new_matrix = fill_none_tiles(unfilled_new_matrix) @matrix_map[position.to_s] = filled_new_matrix end |
#generate_new_zoomed_in_map(position) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/World.rb', line 283 def generate_new_zoomed_in_map(position) zoom_length = next_matrix_width unfilled_new_matrix = [] matrix.each_with_index{ |row, i| unfilled_new_matrix << (zoom_length, matrix[i]) if unfilled_new_matrix.length < zoom_length unfilled_new_matrix << new_row(zoom_length) end } filled_new_matrix = fill_none_tiles(unfilled_new_matrix) @matrix_map[position.to_s] = filled_new_matrix end |
#generate_new_zoomed_out_map(position) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/World.rb', line 298 def generate_new_zoomed_out_map(position) new_matrix = [] matrix.each_with_index{ |row, i| if i.even? new_row = [] matrix[i].each_with_index{ |tile, j| new_row << tile if j.even? } new_matrix << new_row end } @matrix_map[position.to_s] = new_matrix end |
#get_tile_at_position(x, y, current_matrix) ⇒ Object
425 426 427 428 429 430 431 |
# File 'lib/World.rb', line 425 def get_tile_at_position(x, y, current_matrix) begin return current_matrix[y][x] rescue => exception return false end end |
#get_tiles_in_neighboring_positions(x, y, current_matrix) ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/World.rb', line 404 def get_tiles_in_neighboring_positions(x, y, current_matrix) relative_neighboring_positions = [ {x: -1, y: 0}, {x: 1, y: 0}, {x: 0, y: -1}, {x: 0, y: 1}, {x: -1, y: -1}, {x: 1, y: 1}, {x: 1, y: -1}, {x: -1, y: 1}, ] neighboring_tiles = [] relative_neighboring_positions.map { |pos| tile = get_tile_at_position(x + pos[:x], y + pos[:y], current_matrix) neighboring_tiles << tile if tile } neighboring_tiles end |
#increment_x_level(position) ⇒ Object
229 230 231 |
# File 'lib/World.rb', line 229 def increment_x_level(position) add_to_x_level(1, position) end |
#increment_y_level(position) ⇒ Object
237 238 239 |
# File 'lib/World.rb', line 237 def increment_y_level(position) add_to_y_level(1, position) end |
#increment_z_level(position) ⇒ Object
245 246 247 |
# File 'lib/World.rb', line 245 def increment_z_level(position) add_to_z_level(1, position) end |
#initialize_matrix ⇒ Object
62 63 64 |
# File 'lib/World.rb', line 62 def initialize_matrix GenerateMatrix.new.perlin_matrix(4) end |
#initialize_name ⇒ Object
66 67 68 |
# File 'lib/World.rb', line 66 def initialize_name GenerateName.new.world end |
#matrix ⇒ Object
46 47 48 |
# File 'lib/World.rb', line 46 def matrix @matrix_map[@current_position.to_s] end |
#matrix_exists_in_position(position) ⇒ Object
171 172 173 |
# File 'lib/World.rb', line 171 def matrix_exists_in_position(position) @matrix_map.keys.include?(position.to_s) end |
#new_row(zoom_length) ⇒ Object
379 380 381 |
# File 'lib/World.rb', line 379 def new_row(zoom_length) return Array.new(zoom_length, Constants::TILE_TYPES[:NONE]) end |
#next_matrix_width ⇒ Object
167 168 169 |
# File 'lib/World.rb', line 167 def next_matrix_width (current_matrix_width * 2) - 1 end |
#regenerate ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/World.rb', line 36 def regenerate @name = initialize_name @matrix = initialize_matrix @current_position = Point.new(0, 0, 0) @matrix_map = { @current_position.to_s => @matrix } end |
#render_frame_around_map(map) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/World.rb', line 138 def render_frame_around_map(map) framed_map = TTY::Box.frame( align: :center, title: { bottom_left: coordinates_display, bottom_right: zoom_level_display }, border: :thick ) { map } framed_map end |
#render_framed_map(tile_size) ⇒ Object
125 126 127 128 129 |
# File 'lib/World.rb', line 125 def render_framed_map(tile_size) map = render_map(tile_size) render_frame_around_map(map) end |
#render_framed_table_map(tile_size) ⇒ Object
132 133 134 135 136 |
# File 'lib/World.rb', line 132 def render_framed_table_map(tile_size) map = render_table_map(tile_size) render_frame_around_map(map) end |
#render_map(tile_size) ⇒ Object
111 112 113 |
# File 'lib/World.rb', line 111 def render_map(tile_size) render_matrix(colored_matrix, @theme, tile_size) end |
#render_map_as_text(tile_size) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/World.rb', line 104 def render_map_as_text(tile_size) theme = Constants::THEME::BLACKWHITE new_matrix = themed_matrix(theme) render_matrix(new_matrix, theme, tile_size) end |
#render_map_with_info(tile_size) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/World.rb', line 82 def render_map_with_info(tile_size) [ render_map(tile_size), world_name_display, zoom_level_display, coordinates_display, ].join("\n") end |
#render_matrix(matrix_to_render, theme, tile_size) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/World.rb', line 91 def render_matrix(matrix_to_render, theme, tile_size) matrix_to_render.map.with_index { |row, y| row.map.with_index { |tile, x| tile }.join( theme.colored_tile( @pastel, Constants::TILE_TYPES[:NONE] ) * tile_size ) }.join("\n" * tile_size) end |
#render_table_map(tile_size) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/World.rb', line 115 def render_table_map(tile_size) table = TTY::Table.new({rows: colored_matrix}) table.render( :basic, alignments: [:center], padding: tile_size - 1 ) end |
#scroll_down ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/World.rb', line 220 def scroll_down new_position = decrement_y_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_scrolled_down_map(new_position) end @current_position = new_position end |
#scroll_left ⇒ Object
202 203 204 205 206 207 208 209 |
# File 'lib/World.rb', line 202 def scroll_left new_position = decrement_x_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_scrolled_left_map(new_position) end @current_position = new_position end |
#scroll_right ⇒ Object
193 194 195 196 197 198 199 200 |
# File 'lib/World.rb', line 193 def scroll_right new_position = increment_x_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_scrolled_right_map(new_position) end @current_position = new_position end |
#scroll_up ⇒ Object
211 212 213 214 215 216 217 218 |
# File 'lib/World.rb', line 211 def scroll_up new_position = increment_y_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_scrolled_up_map(new_position) end @current_position = new_position end |
#set_name(name) ⇒ Object
70 71 72 |
# File 'lib/World.rb', line 70 def set_name(name) @name = name end |
#themed_matrix(theme) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/World.rb', line 50 def themed_matrix(theme) matrix.map{ |row| row.map{ |tile| theme.colored_tile(@pastel, tile) } } end |
#world_name_display ⇒ Object
151 152 153 |
# File 'lib/World.rb', line 151 def world_name_display "You are exploring #{@pastel.italic(@name)}" end |
#zoom_in ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/World.rb', line 175 def zoom_in new_position = increment_z_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_zoomed_in_map(new_position) end @current_position = new_position end |
#zoom_level_display ⇒ Object
155 156 157 |
# File 'lib/World.rb', line 155 def zoom_level_display "Zoom: #{@current_position.z}" end |
#zoom_out ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/World.rb', line 184 def zoom_out new_position = decrement_z_level(@current_position) if !matrix_exists_in_position(new_position) generate_new_zoomed_out_map(new_position) end @current_position = new_position end |