1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

move_entity_with_delta

This commit is contained in:
mat 2022-06-23 21:31:19 -05:00
parent 37c6618c16
commit 2cdbdcaa27
7 changed files with 42 additions and 35 deletions

View file

@ -433,20 +433,16 @@ impl Client {
// println!("Got rotate head packet {:?}", p);
}
GamePacket::ClientboundMoveEntityPosPacket(p) => {
// println!("Got move entity pos packet {:?}", p);
let mut state_lock = state.lock()?;
let world = state_lock.world.as_mut().unwrap();
world.move_entity_with_delta(p.entity_id, &p.delta)?;
}
GamePacket::ClientboundMoveEntityPosRotPacket(p) => {
let mut state_lock = state.lock()?;
let world = state_lock.world.as_mut().unwrap();
// world.move_entity(
// p.entity_id,
// EntityPos {
// x: p.x,
// y: p.y,
// z: p.z,
// },
// )?;
world.move_entity_with_delta(p.entity_id, &p.delta)?;
}
GamePacket::ClientboundMoveEntityRotPacket(p) => {
println!("Got move entity rot packet {:?}", p);

View file

@ -20,10 +20,12 @@ impl PositionDelta {
}
impl EntityPos {
pub fn apply_delta(&mut self, delta: &PositionDelta) {
pub fn with_delta(&self, delta: &PositionDelta) -> EntityPos {
let (x, y, z) = delta.float();
self.x += x;
self.y += y;
self.z += z;
EntityPos {
x: self.x + x,
y: self.y + y,
z: self.z + z,
}
}
}

View file

@ -93,7 +93,8 @@ mod tests {
let mut buf = Vec::new();
ResourceLocation::new("minecraft:dirt")
.unwrap()
.write_into(&mut buf)?;
.write_into(&mut buf)
.unwrap();
let mut buf = Cursor::new(buf);

View file

@ -14,7 +14,7 @@ pub struct Entity {
/// The incrementing numerical id of the entity.
pub id: u32,
pub uuid: Uuid,
pub pos: EntityPos,
pos: EntityPos,
}
impl Entity {

View file

@ -1,11 +1,11 @@
use azalea_core::{EntityPos, PositionDelta};
use packet_macros::GamePacket;
use azalea_buf::McBuf;
use azalea_core::PositionDelta;
use packet_macros::GamePacket;
#[derive(Clone, Debug, McBuf, GamePacket)]
pub struct ClientboundMoveEntityPosPacket {
#[var]
pub entity_id: i32,
pub entity_id: u32,
pub delta: PositionDelta,
pub on_ground: bool,
}

View file

@ -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);

View file

@ -6,7 +6,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!");
// let address = "95.111.249.143:10000";
let address = "localhost:52722";
let address = "localhost:49982";
// let response = azalea_client::ping::ping_server(&address.try_into().unwrap())
// .await
// .unwrap();