bevy_tnua/ghost_overrides.rs
1use std::marker::PhantomData;
2
3use bevy::prelude::*;
4use bevy_tnua_physics_integration_layer::data_for_backends::TnuaProximitySensorOutput;
5
6use crate::sensor_sets::TnuaSensors;
7use crate::{TnuaBasis, TnuaScheme};
8
9/// A struct with fields of [`TnuaGhostOverwrite`] for each sensor that can have a ghost sensor.
10pub trait TnuaGhostOverwritesForBasis: 'static + Send + Sync + Default {
11 /// A struct that points to the sensor entities. Must match the entities of [the basis'
12 /// sensors](TnuaBasis::Sensors).
13 type Entities: 'static + Send + Sync + Default;
14}
15
16/// Add this component to an entity with a [`TnuaController`](crate::TnuaController) (that has the
17/// same control scheme) to generate ghost sensors and to control them.
18///
19/// This component holds and refers to a struct defined by the
20/// [`GhostOverwrites`](TnuaSensors::GhostOverwrites) of the [`Sensors`](TnuaBasis::Sensors) of the
21/// control scheme's basis. The fields of that struct should be of type [`TnuaGhostOverwrite`], and
22/// should have matching fields in the sensors' [`Entities`](TnuaSensors::Entities) (accessible via
23/// the [`TnuaSensorsEntities`](crate::TnuaSensorsEntities) component) that point to entities with
24/// a [`TnuaGhostSensor`](crate::TnuaGhostSensor) component on them that holds the ghost hits that
25/// can be set in the [`TnuaGhostOverwrite`] using its
26/// [`set`](TnuaGhostOverwrite::set) method.
27#[derive(Component, Deref, DerefMut, Reflect)]
28#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
29pub struct TnuaGhostOverwrites<S: TnuaScheme>(pub <<<S as TnuaScheme>::Basis as TnuaBasis>::Sensors<'static> as TnuaSensors<'static>>::GhostOverwrites);
30
31impl<S: TnuaScheme> FromTemplate for TnuaGhostOverwrites<S> {
32 type Template = TnuaGhostOverwritesTemplate<S>;
33}
34
35pub struct TnuaGhostOverwritesTemplate<S: TnuaScheme> {
36 _phantom: PhantomData<S>,
37}
38
39impl<S: TnuaScheme> Default for TnuaGhostOverwritesTemplate<S> {
40 fn default() -> Self {
41 Self {
42 _phantom: PhantomData,
43 }
44 }
45}
46
47impl<S: TnuaScheme> Template for TnuaGhostOverwritesTemplate<S> {
48 type Output = TnuaGhostOverwrites<S>;
49
50 fn build_template(&self, _: &mut bevy::ecs::template::TemplateContext) -> Result<Self::Output> {
51 Ok(Default::default())
52 }
53
54 fn clone_template(&self) -> Self {
55 Self {
56 _phantom: PhantomData,
57 }
58 }
59}
60
61impl<S: TnuaScheme> AsMut<
62<<<S as TnuaScheme>::Basis as TnuaBasis>::Sensors<'static> as TnuaSensors<'static>>::GhostOverwrites
63> for TnuaGhostOverwrites<S> {
64 fn as_mut(&mut self) -> &mut <<<S as TnuaScheme>::Basis as TnuaBasis>::Sensors<'static> as TnuaSensors<'static>>::GhostOverwrites {
65 &mut self.0
66 }
67}
68
69impl<S: TnuaScheme> Default for TnuaGhostOverwrites<S> {
70 fn default() -> Self {
71 Self(Default::default())
72 }
73}
74
75/// Controls how Tnua uses the ghost sensor of a single
76/// [`TnuaProximitySensor`](crate::TnuaProximitySensor).
77///
78/// Note that this is not a component because it is not stored on the sensor entity - instead it is
79/// stored with the entity that has the [`TnuaController`](crate::TnuaController) component, inside
80/// a [`TnuaGhostOverwrites`] component. The [`TnuaGhostSensor`](crate::TnuaGhostSensor) itself is
81/// stored on the sensor entity - to retrieve that [`Entity`] use the
82/// [`TnuaSensorsEntities`](crate::TnuaSensorsEntities) component.
83#[derive(Default)]
84#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
85pub struct TnuaGhostOverwrite(Option<Entity>);
86
87impl TnuaGhostOverwrite {
88 /// Set an output of the ghost sensor so that the controller will use it instead of the
89 /// (non-ghost) output of the proximity sensor.
90 ///
91 /// Note that the controller does not use that output has is - it picks an output from the
92 /// ghost sensor that matches the output provided here. This is important because the ghost
93 /// sensor will have its list of outputs refreshed before this happens.
94 ///
95 /// If set to `None`, the ghost sensor will not be used that frame and the output of the
96 /// regular proximity sensor will be used.
97 ///
98 /// The ghost overwrite will remain in place until one of the following happens:
99 /// 1. This method is invoked again - either with another output or with `None`.
100 /// 2. The ghost sensor no longer has a matching hit in its outputs list.
101 pub fn set(&mut self, sensor_output: Option<&TnuaProximitySensorOutput>) {
102 self.0 = sensor_output.map(|output| output.entity);
103 }
104
105 pub(crate) fn find_in<'a>(
106 &self,
107 ghost_outputs: &'a [TnuaProximitySensorOutput],
108 ) -> Option<&'a TnuaProximitySensorOutput> {
109 let entity = self.0?;
110 ghost_outputs.iter().find(|output| output.entity == entity)
111 }
112}