World, WorldChunk, WorldBlock structure working

This commit is contained in:
Piotrek
2021-05-06 12:43:03 +02:00
parent c04a1aea11
commit d34196a6bd
5 changed files with 94 additions and 53 deletions
+31 -8
View File
@@ -1,26 +1,49 @@
mod generator;
use generator::WorldGen;
mod chunk;
use chunk::WorldChunk;
mod block;
use cgmath::Point3;
use std::collections::HashMap;
use generator::WorldGen;
use chunk::WorldChunk;
use block::WorldBlock;
pub struct World {
name: String,
chunks: Vec<WorldChunk> // Loaded chunks
chunks: HashMap<Point3<u32>, WorldChunk>
}
impl World {
pub fn new(name: String) -> Self {
Self { name, chunks: Vec::new() }
Self {
name,
chunks: HashMap::new()
}
}
fn load(&mut self) {
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;
let bl_pos = block_pos % chunk::SIZE;
// Create chunk if does not exist
if let None = self.chunks.get_mut(&ch_pos) {
self.chunks.insert(block_pos.clone(), WorldChunk::new());
}
// Generate block for chunk
let chunk = self.chunks.get_mut(block_pos).unwrap();
let block = chunk.get_block(&bl_pos);
WorldGen::generate(block);
block
}
fn save(&mut self) {
pub fn get_block(&mut self, block_pos: &Point3<u32>) -> Option<&WorldBlock> {
// Split world space to chunk and block space
let ch_pos = block_pos / chunk::SIZE;
let bl_pos = block_pos % chunk::SIZE;
// Return block if exists
if let Some(chunk) = self.chunks.get_mut(&ch_pos) {
return Some(chunk.get_block(&bl_pos));
}
None
}
}