Skip to content
Snippets Groups Projects
Commit faa96693 authored by pumapaul's avatar pumapaul
Browse files

Fix level generation so entities can't overlap

parent c892e366
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment