aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-06-17 09:30:09 +1200
committermat <git@matdoes.dev>2025-06-16 21:31:04 +0000
commitfd9bf168716f195e7e6225b93dfb099aa01b1fde (patch)
treee617f464e2df32cbc8678b56c5c1df8cae1c4dcb /azalea-core/src
parent713dae7110ad4119469323b87fd95a7f2a544ed0 (diff)
downloadazalea-drasl-fd9bf168716f195e7e6225b93dfb099aa01b1fde.tar.xz
implement EntityHitResult
Diffstat (limited to 'azalea-core/src')
-rw-r--r--azalea-core/src/hit_result.rs67
-rw-r--r--azalea-core/src/position.rs6
2 files changed, 58 insertions, 15 deletions
diff --git a/azalea-core/src/hit_result.rs b/azalea-core/src/hit_result.rs
index 76f7ca84..3b1e160d 100644
--- a/azalea-core/src/hit_result.rs
+++ b/azalea-core/src/hit_result.rs
@@ -1,3 +1,5 @@
+use bevy_ecs::entity::Entity;
+
use crate::{
direction::Direction,
position::{BlockPos, Vec3},
@@ -9,30 +11,47 @@ use crate::{
#[derive(Debug, Clone, PartialEq)]
pub enum HitResult {
Block(BlockHitResult),
- /// TODO
- Entity,
+ Entity(EntityHitResult),
}
+
impl HitResult {
- pub fn is_miss(&self) -> bool {
+ pub fn miss(&self) -> bool {
match self {
- HitResult::Block(block_hit_result) => block_hit_result.miss,
- HitResult::Entity => false,
+ HitResult::Block(r) => r.miss,
+ HitResult::Entity(_) => false,
}
}
-
- pub fn is_block_hit_and_not_miss(&self) -> bool {
+ pub fn location(&self) -> Vec3 {
match self {
- HitResult::Block(block_hit_result) => !block_hit_result.miss,
- HitResult::Entity => false,
+ HitResult::Block(r) => r.location,
+ HitResult::Entity(r) => r.location,
}
}
+ pub fn new_miss(location: Vec3, direction: Direction, block_pos: BlockPos) -> Self {
+ HitResult::Block(BlockHitResult {
+ location,
+ miss: true,
+ direction,
+ block_pos,
+ inside: false,
+ world_border: false,
+ })
+ }
+
+ pub fn is_block_hit_and_not_miss(&self) -> bool {
+ matches!(self, HitResult::Block(r) if !r.miss)
+ }
+
/// Returns the [`BlockHitResult`], if we were looking at a block and it
/// wasn't a miss.
pub fn as_block_hit_result_if_not_miss(&self) -> Option<&BlockHitResult> {
- match self {
- HitResult::Block(block_hit_result) if !block_hit_result.miss => Some(block_hit_result),
- _ => None,
+ if let HitResult::Block(r) = self
+ && !r.miss
+ {
+ Some(r)
+ } else {
+ None
}
}
}
@@ -40,20 +59,21 @@ impl HitResult {
#[derive(Debug, Clone, PartialEq)]
pub struct BlockHitResult {
pub location: Vec3,
+ pub miss: bool,
+
pub direction: Direction,
pub block_pos: BlockPos,
pub inside: bool,
pub world_border: bool,
- pub miss: bool,
}
-
impl BlockHitResult {
pub fn miss(location: Vec3, direction: Direction, block_pos: BlockPos) -> Self {
Self {
location,
+ miss: true,
+
direction,
block_pos,
- miss: true,
inside: false,
world_border: false,
}
@@ -66,3 +86,20 @@ impl BlockHitResult {
Self { block_pos, ..*self }
}
}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct EntityHitResult {
+ pub location: Vec3,
+ pub entity: Entity,
+}
+
+impl From<BlockHitResult> for HitResult {
+ fn from(value: BlockHitResult) -> Self {
+ HitResult::Block(value)
+ }
+}
+impl From<EntityHitResult> for HitResult {
+ fn from(value: EntityHitResult) -> Self {
+ HitResult::Entity(value)
+ }
+}
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index 5a8d3e0c..c0c25639 100644
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -350,6 +350,12 @@ impl Vec3 {
z: self.z.ceil() as i32,
}
}
+
+ /// Whether the distance between this point and `other` is less than
+ /// `range`.
+ pub fn closer_than(&self, other: Vec3, range: f64) -> bool {
+ self.distance_squared_to(other) < range.powi(2)
+ }
}
/// The coordinates of a block in the world.