Saving world to file

This commit is contained in:
Piotrek
2021-05-08 18:42:32 +02:00
parent 989b75f54d
commit 547f66d214
8 changed files with 130 additions and 29 deletions
+21
View File
@@ -1,6 +1,10 @@
mod generator;
pub mod chunk;
pub mod block;
use std::path::PathBuf;
use std::path::Path;
use std::io::Write;
use std::fs;
use cgmath::Point3;
use std::collections::HashMap;
use generator::WorldGen;
@@ -21,6 +25,23 @@ impl World {
}
}
pub fn save(&self) {
// Get directory
let mut dir = dirs::data_local_dir().unwrap();
dir.push("Voxelgame");
fs::create_dir_all(&dir).expect("Failed to create directory");
// Save all chunks
for (pos, chunk) in self.chunks.iter() {
let bytes = chunk.to_bytes();
let filename = format!("chunk_{}_{}_{}.dat", pos.x, pos.y, pos.z);
let path: PathBuf = [dir.to_str().unwrap(), &filename].iter().collect();
let mut file = fs::File::create(path).expect("Failed to create file");
file.write(&bytes).unwrap();
}
}
pub fn gen_block(&mut self, block_pos: &Point3<u32>) -> &WorldBlock {
// Split world space to chunk and block space
let ch_pos = block_pos / chunk::SIZE;