azalea_core/
hit_result.rs

1use crate::{
2    direction::Direction,
3    position::{BlockPos, Vec3},
4};
5
6/// The block or entity that our player is looking at and can interact with.
7///
8/// If there's nothing, it'll be a [`BlockHitResult`] with `miss` set to true.
9#[derive(Debug, Clone, PartialEq)]
10pub enum HitResult {
11    Block(BlockHitResult),
12    /// TODO
13    Entity,
14}
15impl HitResult {
16    pub fn is_miss(&self) -> bool {
17        match self {
18            HitResult::Block(block_hit_result) => block_hit_result.miss,
19            HitResult::Entity => false,
20        }
21    }
22
23    pub fn is_block_hit_and_not_miss(&self) -> bool {
24        match self {
25            HitResult::Block(block_hit_result) => !block_hit_result.miss,
26            HitResult::Entity => false,
27        }
28    }
29
30    /// Returns the [`BlockHitResult`], if we were looking at a block and it
31    /// wasn't a miss.
32    pub fn as_block_hit_result_if_not_miss(&self) -> Option<&BlockHitResult> {
33        match self {
34            HitResult::Block(block_hit_result) if !block_hit_result.miss => Some(block_hit_result),
35            _ => None,
36        }
37    }
38}
39
40#[derive(Debug, Clone, PartialEq)]
41pub struct BlockHitResult {
42    pub location: Vec3,
43    pub direction: Direction,
44    pub block_pos: BlockPos,
45    pub inside: bool,
46    pub world_border: bool,
47    pub miss: bool,
48}
49
50impl BlockHitResult {
51    pub fn miss(location: Vec3, direction: Direction, block_pos: BlockPos) -> Self {
52        Self {
53            location,
54            direction,
55            block_pos,
56            miss: true,
57            inside: false,
58            world_border: false,
59        }
60    }
61
62    pub fn with_direction(&self, direction: Direction) -> Self {
63        Self { direction, ..*self }
64    }
65    pub fn with_position(&self, block_pos: BlockPos) -> Self {
66        Self { block_pos, ..*self }
67    }
68}