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

track entity rotations

This commit is contained in:
mat 2024-02-24 00:39:38 -06:00
parent 5c85158e7c
commit 64fceff1cc
2 changed files with 51 additions and 4 deletions

View file

@ -419,7 +419,6 @@ pub fn process_packet_events(ecs: &mut World) {
debug!("Got recipe packet"); debug!("Got recipe packet");
} }
ClientboundGamePacket::PlayerPosition(p) => { ClientboundGamePacket::PlayerPosition(p) => {
// TODO: reply with teleport confirm
debug!("Got player position packet {p:?}"); debug!("Got player position packet {p:?}");
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
@ -839,6 +838,10 @@ pub fn process_packet_events(ecs: &mut World) {
if let Some(entity) = entity { if let Some(entity) = entity {
let new_pos = p.position; 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 { commands.entity(entity).add(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(), partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity| { update: Box::new(move |entity| {
@ -846,6 +849,10 @@ pub fn process_packet_events(ecs: &mut World) {
if new_pos != **position { if new_pos != **position {
**position = new_pos; **position = new_pos;
} }
let mut look_direction = entity.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}), }),
}); });
} else { } else {
@ -903,6 +910,11 @@ pub fn process_packet_events(ecs: &mut World) {
if let Some(entity) = entity { if let Some(entity) = entity {
let delta = p.delta.clone(); 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 { commands.entity(entity).add(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(), partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| { update: Box::new(move |entity_mut| {
@ -911,6 +923,10 @@ pub fn process_packet_events(ecs: &mut World) {
if new_pos != **position { if new_pos != **position {
**position = new_pos; **position = new_pos;
} }
let mut look_direction = entity_mut.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}), }),
}); });
} else { } else {
@ -923,8 +939,39 @@ pub fn process_packet_events(ecs: &mut World) {
system_state.apply(ecs); system_state.apply(ecs);
} }
ClientboundGamePacket::MoveEntityRot(_p) => { ClientboundGamePacket::MoveEntityRot(p) => {
// debug!("Got move entity rot packet {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::<LookDirection>().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) => { ClientboundGamePacket::KeepAlive(p) => {
debug!("Got keep alive packet {p:?} for {player_entity:?}"); debug!("Got keep alive packet {p:?} for {player_entity:?}");

View file

@ -203,7 +203,7 @@ impl From<&LastSentPosition> for BlockPos {
pub struct Jumping(bool); pub struct Jumping(bool);
/// A component that contains the direction an entity is looking. /// 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 struct LookDirection {
pub x_rot: f32, pub x_rot: f32,
pub y_rot: f32, pub y_rot: f32,