From faa96693e798086a360492011b29669bcf19c777 Mon Sep 17 00:00:00 2001 From: Paul Weber <work@paul-weber.de> Date: Sun, 4 Jul 2021 02:09:16 +0200 Subject: [PATCH] Fix level generation so entities can't overlap --- src/game_state/level.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/game_state/level.rs b/src/game_state/level.rs index 2b6a8f1..e242636 100644 --- a/src/game_state/level.rs +++ b/src/game_state/level.rs @@ -137,12 +137,26 @@ impl Level { fn add_entity(entity: Tile, board: Board) -> Board { let empty_count = Level::count_empty_tiles(board); - let entity_position = thread_rng().gen_range(0, empty_count); - let entity_x = entity_position / config::BOARD_HEIGHT; - let entity_y = entity_position % config::BOARD_HEIGHT; + let mut entity_position = thread_rng().gen_range(0, empty_count); + let mut board = board.clone(); - board[entity_x][entity_y] = entity; + for x in 0..board.len() { + for y in 0..board[x].len() { + if entity_position > 0 { + match board[x][y] { + Tile::Empty => entity_position -= 1, + _ => () + } + } + if entity_position == 0 { + board[x][y] = entity; + println!("Add {:?} at {:?}, coords: {}, {}", entity, entity_position, x, y); + return board + } + } + } + board } -- GitLab