Loading world, not working yet

This commit is contained in:
Piotrek
2021-05-08 22:55:25 +02:00
parent 547f66d214
commit d0b7a781b4
4 changed files with 91 additions and 20 deletions
+34
View File
@@ -4,6 +4,7 @@ pub mod block;
use std::path::PathBuf;
use std::path::Path;
use std::io::Write;
use std::io::Read;
use std::fs;
use cgmath::Point3;
use std::collections::HashMap;
@@ -42,6 +43,39 @@ impl World {
}
}
pub fn load(&mut self) {
// Get directory
let mut dir = dirs::data_local_dir().unwrap();
dir.push("Voxelgame");
if !dir.is_dir() {
println!("Not exists: {:?}", dir);
return;
}
// Load all chunks
for entry in fs::read_dir(dir).unwrap() {
let path: PathBuf = entry.unwrap().path();
if path.extension().unwrap().to_str() != Some("dat") {
continue;
}
let stem = path.file_stem().unwrap().to_str().unwrap();
if !stem.starts_with("chunk") {
continue;
}
let s: Vec<&str> = stem.split("_").collect();
let pos = Point3{x: s[1].parse::<u32>().unwrap(), y: s[2].parse::<u32>().unwrap(), z: s[3].parse::<u32>().unwrap()};
let mut bytes = Vec::new();
let mut file = fs::File::open(path).expect("Failed to open file");
file.read_to_end(&mut bytes).expect("Failed to read file");
let chunk = WorldChunk::from_bytes(bytes);
println!("loaded {:?}", pos);
self.chunks.insert(pos, chunk);
}
}
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;