Skip to content

Commit

Permalink
Allow From<Vec3<T>> for (_,_,_) for easy destructuring.
Browse files Browse the repository at this point in the history
  • Loading branch information
StripedMonkey committed Sep 5, 2024
1 parent 286253c commit 4dce8aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
6 changes: 6 additions & 0 deletions pumpkin-core/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl<T> From<(T, T, T)> for Vector3<T> {
}
}

impl<T> From<Vector3<T>> for (T, T, T) {
fn from(vector: Vector3<T>) -> Self {
(vector.x, vector.y, vector.z)
}
}

pub trait Math:
Mul<Output = Self>
+ Neg<Output = Self>
Expand Down
18 changes: 6 additions & 12 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ impl Player {
return;
}
let entity = &mut self.entity;
self.lastx = entity.pos.x;
self.lasty = entity.pos.y;
self.lastz = entity.pos.z;
self.last_position = entity.pos;
entity.set_pos(
Self::clamp_horizontal(position.x),
Self::clamp_vertical(position.feet_y),
Expand All @@ -89,9 +87,8 @@ impl Player {
// send new position to all other players
let on_ground = self.on_ground;
let entity_id = entity.entity_id;
let (x, lastx) = (entity.pos.x, self.lastx);
let (y, lasty) = (entity.pos.y, self.lasty);
let (z, lastz) = (entity.pos.z, self.lastz);
let (x, y, z) = entity.pos.into();
let (lastx, lasty, lastz) = self.last_position.into();
let world = self.world.clone();
let world = world.lock().await;
world.broadcast_packet(
Expand Down Expand Up @@ -125,9 +122,7 @@ impl Player {
}
let entity = &mut self.entity;

self.lastx = entity.pos.x;
self.lasty = entity.pos.y;
self.lastz = entity.pos.z;
self.last_position = entity.pos;
entity.set_pos(
Self::clamp_horizontal(position_rotation.x),
Self::clamp_vertical(position_rotation.feet_y),
Expand All @@ -139,9 +134,8 @@ impl Player {
// send new position to all other players
let on_ground = self.on_ground;
let entity_id = entity.entity_id;
let (x, lastx) = (entity.pos.x, self.lastx);
let (y, lasty) = (entity.pos.y, self.lasty);
let (z, lastz) = (entity.pos.z, self.lastz);
let (x, y, z) = entity.pos.into();
let (lastx, lasty, lastz) = self.last_position.into();
let yaw = modulus(entity.yaw * 256.0 / 360.0, 256.0);
let pitch = modulus(entity.pitch * 256.0 / 360.0, 256.0);
// let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor();
Expand Down
12 changes: 3 additions & 9 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ pub struct Player {
/// send `send_abilties_update` when changed
pub abilities: PlayerAbilities,

pub lastx: f64,
pub lasty: f64,
pub lastz: f64,
pub last_position: Vector3<f64>,
// Client side value, Should be not trusted
pub on_ground: bool,

Expand Down Expand Up @@ -132,9 +130,7 @@ impl Player {
abilities: PlayerAbilities::default(),
gamemode,
watched_section: Vector3::new(0, 0, 0),
lastx: 0.0,
lasty: 0.0,
lastz: 0.0,
last_position: Vector3::new(0.0, 0.0, 0.0),
}
}

Expand Down Expand Up @@ -255,9 +251,7 @@ impl Player {
}
let entity = &mut self.entity;
entity.set_pos(x, y, z);
self.lastx = x;
self.lasty = y;
self.lastz = z;
self.last_position = entity.pos;
entity.yaw = yaw;
entity.pitch = pitch;
self.awaiting_teleport = Some((self.teleport_id_count.into(), Vector3::new(x, y, z)));
Expand Down

0 comments on commit 4dce8aa

Please sign in to comment.