From 4eeda83ba4bdb5e20c765a46e43227f4f9b39b69 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 24 Feb 2024 06:02:11 -0600 Subject: [PATCH] add some more convenience functions --- azalea-client/src/movement.rs | 12 ++++++++--- azalea-core/src/position.rs | 19 ++++++++++++++++++ azalea-entity/src/lib.rs | 38 +++++++++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 2f70a40ee..ba47b7c84 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -86,9 +86,7 @@ impl Client { /// Returns whether the player will try to jump next tick. pub fn jumping(&self) -> bool { - let mut ecs = self.ecs.lock(); - let jumping_ref = self.query::<&Jumping>(&mut ecs); - **jumping_ref + *self.component::() } /// Sets the direction the client is looking. `y_rot` is yaw (looking to the @@ -101,6 +99,14 @@ impl Client { (look_direction.y_rot, look_direction.x_rot) = (y_rot, x_rot); } + + /// Returns the direction the client is looking. The first value is the y + /// rotation (ie. yaw, looking to the side) and the second value is the x + /// rotation (ie. pitch, looking up and down). + pub fn direction(&self) -> (f32, f32) { + let look_direction = self.component::(); + (look_direction.y_rot, look_direction.x_rot) + } } /// A component that contains the look direction that was last sent over the diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index e98640357..369607c5c 100755 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -186,6 +186,25 @@ macro_rules! vec3_impl { } } } + + impl From<($type, $type, $type)> for $name { + #[inline] + fn from(pos: ($type, $type, $type)) -> Self { + Self::new(pos.0, pos.1, pos.2) + } + } + impl From<&($type, $type, $type)> for $name { + #[inline] + fn from(pos: &($type, $type, $type)) -> Self { + Self::new(pos.0, pos.1, pos.2) + } + } + impl From<$name> for ($type, $type, $type) { + #[inline] + fn from(pos: $name) -> Self { + (pos.x, pos.y, pos.z) + } + } }; } diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index f39a6e4f9..e08284a92 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -23,7 +23,10 @@ use bevy_ecs::{bundle::Bundle, component::Component}; pub use data::*; use derive_more::{Deref, DerefMut}; pub use dimensions::EntityDimensions; -use std::fmt::Debug; +use std::{ + fmt::Debug, + hash::{Hash, Hasher}, +}; use uuid::Uuid; pub use crate::plugin::*; @@ -199,15 +202,42 @@ impl From<&LastSentPosition> for BlockPos { /// /// If this is true, the entity will try to jump every tick. (It's equivalent to /// the space key being held in vanilla.) -#[derive(Debug, Component, Clone, Deref, DerefMut, Default)] +#[derive(Debug, Component, Copy, Clone, Deref, DerefMut, Default)] pub struct Jumping(bool); /// A component that contains the direction an entity is looking. -#[derive(Debug, Component, Clone, Default, PartialEq)] +#[derive(Debug, Component, Copy, Clone, Default, PartialEq)] pub struct LookDirection { - pub x_rot: f32, + /// Left and right. Aka yaw. pub y_rot: f32, + /// Up and down. Aka pitch. + pub x_rot: f32, +} + +impl LookDirection { + pub fn new(y_rot: f32, x_rot: f32) -> Self { + Self { y_rot, x_rot } + } +} + +impl From for (f32, f32) { + fn from(value: LookDirection) -> Self { + (value.y_rot, value.x_rot) + } +} +impl From<(f32, f32)> for LookDirection { + fn from((y_rot, x_rot): (f32, f32)) -> Self { + Self { y_rot, x_rot } + } +} + +impl Hash for LookDirection { + fn hash(&self, state: &mut H) { + self.y_rot.to_bits().hash(state); + self.x_rot.to_bits().hash(state); + } } +impl Eq for LookDirection {} /// The physics data relating to the entity, such as position, velocity, and /// bounding box.