aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2024-02-24 06:02:11 -0600
committermat <git@matdoes.dev>2024-02-24 06:02:11 -0600
commit4eeda83ba4bdb5e20c765a46e43227f4f9b39b69 (patch)
tree479d06d13870c17abfda96d704b21cc2eb85488b
parent64fceff1cc65ee1dd1c72f08004e40179be9f9a4 (diff)
downloadazalea-drasl-4eeda83ba4bdb5e20c765a46e43227f4f9b39b69.tar.xz
add some more convenience functions
-rw-r--r--azalea-client/src/movement.rs12
-rwxr-xr-xazalea-core/src/position.rs19
-rw-r--r--azalea-entity/src/lib.rs38
3 files changed, 62 insertions, 7 deletions
diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs
index 2f70a40e..ba47b7c8 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::<Jumping>()
}
/// 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::<LookDirection>();
+ (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 e9864035..369607c5 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 f39a6e4f..e08284a9 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<LookDirection> 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<H: Hasher>(&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.