diff options
| author | mat <github@matdoes.dev> | 2022-06-23 21:31:19 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-06-23 21:31:19 -0500 |
| commit | 2cdbdcaa27c812e569c7c1a13d83182446e7f18b (patch) | |
| tree | c4f2970d847ec411bf6e859bc1521e93bc8b3c81 /azalea-world/src | |
| parent | 37c6618c16319a7f40fd2e165190407472598e84 (diff) | |
| download | azalea-drasl-2cdbdcaa27c812e569c7c1a13d83182446e7f18b.tar.xz | |
move_entity_with_delta
Diffstat (limited to 'azalea-world/src')
| -rw-r--r-- | azalea-world/src/lib.rs | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index cbc5c633..bc73c13d 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}; +use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta}; use azalea_entity::Entity; pub use bit_storage::BitStorage; pub use chunk::{Chunk, ChunkStorage}; @@ -61,6 +61,7 @@ impl World { .entity_storage .get_mut_by_id(entity_id) .ok_or_else(|| "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 @@ -72,21 +73,28 @@ impl World { Ok(()) } - // pub fn move_entity_with_delta(&mut self, entity_id: u32, delta: PositionDelta) -> Result<(), String> { - // let entity = self - // .entity_storage - // .get_mut_by_id(entity_id) - // .ok_or_else(|| "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 move_entity_with_delta( + &mut self, + entity_id: u32, + delta: &PositionDelta, + ) -> Result<(), String> { + let entity = self + .entity_storage + .get_mut_by_id(entity_id) + .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + let new_pos = entity.pos().with_delta(delta); + + 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 add_entity(&mut self, entity: Entity) { self.entity_storage.insert(entity); |
