aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/hit_result.rs
blob: e242c4b59705172b630415b2bb891614ab082d02 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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.
#[cfg(feature = "bevy_ecs")]
#[derive(Clone, Debug, PartialEq)]
pub enum HitResult {
    Block(BlockHitResult),
    Entity(EntityHitResult),
}

#[cfg(feature = "bevy_ecs")]
impl HitResult {
    pub fn miss(&self) -> bool {
        match self {
            HitResult::Block(r) => r.miss,
            _ => false,
        }
    }
    pub fn location(&self) -> Vec3 {
        match self {
            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.
    pub fn as_block_hit_result_if_not_miss(&self) -> Option<&BlockHitResult> {
        if let HitResult::Block(r) = self
            && !r.miss
        {
            Some(r)
        } else {
            None
        }
    }

    /// Returns the [`EntityHitResult`], if we were looking at an entity and it
    /// wasn't a miss.
    pub fn as_entity_hit_result(&self) -> Option<&EntityHitResult> {
        if let HitResult::Entity(r) = self {
            Some(r)
        } else {
            None
        }
    }
}

/// The result of raycasting on the blocks in the world.
///
/// Also see [`HitResult`].
#[derive(Clone, Debug, PartialEq)]
pub struct BlockHitResult {
    /// The exact position that the raycast ended at.
    pub location: Vec3,
    pub miss: bool,

    pub direction: Direction,
    /// The block position that was hit.
    ///
    /// If [`Self::miss`] is true, then this will be the position that the
    /// raycast ended at.
    pub block_pos: BlockPos,
    pub inside: bool,
    pub world_border: bool,
}
impl BlockHitResult {
    /// Create a new [`BlockHitResult`] for when nothing was hit.
    pub fn miss(location: Vec3, direction: Direction) -> Self {
        Self {
            location,
            miss: true,

            direction,
            block_pos: BlockPos::from(location),
            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 }
    }
}
#[cfg(feature = "bevy_ecs")]
impl From<BlockHitResult> for HitResult {
    fn from(value: BlockHitResult) -> Self {
        HitResult::Block(value)
    }
}

#[cfg(feature = "bevy_ecs")]
#[derive(Clone, Debug, PartialEq)]
pub struct EntityHitResult {
    pub location: Vec3,
    pub entity: bevy_ecs::entity::Entity,
}

#[cfg(feature = "bevy_ecs")]
impl From<EntityHitResult> for HitResult {
    fn from(value: EntityHitResult) -> Self {
        HitResult::Entity(value)
    }
}