From b030b0ea330c674415f7e30634957167b2fa6a6d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 24 Jun 2022 23:10:59 -0500 Subject: start adding moving --- azalea-world/src/entity.rs | 27 +++++++++++++++++++++++++-- azalea-world/src/lib.rs | 13 +++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'azalea-world/src') diff --git a/azalea-world/src/entity.rs b/azalea-world/src/entity.rs index e4e9864f..5219e410 100644 --- a/azalea-world/src/entity.rs +++ b/azalea-world/src/entity.rs @@ -3,12 +3,13 @@ use azalea_entity::Entity; use log::warn; use nohash_hasher::{IntMap, IntSet}; use std::collections::HashMap; +use uuid::Uuid; #[derive(Debug)] pub struct EntityStorage { by_id: IntMap, - // TODO: this doesn't work yet (should be updated in the set_pos method in azalea-entity) by_chunk: HashMap>, + by_uuid: HashMap, } impl EntityStorage { @@ -16,6 +17,7 @@ impl EntityStorage { Self { by_id: IntMap::default(), by_chunk: HashMap::default(), + by_uuid: HashMap::default(), } } @@ -26,6 +28,7 @@ impl EntityStorage { .entry(ChunkPos::from(entity.pos())) .or_default() .insert(entity.id); + self.by_uuid.insert(entity.uuid, entity.id); self.by_id.insert(entity.id, entity); } @@ -34,9 +37,13 @@ impl EntityStorage { pub fn remove_by_id(&mut self, id: u32) { if let Some(entity) = self.by_id.remove(&id) { let entity_chunk = ChunkPos::from(entity.pos()); + let entity_uuid = entity.uuid; if self.by_chunk.remove(&entity_chunk).is_none() { warn!("Tried to remove entity with id {id} from chunk {entity_chunk:?} but it was not found."); } + if self.by_uuid.remove(&entity_uuid).is_none() { + warn!("Tried to remove entity with id {id} from uuid {entity_uuid:?} but it was not found."); + } } else { warn!("Tried to remove entity with id {id} but it was not found.") } @@ -54,11 +61,27 @@ impl EntityStorage { self.by_id.get_mut(&id) } + /// Get a reference to an entity by its uuid. + #[inline] + pub fn get_by_uuid(&self, uuid: &Uuid) -> Option<&Entity> { + self.by_uuid.get(uuid).and_then(|id| self.by_id.get(id)) + } + + /// Get a mutable reference to an entity by its uuid. + #[inline] + pub fn get_mut_by_uuid(&mut self, uuid: &Uuid) -> Option<&mut Entity> { + self.by_uuid.get(uuid).and_then(|id| self.by_id.get_mut(id)) + } + /// Clear all entities in a chunk. pub fn clear_chunk(&mut self, chunk: &ChunkPos) { if let Some(entities) = self.by_chunk.remove(chunk) { for entity_id in entities { - self.by_id.remove(&entity_id); + if let Some(entity) = self.by_id.remove(&entity_id) { + self.by_uuid.remove(&entity.uuid); + } else { + warn!("While clearing chunk {chunk:?}, found an entity that isn't in by_id {entity_id}."); + } } } } diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index bc73c13d..3afa4fee 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -6,7 +6,7 @@ mod entity; mod palette; use azalea_block::BlockState; -use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta}; +use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta8}; use azalea_entity::Entity; pub use bit_storage::BitStorage; pub use chunk::{Chunk, ChunkStorage}; @@ -16,6 +16,7 @@ use std::{ ops::{Index, IndexMut}, sync::{Arc, Mutex}, }; +use uuid::Uuid; #[cfg(test)] mod tests { @@ -76,7 +77,7 @@ impl World { pub fn move_entity_with_delta( &mut self, entity_id: u32, - delta: &PositionDelta, + delta: &PositionDelta8, ) -> Result<(), String> { let entity = self .entity_storage @@ -112,6 +113,14 @@ impl World { self.entity_storage.get_by_id(id) } + pub fn mut_entity_by_id(&mut self, id: u32) -> Option<&mut Entity> { + self.entity_storage.get_mut_by_id(id) + } + + pub fn entity_by_uuid(&self, uuid: &Uuid) -> Option<&Entity> { + self.entity_storage.get_by_uuid(uuid) + } + /// Get an iterator over all entities. #[inline] pub fn entities(&self) -> std::collections::hash_map::Values<'_, u32, Entity> { -- cgit v1.2.3