bevy_tnua_physics_integration_layer/
obstacle_radar.rs1use bevy::{platform::collections::HashMap, prelude::*};
2
3use crate::math::{Float, Vector3};
4
5#[derive(Component, Reflect)]
14pub struct TnuaObstacleRadar {
15 pub radius: Float,
17 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 pub radius: Float,
33 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 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 pub fn mark_seen(&mut self, entity: Entity) {
88 self.blips.insert(entity, BlipStatus::Seen);
89 }
90
91 pub fn tracked_entity(&self) -> Entity {
93 self.tracked_entity
94 }
95
96 pub fn tracked_position(&self) -> Vector3 {
99 self.tracked_position
100 }
101
102 pub fn up_direction(&self) -> Dir3 {
104 self.up_direction
105 }
106
107 pub fn iter_blips(&self) -> impl '_ + Iterator<Item = Entity> {
109 self.blips.keys().copied()
110 }
111
112 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}