extern crate alloc; use alloc::collections::BTreeMap; use crate::chunk::{Chunk, ChunkId}; use crate::math::Vec3; use crate::procgen::{ProcgenParams, generate_rooms}; pub struct World { active: Option, chunks: BTreeMap, seed: u64, } impl World { pub fn new(seed: u64) -> Self { Self { chunks: BTreeMap::new(), active: None, seed, } } pub fn insert(&mut self, chunk: Chunk) { let id = chunk.id(); self.chunks.insert(id, chunk); } pub fn get(&self, id: &ChunkId) -> Option<&Chunk> { self.chunks.get(id) } pub fn get_mut(&mut self, id: &ChunkId) -> Option<&mut Chunk> { self.chunks.get_mut(id) } pub fn remove(&mut self, id: &ChunkId) -> Option { self.chunks.remove(id) } pub fn active(&self) -> Option<&Chunk> { self.active.as_ref().and_then(|id| self.chunks.get(id)) } pub fn active_id(&self) -> Option { self.active } pub fn set_active(&mut self, id: ChunkId) { self.active = Some(id); } pub fn clear_active(&mut self) { self.active = None; } pub fn trace(&self, from: Vec3, to: Vec3, radius: f32) -> Vec3 { if let Some(chunk) = self.active() { return chunk.trace(from, to, radius); } to } pub fn generate_bsp(&mut self, id: u32, params: Option<&ProcgenParams>) -> &Chunk { let chunk_id = ChunkId::Bsp(id); if !self.chunks.contains_key(&chunk_id) { let default_params = ProcgenParams { seed: (self.seed as u32).wrapping_add(id), ..Default::default() }; let p = params.unwrap_or(&default_params); let bsp = generate_rooms(p); self.chunks.insert(chunk_id, Chunk::Bsp { id, bsp, version: 1 }); } self.chunks.get(&chunk_id).unwrap() } pub fn contains(&self, id: &ChunkId) -> bool { self.chunks.contains_key(id) } pub fn iter(&self) -> impl Iterator { self.chunks.iter() } } impl Default for World { fn default() -> Self { Self::new(12345) } }