stream world data from pxl8d to pxl8
This commit is contained in:
parent
39ee0fefb7
commit
a71a9840b2
55 changed files with 5290 additions and 2131 deletions
87
pxl8d/src/world.rs
Normal file
87
pxl8d/src/world.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
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<ChunkId>,
|
||||
chunks: BTreeMap<ChunkId, Chunk>,
|
||||
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<Chunk> {
|
||||
self.chunks.remove(id)
|
||||
}
|
||||
|
||||
pub fn set_active(&mut self, id: ChunkId) {
|
||||
self.active = Some(id);
|
||||
}
|
||||
|
||||
pub fn active(&self) -> Option<&Chunk> {
|
||||
self.active.as_ref().and_then(|id| self.chunks.get(id))
|
||||
}
|
||||
|
||||
pub fn active_id(&self) -> Option<ChunkId> {
|
||||
self.active
|
||||
}
|
||||
|
||||
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<Item = (&ChunkId, &Chunk)> {
|
||||
self.chunks.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for World {
|
||||
fn default() -> Self {
|
||||
Self::new(12345)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue