diff --git a/src/world.rs b/src/world.rs index 0ff9428..1d15828 100644 --- a/src/world.rs +++ b/src/world.rs @@ -253,18 +253,6 @@ impl Chunk { fn at_mut(&mut self, local: ChunkLocalTilePos) -> &mut Tile { &mut self.tiles[CHUNK_EXTENT as usize * local.y as usize + local.x as usize] } - - fn load_from_region(data: &[u8], x: u8, y: u8) -> Self { - let byte_idx = loc_byte_idx_xy(x, y); - let mut tiles = default_chunk_tiles(); - for (i, t) in tiles.iter_mut().enumerate() { - let off = byte_idx + (i * TILE_BYTES); - t.bg = u16::from_le_bytes(data[off..off + 2].try_into().unwrap()); - t.mid = u16::from_le_bytes(data[off + 2..off + 4].try_into().unwrap()); - t.fg = u16::from_le_bytes(data[off + 4..off + 6].try_into().unwrap()); - } - Self { tiles } - } } fn chunk_exists(reg_filename: &str, pos: ChunkPos) -> bool { diff --git a/src/world/serialization.rs b/src/world/serialization.rs index adc1846..e0767bf 100644 --- a/src/world/serialization.rs +++ b/src/world/serialization.rs @@ -9,7 +9,7 @@ use crate::world::{ REGION_BYTES, TILE_BYTES, }; -use super::{Chunk, ChunkPos}; +use super::{default_chunk_tiles, loc_byte_idx_xy, Chunk, ChunkPos}; pub(super) fn save_chunk(pos: &ChunkPos, chk: &Chunk) { let reg_file_name = format_reg_file_name(pos.region()); @@ -61,6 +61,20 @@ pub(super) fn save_chunk(pos: &ChunkPos, chk: &Chunk) { const COMP_LEVEL: i32 = 9; +impl Chunk { + pub fn load_from_region(data: &[u8], x: u8, y: u8) -> Self { + let byte_idx = loc_byte_idx_xy(x, y); + let mut tiles = default_chunk_tiles(); + for (i, t) in tiles.iter_mut().enumerate() { + let off = byte_idx + (i * TILE_BYTES); + t.bg = u16::from_le_bytes(data[off..off + 2].try_into().unwrap()); + t.mid = u16::from_le_bytes(data[off + 2..off + 4].try_into().unwrap()); + t.fg = u16::from_le_bytes(data[off + 4..off + 6].try_into().unwrap()); + } + Self { tiles } + } +} + #[test] fn test_chunk_seri() { env_logger::builder()