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
86
pxl8d/src/chunk/stream.rs
Normal file
86
pxl8d/src/chunk/stream.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
extern crate alloc;
|
||||
|
||||
use alloc::collections::BTreeMap;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use crate::chunk::ChunkId;
|
||||
use crate::math::Vec3;
|
||||
use crate::transport::ChunkMessage;
|
||||
use crate::voxel::VoxelWorld;
|
||||
|
||||
pub struct ClientChunkState {
|
||||
known: BTreeMap<ChunkId, u32>,
|
||||
pending: Vec<ChunkId>,
|
||||
pending_messages: Vec<ChunkMessage>,
|
||||
}
|
||||
|
||||
impl ClientChunkState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
known: BTreeMap::new(),
|
||||
pending: Vec::new(),
|
||||
pending_messages: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request(&mut self, id: ChunkId) {
|
||||
if !self.known.contains_key(&id) && !self.pending.contains(&id) {
|
||||
self.pending.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_vxl_radius(&mut self, pos: Vec3, radius: i32, world: &VoxelWorld) {
|
||||
let cx = VoxelWorld::world_to_chunk(pos.x);
|
||||
let cy = VoxelWorld::world_to_chunk(pos.y);
|
||||
let cz = VoxelWorld::world_to_chunk(pos.z);
|
||||
|
||||
for dz in -radius..=radius {
|
||||
for dy in -radius..=radius {
|
||||
for dx in -radius..=radius {
|
||||
if world.get_chunk(cx + dx, cy + dy, cz + dz).is_some() {
|
||||
self.request(ChunkId::Vxl(cx + dx, cy + dy, cz + dz));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_pending(&mut self) -> Option<ChunkId> {
|
||||
self.pending.pop()
|
||||
}
|
||||
|
||||
pub fn queue_message(&mut self, msg: ChunkMessage) {
|
||||
self.pending_messages.push(msg);
|
||||
}
|
||||
|
||||
pub fn queue_messages(&mut self, msgs: Vec<ChunkMessage>) {
|
||||
self.pending_messages.extend(msgs);
|
||||
}
|
||||
|
||||
pub fn next_message(&mut self) -> Option<ChunkMessage> {
|
||||
if self.pending_messages.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(self.pending_messages.remove(0))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_sent(&mut self, id: ChunkId, version: u32) {
|
||||
self.known.insert(id, version);
|
||||
}
|
||||
|
||||
pub fn has_pending(&self) -> bool {
|
||||
!self.pending.is_empty() || !self.pending_messages.is_empty()
|
||||
}
|
||||
|
||||
pub fn clear_pending(&mut self) {
|
||||
self.pending.clear();
|
||||
self.pending_messages.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ClientChunkState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue