Saving world to file
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user