Skip to content

Commit

Permalink
add some more convenience functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Feb 24, 2024
1 parent 64fceff commit 4eeda83
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
12 changes: 9 additions & 3 deletions azalea-client/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions azalea-core/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
};
}

Expand Down
38 changes: 34 additions & 4 deletions azalea-entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4eeda83

Please sign in to comment.