aboutsummaryrefslogtreecommitdiff
path: root/azalea-world/src/lib.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-06-19 00:30:24 -0500
committermat <github@matdoes.dev>2022-06-19 00:30:24 -0500
commitbb6b116cb81a64deeb5ee8c1d021f27dba1cbc58 (patch)
tree8d41411757cf46c8ca0277a1466385d290f984b4 /azalea-world/src/lib.rs
parentfc3151f89db1cf018bfebebb8f102e20911e64d3 (diff)
downloadazalea-drasl-bb6b116cb81a64deeb5ee8c1d021f27dba1cbc58.tar.xz
Improvements to azalea-world for entities
Diffstat (limited to 'azalea-world/src/lib.rs')
-rw-r--r--azalea-world/src/lib.rs80
1 files changed, 49 insertions, 31 deletions
diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs
index b47126d4..746143c7 100644
--- a/azalea-world/src/lib.rs
+++ b/azalea-world/src/lib.rs
@@ -6,7 +6,8 @@ mod entity;
mod palette;
use azalea_block::BlockState;
-use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos};
+use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos, EntityPos};
+use azalea_entity::Entity;
use azalea_protocol::mc_buf::{McBufReadable, McBufWritable};
pub use bit_storage::BitStorage;
pub use chunk::{Chunk, ChunkStorage};
@@ -26,61 +27,78 @@ mod tests {
}
}
+#[derive(Debug)]
pub struct World {
- pub storage: ChunkStorage,
- pub entities: EntityStorage,
- pub height: u32,
- pub min_y: i32,
+ chunk_storage: ChunkStorage,
+ entity_storage: EntityStorage,
}
impl World {
+ pub fn new(chunk_radius: u32, height: u32, min_y: i32) -> Self {
+ World {
+ chunk_storage: ChunkStorage::new(chunk_radius, height, min_y),
+ entity_storage: EntityStorage::new(),
+ }
+ }
+
pub fn replace_with_packet_data(
&mut self,
pos: &ChunkPos,
data: &mut impl Read,
) -> Result<(), String> {
- if !self.storage.in_range(pos) {
- println!(
- "Ignoring chunk since it's not in the view range: {}, {}",
- pos.x, pos.z
- );
- return Ok(());
- }
- // let existing_chunk = &self.storage[pos];
+ self.chunk_storage.replace_with_packet_data(pos, data)
+ }
- let chunk = Arc::new(Mutex::new(Chunk::read_with_world(data, self)?));
- println!("Loaded chunk {:?}", pos);
- self.storage[pos] = Some(chunk);
+ pub fn update_view_center(&mut self, pos: &ChunkPos) {
+ self.chunk_storage.view_center = *pos;
+ }
+
+ pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> {
+ self.chunk_storage.get_block_state(pos, self.min_y())
+ }
+ pub fn move_entity(&mut self, entity_id: u32, new_pos: EntityPos) -> Result<(), String> {
+ let entity = self
+ .entity_storage
+ .get_mut_by_id(entity_id)
+ .ok_or("Moving entity that doesn't exist".to_string())?;
+ let old_chunk = ChunkPos::from(entity.pos());
+ let new_chunk = ChunkPos::from(&new_pos);
+ // this is fine because we update the chunk below
+ entity.unsafe_move(new_pos);
+ if old_chunk != new_chunk {
+ self.entity_storage
+ .update_entity_chunk(entity_id, &old_chunk, &new_chunk);
+ }
Ok(())
}
- pub fn update_view_center(&mut self, pos: &ChunkPos) {
- self.storage.view_center = *pos;
+ pub fn add_entity(&mut self, entity: Entity) {
+ self.entity_storage.insert(entity);
}
- pub fn get_block_state(&self, pos: &BlockPos) -> Option<BlockState> {
- self.storage.get_block_state(pos, self.min_y)
+ pub fn height(&self) -> u32 {
+ self.chunk_storage.height
+ }
+
+ pub fn min_y(&self) -> i32 {
+ self.chunk_storage.min_y
+ }
+
+ pub fn entity_by_id(&self, id: u32) -> Option<&Entity> {
+ self.entity_storage.get_by_id(id)
}
}
+
impl Index<&ChunkPos> for World {
type Output = Option<Arc<Mutex<Chunk>>>;
fn index(&self, pos: &ChunkPos) -> &Self::Output {
- &self.storage[pos]
+ &self.chunk_storage[pos]
}
}
impl IndexMut<&ChunkPos> for World {
fn index_mut<'a>(&'a mut self, pos: &ChunkPos) -> &'a mut Self::Output {
- &mut self.storage[pos]
+ &mut self.chunk_storage[pos]
}
}
-// impl Index<&BlockPos> for World {
-// type Output = Option<Arc<Mutex<Chunk>>>;
-
-// fn index(&self, pos: &BlockPos) -> &Self::Output {
-// let chunk = &self[ChunkPos::from(pos)];
-// // chunk.
-
-// }
-// }