From 64fceff1cc65ee1dd1c72f08004e40179be9f9a4 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 24 Feb 2024 00:39:38 -0600 Subject: [PATCH] track entity rotations --- azalea-client/src/packet_handling/game.rs | 53 +++++++++++++++++++++-- azalea-entity/src/lib.rs | 2 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/azalea-client/src/packet_handling/game.rs b/azalea-client/src/packet_handling/game.rs index bd88258ae..30b736c08 100644 --- a/azalea-client/src/packet_handling/game.rs +++ b/azalea-client/src/packet_handling/game.rs @@ -419,7 +419,6 @@ pub fn process_packet_events(ecs: &mut World) { debug!("Got recipe packet"); } ClientboundGamePacket::PlayerPosition(p) => { - // TODO: reply with teleport confirm debug!("Got player position packet {p:?}"); #[allow(clippy::type_complexity)] @@ -839,6 +838,10 @@ pub fn process_packet_events(ecs: &mut World) { if let Some(entity) = entity { let new_pos = p.position; + let new_look_direction = LookDirection { + x_rot: (p.x_rot as i32 * 360) as f32 / 256., + y_rot: (p.y_rot as i32 * 360) as f32 / 256., + }; commands.entity(entity).add(RelativeEntityUpdate { partial_world: instance_holder.partial_instance.clone(), update: Box::new(move |entity| { @@ -846,6 +849,10 @@ pub fn process_packet_events(ecs: &mut World) { if new_pos != **position { **position = new_pos; } + let mut look_direction = entity.get_mut::().unwrap(); + if new_look_direction != *look_direction { + *look_direction = new_look_direction; + } }), }); } else { @@ -903,6 +910,11 @@ pub fn process_packet_events(ecs: &mut World) { if let Some(entity) = entity { let delta = p.delta.clone(); + let new_look_direction = LookDirection { + x_rot: (p.x_rot as i32 * 360) as f32 / 256., + y_rot: (p.y_rot as i32 * 360) as f32 / 256., + }; + commands.entity(entity).add(RelativeEntityUpdate { partial_world: instance_holder.partial_instance.clone(), update: Box::new(move |entity_mut| { @@ -911,6 +923,10 @@ pub fn process_packet_events(ecs: &mut World) { if new_pos != **position { **position = new_pos; } + let mut look_direction = entity_mut.get_mut::().unwrap(); + if new_look_direction != *look_direction { + *look_direction = new_look_direction; + } }), }); } else { @@ -923,8 +939,39 @@ pub fn process_packet_events(ecs: &mut World) { system_state.apply(ecs); } - ClientboundGamePacket::MoveEntityRot(_p) => { - // debug!("Got move entity rot packet {p:?}"); + ClientboundGamePacket::MoveEntityRot(p) => { + let mut system_state: SystemState<( + Commands, + Query<(&EntityIdIndex, &InstanceHolder)>, + )> = SystemState::new(ecs); + let (mut commands, mut query) = system_state.get_mut(ecs); + let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap(); + + let entity = entity_id_index.get(&MinecraftEntityId(p.entity_id)); + + if let Some(entity) = entity { + let new_look_direction = LookDirection { + x_rot: (p.x_rot as i32 * 360) as f32 / 256., + y_rot: (p.y_rot as i32 * 360) as f32 / 256., + }; + + commands.entity(entity).add(RelativeEntityUpdate { + partial_world: instance_holder.partial_instance.clone(), + update: Box::new(move |entity_mut| { + let mut look_direction = entity_mut.get_mut::().unwrap(); + if new_look_direction != *look_direction { + *look_direction = new_look_direction; + } + }), + }); + } else { + warn!( + "Got move entity rot packet for unknown entity id {}", + p.entity_id + ); + } + + system_state.apply(ecs); } ClientboundGamePacket::KeepAlive(p) => { debug!("Got keep alive packet {p:?} for {player_entity:?}"); diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index eb5b5b252..f39a6e4f9 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -203,7 +203,7 @@ impl From<&LastSentPosition> for BlockPos { pub struct Jumping(bool); /// A component that contains the direction an entity is looking. -#[derive(Debug, Component, Clone, Default)] +#[derive(Debug, Component, Clone, Default, PartialEq)] pub struct LookDirection { pub x_rot: f32, pub y_rot: f32,