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 pub fn from_irrelevant_qzones(
22 required_quality: usize,
23 haz_map: &SlotMap<HazKey, Hazard>,
24 ) -> Self {
25 HazKeyFilter(
26 haz_map
27 .iter()
28 .filter_map(|(hkey, h)| {
29 match h.entity {
30 HazardEntity::InferiorQualityZone { quality, .. }
31 if quality < required_quality =>
32 {
33 Some((hkey, ()))
35 }
36 _ => None,
37 }
38 })
39 .collect(),
40 )
41 }
42}
43
44impl HazardFilter for HazKeyFilter {
45 fn is_irrelevant(&self, haz_key: HazKey) -> bool {
46 self.0.contains_key(haz_key)
47 }
48}
49
50impl HazardFilter for HazKey {
52 fn is_irrelevant(&self, hk: HazKey) -> bool {
53 *self == hk
54 }
55}
56
57#[derive(Clone, Debug)]
59pub struct NoFilter;
60
61impl HazardFilter for NoFilter {
62 fn is_irrelevant(&self, _haz_key: HazKey) -> bool {
63 false
64 }
65}
66
67impl<T> HazardFilter for T
70where
71 T: HazardCollector,
72{
73 fn is_irrelevant(&self, hkey: HazKey) -> bool {
74 self.contains_key(hkey)
75 }
76}