Static World Gen and Display for the Overworld #5

Merged
msrd0 merged 21 commits from overworld-house-gen into main 2024-07-06 20:23:25 +00:00
Showing only changes of commit 1b98dad9d0 - Show all commits

View file

@ -56,10 +56,18 @@ pub struct Chunk {
} }
impl 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 self.tiles
.get((local_chunk_coords.y * CHUNK_SIZE + local_chunk_coords.x) as usize) .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)] #[derive(Debug, Default)]
@ -70,7 +78,7 @@ pub struct Overworld {
impl Overworld { impl Overworld {
/// Return a [`Tile`] at the given world coordinates, or `None` if that tile has not /// Return a [`Tile`] at the given world coordinates, or `None` if that tile has not
/// been generated yet. /// 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 { let tile_coords = IVec2 {
x: world_coords.x.div_euclid(CHUNK_SIZE as _), x: world_coords.x.div_euclid(CHUNK_SIZE as _),
y: world_coords.y.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 { fn get_or_generate_tile(&self, world_coords: IVec2) -> &Tile {
unimplemented!() 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)
})
})
}
} }