jagua_rs/collision_detection/hazards/
filter.rs1use crate::collision_detection::hazards::collector::HazardCollector;
2use crate::collision_detection::hazards::{HazKey, Hazard, HazardEntity};
3use slotmap::{SecondaryMap, SlotMap};
4
5pub trait HazardFilter {
8 fn is_irrelevant(&self, haz_key: HazKey) -> bool;
9}
10
11#[derive(Clone, Debug)]
13pub struct HazKeyFilter(pub SecondaryMap<HazKey, ()>);
14
15impl HazKeyFilter {
16 pub fn from_keys(keys: impl IntoIterator<Item = HazKey>) -> Self {
17 HazKeyFilter(keys.into_iter().map(|k| (k, ())).collect())
18 }
19
20 #[must_use]
22 pub fn from_irrelevant_qzones(
23 required_quality: usize,
24 haz_map: &SlotMap<HazKey, Hazard>,
25 ) -> Self {
26 HazKeyFilter(
27 haz_map
28 .iter()
29 .filter_map(|(hkey, h)| {
30 match h.entity {
31 HazardEntity::InferiorQualityZone { quality, .. }
32 if quality < required_quality =>
33 {
34 Some((hkey, ()))
36 }
37 _ => None,
38 }
39 })
40 .collect(),
41 )
42 }
43}
44
45impl HazardFilter for HazKeyFilter {
46 fn is_irrelevant(&self, haz_key: HazKey) -> bool {
47 self.0.contains_key(haz_key)
48 }
49}
50
51impl HazardFilter for HazKey {
53 fn is_irrelevant(&self, hk: HazKey) -> bool {
54 *self == hk
55 }
56}
57
58#[derive(Clone, Debug)]
60pub struct NoFilter;
61
62impl HazardFilter for NoFilter {
63 fn is_irrelevant(&self, _haz_key: HazKey) -> bool {
64 false
65 }
66}
67
68impl<T> HazardFilter for T
71where
72 T: HazardCollector,
73{
74 fn is_irrelevant(&self, hkey: HazKey) -> bool {
75 self.contains_key(hkey)
76 }
77}