aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-protocol')
-rw-r--r--azalea-protocol/src/common/movements.rs63
-rw-r--r--azalea-protocol/src/packets/game/c_teleport_entity.rs2
2 files changed, 61 insertions, 4 deletions
diff --git a/azalea-protocol/src/common/movements.rs b/azalea-protocol/src/common/movements.rs
index ffc3452f..a70342b3 100644
--- a/azalea-protocol/src/common/movements.rs
+++ b/azalea-protocol/src/common/movements.rs
@@ -1,8 +1,11 @@
-use std::io::{self, Cursor, Write};
+use std::{
+ io::{self, Cursor, Write},
+ ops::Add,
+};
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
-use azalea_core::{bitset::FixedBitSet, position::Vec3};
-use azalea_entity::LookDirection;
+use azalea_core::{bitset::FixedBitSet, math, position::Vec3};
+use azalea_entity::{LookDirection, Physics, Position};
/// The updated position, velocity, and rotations for an entity.
///
@@ -32,6 +35,60 @@ impl RelativeMovements {
pub fn all_absolute() -> Self {
RelativeMovements::default()
}
+ pub fn all_relative() -> Self {
+ RelativeMovements {
+ x: true,
+ y: true,
+ z: true,
+ y_rot: true,
+ x_rot: true,
+ delta_x: true,
+ delta_y: true,
+ delta_z: true,
+ rotate_delta: true,
+ }
+ }
+
+ pub fn apply(
+ &self,
+ change: &PositionMoveRotation,
+ position: &mut Position,
+ direction: &mut LookDirection,
+ physics: &mut Physics,
+ ) {
+ let new_position = Vec3::new(
+ apply_change(position.x, self.x, change.pos.x),
+ apply_change(position.y, self.y, change.pos.y),
+ apply_change(position.z, self.z, change.pos.z),
+ );
+
+ let new_look_direction = LookDirection::new(
+ apply_change(direction.y_rot, self.y_rot, change.look_direction.y_rot),
+ apply_change(direction.x_rot, self.x_rot, change.look_direction.x_rot),
+ );
+
+ let mut new_delta = physics.velocity;
+ if self.rotate_delta {
+ let y_rot_delta = direction.y_rot - new_look_direction.y_rot;
+ let x_rot_delta = direction.x_rot - new_look_direction.x_rot;
+ new_delta = new_delta
+ .x_rot(math::to_radians(x_rot_delta as f64) as f32)
+ .y_rot(math::to_radians(y_rot_delta as f64) as f32);
+ }
+ let new_delta = Vec3::new(
+ apply_change(new_delta.x, self.delta_x, change.delta.x),
+ apply_change(new_delta.y, self.delta_y, change.delta.y),
+ apply_change(new_delta.z, self.delta_z, change.delta.z),
+ );
+
+ **position = new_position;
+ *direction = new_look_direction;
+ physics.velocity = new_delta;
+ }
+}
+
+fn apply_change<T: Add<Output = T>>(base: T, condition: bool, change: T) -> T {
+ if condition { base + change } else { change }
}
impl AzaleaRead for RelativeMovements {
diff --git a/azalea-protocol/src/packets/game/c_teleport_entity.rs b/azalea-protocol/src/packets/game/c_teleport_entity.rs
index 92b8f1eb..e19ded58 100644
--- a/azalea-protocol/src/packets/game/c_teleport_entity.rs
+++ b/azalea-protocol/src/packets/game/c_teleport_entity.rs
@@ -9,6 +9,6 @@ pub struct ClientboundTeleportEntity {
#[var]
pub id: MinecraftEntityId,
pub change: PositionMoveRotation,
- pub relatives: RelativeMovements,
+ pub relative: RelativeMovements,
pub on_ground: bool,
}