aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/hit_result.rs
blob: 76f7ca84fd12b96cf78fb52a52572f358bd0ab71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{
    direction::Direction,
    position::{BlockPos, Vec3},
};

/// The block or entity that our player is looking at and can interact with.
///
/// If there's nothing, it'll be a [`BlockHitResult`] with `miss` set to true.
#[derive(Debug, Clone, PartialEq)]
pub enum HitResult {
    Block(BlockHitResult),
    /// TODO
    Entity,
}
impl HitResult {
    pub fn is_miss(&self) -> bool {
        match self {
            HitResult::Block(block_hit_result) => block_hit_result.miss,
            HitResult::Entity => false,
        }
    }

    pub fn is_block_hit_and_not_miss(&self) -> bool {
        match self {
            HitResult::Block(block_hit_result) => !block_hit_result.miss,
            HitResult::Entity => false,
        }
    }

    /// 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,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct BlockHitResult {
    pub location: Vec3,
    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,
            direction,
            block_pos,
            miss: true,
            inside: false,
            world_border: false,
        }
    }

    pub fn with_direction(&self, direction: Direction) -> Self {
        Self { direction, ..*self }
    }
    pub fn with_position(&self, block_pos: BlockPos) -> Self {
        Self { block_pos, ..*self }
    }
}