add iterator to world
This commit is contained in:
parent
88c887a2ea
commit
1b98dad9d0
1 changed files with 21 additions and 2 deletions
|
@ -56,10 +56,18 @@ pub struct Chunk {
|
|||
}
|
||||
|
||||
impl Chunk {
|
||||
fn get_tile(&self, local_chunk_coords: UVec2) -> Option<&Tile> {
|
||||
pub fn get_tile(&self, local_chunk_coords: UVec2) -> Option<&Tile> {
|
||||
self.tiles
|
||||
.get((local_chunk_coords.y * CHUNK_SIZE + local_chunk_coords.x) as usize)
|
||||
}
|
||||
|
||||
/// iterate over all tiles and its local chunk coords
|
||||
pub fn iter_tiles(&self) -> impl Iterator<Item = (UVec2, &Tile)> {
|
||||
self.tiles.iter().enumerate().map(|(i, tile)| {
|
||||
let i = i as u32;
|
||||
(UVec2::new(i % CHUNK_SIZE, i / CHUNK_SIZE), tile)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -70,7 +78,7 @@ pub struct Overworld {
|
|||
impl Overworld {
|
||||
/// Return a [`Tile`] at the given world coordinates, or `None` if that tile has not
|
||||
/// been generated yet.
|
||||
fn get_tile(&self, world_coords: IVec2) -> Option<&Tile> {
|
||||
pub fn get_tile(&self, world_coords: IVec2) -> Option<&Tile> {
|
||||
let tile_coords = IVec2 {
|
||||
x: world_coords.x.div_euclid(CHUNK_SIZE as _),
|
||||
y: world_coords.y.div_euclid(CHUNK_SIZE as _)
|
||||
|
@ -87,4 +95,15 @@ impl Overworld {
|
|||
fn get_or_generate_tile(&self, world_coords: IVec2) -> &Tile {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/// iterate over all tiles and its global coords
|
||||
pub fn iter_tiles(&self) -> impl Iterator<Item = (IVec2, &Tile)> {
|
||||
self.chunks.iter().flat_map(|(chunk_coords, chunk)| {
|
||||
chunk.iter_tiles().map(|(local_coords, tile)| {
|
||||
// never fail because chunksize fits alswas into i32
|
||||
let local_coords: IVec2 = local_coords.try_into().unwrap();
|
||||
(local_coords + (*chunk_coords * CHUNK_SIZE as i32), tile)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue