Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

could some one turn this in to an example ? #517

Open
xiro-codes opened this issue Feb 29, 2024 · 0 comments
Open

could some one turn this in to an example ? #517

xiro-codes opened this issue Feb 29, 2024 · 0 comments

Comments

@xiro-codes
Copy link

Its a basic maze generation

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()
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant