diff --git a/src/game_state/level.rs b/src/game_state/level.rs index 6d160c94753de41862b96abaa3ea983e03da9204..75faa042c74d3309ad98663c491f19de2d6e1270 100644 --- a/src/game_state/level.rs +++ b/src/game_state/level.rs @@ -107,13 +107,13 @@ 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 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 entity_pos = Level::get_empty_tile(board); let mut board = board.clone(); - board[entity_x][entity_y] = entity; + board[entity_pos.x][entity_pos.y] = entity; board } @@ -130,6 +130,24 @@ impl Level { result } + + fn get_empty_tile(board:Board) -> Vec2<usize>{ + let empty_count = Level::count_empty_tiles(board); + let empty_position = thread_rng().gen_range(0, empty_count); + let mut empty_skipped:usize = 0; + for x in 0..board.len() { + for y in 0..board[x].len() { + match board[x][y] { + Tile::Empty => empty_skipped += 1, + _ => {}, + } + if empty_skipped == empty_position{ + return Vec2::new(x,y); + } + } + } + Vec2::new(0,0) + } }