Skip to content
Snippets Groups Projects
Commit a178c997 authored by radow's avatar radow
Browse files

ensured entities spawn on empty tile

parent d66473bd
Branches levelgen
No related tags found
No related merge requests found
...@@ -107,13 +107,13 @@ impl Level { ...@@ -107,13 +107,13 @@ impl Level {
} }
fn add_entity(entity: Tile, board: Board) -> Board { fn add_entity(entity: Tile, board: Board) -> Board {
let empty_count = Level::count_empty_tiles(board); // let empty_count = Level::count_empty_tiles(board);
let entity_position = thread_rng().gen_range(0, empty_count); // let entity_position = thread_rng().gen_range(0, empty_count);
let entity_x = entity_position / config::BOARD_HEIGHT; // let entity_x = entity_position / config::BOARD_HEIGHT;
let entity_y = 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(); let mut board = board.clone();
board[entity_x][entity_y] = entity; board[entity_pos.x][entity_pos.y] = entity;
board board
} }
...@@ -130,6 +130,24 @@ impl Level { ...@@ -130,6 +130,24 @@ impl Level {
result 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)
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment