You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fn setup_tilemap(
mut cmds: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut map: ResMut<Map>,
query: Query<&Visited>,
) {
let texture_handle = asset_server.load("tiles.png");
let mut rng = thread_rng();
const map_size: TilemapSize = TilemapSize { x: 50, y: 50 };
let mut map = [[false; map_size.y as usize]; map_size.x as usize];
let mut frontiers = Vec::new();
let x = rng.gen_range(0..map_size.x);
let y = rng.gen_range(0..map_size.y);
frontiers.push((x, y, x, y));
while !frontiers.is_empty() {
let f = frontiers.remove(rng.gen_range(0..frontiers.len()));
let x = f.2 as usize;
let y = f.3 as usize;
if (map[x][y] == false) {
map[f.0 as usize][f.1 as usize] = true;
map[x][y] = true;
if (x >= 2 && map[x - 2][y] == false) {
let x = x as u32;
let y = y as u32;
frontiers.push((x - 1, y, x - 2, y));
}
if (y >= 2 && map[x][y - 2] == false) {
let x = x as u32;
let y = y as u32;
frontiers.push((x, y - 1, x, y - 2));
}
if (x < (map_size.x as usize - 2) && (map[x + 2][y]) == false) {
let x = x as u32;
let y = y as u32;
frontiers.push((x + 1, y, x + 2, y));
}
if (y < map_size.y as usize - 2 && map[x][y + 2] == false) {
let x = x as u32;
let y = y as u32;
frontiers.push((x, y + 1, x, y + 2));
}
}
}
info!("{:?}", map);
let tilemap_entity = cmds.spawn_empty().id();
let mut tile_storage = TileStorage::empty(map_size);
for x in 0..map_size.x {
for y in 0..map_size.y {
let tile_pos = TilePos { x, y };
let tile_entity = cmds
.spawn(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
texture_index: TileTextureIndex(0),
..Default::default()
})
.id();
if map[x as usize][y as usize] {
cmds.entity(tile_entity)
.insert((TileType::Path, TileTextureIndex(0)));
} else {
cmds.entity(tile_entity)
.insert((TileType::Wall, TileTextureIndex(1)));
}
tile_storage.set(&tile_pos, tile_entity);
}
}
let tile_size = TilemapTileSize {
x: TILE_SIZE.x as f32,
y: TILE_SIZE.y as f32,
};
let grid_size = tile_size.into();
let map_type = TilemapType::default();
cmds.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
The text was updated successfully, but these errors were encountered:
Its a basic maze generation
The text was updated successfully, but these errors were encountered: