Skip to content

Commit

Permalink
track entity rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Feb 24, 2024
1 parent 5c85158 commit 64fceff
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
53 changes: 50 additions & 3 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -839,13 +838,21 @@ 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| {
let mut position = entity.get_mut::<Position>().unwrap();
if new_pos != **position {
**position = new_pos;
}
let mut look_direction = entity.get_mut::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}),
});
} else {
Expand Down Expand Up @@ -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| {
Expand All @@ -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::<LookDirection>().unwrap();
if new_look_direction != *look_direction {
*look_direction = new_look_direction;
}
}),
});
} else {
Expand All @@ -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::<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) => {
debug!("Got keep alive packet {p:?} for {player_entity:?}");
Expand Down
2 changes: 1 addition & 1 deletion azalea-entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 64fceff

Please sign in to comment.