Skip to main content

bevy_tnua_physics_integration_layer/
obstacle_radar.rs

1use bevy::{platform::collections::HashMap, prelude::*};
2
3use crate::math::{Float, Vector3};
4
5/// Add this to a character entity to detect obstacles around it.
6///
7/// Obstacles can be used for environment movement actions like climbing and wall-jumping.
8///
9/// This component stores rather limited data - only the detected entities and nothing about their
10/// form or position. See `TnuaRadarLens` in the main Tnua crate, which wraps this with a
11/// [`TnuaSpatialExt`](crate::spatial_ext::TnuaSpatialExt) to provide many helper methods for
12/// running more queries on the detected obstacles.
13#[derive(Component, Reflect)]
14pub struct TnuaObstacleRadar {
15    /// The radius of the radar's cylinder.
16    pub radius: Float,
17    /// The height of the radar's cylinder.
18    pub height: Float,
19    tracked_entity: Entity,
20    tracked_position: Vector3,
21    up_direction: Dir3,
22    blips: HashMap<Entity, BlipStatus>,
23}
24
25impl FromTemplate for TnuaObstacleRadar {
26    type Template = TnuaObstacleRadarTemplate;
27}
28
29#[derive(Default)]
30pub struct TnuaObstacleRadarTemplate {
31    /// The radius of the radar's cylinder.
32    pub radius: Float,
33    /// The height of the radar's cylinder.
34    pub height: Float,
35}
36
37impl Template for TnuaObstacleRadarTemplate {
38    type Output = TnuaObstacleRadar;
39
40    fn build_template(&self, _: &mut bevy::ecs::template::TemplateContext) -> Result<Self::Output> {
41        Ok(TnuaObstacleRadar::new(self.radius, self.height))
42    }
43
44    fn clone_template(&self) -> Self {
45        Self {
46            radius: self.radius,
47            height: self.height,
48        }
49    }
50}
51
52impl TnuaObstacleRadar {
53    pub fn new(radius: Float, height: Float) -> Self {
54        Self {
55            radius,
56            height,
57            tracked_entity: Entity::PLACEHOLDER,
58            tracked_position: Vector3::NAN,
59            up_direction: Dir3::Y,
60            blips: Default::default(),
61        }
62    }
63
64    /// Physics integration crates must call this each frame before they start calling
65    /// [`mark_seen`](Self::mark_seen), and feed it some general information about the character
66    /// entity.
67    pub fn pre_marking_update(
68        &mut self,
69        tracked_entity: Entity,
70        tracked_position: Vector3,
71        up_direction: Dir3,
72    ) {
73        self.tracked_entity = tracked_entity;
74        self.tracked_position = tracked_position;
75        self.up_direction = up_direction;
76        self.blips.retain(|_, blip_status| match blip_status {
77            BlipStatus::Unseen => false,
78            BlipStatus::Seen => {
79                *blip_status = BlipStatus::Unseen;
80                true
81            }
82        });
83    }
84
85    /// Physics integration crates should call this for each detected entity during each frame,
86    /// after invoking [`pre_marking_update`](Self::pre_marking_update).
87    pub fn mark_seen(&mut self, entity: Entity) {
88        self.blips.insert(entity, BlipStatus::Seen);
89    }
90
91    /// Get the character entity who owns the radar (not the detected obstacle entities!)
92    pub fn tracked_entity(&self) -> Entity {
93        self.tracked_entity
94    }
95
96    /// Get the position of the character who owns the radar (not the detected obstacle's
97    /// positions!)
98    pub fn tracked_position(&self) -> Vector3 {
99        self.tracked_position
100    }
101
102    /// Get the direction considered up.
103    pub fn up_direction(&self) -> Dir3 {
104        self.up_direction
105    }
106
107    /// Iterate over all the blip entities.
108    pub fn iter_blips(&self) -> impl '_ + Iterator<Item = Entity> {
109        self.blips.keys().copied()
110    }
111
112    /// Check if a particular entity has been detected this frame.
113    pub fn has_blip(&self, entity: Entity) -> bool {
114        self.blips.contains_key(&entity)
115    }
116}
117
118#[derive(Reflect)]
119pub enum BlipStatus {
120    Unseen,
121    Seen,
122}